Skip to content

Commit

Permalink
feat!: add VirtIO net device
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Jan 19, 2024
1 parent 67442cf commit 2097ada
Show file tree
Hide file tree
Showing 11 changed files with 1,034 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ARG SANITIZE=no
RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install --no-install-recommends -y \
build-essential vim wget git clang-tidy-15 clang-format-15 lcov \
libboost1.81-dev libssl-dev \
libboost1.81-dev libssl-dev libslirp-dev \
ca-certificates pkg-config lua5.4 liblua5.4-dev \
libgrpc++-dev libprotobuf-dev protobuf-compiler-grpc \
luarocks xxd && \
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Docker targets:
- GNU Make >= 3.81
- GRPC >= 1.45.0
- Lua >= 5.4.4
- Libslirp >= 4.6.0
- Boost >= 1.81

Obs: Please note that Apple Clang Version number does not follow upstream LLVM/Clang.
Expand All @@ -31,8 +32,8 @@ Obs: Please note that Apple Clang Version number does not follow upstream LLVM/C

```bash
sudo apt-get install build-essential wget git clang-tidy-15 clang-format-15 \
libboost1.81-dev libssl-dev \
ca-certificates pkg-config lua5.4 liblua5.4-dev \
libboost1.81-dev libssl-dev libslirp-dev \
ca-certificates pkg-config lua5.4 liblua5.4-dev \
libgrpc++-dev libprotobuf-dev protobuf-compiler-grpc \
luarocks

Expand All @@ -46,7 +47,7 @@ sudo luarocks install --lua-version=5.4 luaposix

##### MacPorts
```bash
sudo port install clang-15 boost libtool wget pkgconfig grpc openssl lua lua-luarocks
sudo port install clang-15 boost libtool wget pkgconfig grpc openssl lua lua-luarocks libslirp

sudo luarocks install --lua-version=5.4 lpeg
sudo luarocks install --lua-version=5.4 dkjson
Expand All @@ -57,7 +58,7 @@ sudo luarocks install --lua-version=5.4 luaposix

##### Homebrew
```bash
brew install llvm@15 boost wget pkg-config grpc openssl lua luarocks
brew install llvm@15 boost wget pkg-config grpc openssl lua luarocks libslirp
luarocks --lua-dir=$(brew --prefix)/opt/lua install lpeg
luarocks --lua-dir=$(brew --prefix)/opt/lua install dkjson
luarocks --lua-dir=$(brew --prefix)/opt/lua install luasocket
Expand Down
31 changes: 19 additions & 12 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,18 @@ endif
# Homebrew installation
ifneq (,$(shell which brew))
BREW_PREFIX = $(shell brew --prefix)
BOOST_LIB_DIR=-L$(BREW_PREFIX)/lib
BOOST_INC=-I$(BREW_PREFIX)/include
SLIRP_LIB=-L$(PORT_PREFIX)/libslirp/lib -lslirp
SLIRP_INC=-I$(PORT_PREFIX)/libslirp/include
GRPC_PROTOBUF_INC=$(shell pkg-config --cflags-only-I grpc++ protobuf)
GRPC_PROTOBUF_LIB=$(shell pkg-config --libs grpc++ protobuf)

# Macports installation
else ifneq (,$(shell which port))
PORT_PREFIX = /opt/local
BOOST_LIB_DIR=-L$(PORT_PREFIX)/libexec/boost/1.81/lib
BOOST_INC=-I$(PORT_PREFIX)/libexec/boost/1.81/include
SLIRP_LIB=-L$(PORT_PREFIX)/libslirp/lib -lslirp
SLIRP_INC=-I$(PORT_PREFIX)/libslirp/include
GRPC_PROTOBUF_INC=-I$(PORT_PREFIX)/include
GRPC_PROTOBUF_LIB=-L$(PORT_PREFIX)/lib -lgrpc++ -lgrpc -lgpr -lprotobuf -lpthread -labsl_synchronization
else
Expand Down Expand Up @@ -116,6 +118,8 @@ AR=ar rcs
INCS=

BOOST_INC=
SLIRP_INC=
SLIRP_LIB=-lslirp
GRPC_PROTOBUF_INC=$(shell pkg-config --cflags-only-I grpc++ protobuf)
GRPC_PROTOBUF_LIB=$(shell pkg-config --libs grpc++ protobuf)
LIBCARTESI=libcartesi-$(EMULATOR_VERSION_MAJOR).$(EMULATOR_VERSION_MINOR).so
Expand All @@ -128,15 +132,15 @@ PROFILE_DATA=

endif

LIBCARTESI_LIBS=
LIBCARTESI_GRPC_LIBS=$(GRPC_PROTOBUF_LIB)
LIBCARTESI_JSONRPC_LIBS=
LUACARTESI_LIBS=
LUACARTESI_GRPC_LIBS=$(GRPC_PROTOBUF_LIB)
LUACARTESI_JSONRPC_LIBS=
REMOTE_CARTESI_MACHINE_LIBS=$(GRPC_PROTOBUF_LIB)
JSONRPC_REMOTE_CARTESI_MACHINE_LIBS=
TEST_MACHINE_C_API_LIBS=$(GRPC_PROTOBUF_LIB)
LIBCARTESI_LIBS=$(SLIRP_LIB)
LIBCARTESI_GRPC_LIBS=$(SLIRP_LIB) $(GRPC_PROTOBUF_LIB)
LIBCARTESI_JSONRPC_LIBS=$(SLIRP_LIB)
LUACARTESI_LIBS=$(SLIRP_LIB)
LUACARTESI_GRPC_LIBS=$(SLIRP_LIB) $(GRPC_PROTOBUF_LIB)
LUACARTESI_JSONRPC_LIBS=$(SLIRP_LIB)
REMOTE_CARTESI_MACHINE_LIBS=$(SLIRP_LIB) $(GRPC_PROTOBUF_LIB)
JSONRPC_REMOTE_CARTESI_MACHINE_LIBS=$(SLIRP_LIB)
TEST_MACHINE_C_API_LIBS=$(SLIRP_LIB) $(GRPC_PROTOBUF_LIB)
HASH_LIBS=

#DEFS+= -DMT_ALL_DIRTY
Expand All @@ -149,7 +153,7 @@ INCS+= \
-I../third-party/tiny_sha3 \
-I../third-party/downloads \
-I../third-party/mongoose-7.12 \
$(BOOST_INC)
$(SLIRP_INC) $(BOOST_INC)

# Use 64-bit offsets for file operations in POSIX APIs
DEFS+=-D_FILE_OFFSET_BITS=64
Expand Down Expand Up @@ -323,6 +327,9 @@ LIBCARTESI_OBJS:= \
virtio-device.o \
virtio-console.o \
virtio-p9fs.o \
virtio-net.o \
virtio-net-carrier-tuntap.o \
virtio-net-carrier-slirp.o \
dtb.o \
os.o \
htif.o \
Expand Down
10 changes: 10 additions & 0 deletions src/dtb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ void dtb_init(const machine_config &c, unsigned char *dtb_start, uint64_t dtb_le
fdt.prop_u32_list<2>("interrupts-extended", {PLIC_PHANDLE, plic_irq_id});
fdt.end_node();
}
if (c.htif.console_getchar) { // virtio net
const uint32_t virtio_idx = 2;
const uint64_t virtio_paddr = PMA_FIRST_VIRTIO_START + virtio_idx * PMA_VIRTIO_LENGTH;
const uint32_t plic_irq_id = virtio_idx + 1;
fdt.begin_node_num("virtio", virtio_paddr);
fdt.prop_string("compatible", "virtio,mmio");
fdt.prop_u64_list<2>("reg", {virtio_paddr, PMA_VIRTIO_LENGTH});
fdt.prop_u32_list<2>("interrupts-extended", {PLIC_PHANDLE, plic_irq_id});
fdt.end_node();
}
fdt.end_node();
}

Expand Down
10 changes: 10 additions & 0 deletions src/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
#include "unique-c-ptr.h"
#include "virtio-console.h"
#include "virtio-factory.h"
#include "virtio-net-carrier-slirp.h"
#include "virtio-net-carrier-tuntap.h"
#include "virtio-net.h"
#include "virtio-p9fs.h"

/// \file
Expand Down Expand Up @@ -456,6 +459,13 @@ machine::machine(const machine_config &c, const machine_runtime_config &r) :
make_virtio_pma_entry(PMA_FIRST_VIRTIO_START + vdev_p9fs->get_virtio_index() * PMA_VIRTIO_LENGTH,
PMA_VIRTIO_LENGTH, "VirtIO p9fs device", &virtio_driver, vdev_p9fs.get()));
m_vdevs.push_back(std::move(vdev_p9fs));

// Register VirtIO network device
auto vdev_net = std::make_unique<virtio_net>(m_vdevs.size(), std::make_unique<virtio_net_carrier_slirp>());
register_pma_entry(
make_virtio_pma_entry(PMA_FIRST_VIRTIO_START + vdev_net->get_virtio_index() * PMA_VIRTIO_LENGTH,
PMA_VIRTIO_LENGTH, "VirtIO network device", &virtio_driver, vdev_net.get()));
m_vdevs.push_back(std::move(vdev_net));
}

// Initialize DTB
Expand Down
Loading

0 comments on commit 2097ada

Please sign in to comment.