Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor GoVPP usage and generation #22

Merged
merged 13 commits into from
Mar 3, 2023
10 changes: 7 additions & 3 deletions examples/gen.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package consumer
//go:build generate

package examples

import (
_ "go.fd.io/govpp/binapi"
_ "go.fd.io/govpp/cmd/binapi-generator"

_ "github.com/calico-vpp/vpplink/pkg"
)

//go:generate go build -buildmode=plugin -o ./.bin/vpplink_plugin.so github.com/calico-vpp/vpplink/pkg
//go:generate go run go.fd.io/govpp/cmd/binapi-generator --plugin ./.bin/vpplink_plugin.so --vpp $VPP_DIR -o ./impl
//go:generate go run go.fd.io/govpp/cmd/binapi-generator --gen ./.bin/vpplink_plugin.so,rpc --input ../../vpp -o ./impl
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ go 1.18

require (
github.com/hashicorp/go-version v1.6.0
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.2.2
go.fd.io/govpp v0.6.1-0.20220928123720-fe96cac6bad7
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.3.0
go.fd.io/govpp v0.8.0-alpha.0.20230207075433-b71f13cf4640
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
)

require (
github.com/bennyscetbun/jsongo v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/lunixbochs/struc v0.0.0-20200521075829-a4cb8d33dbbe // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/text v0.3.7 // indirect
)
28 changes: 16 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
github.com/bennyscetbun/jsongo v1.1.0 h1:ZDSks3aLP13jhY139lWaUqZaU8G0tELMohzumut/KDM=
github.com/bennyscetbun/jsongo v1.1.0/go.mod h1:suxbVmjBV8+A2BBAM5EYVh6Uj8j3rqJhzWf3hv7Ff8U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/lunixbochs/struc v0.0.0-20200521075829-a4cb8d33dbbe h1:ewr1srjRCmcQogPQ/NCx6XCk6LGVmsVCc9Y3vvPZj+Y=
github.com/lunixbochs/struc v0.0.0-20200521075829-a4cb8d33dbbe/go.mod h1:vy1vK6wD6j7xX6O6hXe621WabdtNkou2h7uRtTfRMyg=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
go.fd.io/govpp v0.6.1-0.20220928122602-005dba9b1759 h1:HK0Vnnq2fwEW7dSygqsUs+U36Stg0pzp7qJgLmaJHhQ=
go.fd.io/govpp v0.6.1-0.20220928122602-005dba9b1759/go.mod h1:oTfy7wk/8MSmC796s1OBI1TLLGpI6bwOwed7Htp0uh8=
go.fd.io/govpp v0.6.1-0.20220928123720-fe96cac6bad7 h1:WYc0Ja3ai7F5+d/pIUwbQ4mfMBawiIktfm5mGcMT9yE=
go.fd.io/govpp v0.6.1-0.20220928123720-fe96cac6bad7/go.mod h1:oTfy7wk/8MSmC796s1OBI1TLLGpI6bwOwed7Htp0uh8=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
go.fd.io/govpp v0.8.0-alpha.0.20230207075433-b71f13cf4640 h1:gdxIVZCKmuyRhXVNUb+VvR+JOAPxzeEwBWhDDReclHE=
go.fd.io/govpp v0.8.0-alpha.0.20230207075433-b71f13cf4640/go.mod h1:CP9b1rXbEow1suEDn0k1nwHQdrV0YpuIo7LUPw3+/H4=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
22 changes: 10 additions & 12 deletions pkg/gen_vpplink.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,11 @@ package main
import (
"embed"
_ "embed"

"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
"go.fd.io/govpp/binapigen"
"go.fd.io/govpp/version"

"github.com/calico-vpp/vpplink/pkg/wrappergen"
)
Expand Down Expand Up @@ -57,7 +51,9 @@ func init() {

}

func GenerateFile(gen *binapigen.Generator) {
func GenerateFile(gen *binapigen.Generator, file *binapigen.File) *binapigen.GenFile {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more of a Govpp comment, but anyway, the issue with this is we are going to be called once for every file even though the generation we are doing does not depend on the files being passed.
That's why I originally went with changing the signature for GenerateFile in https://github.com/FDio/govpp/pull/68/files#diff-4d2bacb13fc145b9d2f0e4f6a3771998ef6f3ff8d78dde2682c577febfa0f16eR30

logrus.Infof("[WRAPPERGEN] GenerateFile: %v", file.Desc.Path)

// We output vpplink one directory higher than the regular bindings
basePkgName := filepath.Join(gen.GetOpts().ImportPrefix, "..")
outputDir := filepath.Join(gen.GetOpts().OutputDir, "..")
Expand All @@ -69,15 +65,17 @@ func GenerateFile(gen *binapigen.Generator) {
)

// Execute all the templates
err := parsedTemplates.ExecuteAll(outputDir, data)
err := parsedTemplates.ExecuteAll(outputDir, data, gen)
if err != nil {
logrus.Fatalf("failed to execute template: %s", err)
}

createGenerateLog(gen.GetOpts().ApiDir, filepath.Join(outputDir, generateLogFname))
//createGenerateLog(gen.GetOpts().ApiDir, filepath.Join(outputDir, generateLogFname))

return nil
}

func createGenerateLog(apiDir string, fname string) {
/*func createGenerateLog(apiDir string, fname string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's debatable whether we want to keep this or not. I'd arguee it's always good to have a trail of what generated the bindings. The file headers can serve for this as well, the good thing about having a dedicated file though is that it can be parsed/copied to a container where the file headers are more for human consumption.

vppSrcDir, err := binapigen.FindGitRepoRootDir(apiDir)
if err != nil {
return
Expand All @@ -88,7 +86,7 @@ func createGenerateLog(apiDir string, fname string) {
logrus.Fatalf("Unable to get vpp version : %s", err)
}

cmd := exec.Command("bash", "-c", "git log $(git log origin/master..HEAD --oneline | tail -1 | awk '{print $1}')^ --oneline -1")
cmd := exec.Command("bash", "-c", "git log --oneline -1 $(git log origin/master..HEAD --oneline | tail -1 | awk '{print $1}')")
cmd.Dir = binapigen.ExpandPaths(vppSrcDir)
cmd.Stderr = os.Stderr
out, err := cmd.Output()
Expand Down Expand Up @@ -126,4 +124,4 @@ func createGenerateLog(apiDir string, fname string) {
logrus.Fatalf("Unable to close file %s %s", fname, err)
}

}
}*/
54 changes: 14 additions & 40 deletions pkg/templates/ip_helpers.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package {{ .PackageName }}

import (
"net"

types "github.com/calico-vpp/vpplink/api/v0"

"{{ .BinAPI }}/ip_types"
)

func toVppIPProto(proto types.IPProto) ip_types.IPProto {
switch proto {
case types.UDP:
Expand All @@ -36,32 +37,24 @@ func toVppIPProto(proto types.IPProto) ip_types.IPProto {
}
return ip_types.IP_API_PROTO_RESERVED
}

// Make sure you really call this with an IPv4 address...
func toVppIP4Address(addr net.IP) ip_types.IP4Address {
ip := [4]uint8{}
copy(ip[:], addr.To4())
return ip
}

func toVppIP6Address(addr net.IP) ip_types.IP6Address {
ip := [16]uint8{}
copy(ip[:], addr)
return ip
}

func toVppAddress(addr net.IP) ip_types.Address {
a := ip_types.Address{}
if addr.To4() == nil {
a.Af = ip_types.ADDRESS_IP6
ip := [16]uint8{}
copy(ip[:], addr)
a.Un = ip_types.AddressUnionIP6(ip)
} else {
a.Af = ip_types.ADDRESS_IP4
ip := [4]uint8{}
copy(ip[:], addr.To4())
a.Un = ip_types.AddressUnionIP4(ip)
}
return a
return ip_types.NewAddress(addr)
}

func fromVppIpAddressUnion(Un ip_types.AddressUnion, isv6 bool) net.IP {
if isv6 {
a := Un.GetIP6()
Expand All @@ -71,15 +64,11 @@ func fromVppIpAddressUnion(Un ip_types.AddressUnion, isv6 bool) net.IP {
return net.IP(a[:])
}
}
func fromVppAddress(addr ip_types.Address) net.IP {
return fromVppIpAddressUnion(
ip_types.AddressUnion(addr.Un),
addr.Af == ip_types.ADDRESS_IP6,
)
}

func toVppAddressWithPrefix(prefix *net.IPNet) ip_types.AddressWithPrefix {
return ip_types.AddressWithPrefix(toVppPrefix(prefix))
}

func toVppPrefix(prefix *net.IPNet) ip_types.Prefix {
len, _ := prefix.Mask.Size()
r := ip_types.Prefix{
Expand All @@ -88,9 +77,11 @@ func toVppPrefix(prefix *net.IPNet) ip_types.Prefix {
}
return r
}

func toVppIp4WithPrefix(prefix *net.IPNet) ip_types.IP4AddressWithPrefix {
return ip_types.IP4AddressWithPrefix(toVppIP4Prefix(prefix))
}

func toVppIP4Prefix(prefix *net.IPNet) ip_types.IP4Prefix {
len, _ := prefix.Mask.Size()
r := ip_types.IP4Prefix{
Expand All @@ -99,33 +90,16 @@ func toVppIP4Prefix(prefix *net.IPNet) ip_types.IP4Prefix {
}
return r
}
func fromVppAddressWithPrefix(prefix ip_types.AddressWithPrefix) *net.IPNet {
return fromVppPrefix(ip_types.Prefix(prefix))
}
func fromVppPrefix(prefix ip_types.Prefix) *net.IPNet {
addressSize := 32
if prefix.Address.Af == ip_types.ADDRESS_IP6 {
addressSize = 128
}
return &net.IPNet{
IP: fromVppAddress(prefix.Address),
Mask: net.CIDRMask(int(prefix.Len), addressSize),
}
}
func toVppAddressFamily(isv6 bool) ip_types.AddressFamily {
if isv6 {
return ip_types.ADDRESS_IP6
}
return ip_types.ADDRESS_IP4
}

func ToVppPrefix(prefix *net.IPNet) ip_types.Prefix {
len, _ := prefix.Mask.Size()
r := ip_types.Prefix{
Address: ip_types.AddressFromIP(prefix.IP),
Address: ip_types.NewAddress(prefix.IP),
Len: uint8(len),
}
return r
}

func FromVppPrefix(prefix ip_types.Prefix) *net.IPNet {
addressSize := 32
if prefix.Address.Af == ip_types.ADDRESS_IP6 {
Expand Down
53 changes: 27 additions & 26 deletions pkg/templates/ipip.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,33 @@
package {{ .PackageName }}

import (
types "github.com/calico-vpp/vpplink/api/v0"
"github.com/pkg/errors"
"fmt"
"io"

types "github.com/calico-vpp/vpplink/api/v0"
"{{ .BinAPI }}/interface_types"
"{{ .BinAPI }}/ip_types"
"{{ .BinAPI }}/ipip"
)

func (v *Vpp) ListIPIPTunnels() ([]*types.IPIPTunnel, error) {
v.Lock()
defer v.Unlock()
client := ipip.NewServiceClient(v.conn)

tunnels := make([]*types.IPIPTunnel, 0)
request := &ipip.IpipTunnelDump{
stream, err := client.IpipTunnelDump(v.ctx, &ipip.IpipTunnelDump{
SwIfIndex: interface_types.InterfaceIndex(types.InvalidInterface),
})
if err != nil {
return nil, fmt.Errorf("failed to list IPIP tunnels: %w", err)
}
stream := v.GetChannel().SendMultiRequest(request)
var tunnels []*types.IPIPTunnel
for {
response := &ipip.IpipTunnelDetails{}
stop, err := stream.ReceiveReply(response)
if err != nil {
return nil, errors.Wrapf(err, "error listing IPIP tunnels")
}
if stop {
response, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("failed to list IPIP tunnels: %w", err)
}
tunnels = append(tunnels, &types.IPIPTunnel{
Src: response.Tunnel.Src.ToIP(),
Dst: response.Tunnel.Dst.ToIP(),
Expand All @@ -54,31 +55,31 @@ func (v *Vpp) ListIPIPTunnels() ([]*types.IPIPTunnel, error) {
}

func (v *Vpp) AddIPIPTunnel(tunnel *types.IPIPTunnel) (uint32, error) {
response := &ipip.IpipAddTunnelReply{}
request := &ipip.IpipAddTunnel{
client := ipip.NewServiceClient(v.conn)

response, err := client.IpipAddTunnel(v.ctx, &ipip.IpipAddTunnel{
Tunnel: ipip.IpipTunnel{
Instance: ^uint32(0),
Src: ip_types.AddressFromIP(tunnel.Src),
Dst: ip_types.AddressFromIP(tunnel.Dst),
Src: ip_types.NewAddress(tunnel.Src),
Dst: ip_types.NewAddress(tunnel.Dst),
TableID: tunnel.TableID,
},
}
err := v.SendRequestAwaitReply(request, response)
})
if err != nil {
return InvalidSwIfIndex, err
return InvalidSwIfIndex, fmt.Errorf("failed to add IPIP tunnel: %w", err)
}
tunnel.SwIfIndex = uint32(response.SwIfIndex)
return uint32(response.SwIfIndex), nil
}

func (v *Vpp) DelIPIPTunnel(tunnel *types.IPIPTunnel) (err error) {
response := &ipip.IpipDelTunnelReply{}
request := &ipip.IpipDelTunnel{
func (v *Vpp) DelIPIPTunnel(tunnel *types.IPIPTunnel) error {
client := ipip.NewServiceClient(v.conn)

_, err := client.IpipDelTunnel(v.ctx, &ipip.IpipDelTunnel{
SwIfIndex: interface_types.InterfaceIndex(tunnel.SwIfIndex),
}
err = v.SendRequestAwaitReply(request, response)
})
if err != nil {
return err
return fmt.Errorf("failed to delete IPIP tunnel: %w", err)
}
return nil
}
Expand Down
Loading