Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
261 lines (239 loc) · 11.1 KB
/
Copy pathflake.nix
File metadata and controls
261 lines (239 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#
# flake.nix for rocm-ernic
#
# A reproducible development environment, a hermetic build, and a suite of
# static, dynamic, and fuzz analysis targets for the userspace emulated RDMA
# NIC. The design is modular: this slim flake.nix wires together small
# single-purpose modules under nix/ (see nix/README.md).
#
# Enter the development shell:
# nix develop
#
# Build the server binary:
# nix build .#rocm-ernic # -> ./result/bin/rocm-ernic
#
# Run the analysis (nix/README.md lists every target):
# nix build .#analysis-deep && cat result/summary.txt
#
# Cross-compile for another architecture (from an x86_64-linux host), and
# exercise the result under QEMU emulation:
# nix build .#rocm-ernic-aarch64 # also: -riscv64, -armv7l, -ppc64le
# nix run .#run-aarch64-tests # aarch64/riscv64: boot a microvm + self-test
# nix run .#run-armv7l-tests # armv7l/ppc64le: qemu-user smoke (see nix/microvms)
#
# Try the emulated NIC: boot a NixOS guest with the in-tree ionic drivers
# bound and the RDMA port ACTIVE, at a root shell (x86_64-linux host):
# nix run github:ROCm/rocm-ernic#ionic-vm
# nix run .#ionic-vm -- --ssh-port 2222
# Two guests with RDMA between them over the TCP backend:
# nix run .#ionic-vm-pair -- --ssh-port 2222 # nodes on 2222 and 2223
# nix run .#run-ionic-pair-test
#
# Build an OCI container image (minimal or debug; amd64 or cross arm64/riscv64):
# nix build .#oci-image && ./result | docker load # also: -debug, -aarch64, -riscv64
#
# If flakes are not enabled, prefix commands with:
# nix --extra-experimental-features 'nix-command flakes' <cmd>
#
# NOTE: flakes only see git-tracked files. After adding or editing files
# under nix/ (or this flake), `git add` them before `nix build`/`nix
# develop`, or the changes are invisible to the evaluation.
#
{
description = "rocm-ernic — userspace emulated RDMA NIC for virtual machines";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
# libvfio-user is not in nixpkgs; pin the source and build it from
# nix/libvfio-user.nix. flake = false: it has no flake of its own.
libvfio-user = {
url = "github:nutanix/libvfio-user";
flake = false;
};
# microvm.nix powers the emulated-run layer (nix/microvms/): booting the
# cross-built server under QEMU/TCG to exercise it on non-x86 arches.
microvm = {
url = "github:astro/microvm.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
# Linux-only: the build and analysis depend on libvfio-user, rdma-core and
# ptrace-based tools, none of which work on Darwin. eachSystem (not
# eachDefaultSystem) keeps the flake from generating darwin attributes that
# could only fail to evaluate/build.
outputs = { self, nixpkgs, flake-utils, libvfio-user, microvm }:
flake-utils.lib.eachSystem [ "x86_64-linux" "aarch64-linux" ] (system:
let
pkgs = import nixpkgs { inherit system; };
lib = nixpkgs.lib;
# libvfio-user built from the pinned source input.
libvfioUser = import ./nix/libvfio-user.nix {
inherit lib;
inherit (pkgs) stdenv meson ninja pkg-config json_c cmocka;
src = libvfio-user;
};
# Shared build context: dependency sets, compat cflags, version, and
# the helpers the analysis builds reuse. Built once, threaded below.
ctx = import ./nix/lib.nix {
inherit pkgs lib;
libvfio-user = libvfioUser;
};
rocm-ernic = import ./nix/derivation.nix {
inherit ctx;
src = ./.;
};
devshell = import ./nix/devshell.nix { inherit ctx; };
# Static, dynamic, and fuzz analysis. See nix/analysis/default.nix.
analysis = import ./nix/analysis {
inherit ctx;
src = ./.;
};
# Cross-compiled server binaries. Only meaningful when the build
# host is x86_64-linux (the cross toolchains target from there);
# on a native aarch64 host the default package already covers arm.
# See nix/cross.nix. Each arch is additive: rocm-ernic-<arch>.
# crossResults holds the full per-arch cross.nix output (crossPkgs +
# the cross rocm-ernic), computed once and reused by the OCI images.
crossArches = [ "aarch64" "riscv64" "armv7l" "ppc64le" ];
crossResults =
if system == "x86_64-linux" then
lib.genAttrs crossArches (arch: import ./nix/cross.nix {
inherit nixpkgs lib;
buildSystem = system;
targetArch = arch;
libvfioUserSrc = libvfio-user;
})
else { };
crossPackages = lib.mapAttrs'
(arch: res: lib.nameValuePair "rocm-ernic-${arch}" res.rocm-ernic)
crossResults;
# OCI images (nix/oci.nix), built straight from the closure via
# dockerTools.streamLayeredImage. A minimal (distroless) and a debug
# (adds a shell) variant, for amd64 (the native host) and — from an
# x86_64 host — cross arm64/riscv64 images reusing crossResults.
# loopback/tcp backends containerize cleanly.
# Load: nix build .#oci-image && ./result | docker load
mkOci = args: import ./nix/oci.nix ({ inherit pkgs lib rocm-ernic; } // args);
# Cross arches we ship container images for (subset of crossArches;
# armv7l/ppc64le are build-only here, no image).
ociCrossArches = [ "aarch64" "riscv64" ];
mkCrossOci = arch:
let r = crossResults.${arch}; in [
{ name = "oci-image-${arch}";
value = mkOci { pkgs = r.crossPkgs; rocm-ernic = r.rocm-ernic; variant = "minimal"; }; }
{ name = "oci-image-${arch}-debug";
value = mkOci { pkgs = r.crossPkgs; rocm-ernic = r.rocm-ernic; variant = "debug"; }; }
];
ociPackages =
{
oci-image = mkOci { variant = "minimal"; };
oci-image-debug = mkOci { variant = "debug"; };
}
// lib.optionalAttrs (system == "x86_64-linux")
(lib.listToAttrs (lib.concatMap mkCrossOci ociCrossArches));
# Emulated-run layer: per-arch exercise of the cross-built server.
# aarch64/riscv64 (+ an x86_64/KVM baseline) boot a full NixOS microvm
# under QEMU/TCG and run the loopback self-test in-guest; armv7l/ppc64le
# fall back to a qemu-user smoke (an upstream microvm.nix binary-name
# bug blocks their VM boot). Exposes run-<arch>-tests for every arch,
# plus microvm-<arch> for the VM arches. See nix/microvms.
microvms =
if system == "x86_64-linux" then
import ./nix/microvms {
inherit pkgs lib microvm nixpkgs;
buildSystem = system;
libvfioUserSrc = libvfio-user;
}
else null;
vmPackages = if microvms != null then microvms.packages else { };
# Consumer path for other flakes: ernic-run starts the server for a
# VM launcher, and run-ionic-guest-test boots a guest with
# nixosModules.ernic-guest to prove the in-tree ionic drivers bind.
# See nix/guest-module.nix and nix/ernic-run.nix.
ernic-run = import ./nix/ernic-run.nix { inherit pkgs rocm-ernic; };
guestPackages = lib.optionalAttrs (system == "x86_64-linux") {
run-ionic-guest-test = import ./nix/ionic-guest-test.nix {
inherit pkgs lib nixpkgs;
ernicRun = ernic-run;
guestModule = self.nixosModules.ernic-guest;
};
# Interactive: nix run .#ionic-vm boots a guest to a root shell.
ionic-vm = import ./nix/ionic-vm.nix {
inherit pkgs nixpkgs;
ernicRun = ernic-run;
guestModule = self.nixosModules.ernic-guest;
};
}
# Two guests joined by the TCP backend (nix/ionic-pair.nix):
# run-ionic-pair-test and the interactive ionic-vm-pair.
// lib.optionalAttrs (system == "x86_64-linux") (import ./nix/ionic-pair.nix {
inherit pkgs lib nixpkgs;
ernicRun = ernic-run;
guestModule = self.nixosModules.ernic-guest;
});
in
{
packages = {
inherit rocm-ernic ernic-run;
libvfio-user = libvfioUser;
default = rocm-ernic;
# Guest tool: RDMA errors on an ionic_rdma device, checked against
# hw_counters. See tests/guest/ernic_rdma_errors.c.
ernic-rdma-errors = pkgs.callPackage ./nix/rdma-errors.nix { };
# Compilation database (consumed by clang-tidy / cppcheck).
compile-db = analysis.compileDb;
# Aggregate analysis levels.
analysis-quick = analysis.quick;
analysis-standard = analysis.standard;
analysis-deep = analysis.deep;
# Per-tool analysis targets.
analysis-clang-tidy = analysis.clang-tidy;
analysis-cppcheck = analysis.cppcheck;
analysis-flawfinder = analysis.flawfinder;
analysis-semgrep = analysis.semgrep;
analysis-clang-analyzer = analysis.clang-analyzer;
analysis-gcc-warnings = analysis.gcc-warnings;
analysis-gcc-analyzer = analysis.gcc-analyzer;
# Dynamic analysis: build + exercise on loopback.
analysis-sanitizers = analysis.sanitizers;
analysis-tsan = analysis.thread-sanitizer;
analysis-valgrind = analysis.valgrind;
# Fuzzing.
fuzz = analysis.fuzzers; # build harnesses + corpora
fuzz-run = analysis.fuzz-run; # bounded run, collect crashes
}
# Cross-compiled server binaries (rocm-ernic-<arch>) and the
# emulated-run VMs (microvm-<arch>, run-<arch>-tests), added only on
# an x86_64-linux build host. See nix/cross.nix and nix/microvms.
// crossPackages
// vmPackages
# OCI images: oci-image[-debug] (amd64) always, plus
# oci-image-{aarch64,riscv64}[-debug] on an x86_64 host. See nix/oci.nix.
// ociPackages
# run-ionic-guest-test and ionic-vm (x86_64 hosts): see
# nix/ionic-guest-test.nix and nix/ionic-vm.nix.
// guestPackages;
checks.pair-launchers = import ./nix/pair-launcher-tests.nix { inherit pkgs; };
checks.launchers = import ./nix/launcher-tests.nix { inherit pkgs; };
checks.analysis-runners = pkgs.runCommand "analysis-runner-tests" {
nativeBuildInputs = [ pkgs.python3 pkgs.jq ];
} ''
export ERNIC_TEST_EXERCISE=${import ./nix/analysis/run-loopback.nix {
inherit pkgs;
waitAttempts = 50;
pollSeconds = "0.02";
clientTimeout = "0.4s";
shutdownTimeout = "0.4s";
}}/bin/run-loopback-exercise
python3 ${./nix/analysis}/test_runners.py
touch "$out"
'';
devShells.default = devshell;
})
// {
# Guest side of the emulated NIC for qemu-vm.nix guests: shared guest
# memory, a q35 root port, vfio-user-pci and the ionic modules. Pair it
# with packages.<system>.ernic-run on the host.
nixosModules.ernic-guest = import ./nix/guest-module.nix;
};
}