自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
核心網路單元測試
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
自 Android 5.0 起,在 Linux 核心上正確運作 Android 網路堆疊,需要在較近期上游或尚未上游的許多提交內容。手動驗證所需的核心功能或追蹤缺少的提交內容並不容易,因此 Android 團隊會分享他們使用的測試,以確保核心運作正常。
執行測試的原因
這些測試有三個主要原因:
- 裝置上使用的 Linux 核心確切版本通常是裝置專屬,因此在未執行測試的情況下,很難得知任何核心是否正常運作。
- 將核心修補程式前端移植和後端移植至不同核心版本或不同裝置樹狀結構時,可能會引發細微問題,除非執行測試,否則無法察覺。
- 新的網路功能可能需要新的核心功能或核心錯誤修正。
如果測試未通過,表示裝置的網路堆疊行為不正確,導致使用者可見的連線錯誤 (例如 Wi-Fi 網路連線中斷)。裝置也可能無法通過 Android Compatibility Test Suite (CTS) 測試。
使用測試
這些測試會使用 User-Mode Linux,在 Linux 主機上以程序的形式啟動核心。如需適當的作業系統版本,請參閱「建立建構環境」。單元測試架構會使用適當的磁碟映像檔啟動核心,並透過主機檔案系統執行測試。這些測試是以 Python 編寫,並使用 TAP 介面來測試核心行為和 Socket API。
為 ARCH=um 編譯核心
如要執行測試,核心必須為 ARCH=um SUBARCH=x86_64
編譯。這是在上游和常見的 Android 核心樹狀結構 (例如 android-4.4
) 中皆支援的架構。但有時裝置核心不會以這種模式編譯,因為裝置樹狀結構會在常見檔案 (例如 sys/exit.c
) 中包含裝置專屬或硬體專屬程式碼。
在許多情況下,只要確保硬體專屬程式碼位於 #ifdef
後方即可。通常,這應該是設定選項中的 #ifdef
,用於控制與程式碼相關的特定功能。如果沒有這類設定選項,請將硬體專屬程式碼放入 #ifndef CONFIG_UML
區塊中。
一般來說,修復這個問題應由核心樹供應商 (例如晶片組或 SoC 供應商) 負責。我們正與原始設備製造商 (OEM) 和供應商合作,確保目前和未來的核心會為 ARCH=um
SUBARCH=x86_64
編譯,而無須進行任何變更。
執行測試
測試位於 kernel/tests/net/test
。建議您從 AOSP 主版本執行測試,因為這些測試是最新的;在某些情況下,在特定 Android 版本中,用於確保正常運作的核心功能尚未在該版本中進行完整測試。如要進一步瞭解如何執行測試,請參閱 核心網路測試 README 檔案。基本上,請從核心樹狀結構頂端執行以下命令:
ANDROID_TREE/kernel/tests/net/test/run_net_test.sh all_tests.sh
通過測試
核心網路測試 Python 來源檔案包含註解,可指定已知需要通過測試的核心提交內容。測試應在 AOSP 的 kernel/common
專案中,在常見的核心樹狀結構 (所有常見的核心分支,包括 android-4.4
以上版本) 中通過。因此,只要持續從對應的通用核心分支合併,就能讓核心通過測試。
貢獻
回報問題
請在 Android Issue Tracker 中,使用「Component-Networking」標籤回報任何核心網路測試問題。
記錄提交內容並新增測試
請按照上述方式回報問題,並盡可能上傳修正問題的變更內容,如果符合下列情況:
- 測試無法在常見的核心樹狀結構中通過
- 您發現來源註解中未提及必要的版本,
- 讓測試通過上游核心需要進行重大變更
- 您認為測試過度指定,或測試在未來的核心上失敗
- 您想為現有測試新增更多測試或增加涵蓋範圍。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Kernel networking unit tests\n\nSince Android 5.0, proper operation of the Android networking stack on Linux\nkernels requires a number of commits that were upstreamed relatively recently\nor have not yet made it upstream. It is not easy to manually verify the\nrequired kernel functionality or track the missing commits, so the Android team\nis sharing the tests it uses to ensure the kernel behaves as expected.\n\nReasons to run the tests\n------------------------\n\nThese tests exist for three main\nreasons:\n\n1. The exact version of the Linux kernel used on a device is typically device-specific, and it's difficult to know whether any kernel works properly without running the tests.\n2. Forward-porting and back-porting the kernel patches to different kernel versions or different device trees may introduce subtle issues that can be impossible to spot without running the tests.\n3. New networking features may require new kernel functionality or kernel bug fixes.\n\nIf the tests don't pass, the device's network stack\nbehaves incorrectly, causing user-visible connectivity bugs (such as falling off\nWi-Fi networks). The device will likely also fail Android Compatibility Test\nSuite (CTS) tests.\n\nUse the tests\n-------------\n\nThe tests use [User-Mode Linux](http://user-mode-linux.sourceforge.net/) to boot the\nkernel as a process on a Linux host machine. See\n[Establishing a Build Environment](/docs/setup/initializing) for\nsuitable operating system versions. The unit test framework\nboots the kernel with an appropriate disk image and runs the tests from the\nhost file system. The tests are written in Python and use TAP interfaces to\nexercise kernel behaviour and the socket API.\n\n### Compile the kernel for ARCH=um\n\nFor the tests to run,\nthe kernel must compile for `ARCH=um SUBARCH=x86_64`. This is a\nsupported architecture both upstream and in the common Android kernel trees\n(such as `android-4.4`). But sometimes device\nkernels do not compile in this mode because device trees contain\ndevice-specific or hardware-specific code in common files (for example\n`sys/exit.c`).\n\nIn many cases, it's sufficient to ensure that\nhardware-specific code is behind an `#ifdef`. Typically this should\nbe an `#ifdef` on a configuration option that controls the specific\nfeature relevant to the code. If there is no such configuration option, put\nhardware-specific code inside `#ifndef CONFIG_UML` blocks.\n\nIn\ngeneral, fixing this should be the responsibility of the kernel tree provider\n(such as the chipset or SoC vendor). We're working with OEMs and vendors to ensure\nthat current and future kernels compile for `ARCH=um\nSUBARCH=x86_64` without requiring any changes.\n\n### Run the tests\n\nThe tests are at [`kernel/tests/net/test`](https://android.googlesource.com/kernel/tests/+/android16-release/net/test).\nIt is recommended that the tests **be run from AOSP main** because they\nare the most up-to-date; in some cases, kernel features that are necessary for\nproper operation in a given Android release do not yet have full test coverage\nin the given release. For information on how to run the tests, see the [kernel\nnetwork test README file](https://android.googlesource.com/kernel/tests/+/android16-release/net/test/README). Basically, from the top of your kernel tree, run: \n\n```\nANDROID_TREE/kernel/tests/net/test/run_net_test.sh all_tests.sh\n```\n\n### Pass the tests\n\nThe kernel network test Python\nsource files contain comments that specify kernel commits that are known to be\nrequired to pass the tests. The tests should pass in the common kernel trees - all\ncommon kernel branches `android-4.4` and higher - in the\n[`kernel/common`](https://android-review.googlesource.com/#/q/project:kernel/common)\nproject in AOSP. Therefore, passing the tests on a kernel is simply a matter of\ncontinuously merging from the corresponding common kernel branch.\n\nContributions\n-------------\n\n### Report issues\n\nReport any issues with the kernel network tests in the [Android\nissue tracker](https://code.google.com/p/android/issues/entry?template=Developer%20bug%20report) with the [Component-Networking](https://code.google.com/p/android/issues/list?q=label%3AComponent-Networking)\nlabel.\n\n### Document commits and adding tests\n\nReport issues as described above, and if possible upload a change to fix the issue,\nif:\n\n- The tests do not pass on the common kernel trees\n- You find a necessary commit that is not mentioned in the source comments,\n- Getting the tests to pass on upstream kernels requires major changes\n- You believe that the tests are overspecified, or the test fail on future kernels\n- You'd like to add more tests or more coverage to existing tests."]]