blob: 20fbb9f01bb5a86ef2e558e6d8a03aa63a01cbe5 [file] [log] [blame]
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07001# zygote-start is what officially starts netd (see //system/core/rootdir/init.rc)
2# However, on some hardware it's started from post-fs-data as well, which is just
3# a tad earlier. There's no benefit to that though, since on 4.9+ P+ devices netd
4# will just block until bpfloader finishes and sets the bpf.progs_loaded property.
5#
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -07006# It is important that we start netbpfload after:
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -07007# - /sys/fs/bpf is already mounted,
8# - apex (incl. rollback) is initialized (so that in the future we can load bpf
9# programs shipped as part of apex mainline modules)
10# - logd is ready for us to log stuff
11#
12# At the same time we want to be as early as possible to reduce races and thus
13# failures (before memory is fragmented, and cpu is busy running tons of other
14# stuff) and we absolutely want to be before netd and the system boot slot is
15# considered to have booted successfully.
16#
17on load_bpf_programs
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070018 exec_start netbpfload
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070019
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070020service netbpfload /system/bin/netbpfload
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070021 capabilities CHOWN SYS_ADMIN NET_ADMIN
22 # The following group memberships are a workaround for lack of DAC_OVERRIDE
23 # and allow us to open (among other things) files that we created and are
24 # no longer root owned (due to CHOWN) but still have group read access to
25 # one of the following groups. This is not perfect, but a more correct
26 # solution requires significantly more effort to implement.
27 group root graphics network_stack net_admin net_bw_acct net_bw_stats net_raw system
28 user root
29 #
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070030 # Set RLIMIT_MEMLOCK to 1GiB for netbpfload
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070031 #
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070032 # Actually only 8MiB would be needed if netbpfload ran as its own uid.
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070033 #
34 # However, while the rlimit is per-thread, the accounting is system wide.
35 # So, for example, if the graphics stack has already allocated 10MiB of
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070036 # memlock data before netbpfload even gets a chance to run, it would fail
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070037 # if its memlock rlimit is only 8MiB - since there would be none left for it.
38 #
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070039 # netbpfload succeeding is critical to system health, since a failure will
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070040 # cause netd crashloop and thus system server crashloop... and the only
41 # recovery is a full kernel reboot.
42 #
43 # We've had issues where devices would sometimes (rarely) boot into
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070044 # a crashloop because netbpfload would occasionally lose a boot time
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070045 # race against the graphics stack's boot time locked memory allocation.
46 #
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070047 # Thus netbpfload's memlock has to be 8MB higher then the locked memory
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070048 # consumption of the root uid anywhere else in the system...
49 # But we don't know what that is for all possible devices...
50 #
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070051 # Ideally, we'd simply grant netbpfload the IPC_LOCK capability and it
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070052 # would simply ignore it's memlock rlimit... but it turns that this
53 # capability is not even checked by the kernel's bpf system call.
54 #
55 # As such we simply use 1GiB as a reasonable approximation of infinity.
56 #
57 rlimit memlock 1073741824 1073741824
58 oneshot
59 #
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070060 # How to debug bootloops caused by 'netbpfload-failed'.
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070061 #
62 # 1. On some lower RAM devices (like wembley) you may need to first enable developer mode
63 # (from the Settings app UI), and change the developer option "Logger buffer sizes"
64 # from the default (wembley: 64kB) to the maximum (1M) per log buffer.
65 # Otherwise buffer will overflow before you manage to dump it and you'll get useless logs.
66 #
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070067 # 2. comment out 'reboot_on_failure reboot,netbpfload-failed' below
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070068 # 3. rebuild/reflash/reboot
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070069 # 4. as the device is booting up capture netbpfload logs via:
70 # adb logcat -s 'NetBpfLoad:*' 'NetBpfLoader:*'
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070071 #
72 # something like:
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070073 # $ adb reboot; sleep 1; adb wait-for-device; adb root; sleep 1; adb wait-for-device; adb logcat -s 'NetBpfLoad:*' 'NetBpfLoader:*'
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070074 # will take care of capturing logs as early as possible
75 #
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070076 # 5. look through the logs from the kernel's bpf verifier that netbpfload dumps out,
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070077 # it usually makes sense to search back from the end and find the particular
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070078 # bpf verifier failure that caused netbpfload to terminate early with an error code.
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070079 # This will probably be something along the lines of 'too many jumps' or
80 # 'cannot prove return value is 0 or 1' or 'unsupported / unknown operation / helper',
81 # 'invalid bpf_context access', etc.
82 #
Maciej Żenczykowski283c25a2023-10-02 19:43:30 -070083 reboot_on_failure reboot,netbpfload-failed
Maciej Żenczykowski60c159f2023-10-02 14:54:48 -070084 # we're not really updatable, but want to be able to load bpf programs shipped in apexes
85 updatable