nvme磁盤(pán)故障注入方法
本文分享自天翼云開(kāi)發(fā)者社區《nvme磁盤(pán)故障注入方法》,作者:曹****飛
在存儲系統中,磁盤(pán)的故障是很可能出現的問(wèn)題。存儲軟件的設計需要對故障進(jìn)行處理,提高系統的健壯性。然而磁盤(pán)的故障是不可控的,當我們想測試軟件故障處理的分支時(shí),不是很方便。用軟件模擬的方法能覆蓋的場(chǎng)景比較少,而且和實(shí)際故障的差距會(huì )比較大。因此,如果能讓故障下沉到磁盤(pán),盡可能的靠近磁盤(pán),才能構造出盡可能真實(shí)的故障場(chǎng)景。本文針對nvme磁盤(pán),在磁盤(pán)驅動(dòng)這一層調研了幾種可以注入磁盤(pán)故障的方法。
1. write uncorrectable
通過(guò)向nvme控制器發(fā)送write uncor命令,標記指定的LBA范圍為invalid,當讀到這個(gè)LBA范圍時(shí),ctrl會(huì )返回Unrecovered Read Error錯誤??梢杂糜谀M讀的media error。如果要清除invalid狀態(tài),需要向這個(gè)LBA范圍寫(xiě)入數據,覆蓋調invalid標記。
不過(guò),write uncor命令不是所有廠(chǎng)家的盤(pán)都支持。如果不支持,會(huì )返回invalid op code錯誤。查看是否支持的方法,可以看controller數據結構中的oncs->write_unc是否為1。
還有一個(gè)缺點(diǎn)是只能注入讀故障。
2. write protect
通過(guò)設置namespace的屬性,可以讓namespace寫(xiě)保護,當這個(gè)namespace收到寫(xiě)請求時(shí),會(huì )返回write error。
缺點(diǎn)是有的廠(chǎng)商的盤(pán)不支持這個(gè)屬性,而且只能注入寫(xiě)故障。
3. 驅動(dòng)層模擬故障
這是最通用的一種方法,可以構造出各種故障。spdk的代碼中就實(shí)現了這一功能。下面詳細介紹下它的接口。
/**
* \brief Inject an error for the next request with a given opcode.
* \param ctrlr NVMe controller.
* \param qpair I/O queue pair to add the error command,
* NULL for Admin queue pair.
* \param opc Opcode for Admin or I/O commands.
* \param do_not_submit True if matching requests should not be submitted
* to the controller, but instead completed manually
* after timeout_in_us has expired. False if matching
* requests should be submitted to the controller and
* have their completion status modified after the
* controller completes the request.
* \param timeout_in_us Wait specified microseconds when do_not_submit is true.
* \param err_count Number of matching requests to inject errors.
* \param sct Status code type.
* \param sc Status code.
* \return 0 if successfully enabled, ENOMEM if an error command
* structure cannot be allocated.
* The function can be called multiple times to inject errors for different
* commands. If the opcode matches an existing entry, the existing entry
* will be updated with the values specified.
*/
int spdk_nvme_qpair_add_cmd_error_injection(struct spdk_nvme_ctrlr *ctrlr,
struct spdk_nvme_qpair *qpair,
uint8_t opc,
bool do_not_submit,
uint64_t timeout_in_us,
uint32_t err_count,
uint8_t sct, uint8_t sc);
ctrlr: nvme盤(pán)的控制器數據結構,每個(gè)盤(pán)有一個(gè);
qpair: 注入故障的qpair,可以是io qpair,也可以是admin qpair。使用哪一種取決于希望在IO路徑上還是在管理路徑上產(chǎn)生故障;
opc: 操作類(lèi)型,比如read,write等;
do_not_submit:如果是true,則這個(gè)opc的cmd不會(huì )提交到ctrl,而是在 timeout_in_us 到期后手動(dòng)返回指定的sct,sc。如果是false,則這個(gè)opc的cmd會(huì )提交到ctrl,等ctrl返回cpl后,將sct和sc改成指定的sct,sc,回調的時(shí)候用改過(guò)的cpl。命令實(shí)際上是執行成功的;
timeout_in_us:do_not_submit=true時(shí)有效,等待timeout_in_us后再返回sct,sc。范圍是(0-18446744073709551615)
err_count: 表示注入故障后,此后的多少個(gè)該opc的cmd會(huì )按照注入的故障來(lái)處理,每返回一個(gè)注入的故障,err_count減1。減為0的時(shí)候不再注入故障,但cmd也不會(huì )從 qpair->err_cmd_head 中摘除;
sct: 返回的狀態(tài)碼類(lèi)型;
sc: 返回的狀態(tài)碼;
實(shí)現的原理是向指定qpair的err_cmd_head鏈表中添加一個(gè)錯誤的cmd,當下個(gè)該類(lèi)型的cmd提交到該qpair的時(shí)候在 _nvme_qpair_submit_request 中遍歷這個(gè)鏈表,處理這個(gè)cmd,強制修改cpl中的返回值,然后再調用回調,從而模擬故障。
使用此接口可以模擬出讀寫(xiě)錯誤,IO hang住或超時(shí)的錯誤等,在開(kāi)發(fā)測試中比較實(shí)用。
*博客內容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀(guān)點(diǎn),如有侵權請聯(lián)系工作人員刪除。