10 "git.fd.io/govpp.git/api"
11 "github.com/edwarnicke/exechelper"
12 "github.com/edwarnicke/govpp/binapi/af_packet"
13 interfaces "github.com/edwarnicke/govpp/binapi/interface"
14 "github.com/edwarnicke/govpp/binapi/interface_types"
15 ip_types "github.com/edwarnicke/govpp/binapi/ip_types"
16 "github.com/edwarnicke/govpp/binapi/session"
17 "github.com/edwarnicke/govpp/binapi/vlib"
18 "github.com/edwarnicke/vpphelper"
21 func RegisterActions() {
22 cfgTable = make(map[string]func([]string) *ActionResult)
23 reg("echo-srv-internal", Configure2Veths)
24 reg("echo-cln-internal", Configure2Veths)
25 reg("echo-client", RunEchoClient)
26 reg("echo-server", RunEchoServer)
27 reg("vpp-proxy", ConfigureVppProxy)
28 reg("vpp-envoy", ConfigureEnvoyProxy)
29 reg("http-tps", ConfigureHttpTps)
30 reg("2veths", Configure2Veths)
33 func configureProxyTcp(ifName0, ipAddr0, ifName1, ipAddr1 string) ConfFn {
34 return func(ctx context.Context,
35 vppConn api.Connection) error {
37 _, err := configureAfPacket(ctx, vppConn, ifName0, ipAddr0)
39 fmt.Printf("failed to create af packet: %v", err)
42 _, err = configureAfPacket(ctx, vppConn, ifName1, ipAddr1)
44 fmt.Printf("failed to create af packet: %v", err)
51 func ConfigureVppProxy(args []string) *ActionResult {
52 ctx, cancel := newVppContext()
55 con, vppErrCh := vpphelper.StartAndDialContext(ctx, vpphelper.WithVppConfig(configTemplate))
56 exitOnErrCh(ctx, cancel, vppErrCh)
58 confFn := configureProxyTcp("vpp0", "10.0.0.2/24", "vpp1", "10.0.1.2/24")
59 err := confFn(ctx, con)
61 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
63 writeSyncFile(OkResult())
68 func ConfigureEnvoyProxy(args []string) *ActionResult {
73 Append("use-app-socket-api").
74 Append("evt_qs_memfd_seg").
75 Append("event-queue-length 100000").Close()
76 ctx, cancel := newVppContext()
79 con, vppErrCh := vpphelper.StartAndDialContext(ctx,
80 vpphelper.WithVppConfig(configTemplate+startup.ToString()),
81 vpphelper.WithRootDir("/tmp/vpp-envoy"))
82 exitOnErrCh(ctx, cancel, vppErrCh)
84 confFn := configureProxyTcp("vpp0", "10.0.0.2/24", "vpp1", "10.0.1.2/24")
85 err := confFn(ctx, con)
87 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
89 err0 := exechelper.Run("chmod 777 -R /tmp/vpp-envoy")
91 return NewActionResult(err, ActionResultWithDesc("setting permissions failed"))
93 writeSyncFile(OkResult())
98 func getArgs() string {
100 for i := 2; i < len(os.Args); i++ {
101 s = s + " " + os.Args[i]
106 func ApiCliInband(root, cmd string) *ActionResult {
107 ctx, _ := newVppContext()
108 con := vpphelper.DialContext(ctx, filepath.Join(root, "/var/run/vpp/api.sock"))
109 cliInband := vlib.CliInband{Cmd: cmd}
110 cliInbandReply, err := vlib.NewServiceClient(con).CliInband(ctx, &cliInband)
111 return NewActionResult(err, ActionResultWithStdout(cliInbandReply.Reply))
114 func RunEchoClient(args []string) *ActionResult {
115 outBuff := bytes.NewBuffer([]byte{})
116 errBuff := bytes.NewBuffer([]byte{})
118 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])
119 err := exechelper.Run(cmd,
120 exechelper.WithStdout(outBuff), exechelper.WithStderr(errBuff),
121 exechelper.WithStdout(os.Stdout), exechelper.WithStderr(os.Stderr))
123 return NewActionResult(err, ActionResultWithStdout(string(outBuff.String())),
124 ActionResultWithStderr(string(errBuff.String())))
127 func RunEchoServer(args []string) *ActionResult {
128 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])
129 errCh := exechelper.Start(cmd)
132 writeSyncFile(NewActionResult(err, ActionResultWithDesc("echo_server: ")))
135 writeSyncFile(OkResult())
139 func RunEchoSrvInternal() *ActionResult {
140 cmd := fmt.Sprintf("test echo server %s uri tcp://10.10.10.1/1234", getArgs())
141 return ApiCliInband("/tmp/2veths", cmd)
144 func RunEchoClnInternal() *ActionResult {
145 cmd := fmt.Sprintf("test echo client %s uri tcp://10.10.10.1/1234", getArgs())
146 return ApiCliInband("/tmp/2veths", cmd)
148 func configure2vethsTopo(ifName, interfaceAddress, namespaceId string, secret uint64) ConfFn {
149 return func(ctx context.Context,
150 vppConn api.Connection) error {
152 swIfIndex, err := configureAfPacket(ctx, vppConn, ifName, interfaceAddress)
154 fmt.Printf("failed to create af packet: %v", err)
156 _, er := session.NewServiceClient(vppConn).AppNamespaceAddDelV2(ctx, &session.AppNamespaceAddDelV2{
158 SwIfIndex: swIfIndex,
159 NamespaceID: namespaceId,
162 fmt.Printf("add app namespace: %v", err)
166 _, er1 := session.NewServiceClient(vppConn).SessionEnableDisable(ctx, &session.SessionEnableDisable{
170 fmt.Printf("session enable %v", err)
177 func Configure2Veths(args []string) *ActionResult {
180 NewStanza("session").
182 Append("use-app-socket-api").Close()
184 ctx, cancel := newVppContext()
186 con, vppErrCh := vpphelper.StartAndDialContext(ctx,
187 vpphelper.WithVppConfig(configTemplate+startup.ToString()),
188 vpphelper.WithRootDir(fmt.Sprintf("/tmp/%s", args[1])))
189 exitOnErrCh(ctx, cancel, vppErrCh)
191 var fn func(context.Context, api.Connection) error
192 if args[2] == "srv" {
193 fn = configure2vethsTopo("vppsrv", "10.10.10.1/24", "1", 1)
195 fn = configure2vethsTopo("vppcln", "10.10.10.2/24", "2", 2)
199 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
201 writeSyncFile(OkResult())
206 func configureAfPacket(ctx context.Context, vppCon api.Connection,
207 name, interfaceAddress string) (interface_types.InterfaceIndex, error) {
208 ifaceClient := interfaces.NewServiceClient(vppCon)
209 afPacketCreate := &af_packet.AfPacketCreateV2{
210 UseRandomHwAddr: true,
214 afPacketCreateRsp, err := af_packet.NewServiceClient(vppCon).AfPacketCreateV2(ctx, afPacketCreate)
216 fmt.Printf("failed to create af packet: %v", err)
219 _, err = ifaceClient.SwInterfaceSetFlags(ctx, &interfaces.SwInterfaceSetFlags{
220 SwIfIndex: afPacketCreateRsp.SwIfIndex,
221 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
224 fmt.Printf("set interface state up failed: %v\n", err)
227 ipPrefix, err := ip_types.ParseAddressWithPrefix(interfaceAddress)
229 fmt.Printf("parse ip address %v\n", err)
232 ipAddress := &interfaces.SwInterfaceAddDelAddress{
234 SwIfIndex: afPacketCreateRsp.SwIfIndex,
237 _, errx := ifaceClient.SwInterfaceAddDelAddress(ctx, ipAddress)
239 fmt.Printf("add ip address %v\n", err)
242 return afPacketCreateRsp.SwIfIndex, nil
245 func ConfigureHttpTps(args []string) *ActionResult {
246 ctx, cancel := newVppContext()
248 con, vppErrCh := vpphelper.StartAndDialContext(ctx,
249 vpphelper.WithVppConfig(configTemplate))
250 exitOnErrCh(ctx, cancel, vppErrCh)
252 confFn := configureProxyTcp("vpp0", "10.0.0.2/24", "vpp1", "10.0.1.2/24")
253 err := confFn(ctx, con)
255 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
258 _, err = session.NewServiceClient(con).SessionEnableDisable(ctx, &session.SessionEnableDisable{
262 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
264 Vppcli("", "http tps uri tcp://0.0.0.0/8080")
265 writeSyncFile(OkResult())