Next Stop - Ihcblog!

Some creations and thoughts sharing | sub site:ihc.im

0%

This article will introduce the design and implementation of a Rust FFI (Foreign Function Interface) framework that I created for calling Golang code from Rust. It will cover the design from the perspective of a designer and implementer, considering various options and explaining the choices made and the reasons behind them. It will also cover some implementation details.

The project is open-sourced on GitHub: https://github.com/ihciah/rust2go.

Compared to Golang, Rust programs are not garbage-collected and have stronger compile-time checks. Also thanks to LLVM, Rust gets the best possible compiler optimizations, which results in better performance and safety.

At ByteDance, to promote cost optimization, I participated in the development of a Rust RPC framework and led the implementation of several essential internal Rust SDKs for service discovery, metrics, logging, dynamic configuration, etc. Additionally, I provided compilation and runtime images, internal crates registry, and a public mirror (rsproxy.cn). Building on these infrastructure components, several core services have migrated to Rust, achieving significant performance gains: CPU usage decreased by more than 30%, and a marked reduction in the P99 latency for some latency-sensitive services. However, many of these services, such as proxies and caches, which do not require active maintenance, and services with complex and actively evolving business logic, are harder to migrate to Rust.

In theory, we could rewrite all Golang programs in Rust to achieve better performance, but in practice, this is met with considerable difficulties: First, rewriting all Golang dependencies may not be feasible; second, completing the rewrite all at once is difficult. If we could provide an efficient way of calling Golang from Rust, it would allow businesses to gradually make the switch to Rust, thereby addressing both issues.

This article covers a lot of ground. The overall narrative flow is as follows: first, I’ll discuss the overall solution selection and provide a minimal PoC; then, starting from this minimal PoC, I’ll expand and refine the solution to support the necessary features; finally, I’ll discuss some implementation details of interest from a framework implementation perspective.

Read more »

This article also has a Chinese version.

An HTTP Server internally includes many parts: protocol implementation (h1, h2, compression, etc.), connection state management (keepalive), request distribution, middleware, business logic, and more. Users could implement all of these themselves, however, apart from the business logic, the rest are fairly common capabilities. By decoupling these generic capabilities from the user’s business logic, we arrive at what is known as an HTTP framework.

In the Rust ecosystem, the hyper library already offers a relatively complete implementation of the HTTP protocol. Therefore, building an HTTP framework on top of hyper mainly requires adding capabilities such as routing, shared state, middleware, etc.

This article discusses from the design perspective of an HTTP framework, using the new version of Axum as an example, how to provide rational abstractions and type constraints in Rust’s HTTP frameworks. Levering Rust’s powerful type system, we can write code that is both efficient and correct.

Read more »

This article also has a Chinese version.

This series of blog posts mainly records my process of trying to implement a Hypervisor in Rust.

Why am I writing this series? A few months ago, when I was exploring KVM in my spare time, I encountered some difficulties. Many articles on the Internet did not explain things clearly, and there wasn’t a single article that could build a VMM from scratch and clearly explain the meaning and reason of each Magic Number. I hope my sharing can help beginners avoid some detours to a certain extent. Of course, there may be some misunderstandings in my explanations, and I welcome corrections from everyone.

Table of Contents:

  1. Mini VMM in Rust - Basic
  2. Mini VMM in Rust - Mode Switch
  3. Mini VMM in Rust - Run Real Linux Kernel
  4. Mini VMM in Rust - Implement Virtio Devices

This article is the first in the series, which mainly covers some introductory knowledge and runs some actual code.

Read more »

This article also has a Chinese version.

This article mainly analyzes the currently popular Trojan protocol and proposes a better solution based on the characteristics of current man-in-the-middle (MITM) attacks.

The implementation of this solution is ShadowTLS, for which you can find the complete code and pre-compiled binaries on Github.

Read more »

This article also has a Chinese version.

This series of articles mainly introduces how to design and implement a Runtime based on the io-uring and Thread-per-core model.

Our final Runtime product Monoio is now open source, and you can find it at github.com/bytedance/monoio.

  1. Rust Runtime Design and Implementation - General Introduction
  2. Rust Runtime Design and Implementation - Design Part 1
  3. Rust Runtime Design and Implementation - Design Part 2
  4. Rust Runtime Design and Implementation - Component Part
  5. Rust Runtime Design and Implementation - IO Compatibility Part

This article is the fifth in the series. Originally, the series concluded with four articles, but with the recent addition of epoll support (!73), I decided to write about the design of this part as well.

Read more »

This article also has a Chinese version.

This series of articles mainly introduces how to design and implement a Runtime based on the io-uring and Thread-per-core model.

Our final Runtime product Monoio is now open source, and you can find it at github.com/bytedance/monoio.

  1. Rust Runtime Design and Implementation - General Introduction
  2. Rust Runtime Design and Implementation - Design Part 1
  3. Rust Runtime Design and Implementation - Design Part 2
  4. Rust Runtime Design and Implementation - Component Part
  5. Rust Runtime Design and Implementation - IO Compatibility Part

This article is the fourth in the series, and we have mostly covered the design aspects previously. In this part, we will focus on components such as channels.

Read more »

This article also has a Chinese version.

This series of articles mainly introduces how to design and implement a Runtime based on the io-uring and Thread-per-core model.

Our final Runtime product Monoio is now open source, and you can find it at github.com/bytedance/monoio.

  1. Rust Runtime Design and Implementation - General Introduction
  2. Rust Runtime Design and Implementation - Design Part 1
  3. Rust Runtime Design and Implementation - Design Part 2
  4. Rust Runtime Design and Implementation - Component Part
  5. Rust Runtime Design and Implementation - IO Compatibility Part

This article is the third in the series, continuing the discussion on design trade-offs for the runtime environment.

Read more »

This article also has a Chinese version.

This series of articles mainly introduces how to design and implement a Runtime based on the io-uring and Thread-per-core model.

Our final Runtime product Monoio is now open source, and you can find it at github.com/bytedance/monoio.

  1. Rust Runtime Design and Implementation - General Introduction
  2. Rust Runtime Design and Implementation - Design Part 1
  3. Rust Runtime Design and Implementation - Design Part 2
  4. Rust Runtime Design and Implementation - Component Part
  5. Rust Runtime Design and Implementation - IO Compatibility Part

This article is the second in the series, starting with Monoio as an example to discuss the design choices and trade-offs in Runtimes, while referencing and comparing with the designs of Tokio and Glommio.

Read more »