From 4ab667461e7cea96dfecb5daa10d2316d7e17c85 Mon Sep 17 00:00:00 2001 From: He Sun Date: Fri, 13 Dec 2019 15:33:25 +0800 Subject: [PATCH] Add gRPC demo This demo shows how to run gRPC C++ sample client/server on Occlum. --- demos/README.md | 1 + demos/grpc/.gitignore | 4 + demos/grpc/Makefile.patch | 34 ++++++ demos/grpc/README.md | 39 +++++++ demos/grpc/download_and_install_grpc.sh | 115 +++++++++++++++++++++ demos/grpc/download_and_install_openssl.sh | 30 ++++++ demos/grpc/prepare_client_server.sh | 30 ++++++ demos/grpc/run_client_on_host.sh | 16 +++ demos/grpc/run_client_on_occlum.sh | 45 ++++++++ demos/grpc/run_server_on_host.sh | 16 +++ demos/grpc/run_server_on_occlum.sh | 44 ++++++++ 11 files changed, 374 insertions(+) create mode 100644 demos/grpc/.gitignore create mode 100644 demos/grpc/Makefile.patch create mode 100644 demos/grpc/README.md create mode 100755 demos/grpc/download_and_install_grpc.sh create mode 100755 demos/grpc/download_and_install_openssl.sh create mode 100755 demos/grpc/prepare_client_server.sh create mode 100755 demos/grpc/run_client_on_host.sh create mode 100755 demos/grpc/run_client_on_occlum.sh create mode 100755 demos/grpc/run_server_on_host.sh create mode 100755 demos/grpc/run_server_on_occlum.sh diff --git a/demos/README.md b/demos/README.md index 5bb0f951..c72cb8b6 100644 --- a/demos/README.md +++ b/demos/README.md @@ -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/). diff --git a/demos/grpc/.gitignore b/demos/grpc/.gitignore new file mode 100644 index 00000000..187ca617 --- /dev/null +++ b/demos/grpc/.gitignore @@ -0,0 +1,4 @@ +openssl/ +grpc/ +client/ +server/ diff --git a/demos/grpc/Makefile.patch b/demos/grpc/Makefile.patch new file mode 100644 index 00000000..8604a877 --- /dev/null +++ b/demos/grpc/Makefile.patch @@ -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) + diff --git a/demos/grpc/README.md b/demos/grpc/README.md new file mode 100644 index 00000000..95a336a7 --- /dev/null +++ b/demos/grpc/README.md @@ -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. diff --git a/demos/grpc/download_and_install_grpc.sh b/demos/grpc/download_and_install_grpc.sh new file mode 100755 index 00000000..ea5c9169 --- /dev/null +++ b/demos/grpc/download_and_install_grpc.sh @@ -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" diff --git a/demos/grpc/download_and_install_openssl.sh b/demos/grpc/download_and_install_openssl.sh new file mode 100755 index 00000000..b15037e1 --- /dev/null +++ b/demos/grpc/download_and_install_openssl.sh @@ -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!" diff --git a/demos/grpc/prepare_client_server.sh b/demos/grpc/prepare_client_server.sh new file mode 100755 index 00000000..b7b209a9 --- /dev/null +++ b/demos/grpc/prepare_client_server.sh @@ -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 diff --git a/demos/grpc/run_client_on_host.sh b/demos/grpc/run_client_on_host.sh new file mode 100755 index 00000000..5d201404 --- /dev/null +++ b/demos/grpc/run_client_on_host.sh @@ -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 diff --git a/demos/grpc/run_client_on_occlum.sh b/demos/grpc/run_client_on_occlum.sh new file mode 100755 index 00000000..4ea419a9 --- /dev/null +++ b/demos/grpc/run_client_on_occlum.sh @@ -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 diff --git a/demos/grpc/run_server_on_host.sh b/demos/grpc/run_server_on_host.sh new file mode 100755 index 00000000..c0d37ab7 --- /dev/null +++ b/demos/grpc/run_server_on_host.sh @@ -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 diff --git a/demos/grpc/run_server_on_occlum.sh b/demos/grpc/run_server_on_occlum.sh new file mode 100755 index 00000000..0b603af0 --- /dev/null +++ b/demos/grpc/run_server_on_occlum.sh @@ -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