博客專欄

EEPW首頁 > 博客 > nvme磁盤故障注入方法

nvme磁盤故障注入方法

發(fā)布人:天翼云開發(fā)者 時(shí)間:2024-08-12 來源:工程師 發(fā)布文章

本文分享自天翼云開發(fā)者社區(qū)《nvme磁盤故障注入方法》,作者:曹****飛 

在存儲(chǔ)系統(tǒng)中,磁盤的故障是很可能出現(xiàn)的問題。存儲(chǔ)軟件的設(shè)計(jì)需要對故障進(jìn)行處理,提高系統(tǒng)的健壯性。然而磁盤的故障是不可控的,當(dāng)我們想測試軟件故障處理的分支時(shí),不是很方便。用軟件模擬的方法能覆蓋的場景比較少,而且和實(shí)際故障的差距會(huì)比較大。因此,如果能讓故障下沉到磁盤,盡可能的靠近磁盤,才能構(gòu)造出盡可能真實(shí)的故障場景。本文針對nvme磁盤,在磁盤驅(qū)動(dòng)這一層調(diào)研了幾種可以注入磁盤故障的方法。

1. write uncorrectable

通過向nvme控制器發(fā)送write uncor命令,標(biāo)記指定的LBA范圍為invalid,當(dāng)讀到這個(gè)LBA范圍時(shí),ctrl會(huì)返回Unrecovered Read Error錯(cuò)誤??梢杂糜谀M讀的media error。如果要清除invalid狀態(tài),需要向這個(gè)LBA范圍寫入數(shù)據(jù),覆蓋調(diào)invalid標(biāo)記。

不過,write uncor命令不是所有廠家的盤都支持。如果不支持,會(huì)返回invalid op code錯(cuò)誤。查看是否支持的方法,可以看controller數(shù)據(jù)結(jié)構(gòu)中的oncs->write_unc是否為1。

還有一個(gè)缺點(diǎn)是只能注入讀故障。

2. write protect

通過設(shè)置namespace的屬性,可以讓namespace寫保護(hù),當(dāng)這個(gè)namespace收到寫請求時(shí),會(huì)返回write error。

缺點(diǎn)是有的廠商的盤不支持這個(gè)屬性,而且只能注入寫故障。

3. 驅(qū)動(dòng)層模擬故障

這是最通用的一種方法,可以構(gòu)造出各種故障。spdk的代碼中就實(shí)現(xiàn)了這一功能。下面詳細(xì)介紹下它的接口。

/**

 * \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盤的控制器數(shù)據(jù)結(jié)構(gòu),每個(gè)盤有一個(gè);

qpair: 注入故障的qpair,可以是io qpair,也可以是admin qpair。使用哪一種取決于希望在IO路徑上還是在管理路徑上產(chǎn)生故障;

opc: 操作類型,比如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,回調(diào)的時(shí)候用改過的cpl。命令實(shí)際上是執(zhí)行成功的;

timeout_in_us:do_not_submit=true時(shí)有效,等待timeout_in_us后再返回sct,sc。范圍是(0-18446744073709551615)

err_count: 表示注入故障后,此后的多少個(gè)該opc的cmd會(huì)按照注入的故障來處理,每返回一個(gè)注入的故障,err_count減1。減為0的時(shí)候不再注入故障,但cmd也不會(huì)從 qpair->err_cmd_head 中摘除;

sct: 返回的狀態(tài)碼類型;

sc: 返回的狀態(tài)碼;

實(shí)現(xiàn)的原理是向指定qpair的err_cmd_head鏈表中添加一個(gè)錯(cuò)誤的cmd,當(dāng)下個(gè)該類型的cmd提交到該qpair的時(shí)候在 _nvme_qpair_submit_request 中遍歷這個(gè)鏈表,處理這個(gè)cmd,強(qiáng)制修改cpl中的返回值,然后再調(diào)用回調(diào),從而模擬故障。

使用此接口可以模擬出讀寫錯(cuò)誤,IO hang住或超時(shí)的錯(cuò)誤等,在開發(fā)測試中比較實(shí)用。

*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請聯(lián)系工作人員刪除。



關(guān)鍵詞: nvme 磁盤故障

相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉