Implement part of Berkeley Socket API for libc-wasi (#1036)
Refer to [Networking API design](https://github.com/WebAssembly/WASI/issues/370) and [feat(socket): berkeley socket API v2](https://github.com/WebAssembly/WASI/pull/459): - Support the socket API of synchronous mode, including `socket/bind/listen/accept/send/recv/close/shutdown`, the asynchronous mode isn't supported yet. - Support adding `--addr-pool=<pool1,pool2,..>` argument for command line to identify the valid ip address range - Add socket-api sample and update the document
This commit is contained in:
162
samples/socket-api/CMakeLists.txt
Normal file
162
samples/socket-api/CMakeLists.txt
Normal file
@ -0,0 +1,162 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
cmake_minimum_required(VERSION 2.8...3.18)
|
||||
project(socket_api_sample)
|
||||
|
||||
#######################################
|
||||
## Detect toolchain
|
||||
#######################################
|
||||
message(CHECK_START "Detecting WASI-SDK at /opt/wasi-sdk")
|
||||
if(NOT (DEFINED WASI_SDK_DIR OR DEFINED CACHE{WASI_SDK_DIR}))
|
||||
find_path(WASI_SDK_PARENT
|
||||
wasi-sdk
|
||||
PATHS /opt
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
)
|
||||
if(WASI_SDK_PARENT)
|
||||
set(WASI_SDK_DIR ${WASI_SDK_PARENT}/wasi-sdk)
|
||||
endif()
|
||||
endif()
|
||||
if(WASI_SDK_DIR)
|
||||
message(CHECK_PASS "found")
|
||||
else()
|
||||
message(CHECK_FAIL "not found")
|
||||
endif()
|
||||
|
||||
if(NOT EXISTS ${WASI_SDK_DIR})
|
||||
message(FATAL_ERROR "Please install WASI-SDK under /opt/wasi-sdk")
|
||||
endif()
|
||||
|
||||
message(CHECK_START "Detecting WASI_TOOLCHAIN_FILE at ${WASI_SDK_DIR}")
|
||||
find_file(WASI_TOOLCHAIN_FILE
|
||||
wasi-sdk.cmake
|
||||
PATHS "${WASI_SDK_DIR}/share/cmake"
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
)
|
||||
if(WASI_TOOLCHAIN_FILE)
|
||||
message(CHECK_PASS "found")
|
||||
else()
|
||||
message(CHECK_FAIL "not found")
|
||||
endif()
|
||||
|
||||
if(WASI_TOOLCHAIN_FILE-NOTFOUND)
|
||||
message(FATAL_ERROR "Can not find wasi-sdk.cmake under ${WASI_SDK_DIR}")
|
||||
endif()
|
||||
|
||||
message(CHECK_START "Detecting WASI_SYS_ROOT at ${WASI_SDK_DIR}")
|
||||
find_path(WASI_SYS_ROOT
|
||||
wasi-sysroot
|
||||
PATHS "${WASI_SDK_DIR}/share"
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
)
|
||||
if(WASI_SYS_ROOT)
|
||||
message(CHECK_PASS "found")
|
||||
set(WASI_SYS_ROOT ${WASI_SYS_ROOT}/wasi-sysroot)
|
||||
else()
|
||||
message(CHECK_FAIL "not found")
|
||||
endif()
|
||||
|
||||
if(WASI_SYS_ROOT-NOTFOUND)
|
||||
message(FATAL_ERROR "Can not find wasi-sysroot/ under ${WASI_SDK_DIR}")
|
||||
endif()
|
||||
|
||||
message(STATUS "WASI_SDK_DIR is ${WASI_SDK_DIR}")
|
||||
message(STATUS "WASI_TOOLCHAIN_FILE is ${WASI_TOOLCHAIN_FILE}")
|
||||
message(STATUS "WASI_SYS_ROOT is ${WASI_SYS_ROOT}")
|
||||
|
||||
###############################################################
|
||||
## Build socket applications of wasm version and native version
|
||||
###############################################################
|
||||
include(ExternalProject)
|
||||
|
||||
ExternalProject_Add(wasm-app
|
||||
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src
|
||||
UPDATE_COMMAND ""
|
||||
PATCH_COMMAND ""
|
||||
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../wamr-sdk/app/libc-builtin-sysroot/include/pthread.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/wasm-src/inc
|
||||
&& ${CMAKE_COMMAND}
|
||||
-DWASI_SDK_PREFIX=${WASI_SDK_DIR}
|
||||
-DCMAKE_TOOLCHAIN_FILE=${WASI_TOOLCHAIN_FILE}
|
||||
-DCMAKE_SYSROOT=${WASI_SYS_ROOT}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/wasm-src
|
||||
BUILD_COMMAND ${CMAKE_COMMAND} --build .
|
||||
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy
|
||||
tcp_client.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build
|
||||
tcp_server.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build
|
||||
tcp_client.wasm.dump ${CMAKE_CURRENT_SOURCE_DIR}/build
|
||||
tcp_server.wasm.dump ${CMAKE_CURRENT_SOURCE_DIR}/build
|
||||
)
|
||||
|
||||
add_executable(tcp_server ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src/tcp_server.c)
|
||||
target_link_libraries(tcp_server pthread)
|
||||
add_executable(tcp_client ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src/tcp_client.c)
|
||||
|
||||
############################################
|
||||
## Build iwasm with wasi and pthread support
|
||||
############################################
|
||||
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
|
||||
if (APPLE)
|
||||
add_definitions(-DBH_PLATFORM_DARWIN)
|
||||
endif ()
|
||||
|
||||
# Reset linker flags
|
||||
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
|
||||
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
|
||||
|
||||
# Set WAMR features
|
||||
|
||||
# Set WAMR_BUILD_TARGET, currently values supported:
|
||||
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
|
||||
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
|
||||
if (NOT DEFINED WAMR_BUILD_TARGET)
|
||||
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
|
||||
set (WAMR_BUILD_TARGET "AARCH64")
|
||||
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
|
||||
set (WAMR_BUILD_TARGET "RISCV64")
|
||||
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
# Build as X86_64 by default in 64-bit platform
|
||||
set (WAMR_BUILD_TARGET "X86_64")
|
||||
else ()
|
||||
# Build as X86_32 by default in 32-bit platform
|
||||
set (WAMR_BUILD_TARGET "X86_32")
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (NOT CMAKE_BUILD_TYPE)
|
||||
set (CMAKE_BUILD_TYPE Release)
|
||||
endif ()
|
||||
|
||||
set(WAMR_BUILD_INTERP 1)
|
||||
set(WAMR_BUILD_FAST_INTERP 1)
|
||||
set(WAMR_BUILD_AOT 1)
|
||||
set(WAMR_BUILD_JIT 0)
|
||||
set(WAMR_BUILD_LIBC_BUILTIN 1)
|
||||
set(WAMR_BUILD_LIBC_WASI 1)
|
||||
set(WAMR_BUILD_LIB_PTHREAD 1)
|
||||
|
||||
# compiling and linking flags
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
|
||||
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
|
||||
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
|
||||
endif ()
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
|
||||
|
||||
# build vmlib static lib
|
||||
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
|
||||
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
|
||||
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
|
||||
|
||||
# build iwasm
|
||||
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
|
||||
set (RUNTIME_SOURCE_ALL
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../product-mini/platforms/linux/main.c
|
||||
${UNCOMMON_SHARED_SOURCE}
|
||||
)
|
||||
add_executable (iwasm ${RUNTIME_SOURCE_ALL})
|
||||
target_link_libraries(iwasm vmlib -lpthread -lm)
|
||||
57
samples/socket-api/README.md
Normal file
57
samples/socket-api/README.md
Normal file
@ -0,0 +1,57 @@
|
||||
"socket-api" sample introduction
|
||||
================================
|
||||
|
||||
This sample demonstrates how to use WAMR socket-api to develop wasm network applications.
|
||||
Two wasm applications are provided: tcp-server and tcp-client, and this sample demonstrates
|
||||
how they communicate with each other.
|
||||
|
||||
## Preparation
|
||||
|
||||
Please install WASI SDK, download the [wasi-sdk release](https://github.com/CraneStation/wasi-sdk/releases) and extract the archive to default path `/opt/wasi-sdk`.
|
||||
And install wabt, download the [wabt release](https://github.com/WebAssembly/wabt/releases) and extract the archive to default path `/opt/wabt`
|
||||
|
||||
## Build the sample
|
||||
|
||||
```bash
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
make
|
||||
```
|
||||
|
||||
The file `tcp_server.wasm`, `tcp_client.wasm` and `iwasm` will be created.
|
||||
And also file `tcp_server` and `tcp_client` of native version are created.
|
||||
|
||||
Note that iwasm is built with libc-wasi and lib-pthread enabled.
|
||||
|
||||
## Run workload
|
||||
|
||||
Start the tcp server, which opens port 1234 and waits for clients to connect.
|
||||
```bash
|
||||
cd build
|
||||
./iwasm --addr-pool=0.0.0.0/15 tcp_server.wasm
|
||||
```
|
||||
|
||||
Start the tcp client, which connects the server and receives message.
|
||||
```bash
|
||||
cd build
|
||||
./iwasm --addr-pool=127.0.0.1/15 tcp_client.wasm
|
||||
```
|
||||
|
||||
The output of client is like:
|
||||
```bash
|
||||
[Client] Create socket
|
||||
[Client] Connect socket
|
||||
[Client] Client receive
|
||||
[Client] 115 bytes received:
|
||||
Buffer recieved:
|
||||
Say Hi from the Server
|
||||
Say Hi from the Server
|
||||
Say Hi from the Server
|
||||
Say Hi from the Server
|
||||
Say Hi from the Server
|
||||
|
||||
[Client] BYE
|
||||
```
|
||||
|
||||
Refer to [socket api document](../../doc/socket_api.md) for more details.
|
||||
80
samples/socket-api/wasm-src/CMakeLists.txt
Normal file
80
samples/socket-api/wasm-src/CMakeLists.txt
Normal file
@ -0,0 +1,80 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
cmake_minimum_required(VERSION 2.8...3.18)
|
||||
project(socket_api_sample_wasm_app)
|
||||
|
||||
message(CHECK_START "Detecting WABT")
|
||||
if(NOT (DEFINED WABT_DIR OR DEFINED CACHE{WABT_DIR}))
|
||||
find_path(WABT_DIR
|
||||
wabt
|
||||
PATHS /opt
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
)
|
||||
if(DEFINED WABT_DIR)
|
||||
set(WABT_DIR ${WABT_DIR}/wabt)
|
||||
endif()
|
||||
endif()
|
||||
if(WABT_DIR)
|
||||
message(CHECK_PASS "found")
|
||||
else()
|
||||
message(CHECK_FAIL "not found")
|
||||
endif()
|
||||
|
||||
message(CHECK_START "Detecting WASM_OBJDUMP at ${WABT_DIR}")
|
||||
find_program(WASM_OBJDUMP
|
||||
wasm-objdump
|
||||
PATHS "${WABT_DIR}/bin"
|
||||
NO_DEFAULT_PATH
|
||||
NO_CMAKE_FIND_ROOT_PATH
|
||||
)
|
||||
if(WASM_OBJDUMP)
|
||||
message(CHECK_PASS "found")
|
||||
else()
|
||||
message(CHECK_FAIL "not found")
|
||||
endif()
|
||||
|
||||
set(SRC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../core/iwasm/libraries/lib-socket/lib_socket_wasi.cmake)
|
||||
|
||||
function(COMPILE_WITH_CLANG SOURCE_FILE)
|
||||
get_filename_component(FILE_NAME ${SOURCE_FILE} NAME_WLE)
|
||||
|
||||
set(WASM_MODULE ${FILE_NAME}.wasm)
|
||||
|
||||
set(MAIN_TARGET_NAME MODULE_${FILE_NAME})
|
||||
|
||||
add_executable(${MAIN_TARGET_NAME} ${SOURCE_FILE})
|
||||
set_target_properties(${MAIN_TARGET_NAME} PROPERTIES OUTPUT_NAME ${WASM_MODULE})
|
||||
target_include_directories(${MAIN_TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/inc)
|
||||
target_compile_options(${MAIN_TARGET_NAME} INTERFACE -pthread)
|
||||
target_link_libraries(${MAIN_TARGET_NAME} socket_wasi_ext)
|
||||
target_link_options(${MAIN_TARGET_NAME} PRIVATE
|
||||
LINKER:--export=__heap_base
|
||||
LINKER:--export=__data_end
|
||||
LINKER:--shared-memory,--max-memory=196608
|
||||
LINKER:--no-check-features
|
||||
LINKER:--allow-undefined
|
||||
)
|
||||
|
||||
if(EXISTS ${WASM_OBJDUMP})
|
||||
message(STATUS "Dumping ${WASM_MODULE}...")
|
||||
set(WASM_DUMP ${WASM_MODULE}.dump)
|
||||
set(DUMP_TARGET_NAME DUMP_${FILE_NAME})
|
||||
|
||||
add_custom_command(OUTPUT ${WASM_DUMP}
|
||||
COMMAND ${WASM_OBJDUMP} -dx ${WASM_MODULE} > ${WASM_DUMP}
|
||||
COMMENT "Dumping ${WASM_MODULE}..."
|
||||
DEPENDS ${MAIN_TARGET_NAME}
|
||||
)
|
||||
|
||||
add_custom_target(${DUMP_TARGET_NAME} ALL
|
||||
DEPENDS ${WASM_DUMP}
|
||||
)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
compile_with_clang(tcp_server.c)
|
||||
compile_with_clang(tcp_client.c)
|
||||
0
samples/socket-api/wasm-src/inc/.gitkeep
Normal file
0
samples/socket-api/wasm-src/inc/.gitkeep
Normal file
62
samples/socket-api/wasm-src/tcp_client.c
Normal file
62
samples/socket-api/wasm-src/tcp_client.c
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#ifdef __wasi__
|
||||
#include <wasi_socket_ext.h>
|
||||
#endif
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int socket_fd, ret, total_size = 0;
|
||||
char buffer[1024] = { 0 };
|
||||
struct sockaddr_in server_address = { 0 };
|
||||
|
||||
printf("[Client] Create socket\n");
|
||||
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (socket_fd == -1) {
|
||||
perror("Create socket failed");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* 127.0.0.1:1234 */
|
||||
server_address.sin_family = AF_INET;
|
||||
server_address.sin_port = htons(1234);
|
||||
server_address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
|
||||
printf("[Client] Connect socket\n");
|
||||
if (connect(socket_fd, (struct sockaddr *)&server_address,
|
||||
sizeof(server_address))
|
||||
== -1) {
|
||||
perror("Connect failed");
|
||||
close(socket_fd);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf("[Client] Client receive\n");
|
||||
while (1) {
|
||||
ret = recv(socket_fd, buffer + total_size, sizeof(buffer) - total_size,
|
||||
0);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
total_size += ret;
|
||||
}
|
||||
|
||||
printf("[Client] %d bytes received:\n", total_size);
|
||||
if (total_size > 0) {
|
||||
printf("Buffer recieved:\n%s\n", buffer);
|
||||
}
|
||||
|
||||
close(socket_fd);
|
||||
printf("[Client] BYE \n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
118
samples/socket-api/wasm-src/tcp_server.c
Normal file
118
samples/socket-api/wasm-src/tcp_server.c
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#ifdef __wasi__
|
||||
#include <wasi_socket_ext.h>
|
||||
#endif
|
||||
|
||||
#define WORKER_NUM 5
|
||||
|
||||
void *
|
||||
run(void *arg)
|
||||
{
|
||||
const char *message = "Say Hi from the Server\n";
|
||||
int new_socket = *(int *)arg;
|
||||
int i;
|
||||
|
||||
printf("[Server] Communicate with the new connection #%u @ %p ..\n",
|
||||
new_socket, (void *)(uintptr_t)pthread_self());
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
if (send(new_socket, message, strlen(message), 0) < 0) {
|
||||
perror("Send failed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("[Server] Shuting down the new connection #%u ..\n", new_socket);
|
||||
shutdown(new_socket, SHUT_RDWR);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int socket_fd = -1, addrlen = 0;
|
||||
struct sockaddr_in addr = { 0 };
|
||||
unsigned connections = 0;
|
||||
pthread_t workers[WORKER_NUM] = { 0 };
|
||||
int client_sock_fds[WORKER_NUM] = { 0 };
|
||||
|
||||
printf("[Server] Create socket\n");
|
||||
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (socket_fd < 0) {
|
||||
perror("Create socket failed");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* 0.0.0.0:1234 */
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(1234);
|
||||
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
printf("[Server] Bind socket\n");
|
||||
addrlen = sizeof(addr);
|
||||
if (bind(socket_fd, (struct sockaddr *)&addr, addrlen) < 0) {
|
||||
perror("Bind failed");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
printf("[Server] Listening on socket\n");
|
||||
if (listen(socket_fd, 3) < 0) {
|
||||
perror("Listen failed");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
printf("[Server] Wait for clients to connect ..\n");
|
||||
while (connections < WORKER_NUM) {
|
||||
client_sock_fds[connections] =
|
||||
accept(socket_fd, (struct sockaddr *)&addr, (socklen_t *)&addrlen);
|
||||
if (client_sock_fds[connections] < 0) {
|
||||
perror("Accept failed");
|
||||
break;
|
||||
}
|
||||
|
||||
printf("[Server] Client connected\n");
|
||||
if (pthread_create(&workers[connections], NULL, run,
|
||||
&client_sock_fds[connections])) {
|
||||
perror("Create a worker thread failed");
|
||||
shutdown(client_sock_fds[connections], SHUT_RDWR);
|
||||
break;
|
||||
}
|
||||
|
||||
connections++;
|
||||
}
|
||||
|
||||
if (connections == WORKER_NUM) {
|
||||
printf("[Server] Achieve maximum amount of connections\n");
|
||||
}
|
||||
|
||||
for (int i = 0; i < WORKER_NUM; i++) {
|
||||
pthread_join(workers[i], NULL);
|
||||
}
|
||||
|
||||
printf("[Server] Shuting down ..\n");
|
||||
shutdown(socket_fd, SHUT_RDWR);
|
||||
sleep(3);
|
||||
printf("[Server] BYE \n");
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
fail:
|
||||
printf("[Server] Shuting down ..\n");
|
||||
if (socket_fd >= 0)
|
||||
close(socket_fd);
|
||||
sleep(3);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
Reference in New Issue
Block a user