Add gRPC demo

This demo shows how to run gRPC C++ sample client/server on Occlum.
This commit is contained in:
He Sun 2019-12-13 15:33:25 +08:00 committed by Tate, Hongliang Tian
parent 0cef5b1b53
commit 4ab667461e
11 changed files with 374 additions and 0 deletions

@ -15,6 +15,7 @@ This set of demos shows how the Occlum toolchain can be used with different buil
This set of demos shows how real-world apps can be easily run inside SGX enclaves with Occlum.
* `https_server/`: A HTTPS file server based on [Mongoose Embedded Web Server Library](https://github.com/cesanta/mongoose).
* `grpc/`: A client and server communicating through [gRPC](https://grpc.io/).
* `openvino/` A benchmark of [OpenVINO Inference Engine](https://docs.openvinotoolkit.org/2019_R3/_docs_IE_DG_inference_engine_intro.html).
* `tensorflow_lite/`: A demo and benchmark of [Tensorflow Lite](https://www.tensorflow.org/lite) inference engine.
* `xgboost/`: A demo of [XGBoost](https://xgboost.readthedocs.io/en/latest/).

4
demos/grpc/.gitignore vendored Normal file

@ -0,0 +1,4 @@
openssl/
grpc/
client/
server/

34
demos/grpc/Makefile.patch Normal file

@ -0,0 +1,34 @@
diff --git a/examples/cpp/helloworld/Makefile b/examples/cpp/helloworld/Makefile
index 9af7337..c46c525 100644
--- a/examples/cpp/helloworld/Makefile
+++ b/examples/cpp/helloworld/Makefile
@@ -16,8 +16,8 @@
HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
SYSTEM ?= $(HOST_SYSTEM)
-CXX = g++
-CPPFLAGS += `pkg-config --cflags protobuf grpc`
+CXX = occlum-g++
+CPPFLAGS += -I/usr/local/occlum/x86_64-linux-musl/include
CXXFLAGS += -std=c++11
ifeq ($(SYSTEM),Darwin)
LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
@@ -25,7 +25,8 @@ LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
-lgrpc++_reflection\
-ldl
else
-LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
+LDFLAGS += -L/usr/local/occlum/x86_64-linux-musl/lib -lgrpc++ -lgrpc -lprotobuf -lgpr \
+ -lcares -lz -laddress_sorting -pie\
-pthread\
-Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
-ldl
@@ -34,7 +35,7 @@ PROTOC = protoc
GRPC_CPP_PLUGIN = grpc_cpp_plugin
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
-PROTOS_PATH = ../../protos
+PROTOS_PATH = .
vpath %.proto $(PROTOS_PATH)

39
demos/grpc/README.md Normal file

@ -0,0 +1,39 @@
# Run gRPC C++ Client/Server on Occlum
## Step 1:
Downlaod, build and install openssl into `/usr/local/occlum/x86_64-linux-musl/lib`:
```
./download_and_install_openssl.sh
```
## Step 2:
Download, build and install cares, zlib, protobuf and finally gRPC into `/usr/local/occlum/x86_64-linux-musl/lib`:
```
./download_and_install_grpc.sh
```
## Step 3:
Prepare the gRPC C++ Hello World sample project, which consists of a client and server:
```
./prepare_client_server.sh
```
Then you can see the source code in client and server if you want.
## Step 4:
Run the demo `server` which will listen on port `50051` on occlum:
```
./run_server_on_occlum.sh
```
or on host:
```
./run_server_on_host.sh
```
Then you can invoke gRPC service by running `client` in a different terminal on occlum:
```
./run_client_on_occlum.sh
```
or on host:
```
./run_server_on_host.sh
```
And you will see the "Greeter received: Hello world" in the client side output.

@ -0,0 +1,115 @@
#!/bin/sh
install_dir=/usr/local/occlum/x86_64-linux-musl/
export PATH=$PATH:$install_dir/bin
git clone https://github.com/grpc/grpc.git
cd grpc
git checkout tags/v1.24.3
if [ $? -ne 0 ]
then
echo "git clone failed"
exit 1
fi
# Install c-ares
cd third_party/cares/cares
git submodule update --init .
git checkout tags/cares-1_15_0
mkdir -p build
cd build
cmake ../ \
-DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=occlum-gcc \
-DCMAKE_INSTALL_PREFIX=$install_dir
if [ $? -ne 0 ]
then
echo "cares cmake failed"
exit 1
fi
make -j8
if [ $? -ne 0 ]
then
echo "cares make failed"
exit 1
fi
make install
cd ../../../..
# Install zlib
cd third_party/zlib
git submodule update --init .
git checkout tags/v1.2.11
mkdir -p build
cd build
cmake ../ \
-DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=occlum-gcc \
-DCMAKE_CXX_COMPILER=occlum-g++ -DCMAKE_INSTALL_PREFIX=$install_dir \
-DCMAKE_NO_SYSTEM_FROM_IMPORTED=TRUE
if [ $? -ne 0 ]
then
echo "zlib cmake failed"
exit 1
fi
make -j8
if [ $? -ne 0 ]
then
echo "zlib make failed"
exit 1
fi
make install
cd ../../..
# Install protobuf
cd third_party/protobuf
git submodule update --init .
git checkout tags/v3.10.0
cd cmake
mkdir -p build
cd build
cmake ../ \
-Dprotobuf_BUILD_TESTS=OFF -DBUILD_SHARED_LIBS=TRUE \
-DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=occlum-gcc \
-DCMAKE_CXX_COMPILER=occlum-g++ -DCMAKE_INSTALL_PREFIX=$install_dir \
-DCMAKE_NO_SYSTEM_FROM_IMPORTED=TRUE
if [ $? -ne 0 ]
then
echo "protobuf cmake failed"
exit 1
fi
make -j8
if [ $? -ne 0 ]
then
echo "protobuf make failed"
exit 1
fi
make install
cd ../../../..
cp $install_dir/bin/protoc /usr/bin
# Install gRPC
cd cmake
mkdir -p build
cd build
cmake ../.. \
-DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=occlum-gcc \
-DCMAKE_CXX_COMPILER=occlum-g++ -DgRPC_INSTALL=ON -DgRPC_PROTOBUF_PROVIDER=package \
-DgRPC_ZLIB_PROVIDER=package -DgRPC_CARES_PROVIDER=package \
-DgRPC_SSL_PROVIDER=package -DCMAKE_PREFIX_PATH=$install_dir \
-DCMAKE_NO_SYSTEM_FROM_IMPORTED=TRUE -DCMAKE_INSTALL_PREFIX=$install_dir
if [ $? -ne 0 ]
then
echo "grpc cmake failed"
exit 1
fi
make -j8
if [ $? -ne 0 ]
then
echo "grpc make failed"
exit 1
fi
make install
echo "gRPC build success"

@ -0,0 +1,30 @@
#!/bin/sh
#copyright@antfinancial:adopted from a script written by geding
git clone http://github.com/openssl/openssl
cd openssl
git checkout tags/OpenSSL_1_1_1
CC=occlum-gcc ./config \
--prefix=/usr/local/occlum/x86_64-linux-musl \
--openssldir=/usr/local/occlum/x86_64-linux-musl/ssl \
--with-rand-seed=rdcpu \
no-async no-zlib
if [ $? -ne 0 ]
then
echo "./config command failed."
exit 1
fi
make -j8
if [ $? -ne 0 ]
then
echo "make command failed."
exit 1
fi
make install
if [ $? -ne 0 ]
then
echo "make install command failed."
exit 1
fi
echo "build and install openssl success!"

@ -0,0 +1,30 @@
#!/bin/sh
cd grpc/examples/cpp/helloworld
git apply ../../../../Makefile.patch
if [ $? -ne 0 ]
then
echo "patch failed"
exit 1
fi
cp ../../protos/helloworld.proto .
cd ../../../../
mkdir -p client
mkdir -p server
cp -R grpc/examples/cpp/helloworld/* client
cp -R grpc/examples/cpp/helloworld/* server
cd grpc
git checkout examples/cpp/helloworld/Makefile
cd ..
rm -rf client/cocoapods/
rm -rf client/cmake_externalproject/
rm client/CMakeLists.txt
rm -rf server/cocoapods/
rm -rf server/cmake_externalproject/
rm server/CMakeLists.txt

@ -0,0 +1,16 @@
#!/bin/sh
install_dir=/usr/local/occlum/x86_64-linux-musl
export PATH=$PATH:$install_dir/bin
cd client
make -j8
if [ $? -ne 0 ]
then
echo "demo make failed"
exit 1
fi
./greeter_client

@ -0,0 +1,45 @@
#!/bin/bash
install_dir=/usr/local/occlum/x86_64-linux-musl
export PATH=$PATH:$install_dir/bin
cd client
make -j8
if [ $? -ne 0 ]
then
echo "demo make failed"
exit 1
fi
rm -rf occlum_context
mkdir occlum_context
cd occlum_context
occlum init
if [ $? -ne 0 ]
then
echo "occlum init failed"
exit 1
fi
mkdir -p image/etc
cp /etc/resolv.conf image/etc
cp ../greeter_client image/bin
cp $install_dir/lib/libprotobuf.so.3.10.0.0 image/lib
cp $install_dir/lib/libcares.so.2 image/lib
cp $install_dir/lib/libz.so.1 image/lib
if [ $? -ne 0 ]
then
echo "libraries copied failed"
exit 1
fi
occlum build
if [ $? -ne 0 ]
then
echo "occlum build failed"
exit 1
fi
occlum run /bin/greeter_client

@ -0,0 +1,16 @@
#!/bin/sh
install_dir=/usr/local/occlum/x86_64-linux-musl
export PATH=$PATH:$install_dir/bin
cd server
make -j8
if [ $? -ne 0 ]
then
echo "demo make failed"
exit 1
fi
./greeter_server

@ -0,0 +1,44 @@
#!/bin/bash
install_dir=/usr/local/occlum/x86_64-linux-musl
export PATH=$PATH:$install_dir/bin
cd server
make -j8
if [ $? -ne 0 ]
then
echo "demo make failed"
exit 1
fi
rm -rf occlum_context
mkdir occlum_context
cd occlum_context
occlum init
if [ $? -ne 0 ]
then
echo "occlum init failed"
exit 1
fi
mkdir -p image/etc
cp /etc/resolv.conf image/etc
cp ../greeter_server image/bin
cp $install_dir/lib/libprotobuf.so.3.10.0.0 image/lib
cp $install_dir/lib/libcares.so.2 image/lib
cp $install_dir/lib/libz.so.1 image/lib
if [ $? -ne 0 ]
then
echo "libraries copied failed"
exit 1
fi
occlum build
if [ $? -ne 0 ]
then
echo "occlum build failed"
exit 1
fi
occlum run /bin/greeter_server