Category: kernel

  • The packet flow, from userspace to kernel driver in Linux network stack

    這是一個想要從 userspace 一直深入到 driver,理解 Linux packet 究竟如何運行的流程圖。 我在交大做的東西都在 driver space,使用 ath9k/ath10k 做實驗。我所理解的是 ath9k 上面對著 mac80211 (Linux 的 softmac),也就是說,driver 在功能上,只需要實做`struct ieee80211_ops` 的功能即可。主要都在處理 `.wake_tx_queue` 的功能。 對於他之上,頂多知道 ieee80211_queue_skb 最後會 call drv_wake_tx_queue 然後根據  driver 不同呼叫不同 driver 的實做,以 ath9k 而言就是 ath9k_wake_tx_queue。 還是想要知道一下,究竟這個 packet 是如何被傳入 mac80211 這層,順便解答一個以前問自己的問題「wireshark 抓封包時究竟是從那一層抓的」。 使用兩種工具,一個是 cflow、一個是 pycflow2dot。前者用來描述 call graph,後者拿來轉成 graphviz 的 dot 語言並且輸出 svg。兩者都有做修改,cflow 的部份請參考備註的部份,pycflow2dot 請參考我修改的版本: mlouielu/pycflow2dot。…

  • netlink: No buffer space available

    問題在於 kernel buffer 不足。 http://lists.netfilter.org/pipermail/netfilter/2007-February/067929.html 解法: echo 500000 > /proc/sys/net/core/rmem_max  

  • Intel 64 and IA32 architectures Memory Model – Segmentation and Paging

    32-bit paging: CR0.PG = 1 & CR4.PAE = 0 (section 4.3) CR0.WP CR4.PSE CR4.PGE CR4.SMEP CR4.SMAP PAE paging; CR0.PG = 1 & CR4.PAE = 1 & IA32_EFER.LME = 0 (section 4.4) CR0.WP CR4.PGE CR4.SMEP CR4.SMAP IA32_EFER.NXE 4-level paging: CR0.PG = 1 & CR4.PAE = 1 & IE32_EFER.LME = 1 (section 4.5) CR0.WP CR4.PGE CR4.PCIDE CR4.SMEP…

  • Python socket OSError: [Errno 105] No buffer space available

    今天在處理 netlink 的時候,使用 Python 當作 user-space 承接來自 kernel-space 訊息的工具。我的情況是大約每 10ms 會由 kernel-space 發起一次 netlink multicast 到 user-space。當在運行 user-space script 的時候,過一陣子會出現這個錯誤: Traceback (most recent call last): File “measure_netlink.py”, line 33, in <module> b = sock.recvfrom(NETLINK_BUF_LENGTH)[0] OSError: [Errno 105] No buffer space available 一陣搜尋後, 其中一個方法是調整 sysctl 的參數,沒用。 修改 net.ipv4.xfrm4_gc_thresh=999999,沒用。 來自 netfilter 的說明: 來自 netfilter 說明:這代表 kernel-side 的…

  • TIL: private data struct in Linux kernel

    在 Linux kernel 當中有許多的 common struct,例如說 ieee80211_txq 這樣的結構,透過這樣的方式,可以讓不同的 driver 重用相同的 struct。但是,針對每個不同的 driver,還是會有不同的 private data,這時候就會用 private data struct 的方式來處理。 例如說,struct ieee80211_txq 是 mac80211 中的 intermediate tx queue,目前有使用的 wireless driver 為 ath9k, ath10k 以及 mt76。struct ieee80211_txq 的整個結構為: /** * struct ieee80211_txq – Software intermediate tx queue * * @vif: &struct ieee80211_vif pointer from the add_interface callback. * @sta: station table…

  • TIL: ARRAY_SIZE in Linux kernel

    from net/mac80211/sta_info.c, define at include/linux/kernel.h /** * ARRAY_SIZE – get the number of elements in array @arr * @arr: array to be sized */ #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) __must_be_array: 檢查 a 是否為 array,當 a 為 array 時,會因為 &(a)[0] 變成 pointer,而讓 __same_type 產生 0 的結果,反之如果 a 為 pointer,因為 &(a)[0] 仍然是 pointer, __same_type 回傳…

  • Qualcomm ath9k 驅動程式解析 (二) – 幾個重要結構

    ieee80211_local *local 用途: contains information about the real hardware, and is created when the interface is first added. (Daniel Caps Mur, Linux Wi-Fi open source drivers-mac80211,ath9k/ath5k-, pp.1) each instance of these (hw is embedded into local) represents a wireless driver, ieee80211_hw is the part of the ieee80211_local that is visible to drivers, contains all operating…

  • Qualcomm ath9k 驅動程式解析 (一) – 初始化過程

    以 linux-4.19-rc2 為例,平台為 TP-Link WDR4300。程式碼以 linux-4.19-rc2 為準,有特別標示者以 OpenWrt/LEDE 為準。 1. ath9k 驅動程式註冊點:ath9k/init.c#ath9k_init(void) static int __init ath9k_init(void) { int error; error = ath_pci_init(); if (error < 0) { pr_err(“No PCI devices found, driver not installed\n”); error = -ENODEV; goto err_out; } error = ath_ahb_init(); if (error < 0) { error = -ENODEV; goto err_pci_exit; } dmi_check_system(ath9k_quirks);…

  • TIL: ALIGN in Linux kernel

    from include/uapi/linux/kernel.h and scripts/dtc/dtc.h #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))a – 1) #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask)) or #define ALIGN(x, a) (((x) + (a) – 1) & ~((a) – 1))  

  • Perf tracing wireless driver on LEDE/OpenWRT

    Perf tracing wireless driver on LEDE/OpenWRT

    1. Config LEDE/OpenWRT with CONFIG_PACKAGE_perf=y CONFIG_KERNEL_PERF_EVENTS=y 2. Add dynamic probe with external modules $ perf probe -a -m /lib/modules/4.9.109/ath9k.ko ‘ath_tx*’ $ perf probe -a -m /lib/modules/4.9.109/ath9k.ko ‘ath9k_wake_tx_queue’ $ perf probe -a -m /lib/modules/4.9.109/mac80211.ko ‘fq_*’ $ perf probe -a -m /lib/modules/4.9.109/mac80211.ko ‘ieee80211_tx*’ 3. Perf Recording # Sample CPU stack traces, only with “probe:” events, by ping,…