Skip to content

Commit

Permalink
refactor(crit): move pb.go files to separate dirs
Browse files Browse the repository at this point in the history
Using the new Python script, the protobuf bindings are now generated in
independent packages. This commit refactors CRIT to work with the new
organisation of files. It does not change the implementation of CRIT
in any way apart from resolving the imports correctly.

Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
  • Loading branch information
snprajwal committed Apr 4, 2023
1 parent 7a3d779 commit efa03cd
Show file tree
Hide file tree
Showing 86 changed files with 977 additions and 941 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ jobs:
- name: Test crit
run: |
if [ "${{ matrix.criu_branch }}" = "criu-dev" ]; then
# We need to use the protobuf definitions from the criu-dev
# branch as it might have changed.
make -C crit update-proto GIT_BRANCH=${{ matrix.criu_branch }}
# First update protobuf. It is too old in the Ubuntu image.
curl -Lo protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v3.20.3/protoc-3.20.3-linux-x86_64.zip
curl -Lo protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v22.2/protoc-22.2-linux-x86_64.zip
sudo unzip -o protoc.zip -d /usr
sudo -E make -C crit clean
make -C crit gen-proto
sudo -E make -C crit all
# We need to use the protobuf definitions from the criu-dev
# branch as it might have changed.
make -C scripts/proto-gen proto-update GIT_BRANCH=${{ matrix.criu_branch }}
# Generate the bindings
make -C scripts/proto-gen
# Build the CRIT binary
sudo make -C crit
fi
sudo -E make -C test crit-test
Expand Down
38 changes: 1 addition & 37 deletions crit/Makefile
Original file line number Diff line number Diff line change
@@ -1,45 +1,9 @@
GO ?= go

# The import path that protoc will use if a proto file imports another one
import_path := github.com/checkpoint-restore/go-criu/crit/images
# Path to .proto source files
proto_path := ./images
# Generate string of all .proto filenames
proto_files := $(sort $(subst $(proto_path)/,,$(wildcard $(proto_path)/*.proto)))
# Generate M flag to specify import path for all .proto files
# and replace all spaces with commas to use with go_opt flag
comma := ,
proto_opts := $(subst $() $(),$(comma),$(patsubst %,M%=$(import_path),$(proto_files)))
GIT_BRANCH := master

all: gen-proto bin/crit

update-proto:
rm ./images/*.proto || true
git clone --depth 1 --branch $(GIT_BRANCH) https://github.com/checkpoint-restore/criu criu-temp
cp criu-temp/images/*.proto ./images/
# rpc.proto is not an image and it is used only to communicate criu-service and swrk.
rm -rf criu-temp images/rpc.proto
# To prevent namespace conflict with proto files
# in github.com/letsencrypt/boulder, we prepend
# a prefix to the filenames.
mv ./images/sa.proto ./images/criu-sa.proto
sed -i 's/sa\.proto/criu-sa\.proto/g' images/*.proto
mv ./images/core.proto ./images/criu-core.proto
sed -i 's/core\.proto/criu-core\.proto/g' images/*.proto

gen-proto:
rm -f ./images/*.pb.go
@protoc \
--proto_path=$(proto_path) \
--go_out=$(proto_path) \
--go_opt=paths=source_relative,$(proto_opts) \
$(proto_files)

bin/crit: cmd/cli.go
$(GO) build -o $@ $^

clean:
rm -f bin/crit

.PHONY: all gen-proto update-proto clean
.PHONY: clean
2 changes: 1 addition & 1 deletion crit/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

var (
// The crit service used to invoke all commands
c crit.CritSvc
c crit.Critter

// All members needed for crit struct
inputFilePath string
Expand Down
10 changes: 5 additions & 5 deletions crit/crit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"os"
)

// CritSvc is the interface that wraps all CRIT operations.
// Critter is the interface that wraps all CRIT operations.
// To create a CRIT service instance, use New().
type CritSvc interface {
type Critter interface {
// Read binary image file into Go struct (decode.go)
Decode() (*CriuImage, error)
// Read only counts of image file entries into Go struct
Expand All @@ -26,7 +26,7 @@ type CritSvc interface {
ExploreRss() ([]*RssMap, error)
}

// crit implements the CritSvc interface. It contains:
// crit implements the Critter interface. It contains:
// * Path of the input file
// * Path of the output file
// * Path of the input directory (for `crit explore`)
Expand All @@ -48,7 +48,7 @@ func New(
inputFilePath, outputFilePath,
inputDirPath string,
pretty, noPayload bool,
) CritSvc {
) Critter {
return &crit{
inputFilePath: inputFilePath,
outputFilePath: outputFilePath,
Expand All @@ -66,7 +66,7 @@ func NewCli(
inputFilePath, outputFilePath,
inputDirPath string,
pretty, noPayload bool,
) CritSvc {
) Critter {
return &crit{
inputFilePath: inputFilePath,
outputFilePath: outputFilePath,
Expand Down
24 changes: 15 additions & 9 deletions crit/decode-extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import (
"io"
"os"

"github.com/checkpoint-restore/go-criu/v6/crit/images"
bpfmap_data "github.com/checkpoint-restore/go-criu/v6/crit/images/bpfmap-data"
ipc_msg "github.com/checkpoint-restore/go-criu/v6/crit/images/ipc-msg"
ipc_sem "github.com/checkpoint-restore/go-criu/v6/crit/images/ipc-sem"
ipc_shm "github.com/checkpoint-restore/go-criu/v6/crit/images/ipc-shm"
pipe_data "github.com/checkpoint-restore/go-criu/v6/crit/images/pipe-data"
sk_packet "github.com/checkpoint-restore/go-criu/v6/crit/images/sk-packet"
tcp_stream "github.com/checkpoint-restore/go-criu/v6/crit/images/tcp-stream"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
Expand All @@ -19,7 +25,7 @@ func decodePipesData(
payload proto.Message,
noPayload bool,
) (string, error) {
p, ok := payload.(*images.PipeDataEntry)
p, ok := payload.(*pipe_data.PipeDataEntry)
if !ok {
return "", errors.New("Unable to assert payload type")
}
Expand All @@ -45,7 +51,7 @@ func decodeSkQueues(
payload proto.Message,
noPayload bool,
) (string, error) {
p, ok := payload.(*images.SkPacketEntry)
p, ok := payload.(*sk_packet.SkPacketEntry)
if !ok {
return "", errors.New("Unable to assert payload type")
}
Expand Down Expand Up @@ -76,7 +82,7 @@ func decodeTcpStream(
payload proto.Message,
noPayload bool,
) (string, error) {
p, ok := payload.(*images.TcpStreamEntry)
p, ok := payload.(*tcp_stream.TcpStreamEntry)
if !ok {
return "", errors.New("Unable to assert payload type")
}
Expand Down Expand Up @@ -113,7 +119,7 @@ func decodeBpfmapData(
payload proto.Message,
noPayload bool,
) (string, error) {
p, ok := payload.(*images.BpfmapDataEntry)
p, ok := payload.(*bpfmap_data.BpfmapDataEntry)
if !ok {
return "", errors.New("Unable to assert payload type")
}
Expand All @@ -139,7 +145,7 @@ func decodeIpcSem(
payload proto.Message,
noPayload bool,
) (string, error) {
p, ok := payload.(*images.IpcSemEntry)
p, ok := payload.(*ipc_sem.IpcSemEntry)
if !ok {
return "", errors.New("Unable to assert payload type")
}
Expand Down Expand Up @@ -178,7 +184,7 @@ func decodeIpcShm(
payload proto.Message,
noPayload bool,
) (string, error) {
p, ok := payload.(*images.IpcShmEntry)
p, ok := payload.(*ipc_shm.IpcShmEntry)
if !ok {
return "", errors.New("Unable to assert payload type")
}
Expand Down Expand Up @@ -210,7 +216,7 @@ func decodeIpcMsg(
payload proto.Message,
noPayload bool,
) (string, error) {
p, ok := payload.(*images.IpcMsgEntry)
p, ok := payload.(*ipc_msg.IpcMsgEntry)
if !ok {
return "", errors.New("Unable to assert payload type")
}
Expand All @@ -234,7 +240,7 @@ func decodeIpcMsg(
if _, err = f.Read(msgBuf); err != nil {
return "", err
}
msg := &images.IpcMsg{}
msg := &ipc_msg.IpcMsg{}
if err = proto.Unmarshal(msgBuf, msg); err != nil {
return "", err
}
Expand Down
10 changes: 6 additions & 4 deletions crit/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"

"github.com/checkpoint-restore/go-criu/v6/crit/images"
ghost_file "github.com/checkpoint-restore/go-criu/v6/crit/images/ghost-file"
"github.com/checkpoint-restore/go-criu/v6/crit/images/pagemap"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -101,7 +103,7 @@ func (img *CriuImage) decodeDefault(
func (img *CriuImage) decodePagemap(f *os.File) error {
sizeBuf := make([]byte, 4)
// First entry is pagemap head
var payload proto.Message = &images.PagemapHead{}
var payload proto.Message = &pagemap.PagemapHead{}
// Read payload size and payload until EOF
for {
if n, err := f.Read(sizeBuf); err != nil {
Expand All @@ -122,7 +124,7 @@ func (img *CriuImage) decodePagemap(f *os.File) error {
entry := CriuEntry{Message: payload}
img.Entries = append(img.Entries, &entry)
// Create struct for next entry
payload = &images.PagemapEntry{}
payload = &pagemap.PagemapEntry{}
}
return nil
}
Expand All @@ -134,7 +136,7 @@ func (img *CriuImage) decodeGhostFile(f *os.File, noPayload bool) error {
return err
}
// Create proto struct for primary entry
payload := &images.GhostFileEntry{}
payload := &ghost_file.GhostFileEntry{}
payloadSize := uint64(binary.LittleEndian.Uint32(sizeBuf))
payloadBuf := make([]byte, payloadSize)
if _, err := f.Read(payloadBuf); err != nil {
Expand All @@ -156,7 +158,7 @@ func (img *CriuImage) decodeGhostFile(f *os.File, noPayload bool) error {
return err
}
// Create proto struct for chunk
payload := &images.GhostChunkEntry{}
payload := &ghost_file.GhostChunkEntry{}
payloadSize := uint64(binary.LittleEndian.Uint32(sizeBuf))
payloadBuf := make([]byte, payloadSize)
if _, err := f.Read(payloadBuf); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions crit/encode-extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/binary"
"encoding/json"

"github.com/checkpoint-restore/go-criu/v6/crit/images"
ipc_msg "github.com/checkpoint-restore/go-criu/v6/crit/images/ipc-msg"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -91,7 +91,7 @@ func encodeIpcMsg(extra string) ([]byte, error) {
sizeBuf := make([]byte, 4)

for i := 0; i < len(extraEntries)/2; i++ {
msg := &images.IpcMsg{}
msg := &ipc_msg.IpcMsg{}
// Unmarshal JSON into proto struct
if err := protojson.Unmarshal([]byte(extraEntries[i]), msg); err != nil {
return nil, err
Expand Down

0 comments on commit efa03cd

Please sign in to comment.