Next Stop - Ihcblog!

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

0%

This article also has an Chinese version.

This article discusses my thoughts, designs, and implementations on creating a reliable context-passing component in Rust. My project, certain-map, has been open-sourced (it was first released over a year ago, with more improvements made recently, which I will discuss later in this article). Feel free to use it!

Project URL: https://github.com/ihciah/certain-map

What problem does it solve:

  1. When passing context across components, it can leverage the compiler to ensure the existence of fields (i.e., when a component has a read dependency on a field in the Context, the preceding component must have written to that field, otherwise it will not compile).
  2. The context required by generic components can be defined as a generic parameter and constrained, which makes the component implementation more generic and not coupled to a specific type of Context.

Note: Although the project name seems to suggest a map implementation, it is actually a struct generated using procedural macros. The reason it is named certain-map is that it was originally designed to replace TypeMap and ensure the existence of fields.

Read more »

This article also has a Chinese version.

This series of articles mainly records my attempt to implement a Hypervisor using Rust. The 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 fourth in the series. It will cover implementing a Virtio Queue and virtio-net from scratch and using TAP as the backend for a virtio-net device. To better assemble these components, additional components such as a Bus and EventLoop will also be added.

During this experiment, I also contributed some PRs to firecracker and cloud-hypervisor, please refer to the end of the article.

There is a lot of code in the article, please use the directory navigation on the right when necessary.

The previous three articles were completed in the second half of 2022, while this chapter and the corresponding experimental code have been in draft form until recently (now it is 2024). Over a few weekends, I added some code and completed this article.

The next article may support PCI devices and direct I/O for VF devices if I have the time(but don’t expect it, haha).

Read more »

This article also has a Chinese version.

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. It is a personal hobby project from the beginning, but it is also used in my current company. I will share this topic at this year’s Rust Conf China(2024) and welcome to attend.

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 drive cost optimization, I developed from scratch multiple business-critical Rust SDKs, including service discovery, metrics, log, and dynamic configuration. I initiated and participated in the development of a Rust RPC framework, as well as provided compilation and runtime images, internal crates sources, and a public mirror (rsproxy.cn). Built on top of these infrastructural projects, several core services were migrated to Rust, achieving significant performance gains: a reduction of over 30% in CPU usage and a notable decrease in the P99 latency for some latency-sensitive services. However, many of these services are such that they do not require active maintenance—like proxy and caching services—and hence were easier to migrate. Services with more complex and actively iterative business logic proved more challenging to shift 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.

I shared this topic at the 2024 RustChinaConf. If you are interested, feel free to check out the video replay or the PPT(video link, PPT link).

Please note these sharings are in Chinese. But if you have any question, feel free to reach me in any way(you can find my email and telegram in about page).

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 »