# 2024q1 Homework5 (assessment)
contributed by < `jychen0611` >
## 測驗題改進與提問
>從前 4 週的測驗題選出 3 題改進 (含延伸問題,要有對應的 Linux 核心原始程式碼案例探討),觀摩其他學員並參照授課教師進行的作業檢討和回顧,若有不能理解的部分,請標註出來。善用 HackMD 的語法 :::info 和 ::: 標註你的提問,在原有的筆記頁面更新即可。
## 閱讀〈因為自動飲料機而延畢的那一年〉
>紀錄閱讀〈因為自動飲料機而延畢的那一年〉的啟發,特別在學習本課程 5 週之後的感想,應具體描述你在課堂、課後創作,和觀摩其他學員的成果時,理解到系統軟體的開發態度、對細節的重視,以及理論和實務的融會貫通。
>*資工系的學生不會寫程式,機械系的學生不會做機械,電工系的學生不會焊電路* QAQ
回想我從小到大在學校接觸過 99.99% 的問題都具有一定程度上的理想化
>*飲料機的程式愷宏是寫不出來的,之所以能在一個月內完成,是因為我在大學期間就寫過好幾個網站了。愷宏能和工廠溝通、設計出可用的零件,是因為他在大一就在跑工廠做東西了。紘銘能輕易的設計出飲料機的電路,是因為他曾花了很多時間在電子電路課上頭,做了很多習題,纏著教授把每個疑惑都搞懂才罷休。*
機會是留給準備好的人。經歷了前五週的學習,我才發現自己對於基礎學科的掌握程度是如此的差勁,統計沒學好,數學學了不會用,英文看不懂,連個簡單的程式碼都能有 bug。
有些項目看似修課考試都考得不錯,作業也都有完成,但實際上去面對問題的時候要如何應用所學卻是相當困難。
不過我大學階段甚至連有些科目的作業考試都沒做好,基礎科目還是在準備考研究所時才認真把內容理解,仔細推導出公式。
雖然研究所考試也沒考多好,但至少在步入考場那一刻我的內心是平靜的,考完試我的心理是踏實的。
上了研究所後才意識到打好基礎的重要性,也額外撥時間閱讀基礎科目的原文書,如果大學有現在的一半認真也許就不會被當了。
我到現在還有點像是隻無頭蒼蠅,不清楚自身的價值在哪裡,只能把當前的事先做出一定程度,也許是假裝努力吧,至少比以前直接擺爛還好看一點。
但到現在至少我確定的一點就是,我會去享受了解一件事情細節的過程,儘管我理解的速度可能不快。
## 研讀第 1 到第 6 週「課程教材」和 CS:APP 3/e
>研讀第 1 到第 6 週「課程教材」和 CS:APP 3/e (至少到第二章),紀錄心得和提問。針對自訂題目,例如貢獻程式碼到 Linux 核心,也將自己的構想和規劃記錄下來,隨後與授課教師一對一討論時可運用。
## 簡述想投入的專案
> 參照 [2023 年期末專題](https://hackmd.io/@sysprog/linux2023-projects),簡述你想投入的專案 (亦可建立新專案),至少選出 (或訂出) 二個。
1. Homework6 integration 整合井字遊戲對弈,並從中延伸
2. vwifi 虛擬無線網路裝置驅動程式和實驗環境
> [vwifi](https://github.com/sysprog21/vwifi) 是個程式碼約~~五百行~~三千行,具體而微的 WiFi 裝置驅動程式,採用 cfg80211 框架。目前 vwifi 支援 scan, connect, disconnect 等 cfg80211 的介面操作,並得以正確處理 Tx/Rx 封包。
* 2022 執行人: rickywu0421 → [開發記錄](https://hackmd.io/@rickywu0421/FinalProject)
* 2023 執行人: willwillhi1 → [開發紀錄](https://hackmd.io/@sysprog/rJD2joOSh)
___
TODO:
* 針對 cfg80211 和核心的版本,在 GitHub 提交 issue => Linux 版本之間的變異
***[Align with recent cfg80211 header #61](https://github.com/sysprog21/vwifi/issues/61)***
### issue1 : The scan_width field is no longer present in the cfg80211_inform_bss structure.
***[commit 5add321](https://github.com/torvalds/linux/commit/5add321c329b1746589b51359259666ca3dbe219) wifi: cfg80211: remove scan_width support***
kernel version : *v6.9-rc6 ~ v6.7-rc1*
date : Sep 13, 2023
>There really isn't any support for scanning at different
channel widths than 20 MHz since there's no way to set it.
Remove this support for now, if somebody wants to maintain
this whole thing later we can revisit how it should work.
```diff
- bss_data.scan_width = NL80211_BSS_CHAN_WIDTH_20;
```
### issue2: The parameters in the change_beacon function have been updated to cfg80211_ap_update.
***[commit bb55441](https://github.com/torvalds/linux/commit/bb55441c57ccc5cc2eab44e1a97698b9d708871d) wifi: cfg80211: split struct cfg80211_ap_settings***
kernel version : *v6.9-rc6 ~ v6.7-rc1*
date : Sep 25, 2023
>Using the full struct cfg80211_ap_settings for an update is
misleading, since most settings cannot be updated. Split the
update case off into a new struct cfg80211_ap_update.
```diff
/**
* struct cfg80211_ap_update - AP configuration update
*
* Subset of &struct cfg80211_ap_settings, for updating a running AP.
*
* @beacon: beacon data
* @fils_discovery: FILS discovery transmission parameters
* @unsol_bcast_probe_resp: Unsolicited broadcast probe response parameters
*/
+struct cfg80211_ap_update {
+ struct cfg80211_beacon_data beacon;
+ struct cfg80211_fils_discovery fils_discovery;
+ struct cfg80211_unsol_bcast_probe_resp unsol_bcast_probe_resp;
+};
int (*change_beacon)(struct wiphy *wiphy, struct net_device *dev,
- struct cfg80211_ap_settings *info);
+ struct cfg80211_ap_update *info);
```
***[commit 66f85d5](https://github.com/torvalds/linux/commit/66f85d57b7109baf8a7d5ee04049ac9412611d35) wifi: cfg80211: modify prototype for change_beacon***
kernel version : *v6.9-rc6 ~ v6.7-rc1*
date : Sep 13, 2023
>Modify the prototype for change_beacon() in struct cfg80211_op to
accept cfg80211_ap_settings instead of cfg80211_beacon_data so that
it can process data in addition to beacons.
Modify the prototypes of ieee80211_change_beacon() and driver specific
functions accordingly.
```diff
int (*change_beacon)(struct wiphy *wiphy, struct net_device *dev,
- struct cfg80211_beacon_data *info);
+ struct cfg80211_ap_settings *info);
```
Simply put, the change_beacon function now includes two additional parameters, fils_discovery and unsol_bcast_probe_resp, which are part of the cfg80211_ap_update structure.
* fils_discovery : FILS discovery transmission parameters
* unsol_bcast_probe_resp : Unsolicited broadcast probe response parameters
___
TODO:
* 提出在六月底之前待做任務
### 前置任務
- [x] 閱讀 [802.11 Wireless Networks: The Definitive Guide, 2/e ](https://www.tenlong.com.tw/products/9780596100520) 建立基礎概念 $\rightarrow$ [筆記](https://hackmd.io/@jychen0611/IEEE_802_11)
- [x] 對 802.11 有基礎的了解, 了解 MAC layer 狀態機:(ch7)
Scan
802.11 Authentication
Association
(802.1X Authentication)
(4 way handshake)
- [x] 以及掌握 frame type 以及對應的 subtype: (ch4)
Data frame
data
QOS data
…
Management frame
beacon
probe resquest
probe response
…
Control frame
ACK
CTS
RTS
…
- [x] 以及 frame structure, 尤其是 management 的 frame structure: (ch4)
MAC header
Mandatory field
Information element
- [x] 針對特定主題查閱 IEEE 802.11 standard
- [x] Layer management (MLME)
- [ ] Linux kernel: [include/linux/ieee80211.h](https://github.com/torvalds/linux/blob/master/include/linux/ieee80211.h) 當作 frame structure 的對照
- [x] 閱讀 [LKMPG](https://sysprog21.github.io/lkmpg/)
目前進度 : ch20
- [ ] [Linux 802.11 Driver Developer’s Guide](https://docs.kernel.org/driver-api/80211/index.html)
- [ ] [cfg80211 subsystem](https://docs.kernel.org/driver-api/80211/cfg80211.html)
- [ ] [mac80211 subsystem (basics)](https://docs.kernel.org/driver-api/80211/mac80211.html)
- [ ] [mac80211 subsystem (advanced)](https://docs.kernel.org/driver-api/80211/mac80211-advanced.html)
- [ ] [sk_buff](https://github.com/torvalds/linux/blob/master/include/linux/skbuff.h#L859) and [net_device](https://docs.kernel.org/networking/kapi.html#c.net_device)
- [x] 閱讀 [vwifi 程式碼導讀](https://hackmd.io/@sysprog/rJD2joOSh#vwifi-%E7%A8%8B%E5%BC%8F%E7%A2%BC%E5%B0%8E%E8%AE%80)
- [x] [Linux 核心設計: 淺談同步機制](https://hackmd.io/@sysprog/linux-sync?type=view)
- [x] [Linux 核心模組運作原理](https://hackmd.io/@sysprog/linux-kernel-module)
- [ ] vwifi $\rightarrow$ [筆記](https://hackmd.io/@jychen0611/VWIFI2024)
### TODO : 修正 kernel v6.7 後的編譯問題,同時維持 v6.7 版本前的相容性
:::success
***[commit 35ca058](https://github.com/sysprog21/vwifi/commit/35ca058e767d87a7c809091ae910b2bf02687870)***
Align with recent cfg80211 header ([#63](https://github.com/sysprog21/vwifi/pull/63))
:::
### TODO : Modify indexing for function vwifi_interface_add [PR#58](https://github.com/sysprog21/vwifi/pull/58)
### TODO : Modularize the source files [ISSUE#64](https://github.com/sysprog21/vwifi/issues/64)
### TODO : Emulate rate and mcs
:::success
***[commit a8bdf8a](https://github.com/sysprog21/vwifi/commit/a8bdf8a4405cbc5e25beb29bb1956bfac82ca303)***
Support data rate and MCS ([#67](https://github.com/sysprog21/vwifi/pull/67))
:::
ref :
[WI-FI 7 MCS TABLE](https://semfionetworks.com/blog/wi-fi-7-mcs-table/)
[MCS Table](https://mcsindex.net/)
* QPSK (4-QAM)
* 利用 $0^\circ, 90^\circ, 180^\circ, 270^\circ$ 四個相位表示00 01 10 11,因此可表達 2 bits
* QAM
* $2^n$ QAM 表一個 symbol 可表達 n bits


採用 802.11n(HT) 做為 PHY,設置如下 :
Modulation : 64-QAM
Data Bandwidth : 20MHz
Number of Spatial Streams : 4
參照 802.11n(HT) 調變對照表可得
Number of Data Subcarriers : 52
Number of Coded Bits per Subcarrier per Stream : 6
Coding : 5/6
OFDM Symbol Duration : 3.2 us
Guard Interval Duration : 0.8 us
經公式推算後可得 data rate = 260 Mbps 與顯示結果相符
即
$N_{SD} =52$
$N_{BPSCS} =6$
$R = \frac{5}{6}$
$N_{SS} = 4$
$T_{DFT} = 3.2\ us$
$T_{GI} = 0.8\ µs$
```diff
sinfo->filled = BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
BIT_ULL(NL80211_STA_INFO_RX_PACKETS) |
BIT_ULL(NL80211_STA_INFO_TX_FAILED) |
BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
BIT_ULL(NL80211_STA_INFO_SIGNAL) |
BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME)|
+ BIT_ULL(NL80211_STA_INFO_RX_BITRATE)|
+ BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
/* Support 64-QAM modulation with 20MHz data bandwidth*/
+ sinfo->rxrate.flags |= RATE_INFO_FLAGS_MCS;
+ sinfo->rxrate.mcs = 31;
+ sinfo->rxrate.bw = RATE_INFO_BW_20;
+ sinfo->rxrate.n_bonded_ch = 1;
+ sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
+ sinfo->txrate.mcs = 31;
+ sinfo->txrate.bw = RATE_INFO_BW_20;
+ sinfo->txrate.n_bonded_ch = 1;
```
```shell
tx bitrate: 260.0 MBit/s MCS 31
rx bitrate: 260.0 MBit/s MCS 31
```
### TODO : implement vwifi_set_tx_power & vwifi_get_tx_power
```diff
struct vwifi_vif {
+ /* Transmission power */
+ s32 tx_power;
};
```
```diff
static struct cfg80211_ops vwifi_cfg_ops = {
+ .set_tx_power = vwifi_set_tx_power,
+ .get_tx_power = vwifi_get_tx_power,
};
```
```shell
# set transmit power
$sudo iwconfig vw0 txpower 11
$sudo iwconfig vw1 txpower 12
$sudo iwconfig vw2 txpower 13
# get phy number of each interface
$sudo iw dev
phy#12
Interface vw2
ifindex 14
wdev 0xc00000001
addr 00:76:77:32:00:00
type managed
txpower 13.00 dBm
phy#11
Interface vw1
ifindex 13
wdev 0xb00000001
addr 00:76:77:31:00:00
type managed
txpower 12.00 dBm
phy#10
Interface vw0
ifindex 12
wdev 0xa00000001
addr 00:76:77:30:00:00
type managed
txpower 11.00 dBm
```
為了維持命令一致性,改採用 `sudo iw dev [interface] set txpower auto/limit/fixed [transmit power (mBm)]` 設置 transmit power。
由於使用上述命令會導致 `wdev` 參數為 NULL,因此新增函式 `wiphy_get_vwifi_vif` 取得 virtual interface。
此外,亦利用參數 `type` 以區別命令為 auto/limit/fixed。
auto : 使用預設傳輸功率
limit : 限制功率上下限於特定值
fixed : 可隨意設置功率
___
## 主題一 Roaming
### TODO : 新增平面空間絕對位置模擬,使訊號強度可被距離推算
`rickywu` 以 sin 函數使訊號強度隨時間周期性變化並落在合理範圍內,然而訊號強度與距離和障礙物阻擋有關 (Path Loss and Shadowing)。

因此我想設定一個合理的二維平面空間範圍(約莫等於 wifi 的極限傳輸範圍),使 STA 生成時隨機座落在此範圍內,並設定多個 AP ,使 STA 能選擇訊號品質較高的 AP 連線,模擬真實訊號強度。
情境假設為以下列條件:
* Free-Space Path Loss
* line-of-sight (LOS) channel
receive power $P_r\ (W)$
**求法一**
$$P_r=\frac{A_eG_tP_t}{4{\pi}d^2}$$
$$G_r=\frac{4{\pi}A_e}{{\lambda}^2}$$
$$P_r=\frac{G_rG_tP_t}{(\frac{4{\pi}d}{\lambda})^2}$$
* $G_t$ : the transmitting antenna gain
* $G_r$ : the receiving antenna gain
* $P_t$ : transmitter power
* $d$ : distance
**求法二**
$$L_f(dB)=\frac{P_t}{P_r}=32.45+20\log_{10} {f_c}(MHz)+20\log_{10}{d}(km)$$
$$P_r=\frac{P_t}{L_f}$$
* $f_c$ : carrier frequency
### TODO : 新增 STA mobility 演算法,模擬 STA 隨機移動,進而使 roaming 機制能夠運作
### TODO : 設計 roaming 演算法,探討不同演算法在不同實驗設置下的表現
## 主題二 : Support adhoc (IBSS) mode