10 "git.fd.io/govpp.git/api"
11 "github.com/edwarnicke/exechelper"
12 "github.com/edwarnicke/govpp/binapi/af_packet"
13 "github.com/edwarnicke/govpp/binapi/ethernet_types"
14 interfaces "github.com/edwarnicke/govpp/binapi/interface"
15 "github.com/edwarnicke/govpp/binapi/interface_types"
16 ip_types "github.com/edwarnicke/govpp/binapi/ip_types"
17 "github.com/edwarnicke/govpp/binapi/session"
18 "github.com/edwarnicke/govpp/binapi/vlib"
19 "github.com/edwarnicke/vpphelper"
22 type ConfFn func(context.Context, api.Connection) error
27 func configureProxyTcp(ifName0, ipAddr0, ifName1, ipAddr1 string) ConfFn {
28 return func(ctx context.Context,
29 vppConn api.Connection) error {
31 _, err := configureAfPacket(ctx, vppConn, ifName0, ipAddr0)
33 fmt.Printf("failed to create af packet: %v", err)
36 _, err = configureAfPacket(ctx, vppConn, ifName1, ipAddr1)
38 fmt.Printf("failed to create af packet: %v", err)
45 func (a *Actions) RunHttpCliSrv(args []string) *ActionResult {
46 cmd := fmt.Sprintf("http cli server")
47 return ApiCliInband("/tmp/Configure2Veths", cmd)
50 func (a *Actions) RunHttpCliCln(args []string) *ActionResult {
51 cmd := fmt.Sprintf("http cli client uri http://10.10.10.1/80 query %s", getArgs())
53 return ApiCliInband("/tmp/Configure2Veths", cmd)
56 func (a *Actions) ConfigureVppProxy(args []string) *ActionResult {
57 ctx, cancel := newVppContext()
60 con, vppErrCh := vpphelper.StartAndDialContext(ctx, vpphelper.WithVppConfig(configTemplate))
61 exitOnErrCh(ctx, cancel, vppErrCh)
63 confFn := configureProxyTcp("vpp0", "10.0.0.2/24", "vpp1", "10.0.1.2/24")
64 err := confFn(ctx, con)
66 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
68 writeSyncFile(OkResult())
73 func (a *Actions) ConfigureEnvoyProxy(args []string) *ActionResult {
78 Append("use-app-socket-api").
79 Append("evt_qs_memfd_seg").
80 Append("event-queue-length 100000").Close()
81 ctx, cancel := newVppContext()
84 con, vppErrCh := vpphelper.StartAndDialContext(ctx,
85 vpphelper.WithVppConfig(configTemplate+startup.ToString()),
86 vpphelper.WithRootDir("/tmp/vpp-envoy"))
87 exitOnErrCh(ctx, cancel, vppErrCh)
89 confFn := configureProxyTcp("vpp0", "10.0.0.2/24", "vpp1", "10.0.1.2/24")
90 err := confFn(ctx, con)
92 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
94 err0 := exechelper.Run("chmod 777 -R /tmp/vpp-envoy")
96 return NewActionResult(err, ActionResultWithDesc("setting permissions failed"))
98 writeSyncFile(OkResult())
103 func getArgs() string {
105 for i := 2; i < len(os.Args); i++ {
106 s = s + " " + os.Args[i]
111 func ApiCliInband(root, cmd string) *ActionResult {
112 ctx, _ := newVppContext()
113 con := vpphelper.DialContext(ctx, filepath.Join(root, "/var/run/vpp/api.sock"))
114 cliInband := vlib.CliInband{Cmd: cmd}
115 cliInbandReply, err := vlib.NewServiceClient(con).CliInband(ctx, &cliInband)
116 return NewActionResult(err, ActionResultWithStdout(cliInbandReply.Reply))
119 func (a *Actions) RunEchoClient(args []string) *ActionResult {
120 outBuff := bytes.NewBuffer([]byte{})
121 errBuff := bytes.NewBuffer([]byte{})
123 cmd := fmt.Sprintf("vpp_echo client socket-name /tmp/echo-cln/var/run/app_ns_sockets/2 use-app-socket-api uri %s://10.10.10.1/12344", args[2])
124 err := exechelper.Run(cmd,
125 exechelper.WithStdout(outBuff), exechelper.WithStderr(errBuff),
126 exechelper.WithStdout(os.Stdout), exechelper.WithStderr(os.Stderr))
128 return NewActionResult(err, ActionResultWithStdout(string(outBuff.String())),
129 ActionResultWithStderr(string(errBuff.String())))
132 func (a *Actions) RunEchoServer(args []string) *ActionResult {
133 cmd := fmt.Sprintf("vpp_echo server TX=RX socket-name /tmp/echo-srv/var/run/app_ns_sockets/1 use-app-socket-api uri %s://10.10.10.1/12344", args[2])
134 errCh := exechelper.Start(cmd)
137 writeSyncFile(NewActionResult(err, ActionResultWithDesc("echo_server: ")))
140 writeSyncFile(OkResult())
144 func (a *Actions) RunEchoSrvInternal(args []string) *ActionResult {
145 cmd := fmt.Sprintf("test echo server %s uri tcp://10.10.10.1/1234", getArgs())
146 return ApiCliInband("/tmp/Configure2Veths", cmd)
149 func (a *Actions) RunEchoClnInternal(args []string) *ActionResult {
150 cmd := fmt.Sprintf("test echo client %s uri tcp://10.10.10.1/1234", getArgs())
151 return ApiCliInband("/tmp/Configure2Veths", cmd)
154 func (a *Actions) RunVclEchoServer(args []string) *ActionResult {
155 f, err := os.Create("vcl_1.conf")
157 return NewActionResult(err, ActionResultWithStderr(("create vcl config: ")))
159 fmt.Fprintf(f, vclTemplate, "/tmp/echo-srv/var/run/app_ns_sockets/1", "1")
162 os.Setenv("VCL_CONFIG", "/vcl_1.conf")
163 cmd := fmt.Sprintf("vcl_test_server -p %s 12346", args[2])
164 errCh := exechelper.Start(cmd)
167 writeSyncFile(NewActionResult(err, ActionResultWithDesc("vcl_test_server: ")))
170 writeSyncFile(OkResult())
174 func (a *Actions) RunVclEchoClient(args []string) *ActionResult {
175 outBuff := bytes.NewBuffer([]byte{})
176 errBuff := bytes.NewBuffer([]byte{})
178 f, err := os.Create("vcl_2.conf")
180 return NewActionResult(err, ActionResultWithStderr(("create vcl config: ")))
182 fmt.Fprintf(f, vclTemplate, "/tmp/echo-cln/var/run/app_ns_sockets/2", "2")
185 os.Setenv("VCL_CONFIG", "/vcl_2.conf")
186 cmd := fmt.Sprintf("vcl_test_client -U -p %s 10.10.10.1 12346", args[2])
187 err = exechelper.Run(cmd,
188 exechelper.WithStdout(outBuff), exechelper.WithStderr(errBuff),
189 exechelper.WithStdout(os.Stdout), exechelper.WithStderr(os.Stderr))
191 return NewActionResult(err, ActionResultWithStdout(string(outBuff.String())),
192 ActionResultWithStderr(string(errBuff.String())))
195 func configure2vethsTopo(ifName, interfaceAddress, namespaceId string, secret uint64, optionalHardwareAddress ...string) ConfFn {
196 return func(ctx context.Context,
197 vppConn api.Connection) error {
199 var swIfIndex interface_types.InterfaceIndex
201 if optionalHardwareAddress == nil {
202 swIfIndex, err = configureAfPacket(ctx, vppConn, ifName, interfaceAddress)
204 swIfIndex, err = configureAfPacket(ctx, vppConn, ifName, interfaceAddress, optionalHardwareAddress[0])
207 fmt.Printf("failed to create af packet: %v", err)
209 _, er := session.NewServiceClient(vppConn).AppNamespaceAddDelV2(ctx, &session.AppNamespaceAddDelV2{
211 SwIfIndex: swIfIndex,
212 NamespaceID: namespaceId,
215 fmt.Printf("add app namespace: %v", err)
219 _, er1 := session.NewServiceClient(vppConn).SessionEnableDisable(ctx, &session.SessionEnableDisable{
223 fmt.Printf("session enable %v", err)
230 func (a *Actions) Configure2Veths(args []string) *ActionResult {
233 NewStanza("session").
235 Append("use-app-socket-api").Close()
237 ctx, cancel := newVppContext()
239 con, vppErrCh := vpphelper.StartAndDialContext(ctx,
240 vpphelper.WithVppConfig(configTemplate+startup.ToString()),
241 vpphelper.WithRootDir(fmt.Sprintf("/tmp/%s", args[1])))
242 exitOnErrCh(ctx, cancel, vppErrCh)
244 var fn func(context.Context, api.Connection) error
245 if args[2] == "srv" {
246 fn = configure2vethsTopo("vppsrv", "10.10.10.1/24", "1", 1)
247 } else if args[2] == "srv-with-preset-hw-addr" {
248 fn = configure2vethsTopo("vppsrv", "10.10.10.1/24", "1", 1, "00:00:5e:00:53:01")
250 fn = configure2vethsTopo("vppcln", "10.10.10.2/24", "2", 2)
254 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
256 writeSyncFile(OkResult())
261 func configureAfPacket(ctx context.Context, vppCon api.Connection,
262 name, interfaceAddress string, optionalHardwareAddress ...string) (interface_types.InterfaceIndex, error) {
264 ifaceClient := interfaces.NewServiceClient(vppCon)
265 afPacketCreate := af_packet.AfPacketCreateV2{
266 UseRandomHwAddr: true,
270 if len(optionalHardwareAddress) > 0 {
271 afPacketCreate.HwAddr, err = ethernet_types.ParseMacAddress(optionalHardwareAddress[0])
273 fmt.Printf("failed to parse mac address: %v", err)
276 afPacketCreate.UseRandomHwAddr = false
278 afPacketCreateRsp, err := af_packet.NewServiceClient(vppCon).AfPacketCreateV2(ctx, &afPacketCreate)
280 fmt.Printf("failed to create af packet: %v", err)
283 _, err = ifaceClient.SwInterfaceSetFlags(ctx, &interfaces.SwInterfaceSetFlags{
284 SwIfIndex: afPacketCreateRsp.SwIfIndex,
285 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
288 fmt.Printf("set interface state up failed: %v\n", err)
291 ipPrefix, err := ip_types.ParseAddressWithPrefix(interfaceAddress)
293 fmt.Printf("parse ip address %v\n", err)
296 ipAddress := &interfaces.SwInterfaceAddDelAddress{
298 SwIfIndex: afPacketCreateRsp.SwIfIndex,
301 _, errx := ifaceClient.SwInterfaceAddDelAddress(ctx, ipAddress)
303 fmt.Printf("add ip address %v\n", err)
306 return afPacketCreateRsp.SwIfIndex, nil
309 func (a *Actions) ConfigureHttpTps(args []string) *ActionResult {
310 ctx, cancel := newVppContext()
312 con, vppErrCh := vpphelper.StartAndDialContext(ctx,
313 vpphelper.WithVppConfig(configTemplate))
314 exitOnErrCh(ctx, cancel, vppErrCh)
316 confFn := configureProxyTcp("vpp0", "10.0.0.2/24", "vpp1", "10.0.1.2/24")
317 err := confFn(ctx, con)
319 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
322 _, err = session.NewServiceClient(con).SessionEnableDisable(ctx, &session.SessionEnableDisable{
326 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
328 Vppcli("", "http tps uri tcp://0.0.0.0/8080")
329 writeSyncFile(OkResult())