資源描述:
《linux軟中斷淺析》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、1、軟中斷軟中斷的原理就略過了,講內(nèi)核的書上都有,此處省略1500字。。。。。。1.1注冊還是以我最熟悉的兩個老朋友做為開篇:????open_softirq(NET_TX_SOFTIRQ,net_tx_action);????open_softirq(NET_RX_SOFTIRQ,net_rx_action);open_softirq向內(nèi)核注冊一個軟中斷,其實質(zhì)是設(shè)置軟中斷向量表相應(yīng)槽位,注冊其處理函數(shù):1.voidopen_softirq(intnr,void(*action)(struct
2、softirq_action*))2.{3.????softirq_vec[nr].action=action;4.}復(fù)制代碼softirq_vec是整個軟中斷的向量表:1.structsoftirq_action2.{3.????void????(*action)(structsoftirq_action*);4.};5.6.staticstructsoftirq_actionsoftirq_vec[NR_SOFTIRQS]__cacheline_aligned_in_smp;復(fù)制代碼NR_S
3、OFTIRQS是最大軟中斷向量數(shù),內(nèi)核支持的所有軟中斷如下:1.enum2.{3.????HI_SOFTIRQ=0,4.????TIMER_SOFTIRQ,5.????NET_TX_SOFTIRQ,6.????NET_RX_SOFTIRQ,7.????BLOCK_SOFTIRQ,8.????TASKLET_SOFTIRQ,9.????SCHED_SOFTIRQ,1.????HRTIMER_SOFTIRQ,2.????RCU_SOFTIRQ,????/*PreferableRCUshouldalw
4、aysbethelastsoftirq*/3.4.????NR_SOFTIRQS5.};復(fù)制代碼好像后為為RPS新增了一個,不過這我的內(nèi)核版本偏低。1.2激活?當(dāng)需要調(diào)用軟中斷時,需要調(diào)用raise_softirq函數(shù)激活軟中斷,這里使用術(shù)語“激活”而非“調(diào)用”,是因為在很多情況下不能直接調(diào)用軟中斷。所以只能快速地將其標(biāo)志為“可執(zhí)行”,等待未來某一時刻調(diào)用。為什么“在很多情況下不能直接調(diào)用軟中斷”?試想一下下半部引入的理念,就是為了讓上半部更快地執(zhí)行。如果在中斷程序代碼中直接調(diào)用軟中斷函數(shù),那么
5、就失去了上半部與下半部的區(qū)別,也就是失去了其存在的意義。內(nèi)核使用一個名為__softirq_pending的位圖來描述軟中斷,每一個位對應(yīng)一個軟中斷,位圖包含在結(jié)構(gòu)irq_stat中:1.typedefstruct{2.????unsignedint__softirq_pending;3.????……4.}____cacheline_alignedirq_cpustat_t;5.6.DECLARE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t,irq_stat);復(fù)制
6、代碼宏or_softirq_pending用于設(shè)置相應(yīng)的位(位或操作):1.#defineor_softirq_pending(x)????percpu_or(irq_stat.__softirq_pending,(x))復(fù)制代碼local_softirq_pending用于取得整個位圖(而非某一位):1.#definelocal_softirq_pending()????percpu_read(irq_stat.__softirq_pending)復(fù)制代碼宏__raise_softirq_ir
7、qoff是or_softirq_pending的包裹:1.#define__raise_softirq_irqoff(nr)do{or_softirq_pending(1UL<<(nr));}while(0)復(fù)制代碼raise_softirq_irqoff通過調(diào)用__raise_softirq_irqoff實現(xiàn)激活軟中斷,它的參數(shù)nr即位軟中斷對應(yīng)的位圖槽位:1./*2.*Thisfunctionmustrunwithirqsdisabled!3.*/4.inlinevoidraise_soft
8、irq_irqoff(unsignedintnr)5.{6.????//置位圖,即標(biāo)記為可執(zhí)行狀態(tài)7.????__raise_softirq_irqoff(nr);8.9.????/*10.??????*Ifwe'reinaninterruptorsoftirq,we'redone11.??????*(thisalsocatchessoftirq-disabledcode).Wewill12.??????*actuallyrunthesoftirqoncewereturnfrom13.?????