7 "git.fd.io/govpp.git/api"
8 interfaces "github.com/edwarnicke/govpp/binapi/interface"
9 "github.com/edwarnicke/govpp/binapi/interface_types"
10 ip_types "github.com/edwarnicke/govpp/binapi/ip_types"
11 "github.com/edwarnicke/govpp/binapi/session"
12 "github.com/edwarnicke/govpp/binapi/tapv2"
13 "github.com/edwarnicke/vpphelper"
17 workDir, _ = os.Getwd()
20 type ConfFn func(context.Context, api.Connection) error
25 func (a *Actions) ConfigureTap(args []string) *ActionResult {
30 Append("use-app-socket-api").Close()
32 ctx, cancel := newVppContext()
34 con, vppErrCh := vpphelper.StartAndDialContext(ctx,
35 vpphelper.WithRootDir(workDir),
36 vpphelper.WithVppConfig(configTemplate+startup.ToString()))
37 exitOnErrCh(ctx, cancel, vppErrCh)
38 ifaceClient := interfaces.NewServiceClient(con)
40 pref, err := ip_types.ParseIP4Prefix("10.10.10.2/24")
42 return NewActionResult(err, ActionResultWithDesc("failed to parse ip4 address"))
44 createTapReply, err := tapv2.NewServiceClient(con).TapCreateV2(ctx, &tapv2.TapCreateV2{
47 HostIP4PrefixSet: true,
48 HostIP4Prefix: ip_types.IP4AddressWithPrefix(pref),
51 return NewActionResult(err, ActionResultWithDesc("failed to configure tap"))
53 ipPrefix, err := ip_types.ParseAddressWithPrefix("10.10.10.1/24")
55 return NewActionResult(err, ActionResultWithDesc("parsing ip address failed"))
57 ipAddress := &interfaces.SwInterfaceAddDelAddress{
59 SwIfIndex: createTapReply.SwIfIndex,
62 _, errx := ifaceClient.SwInterfaceAddDelAddress(ctx, ipAddress)
64 return NewActionResult(err, ActionResultWithDesc("configuring ip address failed"))
66 _, err = ifaceClient.SwInterfaceSetFlags(ctx, &interfaces.SwInterfaceSetFlags{
67 SwIfIndex: createTapReply.SwIfIndex,
68 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
71 return NewActionResult(err, ActionResultWithDesc("failed to set interface state"))
73 _, err = session.NewServiceClient(con).SessionEnableDisable(ctx, &session.SessionEnableDisable{
77 return NewActionResult(err, ActionResultWithDesc("configuration failed"))
79 writeSyncFile(OkResult())