2016年4月28日 星期四

Raspberry Pi - 無線基地台橋接模式

1. 網路架構圖
          ---------------------------------------------
         |        wlan0          |         eth0        |
       LAN PC               Raspberry Pi             Modem
  IP: 192.168.0.100             br0             IP: 192.168.0.1
  GW: 192.168.0.1               
2. 準備一張Edimax EW-7811Un無線網卡wlan0,當作Raspberry Pi的LAN
內建的Ethernet網卡eth0,當作Raspberry Pi的WAN
http://www.edimax.com/edimax/merchandise/merchandise_detail/data/edimax/tw/wireless_adapters_n150/ew-7811un/
3. Raspberry Pi啟動後,確認系統是否有正確抓到無線網卡
$ lsusb
Bus 001 Device 005: ID 3538:0901 Power Quotient International Co., Ltd
Bus 001 Device 004: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS]
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
4. 安裝hostapd套件,讓無線網卡模擬成Soft AP
$ sudo apt-get install hostapd
由於內建的hostapd不支援此張網卡,需要先移除hostpad程式,但保留相關的設定檔
$ sudo apt-get remove hostapd
5. Edimax EW-7811Un的驅動程式,需要從Realtek官網下載,請選擇RTL8188CUS的Linux版本
首頁 > 下載中心 > 通訊網路 ICs > Wireless LAN ICs > WLAN NIC > IEEE 802.11b/g/n Single-Chip > Software > RTL8188CUS Linux
http://www.realtek.com/downloads/downloadsView.aspx?Langid=2&PNid=21&PFid=48&Level=5&Conn=4&DownTypeID=3&GetDown=false&Downloads=true#2742

透過wget命令,下載原廠無線網卡驅動程式到Raspberry Pi
$ wget http://12244.wpc.azureedge.net/8012244/drivers/rtdrivers/cn/wlan/0001-RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip
6. 安裝hostapd程式
$ unzip 0001-RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip

$ cd RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/

$ tar -zxvf wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz

$ cd wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd

$ make clean && make && sudo make install
將hostapd相關檔案複製到/usr/bin
$ sudo cp /usr/local/bin/hostapd* /usr/sbin
7. 安裝wireless工具
$ cd ../../../wireless_tools

$ tar -zxvf wireless_tools.30.rtl.tar.gz

$ cd wireless_tools.30.rtl

$ make clean && make && sudo make install
8. 建立hostapd設定檔
driver: 無線網卡的驅動程式
ssid: 基地台名稱。
channel: 無線通訊的頻道,從1到11
wpa: 無線網路加密協定,1是WPA,2是WPA2
wpa_passphrase: 無線基地台的密碼。
$ sudo vi /etc/hostapd/hostapd.conf

interface=wlan0
driver=rtl871xdrv
bridge=br0
ssid=pi
channel=6
hw_mode=g
ieee80211n=1
wmm_enabled=1
wpa=2
wpa_passphrase=raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0
測試hosapd設定檔
$ sudo hostapd -dd /etc/hostapd/hostapd.conf
如沒有出現錯誤,按Ctrl+C停止hostapd,編輯hostapd服務設定檔
$ sudo vi /etc/default/hostapd

# Defaults for hostapd initscript
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
#DAEMON_CONF=""
DAEMON_CONF="/etc/hostapd/hostapd.conf"

# Additional daemon options to be appended to hostapd command:-
#       -d   show more debug messages (-dd for even more)
#       -K   include key data in debug messages
#       -t   include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
#DAEMON_OPTS=""
啟動hostapd服務
$ sudo service hostapd restart
開機時自動啟動hostapd服務
$ sudo update-rc.d hostapd defaults
9. 安裝網路橋接功能套件
$ sudo apt-get -y install bridge-utils
10. 修改無線網卡的設定值,加入橋接功能
$ sudo vi /etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

#allow-hotplug wlan0
#iface wlan0 inet manual
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

#allow-hotplug wlan1
#iface wlan1 inet manual
#    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

auto br0
iface br0 inet dhcp
bridge_ports eth0 wlan0
11. 重新啟動
$ sudo reboot
12. 重新啟動後,可下brctl橋接命令,確認橋接模式
$ brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.74da3855819b       no              eth0
                                                        wlan0
13. 參考來源
RPI-Wireless-Hotspot
Raspberry Pi 的應用 - Wi-Fi 無線基地台
Raspberry Pi 的實作 - 自動配置 IP 位址資訊的 DHCP Server
敗家學習之旅之樹莓派(Raspberry Pi) Realtek WIFI AP 架設成功心得分享

2016年4月22日 星期五

Raspberry Pi - 無線基地台路由模式 hostapd + dnsmasq

1. 網路架構圖
          ---------------------------------------------
         |        wlan0          |         eth0        |
       LAN PC               Raspberry Pi             Modem
  IP: 192.168.1.100    WAN eth0:  192.168.0.100   IP: 192.168.0.1
  GW: 192.168.1.1      LAN wlan0: 192.168.1.1         
2. 準備一張Edimax EW-7811Un無線網卡wlan0,當作Raspberry Pi的LAN
內建的Ethernet網卡eth0,當作Raspberry Pi的WAN
http://www.edimax.com/edimax/merchandise/merchandise_detail/data/edimax/tw/wireless_adapters_n150/ew-7811un/
3. Raspberry Pi啟動後,確認系統是否有正確抓到無線網卡
$ lsusb
Bus 001 Device 005: ID 3538:0901 Power Quotient International Co., Ltd
Bus 001 Device 004: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS]
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
4. 安裝hostapd套件,讓無線網卡模擬成Soft AP
$ sudo apt-get install hostapd
由於內建的hostapd不支援此張網卡,需要先移除hostpad程式,但保留相關的設定檔
$ sudo apt-get remove hostapd
5. Edimax EW-7811Un的驅動程式,需要從Realtek官網下載,請選擇RTL8188CUS的Linux版本
首頁 > 下載中心 > 通訊網路 ICs > Wireless LAN ICs > WLAN NIC > IEEE 802.11b/g/n Single-Chip > Software > RTL8188CUS Linux
http://www.realtek.com/downloads/downloadsView.aspx?Langid=2&PNid=21&PFid=48&Level=5&Conn=4&DownTypeID=3&GetDown=false&Downloads=true#2742

透過wget命令,下載原廠無線網卡驅動程式到Raspberry Pi
$ wget http://12244.wpc.azureedge.net/8012244/drivers/rtdrivers/cn/wlan/0001-RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip
6. 安裝hostapd程式
$ unzip 0001-RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip

$ cd RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/

$ tar -zxvf wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz

$ cd wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd

$ make clean && make && sudo make install
將hostapd相關檔案複製到/usr/bin
$ sudo cp /usr/local/bin/hostapd* /usr/sbin
7. 安裝wireless工具
$ cd ../../../wireless_tools

$ tar -zxvf wireless_tools.30.rtl.tar.gz

$ cd wireless_tools.30.rtl

$ make clean && make && sudo make install
8. 建立hostapd設定檔
driver: 無線網卡的驅動程式
ssid: 基地台名稱。
channel: 無線通訊的頻道,從1到11
wpa: 無線網路加密協定,1是WPA,2是WPA2
wpa_passphrase: 無線基地台的密碼。
$ sudo vi /etc/hostapd/hostapd.conf

interface=wlan0
driver=rtl871xdrv
bridge=br0
ssid=pi
channel=6
hw_mode=g
ieee80211n=1
wmm_enabled=1
wpa=2
wpa_passphrase=raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0
測試hosapd設定檔
$ sudo hostapd -dd /etc/hostapd/hostapd.conf
如沒有出現錯誤,按Ctrl+C停止hostapd,編輯hostapd服務設定檔
$ sudo vi /etc/default/hostapd

# Defaults for hostapd initscript
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
#DAEMON_CONF=""
DAEMON_CONF="/etc/hostapd/hostapd.conf"

# Additional daemon options to be appended to hostapd command:-
#       -d   show more debug messages (-dd for even more)
#       -K   include key data in debug messages
#       -t   include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
#DAEMON_OPTS=""
啟動hostapd服務
$ sudo service hostapd restart
開機時自動啟動hostapd服務
$ sudo update-rc.d hostapd defaults
9. 修改無線網路卡的設定值,設定一個指定的靜態 IP 位址
$ sudo vi /etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.1.1
netmask 255.255.255.0
10. 路由模式需要在Raspberry Pi架設dnsmasq
$ sudo apt-get -y install dnsmasq
修改dnsmasq設定檔
$ sudo vi /etc/dnsmasq.conf

# If you want dnsmasq to listen for DHCP and DNS requests only on
# specified interfaces (and the loopback) give the name of the
# interface (eg eth0) here.
# Repeat the line for more than one interface.
#interface=
interface=wlan0


# Uncomment this to enable the integrated DHCP server, you need
# to supply the range of addresses available for lease and optionally
# a lease time. If you have more than one network, you will need to
# repeat this for each network on which you want to supply DHCP
# service.
#dhcp-range=192.168.0.50,192.168.0.150,12h
dhcp-range=192.168.1.100,192.168.1.254,12h
如果想要開機時使用dnsmasq設定,需要修改設定
$ sudo vi /etc/default/dnsmasq

#DOMAIN_SUFFIX=`dnsdomainname`
DNSMASQ_OPTS="--conf-file=/etc/dnsmasq.conf"


# By default search this drop directory for configuration options.
# Libvirt leaves a file here to make the system dnsmasq play nice.
# Comment out this line if you don't want this. The dpkg-* are file
# endings which cause dnsmasq to skip that file. This avoids pulling
# in backups made by dpkg.
#CONFIG_DIR=/etc/dnsmasq.d,.dpkg-dist,.dpkg-old,.dpkg-new
啟動dnsmasq服務
$ sudo service dnsmasq restart
開機時自動啟動dnsmasq服務
$ sudo update-rc.d dnsmasq defaults
11. 啟動IP Forwarding的功能,將net.ipv4.ip_forward=1拿掉註解
$ sudo vi /etc/sysctl.conf

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
12. 啟用iptables NAT功能
$ sudo iptables -F
$ sudo iptables -F -t nat
$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
$ sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
$ sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
儲存目前iptables設定值
$ sudo bash -c 'iptables-save > /etc/network/iptables'
$ sudo iptables-restore < /etc/network/iptables
設定開機時,讓iptables的規則自動被載入
$ sudo vi /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

sudo iptables-restore < /etc/network/iptables

exit 0
13. 重新啟動
$ sudo reboot
14. 參考來源
RPI-Wireless-Hotspot
Raspberry Pi 的應用 - Wi-Fi 無線基地台
Raspberry Pi 的實作 - 自動配置 IP 位址資訊的 DHCP Server
敗家學習之旅之樹莓派(Raspberry Pi) Realtek WIFI AP 架設成功心得分享

2016年4月21日 星期四

新加坡四日遊 - 行程規劃、住宿、旅遊票卷

行程表
---------------------------------------------------------------------
10/8 (四)
01:05 - 05:50 桃園機場 → 樟宜機場

到新加坡首先要解決的是交通與上網的問題
- 辦SingTel hi! Tourist SIM $15,可到以下三個地方辦,便利商店是最方便,辦的時候記得指定SD, micro SD或nano SD卡。
  1. RHB Bank Currency Exchange Counters
  2. Changi Recommends Counters
  3. 便利商店
- 辦易通卡(EZ - Link Card),建議旅人從樟宜機場第二航廈到達 SMRT 地鐵站後,就先在服務處購買一張 EZ-Link Card。

早餐
- Toast Box 吐司工坊 24 hours [Changi Airport T3]

- Haji Lane蘇丹回教堂 [EW12,Bugis 武吉士]
- 亞拉街(Arab St.) [EW12,Bugis 武吉士] 蘇丹回教堂後面

* 唐城坊 天宇旅行社,買優惠票 10:00~20:00 [NE4,China Town 牛車水 E出路]
  如果機場沒辦法辦Sintel,可以到唐城坊辦

- 牛車水郵局買郵票,在唐城坊附近 [NE4,China Town 牛車水 E出路]

午餐
- Maxwell Food Centre麥士威熟食中心 [EW15,Tanjong Pagar 、NE4,Chinatown 牛車水]
  天海南雞飯11:00~17:00
  松發肉骨茶
  金華魚片米粉
  老伴豆花
  真真粥品
  薏米水

- 馬里安興都廟 [NE4,China Town 牛車水]
- 亞坤咖椰吐司 總店 [NE4,China Town 牛車水]
- 史密斯街 寶塔街  [NE4,China Town 牛車水]
- 林志源肉乾 [NE4,China Town 牛車水]
- 味香園  [NE4,China Town 牛車水]
- 佛牙寺 [NE4,Chinatown 牛車水]

- 克拉碼頭 (末班22:30)  [NE5,克拉碼頭]
- 慕達發中心 [NE8,Farrer Park 花拉公園]

10/9 (五)
早餐
- Zam Zam 印度烤餅 [EW12,Bugis 武吉士]
- 哈芝巷喝咖啡 [EW12,Bugis 武吉士]
- 小印度拱廊 [NE7,Little India小印度、NE8,Farrer Park 花拉公園]
- 阿都卡夫回教堂 (09:00~13:00/ 14:30~18:30)  [NE7,Little India 小印度]

午餐
- 竹腳市場 囉惹 黃薑飯 印度煎餠 [NE7,Little India 小印度]
- 實龍崗路  [NE7,Little India 小印度]
- 烏節路 [NS22,Orcharad 烏節]

- 夜間動物園 19:30~24:00  [NS16,Ang Mo Kio 宏茂橋站] 走C出口轉138公車 30~40分
  http://www.nightsafari.com.sg/
  夜間動物表演: 晚上7.30、8.30、9.30以及10.30  *仅限星期五、星期六以及公共假期前夕
  婆羅洲土著表演: 傍晚6.45、晚上8.00、9.00以及10.00  *仅限星期五、星期六以及公共假期前夕。
  遊覽車探險體驗: 40分

10/10 (六)
早餐
- Luge斜坡渦車 Skyride空中吊車 [NE1,Harbour Front 港灣站 → 聖淘沙 Imbiah Station 英比奧捷運站]
- 海洋館 [NE1,Harbour Front 港灣站 → 聖淘沙 Imbiah Station 英比奧捷運站]

午餐
- 聖安德烈教堂 (EW13/NS25,City Hall 政府大廈)
- 萊佛士商場 [EW14/NS26,Raffles Place 萊佛士坊]
- 老巴剎沙嗲 [EW14/NS26,Raffles Place 萊佛士坊]

- Cavenagh Bridge加文納橋 [EW14/NS26,Raffles Place 萊佛士坊]
- Gardens by the bay 濱海灣花園 [CE1,Bayfront 海灣舫]
- Singapore Flyer新加坡摩天觀景輪 before 18:00 [CE1,Bayfront 海灣舫]
- 魚尾獅公園 燈光秀 8:00PM、9:30PM [EW14/NS26,Raffles Place 萊佛士坊]

10/11 (日)
- 結霜橋叻沙 09:00~18:00 [NE8,Farrer Park 花拉公園]
- Lady M 甜點 [EW14/NS26,Raffles Place 萊佛士坊]

10/12 (一)
00:45 - 05:20 樟宜機場 → 桃園機場


新加坡天氣
---------------------------------------------------------------------
匯率:1:24
氣候::年均低溫24度,高溫31度 (短袖、短褲、雨傘)
簽證:免簽30天
桃園機場-->新加坡:約四個半小時
電器插頭:三孔式 (220-240V)


新加坡交通
---------------------------------------------------------------------
1. 易通卡 (EZ - Link Card)
http://home.ezlink.com.sg/get-your-ez-link-card/where-the-cards-are-sold

EZ - Link 卡儲值有效期為 5 年,可在各大地鐵站的 Transitlink Ticket Office 和 Passenger Service Centre 購買,
建議旅人從樟宜機場第二航廈到達 SMRT 地鐵站後,就先在服務處購買一張 EZ-Link Card,費用 SD 12 元,
其中包括可退的儲值金 SD 7 元及不可退的製卡工本費 SD 5 元。
PS. 搭乘地鐵最低餘額須大於 SD 3 元,每次加值最低須加值 SD 10 元。

SMRT 營業時間 週一至周六 首班車 05:16 末班車 23:30 /週日及例假日 首班車 05:35 末班車 23:30。 從樟宜機場站(Changi Airport)開往丹那美拉站(Tanah Merah) 週一至周六 首班車 05:31 末班車 00:06 /週日及假日 首班車 05:59 末班車 00:06 從丹那美拉站(Tanah Merah)開往樟宜機場站(Changi Airport) 週一至周六 首班車 05:20 末班車 23:50 /週日及假日 首班車 05:47 末班車 23:50 【東行】2014年新加坡自由行--新加坡EZ-LINK儲值教學、交通方式介紹 新加坡上網 --------------------------------------------------------------------- 1. SingTel 樟宜機場 or 唐城坊 新加坡旅遊超便宜預付卡手機3G/4G行動上網 Singtel hi!Tourist SIM: Packed with everything you need. hi! Tourist SIM (可以開熱點分享,也可以放到Wifi分享器供同行者使用,連線速度看別人寫的經驗大於3G) 15元:5天使用期,可享有500分鐘當地通話,100則SMS訊息,30分鐘國際電話,100GB流量,有Normal、Micro及Nano三種尺寸。可以再儲值1天5新幣延長使用天數,但是通話時間、簡訊、上網流量不會增加。
新加坡住宿 --------------------------------------------------------------------- 1. Central 65 hotel [EW12,Bugis 武吉士] 1km,12分 Address: 134 Jalan Besar Singapore 208852 Front Desk Operation: 7:00am – 11:00pm Breakfast: 7:30am – 10:30am TEL: (+65) 6298 0015 http://central65hostel.com/ 2. Hotel ibis Singapore on Bencoolen [EW12,Bugis 武吉士] 500m,7分 Address: 170 BENCOOLEN STREET 189657 - SINGAPORE TEL: (+65)65932888 http://www.ibis.com/gb/hotel-6657-ibis-singapore-on-bencoolen/index.shtml 票價優惠 - 天宇旅行社 --------------------------------------------------------------------- 唐城坊 天宇旅行社10:00~20:00 [NE4,China Town 牛車水 E出路] [新加坡] 便宜票券哪裡買??到天宇旅行社吧!! 新加坡地鐵站和聖淘沙地圖 ---------------------------------------------------------------------

Raspberry Pi - 無線基地台路由模式 hostapd + udhcpd

1. 網路架構圖
          ---------------------------------------------
         |        wlan0          |         eth0        |
       LAN PC               Raspberry Pi             Modem
  IP: 192.168.1.100    WAN eth0:  192.168.0.100   IP: 192.168.0.1
  GW: 192.168.1.1      LAN wlan0: 192.168.1.1         
2. 準備一張Edimax EW-7811Un無線網卡wlan0,當作Raspberry Pi的LAN
內建的Ethernet網卡eth0,當作Raspberry Pi的WAN
http://www.edimax.com/edimax/merchandise/merchandise_detail/data/edimax/tw/wireless_adapters_n150/ew-7811un/
3. Raspberry Pi啟動後,確認系統是否有正確抓到無線網卡
$ lsusb
Bus 001 Device 005: ID 3538:0901 Power Quotient International Co., Ltd
Bus 001 Device 004: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS]
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
4. 安裝hostapd套件,讓無線網卡模擬成Soft AP
$ sudo apt-get install hostapd
由於內建的hostapd不支援此張網卡,需要先移除hostpad程式,但保留相關的設定檔
$ sudo apt-get remove hostapd
5. Edimax EW-7811Un的驅動程式,需要從Realtek官網下載,請選擇RTL8188CUS的Linux版本
首頁 > 下載中心 > 通訊網路 ICs > Wireless LAN ICs > WLAN NIC > IEEE 802.11b/g/n Single-Chip > Software > RTL8188CUS Linux
http://www.realtek.com/downloads/downloadsView.aspx?Langid=2&PNid=21&PFid=48&Level=5&Conn=4&DownTypeID=3&GetDown=false&Downloads=true#2742

透過wget命令,下載原廠無線網卡驅動程式到Raspberry Pi
$ wget http://12244.wpc.azureedge.net/8012244/drivers/rtdrivers/cn/wlan/0001-RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip
6. 安裝hostapd程式
$ unzip 0001-RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip

$ cd RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/

$ tar -zxvf wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz

$ cd wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd

$ make clean && make && sudo make install
將hostapd相關檔案複製到/usr/bin
$ sudo cp /usr/local/bin/hostapd* /usr/sbin
7. 安裝wireless工具
$ cd ../../../wireless_tools

$ tar -zxvf wireless_tools.30.rtl.tar.gz

$ cd wireless_tools.30.rtl

$ make clean && make && sudo make install
8. 建立hostapd設定檔
driver: 無線網卡的驅動程式
ssid: 基地台名稱。
channel: 無線通訊的頻道,從1到11
wpa: 無線網路加密協定,1是WPA,2是WPA2
wpa_passphrase: 無線基地台的密碼。
$ sudo vi /etc/hostapd/hostapd.conf

interface=wlan0
driver=rtl871xdrv
bridge=br0
ssid=pi
channel=6
hw_mode=g
ieee80211n=1
wmm_enabled=1
wpa=2
wpa_passphrase=raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
auth_algs=1
macaddr_acl=0
測試hosapd設定檔
$ sudo hostapd -dd /etc/hostapd/hostapd.conf
如沒有出現錯誤,按Ctrl+C停止hostapd,編輯hostapd服務設定檔
$ sudo vi /etc/default/hostapd

# Defaults for hostapd initscript
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
#DAEMON_CONF=""
DAEMON_CONF="/etc/hostapd/hostapd.conf"

# Additional daemon options to be appended to hostapd command:-
#       -d   show more debug messages (-dd for even more)
#       -K   include key data in debug messages
#       -t   include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
#DAEMON_OPTS=""
啟動hostapd服務
$ sudo service hostapd restart
開機時自動啟動hostapd服務
$ sudo update-rc.d hostapd defaults
9. 修改無線網路卡的設定值,設定一個指定的靜態 IP 位址
$ sudo vi /etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet static
address 192.168.1.1
netmask 255.255.255.0
10. 路由模式需要在Raspberry Pi架設DHCP Server
$ sudo apt-get -y install udhcpd
將DHCPD_ENABLED="no"加上註解
$ sudo vi /etc/default/udhcpd 

# Comment the following line to enable
#DHCPD_ENABLED="no"

# Options to pass to busybox' udhcpd.
#
# -S    Log to syslog
# -f    run in foreground

DHCPD_OPTS="-S"
修改DHCP Server設定檔
$ sudo vi /etc/udhcpd.conf

# The start and end of the IP lease block

start           192.168.1.100   #default: 192.168.0.20
end             192.168.1.254   #default: 192.168.0.254

# The interface that udhcpd will use

interface       wlan0           #default: eth0


#Examles
opt     dns     8.8.8.8 192.168.1.1
option  subnet  255.255.255.0
opt     router  192.168.1.1
#opt    wins    192.168.10.10
#option dns     129.219.13.81   # appened to above DNS servers for a total of 3
#option domain  local
option  lease   864000          # 10 days of seconds
啟動DHCP Server服務
$ sudo service udhcpd restart
開機時自動啟動DHCP Server服務
$ sudo update-rc.d udhcpd defaults
11. 啟動IP Forwarding的功能,將net.ipv4.ip_forward=1拿掉註解
$ sudo vi /etc/sysctl.conf

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
12. 啟用iptables NAT功能
$ sudo iptables -F
$ sudo iptables -F -t nat
$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
$ sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
$ sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
儲存目前iptables設定值
$ sudo bash -c 'iptables-save > /etc/network/iptables'
$ sudo iptables-restore < /etc/network/iptables
設定開機時,讓iptables的規則自動被載入
$ sudo vi /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

sudo iptables-restore < /etc/network/iptables

exit 0
13. 重新啟動
$ sudo reboot
14. 參考來源
RPI-Wireless-Hotspot
Raspberry Pi 的應用 - Wi-Fi 無線基地台
Raspberry Pi 的實作 - 自動配置 IP 位址資訊的 DHCP Server
敗家學習之旅之樹莓派(Raspberry Pi) Realtek WIFI AP 架設成功心得分享

2016年4月20日 星期三

Raspberry Pi - 播放MP3音效檔

1. 在Raspberry Pi播放MP3音效檔並不難,內建就有一個音效播放程式omxplayer,首先,先取得一個mp3音效檔
$ wget https://goo.gl/XJuOUW -O example.mp3 --no-check-certificate
2. 透過headphone jack介面播放測試音效
$ omxplayer -o local example.mp3
Audio codec mp3 channels 1 samplerate 11025 bitspersample 16
Subtitle count: 0, state: off, index: 1, delay: 0
如果想要透過HDMI介面播放測試音效,可以藉由參數來設定
$ omxplayer -o hdmi example.mp3
Audio codec mp3 channels 1 samplerate 11025 bitspersample 16
Subtitle count: 0, state: off, index: 1, delay: 0
3. 參考來源
https://www.raspberrypi.org/documentation/usage/audio/

Raspberry Pi - Hello audio 音效測試

1. Raspberry Pi提供一些Demo程式,為了測試音效的部份,需要進入hello_pi目錄
$ cd /opt/vc/src/hello_pi
2. 編譯Demo程式
./rebuild.sh
3. 進入Audo demo程式的目錄
$ cd hello_audio

$ ls
audio.c  audioplay.h  hello_audio.bin  Makefile  sinewave.c
4. 透過headphone jack介面播放測試音效
$ ./hello_audio.bin
Outputting audio to analogue
如果想要透過HDMI介面播放測試音效,可以藉由參數來設定
$ ./hello_audio.bin 1
Outputting audio to hdmi
5. 參考來源
https://www.raspberrypi.org/documentation/usage/demos/README.md
https://www.raspberrypi.org/documentation/usage/demos/hello-audio.md

2016年4月14日 星期四

debugfs usage - SD card

1. To check SD speed mode by debugfs. It needs to enable the "Debug Fiflesystem" from menuconfig and then re-build kernel.
$ make linux-menuconfig

Kernel hacking  --->
  [*] Debug Filesystem
2. If the SPI mode is correct, the messages will be shown like below "5 (sd uhs DDR50)".
$ mount -t debugfs debug /mnt

$ cat /mnt/mmc0/ios
clock:          50000000 Hz
vdd:            15 (2.7 ~ 2.8 V)
bus mode:       2 (push-pull)
chip select:    0 (don't care)
power mode:     2 (on)
bus width:      2 (4 bits)
timing spec:    5 (sd uhs DDR50)

Linux tcpdump 命令

1. tcpdump參數說明
Usage: tcpdump [-aAbdDefIKlLnNOpqRStuUvxX] [ -B size ] [ -c count ]
                [ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]
                [ -i interface ] [ -M secret ] [ -r file ]
                [ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ]
                [ -y datalinktype ] [ -z command ] [ -Z user ]
                [ expression ]

-n: 不把網路位址轉換成DNS名字
-i: 監控封包的網路介面
-w: 將監控封包儲存成檔案
2. 監控網路介面 br0
$ tcpdump -n -i br0 -w a.cap &
3. 監控網路介面 eth0
$ tcpdump -i eth0 -w a.cap
4. 監控網路介面 eth0,分割每個儲存的封包(size 1500bytes)
$ tcpdump -i eth0 -s 1500 -w a.cap
5. 顯示截取內容
$ tcpdump -r a.cap

Linux VLAN 命令

1. VLAN命令參數
Usage: vconfig COMMAND [OPTIONS]

Create and remove virtual ethernet devices

        add             [interface-name] [vlan_id]
        rem             [vlan-name]
        set_flag        [interface-name] [flag-num] [0 | 1]
        set_egress_map  [vlan-name] [skb_priority] [vlan_qos]
        set_ingress_map [vlan-name] [skb_priority] [vlan_qos]
        set_name_type   [name-type]
2. Create VLAN interface
$ vconfig add eth1 100
Added VLAN with VID == 100 to IF -:eth1:-
3. Remove VLAN interface
$ vconfig rem eth1.100

公司網路設定 - 內外網域共存

1. 有兩個網域,192.168.3.0為外網和10.194.0.0為內網,為了讓兩個共存需要修改routing table. 修改routing table,將192.168.0.0的網域提高priority。
$ route change 0.0.0.0 mask 0.0.0.0 192.168.3.1 if 11 metric 10
$ route change 0.0.0.0 mask 0.0.0.0 10.194.26.1 if 12 metric 30
2. 增加一條routing rule給DNS Server到172.18.0.0網域,走10.194.26.1的介面
$ route -p add 172.18.0.0 mask 255.255.0.0 10.194.26.1
3. 將DNS的mapping直接加入table,所以不需要DNS query
C:\WINDOWS\system32\drivers\etc\host

2016年4月13日 星期三

iptables - port forwarding

PACKET IN
    |
PREROUTING--[routing]-->--FORWARD-->--POSTROUTING-->--OUT
 - nat (dst)   |           - filter      - nat (src)
               |                            |
               |                            |
              INPUT                       OUTPUT
              - filter                    - nat (dst)
               |                          - filter
               |                            |
               `----->-----[app]----->------'

      
The filter table has three built-in chains:
Forward chain: Filters packets destined for networks protected by the firewall.
Input chain: Filters packets destined for the firewall.
Output chain: Filters packets originating from the firewall.


The nat table has the following built-in chains:
Pre-routing chain: NATs packets when the destination address of the packet needs to be changed.
Post-routing chain: NATs packets when the source address of the packet needs to be changed.
Output chain: NATs packets originating from the firewall.
1. One to One port mapping
$ iptables -t nat -A PREROUTING -i eth1 -p udp --dport 1000 -j DNAT --to 192.168.1.10
$ iptables -t filter -A FORWARD -i eth1 -p udp -d 192.168.1.10 --dport 1000 -j ACCEPT

1000 -> 1000

$ iptables -t nat -A PREROUTING -i eth1 -p udp --dport 1000 -j DNAT --to 192.168.1.10:3000
$ iptables -t filter -A FORWARD -i eth1 -p udp -d 192.168.1.10 --dport 3000 -j ACCEPT

1000 -> 3000
2. Many to One port range mapping
$ iptables -t nat -A PREROUTING -i eth1 -p udp --dport 1000:2000 -j DNAT --to 192.168.1.10:3000
$ iptables -t filter -A FORWARD -i eth1 -p udp -d 192.168.1.10 --dport 3000 -j ACCEPT

1000 -> 3000
1001 -> 3000
...
1999 -> 3000
2000 -> 3000
3. One to One port range mapping
$ iptables -t nat -A PREROUTING -i eth1 -p udp --dport 1000:2000 -j DNAT --to 192.168.1.10
$ iptables -t filter -A FORWARD -i eth1 -p udp -d 192.168.1.10 --dport 1000:2000 -j ACCEPT

1000 -> 1000
1001 -> 1001
...
1999 -> 1999
2000 -> 2000
4. One to One port shift mapping
$ iptables -t nat -A PREROUTING -i eth1 -p udp --dport 1000:2000 -j DNAT --to 192.168.1.10:3000-4000
$ iptables -t filter -A FORWARD -i eth1 -p udp -d 192.168.1.10 --dport 3000:4000 -j ACCEPT

1000 -> 3000
1001 -> 3000
...
1999 -> 3000
2000 -> 3000

Linux 壓縮命令整理

1 .tar
- tar
$ tar -cvf File.tar Dir

- untar
$ tar -xvf File.tar


-c, --create            create a new archive
-x, --extract, --get    extract files from an archive
-v, --verbose           verbosely list files processed
-f, --file=ARCHIVE      use archive file or device ARCHIVE
2. .tar.gz
- tar
$ tar -zcvf File.tar.gz Dir

- untar
$ tar -zxvf File.tar.gz


-z, --gzip,             filter the archive through gzip
    --gunzip,
    --ungzip
-c, --create            create a new archive
-x, --extract, --get    extract files from an archive
-v, --verbose           verbosely list files processed
-f, --file=ARCHIVE      use archive file or device ARCHIVE
3 .tar.bz2
- tar
$ tar -jcvf File.tar.bz2 Dir

- untar
$ tar -jxvf File.tar.bz2


-j, --bzip2             filter the archive through bzip2
-c, --create            create a new archive
-x, --extract, --get    extract files from an archive
-v, --verbose           verbosely list files processed
-f, --file=ARCHIVE      use archive file or device ARCHIVE
4 .tar.xz
- tar
$ tar -Jcvf File.tar.xz Dir

- untar
$ tar -Jxvf File.tar.xz


-J, --xz                filter the archive through xz
    --lzip              filter the archive through lzip
    --lzma              filter the archive through xz
    --lzop
-c, --create            create a new archive
-x, --extract, --get    extract files from an archive
-v, --verbose           verbosely list files processed
-f, --file=ARCHIVE      use archive file or device ARCHIVE
5. 參考來源
GNU / Linux 各種壓縮與解壓縮指令

Linux kernel - printk level使用說明

1. Printk level說明
#define KERN_EMERG      "<0>"   /* system is unusable                   */
#define KERN_ALERT      "<1>"   /* action must be taken immediately     */
#define KERN_CRIT       "<2>"   /* critical conditions                  */
#define KERN_ERR        "<3>"   /* error conditions                     */
#define KERN_WARNING    "<4>"   /* warning conditions                   */
#define KERN_NOTICE     "<5>"   /* normal but significant condition     */
#define KERN_INFO       "<6>"   /* informational                        */
#define KERN_DEBUG      "<7>"   /* debug-level messages                 */
2. 修改printk level範例
$ echo 7 4 1 7 > /proc/sys/kernel/printk
3. 參數說明
3.1. console_loglevel
Messages with a higher priority than console_loglevel will be printed to the console.

3.2. default_message_level
Messages without an explicit priority will be printed with priority default_message_level.
              
3.3. Minimum_console_loglevel
minimum_console_loglevel is the minimum (highest) value to which console_loglevel can be set.

3.4. Default_console_loglevel
Default_console_loglevel is the default value for console_loglevel.
4. 參考來源
http://elinux.org/Debugging_by_printing

使用Linux script檢查coding style

1. checkpatch.pl Script位於kernel目錄下
./scripts/checkpatch.pl --help
Options:
  -q, --quiet                quiet
  --no-tree                  run without a kernel tree
  --no-signoff               do not check for 'Signed-off-by' line
  --patch                    treat FILE as patchfile (default)
  --emacs                    emacs compile window format
  --terse                    one line per report
  --showfile                 emit diffed file position, not input file position
  -f, --file                 treat FILE as regular source file
  --subjective, --strict     enable more subjective tests
  --types TYPE(,TYPE2...)    show only these comma separated message types
  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
  --max-line-length=n        set the maximum line length, if exceeded, warn
  --min-conf-desc-length=n   set the min description length, if shorter, warn
  --show-types               show the message "types" in the output
  --root=PATH                PATH to the kernel tree root
  --no-summary               suppress the per-file summary
  --mailback                 only produce a report in case of warnings/errors
  --summary-file             include the filename in summary
  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
                             'values', 'possible', 'type', and 'attr' (default
                             is all off)
  --test-only=WORD           report only warnings/errors containing WORD
                             literally
  --fix                      EXPERIMENTAL - may create horrible results
                             If correctable single-line errors exist, create
                             ".EXPERIMENTAL-checkpatch-fixes"
                             with potential errors corrected to the preferred
                             checkpatch style
  --fix-inplace              EXPERIMENTAL - may create horrible results
                             Is the same as --fix, but overwrites the input
                             file.  It's your fault if there's no backup or git
  --ignore-perl-version      override checking of perl version.  expect
                             runtime errors.
  --codespell                Use the codespell dictionary for spelling/typos
                             (default:/usr/share/codespell/dictionary.txt)
  --codespellfile            Use this codespell dictionary
  --color                    Use colors when output is STDOUT (default: on)
  -h, --help, --version      display this help and exit
2. 帶入--file參數,檢查C source
$ scripts/checkpatch.pl --file *.c
3. 檢查patch file
$ scripts/checkpatch.pl *.patch
4. 一個範例 hello.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Allen");
MODULE_DESCRIPTION("A Simple Hello World module");

static int __init hello_init(void)
{
        printk(KERN_INFO "Enter module. Hello world!\n");
        return 0;
}

static void __exit hello_exit(void)
{
        printk(KERN_INFO "Exit module.\n");
}

module_init(hello_init);
module_exit(hello_exit);
$ ./scripts/checkpatch.pl --file hello.c 
WARNING: Prefer [subsystem eg: netdev]_info([subsystem]dev, ... then dev_info(dev, ... then pr_info(...  to printk(KERN_INFO ...
#11: FILE: ../../module/hello.c:11:
+ printk(KERN_INFO "Enter module. Hello world!\n");

WARNING: Prefer [subsystem eg: netdev]_info([subsystem]dev, ... then dev_info(dev, ... then pr_info(...  to printk(KERN_INFO ...
#17: FILE: ../../module/hello.c:17:
+ printk(KERN_INFO "Exit module.\n");

total: 0 errors, 2 warnings, 21 lines checked

../../module/hello.c has style problems, please review.

If any of these errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
5. 參考來源
https://github.com/torvalds/linux/blob/master/scripts/checkpatch.pl

SHELL Script - 一個while loop的範例

1. Shell Script中,使用while loop語法
#!/bin/sh
i=1
while [ "$i" != "10" ]
do
    echo "loop" $i
    i=$(($i+1))
done

Result:
loop 1
loop 2
loop 3
loop 4
loop 5
loop 6
loop 7
loop 8
loop 9
2. 利用Shell Script產生多個VLAN interface
#!/bin/sh
i=1
while [ "$i" != "10" ]
do
    s=$(($i+1))
    vconfig add eth0 $i
    vconfig add eth1 $i
    ifconfig eth0.$i up
    ifconfig eth1.$i up
    brctl addbr br$i
    brctl addif br$i eth0.$i
    brctl addif br$i eth1.$i
    ifconfig br$i 192.168.$s.1 up
done

iperf 測試IPv6網路環境

1. Network topology
          ---------------------------------------------
         |        eth0          |         eth1        |
       LAN PC                 Router                WAN PC
  ip: 192.168.1.100    eth0: 192.168.1.1       ip: 10.1.1.100    IPv4
  gw: 192.168.1.1      eth1: 10.1.1.1          gw: 10.1.1.1 

  ip: 2001::100/64     eth0: 2001::10          ip: 2002::100     IPv6
  gw: 2002::/64        eth1: 2002::10          gw: 2001::/64
2. Set IPv6 address and default route

LAN PC:
$ ifconfig eth0 192.168.1.100
$ route add -net 10.1.0.0 gw 192.168.1.1
$ route add default gw 192.168.1.1

$ ip -6 addr add 2001::100/64 dev eth0
$ ip -6 route add 2002::/64 via 2001::10
DUT:
$ ifconfig eth0 192.168.1.1
$ ifconfig eth1 10.1.1.1
$ roue add default gw 10.1.1.100

$ ip -6 addr add 2001::10/64 dev eth0
$ ip -6 addr add 2002::10/64 dev eth1
WAN PC:
$ ifconfig eth0 10.1.1.100
$ route add -net 192.168.1.0 gw 10.1.1.1
$ route add default gw 10.1.1.1

$ ip -6 addr add 2002::100/64 dev eth1
$ ip -6 route add 2001::/64 via 2002::10
3. Enable IPv4 & IPv6 forwarding
$ echo 1 > /proc/sys/net/ipv4/ip_forward
$ echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
4. IPv6 command
關閉IPv6: echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
開啟forward功能: echo 0 > /proc/sys/net/ipv6/disable_forward

新增網卡IP: ip -6 addr add 2001::1234 dev eth0
新增網卡IP(2): ifconfig eth0 inet6 add 2001::1234/64
清除網卡IP: ip -6 addr flush dev eth0

新增路由: ip -6 route add 2345::/64 via fe80::1234 dev eth0
刪除路由: ip -6 route del 2345::/64 via fe80::1234 dev eth0
新增預設路由: ip -6 route add default via fe80::1234 dev eth0 
清除所有路由: ip -6 route flush dev eth0

iperf 測試IPv4網路環境

1. Network topology
          ---------------------------------------------
         |        eth0          |         eth1        |
       LAN PC                 Router                WAN PC
  ip: 192.168.1.100    eth0: 192.168.1.1       ip: 10.1.1.100
  gw: 192.168.1.1      eth1: 10.1.1.1          gw: 10.1.1.1
                       gw  : 10.1.1.100
2. Set IPv4 address and default route

LAN PC:
$ ifconfig eth0 192.168.1.100
$ route add -net 10.1.0.0 gw 192.168.1.1
$ route add default gw 192.168.1.1
DUT:
$ ifconfig eth0 192.168.1.1
$ ifconfig eth1 10.1.1.1
$ roue add default gw 10.1.1.100
WAN PC:
$ ifconfig eth0 10.1.1.100
$ route add -net 192.168.1.0 gw 10.1.1.1
$ route add default gw 10.1.1.1
3. Enable IPv4 forwarding
$ echo 1 > /proc/sys/net/ipv4/ip_forward
4. IPv4 command
啟動網卡: ifconfig eth0 up
啟動網卡(2): ifup eth0
關閉網卡: ifconfig eth0 down
關閉網卡(2): ifdown eth0

設定MTU: ifconfig eth0 1500
開啟forward功能: echo 1 > /proc/sys/net/ipv4/ip_forward

設定網卡IP: ifconfig eth0 192.168.0.1/24
增加網卡IP: ip -4 addr add 192.168.0.2/24 dev eth0
刪除網卡IP: ip -4 addr del 192.168.0.2/24 dev eth0
清除網卡IP: ip -4 addr flush dev eth0

透過DHCP取得IP: dhclient eth0
釋放DHCP IP: dhclient -r eth0

新增路由: route add -net 192.168.0.0/24 gw 192.168.0.1 dev eth0
刪除路由: route del -net 192.168.0.0/24 gw 192.168.0.1 dev eth0
新增預設路由: route add default gw 192.168.0.1 dev eth0

Raspberry Pi - 建立一個網路橋接器 Bridge

1. 網路架構圖
  Bridge
        br0             virtual adapter
         |
   --------------       bridge
   |            |
  eth0         eth1     physical adapters
2. 將網路介面啟動
$ ifconfig eth0 0.0.0.0 up
$ ifconfig eth1 0.0.0.0 up
3. 新增一個bridge介面,並把實體介面加入
$ brctl addbr br0
$ brctl addif br0 eth0
$ brctl addif br0 eth1
4. 設定bridge介面的IP address
$ ifconfig br0 192.168.1.1

線上程式編譯器 tutorialspoint

1. 進入tutorialspoint網站,點選C Compiler
http://www.tutorialspoint.com/codingground.htm
2. 點選Compiler編譯原始碼
3. 執行程式

2016年4月12日 星期二

WPS PIN Code的規則

1. WPS PIN Code checksum運算
For 8-digit numeric PINs, the last digit in the PIN is used as a checksum of the other digits. 

Ex:
The 7-digit numeric PINs is "1234567".
The 8th digit should be "0".

 ( 1*3 + 2*1 + 3*3 + 4*1 + 5*3 + 6*1 + 7*3 ) = 60
 60 % 10 = 0
 10 - 0 = 10
 10 % 10 = 0
 Get the 8th digit is "0".
 So the 8-digit numberic PINs is "12345670".
 
If the 7-digit numberic PINs is "5967201", 
the 8th digit should be "2".
 ( 5*3 + 9*1 + 6*3 + 7*1 + 2*3 + 0*1 + 1*3) = 58
 58 % 10 = 8
 10 - 8 = 2
 2 % 10 = 2
 Get the 8th digit is "2".
 So the 8-digit numberic PINs is "59672012".
The corresponding algorithm to compute the checksum digit given the other seven random PIN digits is:
/**
 * wps_pin_checksum - Compute PIN checksum
 * @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit)
 * Returns: Checksum digit
 */
unsigned int wps_pin_checksum(unsigned int pin)
{
 unsigned int accum = 0;
 while (pin) {
  accum += 3 * (pin % 10);
  pin /= 10;
  accum += pin % 10;
  pin /= 10;
 }

 return (10 - accum % 10) % 10;
}
2. 驗證WPS PIN Code是否合法 The algorithm to validate the checksum is given in C code below.
/**
 * wps_pin_valid - Check whether a PIN has a valid checksum
 * @pin: Eight digit PIN (i.e., including the checksum digit)
 * Returns: 1 if checksum digit is valid, or 0 if not
 */
unsigned int wps_pin_valid(unsigned int pin)
{
 return wps_pin_checksum(pin / 10) == (pin % 10);
}
3. 參考來源
http://ftp.netbsd.org/pub/NetBSD/NetBSD-release-6/src/external/bsd/wpa/dist/src/wps/wps_common.c

無線網路 802.11n 20MHz 和 40MHz 的 Data Rate

2016年4月10日 星期日

信用卡紅利點數換取eTag回饋金

現在生活這麼難過,除了能省則省外,也要懂得想些方法賺現金,其中一個方法就是刷信用卡消費,利用紅利點數換取eTag回饋金,以下以國泰世華信用卡為範例。

1. 紅利點數說明
刷卡享2倍紅利或現金回饋
卡友每刷卡 20 元就可累積紅利點數 2 點或 0.5% 現金回饋 ( 二擇一 ) ,點數期限三年有效。

2. 紅利點數兑換項目
刷卡NT$20累積紅利1點,每1,000點紅利可兌換60元eTag回饋金。
3. 回饋利率說明
國泰世華網頁說明,1000點可以換取60元eTag回饋金
信用卡的紅利點數說明,20元可以累積紅利點數2點,1000點則需要消費10000元。 利率計算 60元 / 10000元 = 0.006,相當於0.6%。

Google AdSense 申請成功

2016/04/02:
2016/04/03:

2016年4月8日 星期五

Git GUI 工具整理

1. Linux base git tools
Git-cola
SmartGit
Git Gui

2. Linux base git viewer
Gitg
Giggle
qGit
Gitk

3. Windows base tools
TortoiseGit
Git Hub Desktop

Linux Git 圖形界面 - Qgit

1. 在putty,enable X11 fordwarding
2. 透過putty登入伺服器主機

3. 安裝qgit
$ sudo apt-get install qgit
4. 一個lmbench的範例,從github下載lmbench,在lmbench的git repository資料夾裡,啟動qgit
$ qgit
5. 參考來源
http://digilander.libero.it/mcostalba/
https://www.maketecheasier.com/6-useful-graphical-git-client-for-linux/

Linux Git 圖形界面 - Giggle

1. 在putty,enable X11 fordwarding
2. 透過putty登入伺服器主機

3. 安裝giggle
$ sudo apt-get install giggle
4. 一個lmbench的範例,從github下載lmbench,在lmbench的git repository資料夾裡,啟動giggle
$ giggle
5. 參考來源
https://wiki.gnome.org/Apps/giggle/
https://www.maketecheasier.com/6-useful-graphical-git-client-for-linux/

Linux Git 圖形界面 - Gitg

1. 在putty,enable X11 fordwarding
2. 透過putty登入伺服器主機

3. 安裝gitg
$ sudo apt-get install gitg
4. 一個lmbench的範例,從github下載lmbench,在lmbench的git repository資料夾裡,啟動gitg
$ gitg
5. 參考來源
https://wiki.gnome.org/Apps/Gitg/
https://www.maketecheasier.com/6-useful-graphical-git-client-for-linux/

2016年4月7日 星期四

Linux Git 圖形界面 - Gitk

1. 在putty,enable X11 fordwarding
2. 透過putty登入伺服器主機

3. 一個lmbench的範例,從github下載lmbench,在lmbench的git repository資料夾裡,啟動gitk
$ gitk --all
4. 參考來源
https://git-scm.com/docs/gitk

Raspberry Pi - 建立一個網路路由器 Router

1. 網路架構圖
          ---------------------------------------------
         |        eth0          |         eth1        |
       LAN PC                 Router                WAN PC
  ip: 192.168.1.100    eth0: 192.168.1.1       ip: 10.1.1.100
  gw: 192.168.1.1      eth1: 10.1.1.1          gw: 10.1.1.1
2. 設定網路介面的IP address,eth0為LAN interface,eth1為WAN interface
$ ifconfig eth0 192.168.1.1
$ ifconfig eth1 up
3. 在WAN interface eth1啟動dhcp client去跟外面的dhcp server要IP
$ udhcpc -i eth1
4. 開啟IPv4 forwrding功能
$ sudo bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
5. 開啟iptables NAT功能
$ iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
6. 設定default gateway
$ route add default gw 10.1.1.1 
7. 在LAN interface eth0啟動dhcp server,設定dhcp server的configuration file
example 1:
echo "start     192.168.1.20" > /etc/udhcpd.conf
echo "end       192.168.1.254" >> /etc/udhcpd.conf
echo "interface br0" >> /etc/udhcpd.conf

echo "opt       dns     192.168.1.1 192.168.1.1" >> /etc/udhcpd.conf
echo "option    subnet  255.255.255.0" >> /etc/udhcpd.conf
echo "opt       router  192.168.1.1" >> /etc/udhcpd.conf
echo "opt       wins    192.168.1.1" >> /etc/udhcpd.conf
echo "option    dns     192.168.1.1" >> /etc/udhcpd.conf
echo "option    domain  local" >> /etc/udhcpd.conf
echo "option    lease   864000" >> /etc/udhcpd.conf

echo "static_lease 00:24:BE:7E:AE:C1 192.168.1.100" >> /etc/udhcpd.conf
example 2:
echo "start     192.168.1.20" > /etc/udhcpd.conf
echo "end       192.168.1.254" >> /etc/udhcpd.conf
echo "interface eth0" >> /etc/udhcpd.conf

echo "opt       dns     192.168.1.1 192.168.1.1" >> /etc/udhcpd.conf
echo "option    subnet  255.255.255.0" >> /etc/udhcpd.conf
echo "opt       router  192.168.1.1" >> /etc/udhcpd.conf
echo "opt       wins    192.168.1.1" >> /etc/udhcpd.conf
echo "option    dns     192.168.1.1" >> /etc/udhcpd.conf
echo "option    domain  local" >> /etc/udhcpd.conf
echo "option    lease   864000" >> /etc/udhcpd.conf

echo "static_lease 00:24:BE:7E:AE:C1 192.168.1.100" >> /etc/udhcpd.conf
8. 在LAN interface eth0啟動dhcp server,以供LAN side的主機取得IP
$ mkdir /var/lib/misc/
$ touch /var/lib/misc/udhcpd.leases
$ udhcpd
9. 啟動DNS MASQUERATE
$ dnsmasq

putty 避免SSH連線中斷 keepalives

Connection->Seconds between keepalives

Kernel trace

include/linux/etherdevice.h
include/linux/netdevice.h

// ethernet header
include/linux/if_ether.h
// pppoe header    
include/linux/if_pppox.h
include/linux/if_vlan.h
// IPv4 header
include/linux/ip.h
// IPv6 header
include/linux/ipv6.h
// TCP header
include/linux/tcp.h
// UDP header
include/linux/udp.h

// SK buffer
include/linux/skbuff.h

include/linux/netfilter_bridge/ebt_pppoe.h

include/net/sock.h



net/core/dev.c
net/ethernet/eth.c
net/core/skbuff.c

// bridge
net/bridge/br_forward.c

net/bridge/netfilter/ebt_pppoed.c
net/bridge/netfilter/ebt_pppoes.c

// VLAN packet
net/8021q/vlan_dev.c

// Arp packet
net/ipv4/arp.c

// ICMP packet
net/ipv4/icmp.c

SHELL Script加入文字樣式, 文字顏色, 背景顏色

以shell 的顯示為例,顯示的格式如下:
\33[ 文字樣式 ; 文字顏色 ; 背景顏色 m
文字內容………………
\33[0m
1. 文字樣式,有列下幾種色碼可以使用:
0 一般亮度
1 高度度
4 加底線
5 灰底

2. 文字顏色,則都是3開頭,有下列色碼:
30 黑色
31 紅色
32 綠色
33 黃色
34 藍色
35 紫色
36 青綠
37 白色

3. 背景顏色,色碼都是4開頭:
40 黑色
41 紅色
42 綠色
43 黃色
44 藍色
45 紫色
46 青綠
47 白色
4.範例
如果我要在畫面上顯示高亮度的綠色(32)及黑色背景,可以如下:
echo -e "\e[1;32;40m 我的文字 \e[0m"
也可以分開來寫比較清楚
echo -e "\e[1;32;40m "  
echo -e "我的文字"  
echo -e "\e[0m"

一個應用程式Makefile範例

cc = gcc
strip = strip
CFLAGS = -Wall -Wextra -static
APPLET = myapp

all: $(TARGET)

$(TARGET): main.o
        @echo "\033[32mBuilding $(TARGET) ...\033[0m"
        @$(CC) $(CFLAGS) -o $@ $^
        @(STRIP) $(TARGET)
        @./$(TARGET)

install:
        @echo "\033[32mInstalling $(TARGET) ...\033[0m"

clean:
        @echo "\033[32mCleaning $(TARGET) ...\033[0m"
        @rm -f $(TARGET) *.o

.PHONY: all install clean

Kernel module和Makefile範例

1. 準備hello.c和Makefile
$ vi hello.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Allen");
MODULE_DESCRIPTION("A Simple Hello World module");

static int __init hello_init(void)
{
    printk(KERN_INFO "Enter module. Hello world!\n");
    return 0;
}

static void __exit hello_exit(void)
{
    printk(KERN_INFO "Exit module.\n");
}

module_init(hello_init);
module_exit(hello_exit);
$ vi Makefile

obj-m += hello.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
2. 編譯Hello world的kernel module
$ make
make -C /lib/modules/3.11.0-15-generic/build M=/home/allen/Work/module modules
make[1]: Entering directory `/usr/src/linux-headers-3.11.0-15-generic'
  CC [M]  /home/allen/Work/module/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/allen/Work/module/hello.mod.o
  LD [M]  /home/allen/Work/module/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.11.0-15-generic'


$ ls
hello.c   hello.mod.c  hello.o   modules.order
hello.ko  hello.mod.o  Makefile  Module.symvers
3. 掛載Hello world的kernel module
$ sudo insmod hello.ko

$ dmesg | tail -1
[18253.619885] Enter module. Hello world!
4. 卸載Hello world的kernel module
$ sudo rmmod hello

$ dmesg | tail -1
[18291.787154] Exit module.
5. 列出已掛載kernel module
$ lsmod | grep hello
hello                  12425  0
6. 顯示module的資訊
$ modinfo hello.ko
filename:       hello.ko
description:    A Simple Hello World module
author:         Allen
license:        GPL
srcversion:     4022E1AB22D5C59AACC7122
depends:
vermagic:       3.11.0-15-generic SMP mod_unload modversions
7. 參考來源
How to Write Your Own Linux Kernel Module with a Simple Example

2016年4月6日 星期三

英文標點符號的使用

句點(Full Stop / Period, “.”) 

問號(Question Mark, “?”) 

感嘆號(Exclamation Mark, “!”) 

逗點(Comma, “,”) 

冒號(Colon, “:”) 

分號(Semicolon, “;”) 

連字元(Hyphen, “-”) 

連接號(En Dash, “–”) 

破折號(Em Dash, “—”) 

括弧(Parentheses, 小括弧“( )”; 中括弧“[]”; 大括弧“{}”) 

引號(Quotation Marks, 雙引號“"”; 單引號“‘”) 

縮寫及所有格符號(Apostrophe, “‘”)
參考來源
http://tw.blog.voicetube.com/2014/11/21/%E3%80%90%E5%AF%A6%E7%94%A8%E3%80%91%E4%BD%A0%E7%9F%A5%E9%81%93%E8%8B%B1%E6%96%87%E7%9A%84%E6%A8%99%E9%BB%9E%E7%AC%A6%E8%99%9F%E6%80%8E%E9%BA%BC%E4%BD%BF%E7%94%A8%E5%97%8E%EF%BC%9F%E6%96%87%E7%AB%A0

https://www.google.com.tw/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0ahUKEwiwiemKqPnLAhXKoZQKHaGXD1oQFgghMAE&url=http%3A%2F%2Flms.ctl.cyut.edu.tw%2Fblog%2Flib%2Fread_attach.php%3Fid%3D1859&usg=AFQjCNF9OVsPHsfW8urKmIYZnwNs8eY6Wg&sig2=EHhOl5bTtb7D4ODnWb6FbA

WebEx錄像軟體用法

1. 下載WebEx
http://www.webex.com.hk/support/play-webex-recording.html

2. 安裝WebEx
3. 開啟WebEx,開始錄像
4. 參考來源
http://www.webex.com.hk/support/play-webex-recording.html

2016年4月5日 星期二

iptables port forwarding範例

1. 網路架構圖
# Port forward
        192.168.1.1    20.1.1.10
LAN PC ------------ DUT ------------- WAN PC
192.168.1.100:5555                   20.1.1.100
2. 各主機所下達的命令 LAN PC:
$ iperf -s -u -p 5555
WAN PC:
$ iperf -c 20.1.1.10 -t 600 -i 5 -u -l 1400 -p 4444
DUT:
$ iptables -t nat -A PREROUTING -d 20.1.1.10 -p udp -i ppp0 --dport 4444 -j DNAT --to-destination 192.168.1.100:5555

iperf使用範例

iperf是用來產生網路流量,可以用來測試網路設備,
# iperf -h
Usage: iperf [-s|-c host] [options]
       iperf [-h|--help] [-v|--version]

Client/Server:
  -f, --format    [kmKM]   format to report: Kbits, Mbits, KBytes, MBytes
  -i, --interval  #        seconds between periodic bandwidth reports
  -l, --len       #[KM]    length of buffer to read or write (default 8 KB)
  -m, --print_mss          print TCP maximum segment size (MTU - TCP/IP header)
  -o, --output     output the report or error message to this specified file
  -p, --port      #        server port to listen on/connect to
  -u, --udp                use UDP rather than TCP
  -w, --window    #[KM]    TCP window size (socket buffer size)
  -B, --bind         bind to , an interface or multicast address
  -C, --compatibility      for use with older versions does not sent extra msgs
  -M, --mss       #        set TCP maximum segment size (MTU - 40 bytes)
  -N, --nodelay            set TCP no delay, disabling Nagle's Algorithm
  -V, --IPv6Version        Set the domain to IPv6

Server specific:
  -s, --server             run in server mode
  -U, --single_udp         run in single threaded UDP mode
  -D, --daemon             run the server as a daemon

Client specific:
  -b, --bandwidth #[KM]    for UDP, bandwidth to send at in bits/sec
                           (default 1 Mbit/sec, implies -u)
  -c, --client       run in client mode, connecting to 
  -d, --dualtest           Do a bidirectional test simultaneously
  -n, --num       #[KM]    number of bytes to transmit (instead of -t)
  -r, --tradeoff           Do a bidirectional test individually
  -t, --time      #        time in seconds to transmit for (default 10 secs)
  -F, --fileinput    input the data to be transmitted from a file
  -I, --stdin              input the data to be transmitted from stdin
  -L, --listenport #       port to receive bidirectional tests back on
  -P, --parallel  #        number of parallel client threads to run
  -T, --ttl       #        time-to-live, for multicast (default 1)
  -Z, --linux-congestion   set TCP congestion control algorithm (Linux only)

Miscellaneous:
  -x, --reportexclude [CDMSV]   exclude C(connection) D(data) M(multicast) S(settings) V(server) reports
  -y, --reportstyle C      report as a Comma-Separated Values
  -h, --help               print this message and quit
  -v, --version            print version information and quit


產生IPv6的封包: -V, --IPv6Version Set the domain to IPv6
Server:
$ iperf -V -s -i 5

Client
$ iperf -V -c 2001:2222::2 -t 172800 -i 5 -u


產生TCP的封包: 預設為TCP封包
Server
$ iperf -s -i 5

Client
$ iperf -c 192.168.1.100 -t 172800 -i 5


產生UDP的封包: -u, --udp use UDP rather than TCP
Server
$ iperf -s -i 5 -u

Client
$ iperf -c 192.168.1.100 -t 172800 -i 5 -u


產生指定Port number的封包: -p, --port # server port to listen on/connect to
Server
$ iperf -s -i 5 -p 100 -u

Client
$ iperf -c 192.168.1.100 -t 172800 -i 5 -p 100 -u


產生指定Bandwidth的流量: -b, --bandwidth #[KM] for UDP, bandwidth to send at in bits/sec
Server
$ iperf -s -i 5 -u

Client
$ iperf -c 192.168.1.100 -t 172800 -i 5 -b 10M -u


產生DSCP的封包: 7 (28/ 4)的封包
Server
$ iperf -s -i 5 -u

Client
$ iperf -c 192.168.1.100 -t 172800 -i 5 -S 28 -u


產生指定Payload size的封包: 1200 bytes的封包
Server
$ iperf -s -i 5 -u

Client
$ iperf -c 20.1.1.100 -t 20 -i 5 -u -l 1200
參考來源
https://iperf.fr/
https://iperf.fr/iperf-doc.php

Git 命令整理

git add 新增檔案
git add . 新增目錄下所有檔案
git commit 提交檔案
git commit -m 'message' 直接提交,不進入編輯器
git commit 提交檔案,進入編輯器
git commit --amend 編輯已提交的註釋
git reset 回復檔案
git reset 放棄add,保留修改
git reset --hard 放棄所有修改,回到上個提交的狀態
git reset --hard HEAD 回到最新提交的版本
git reset --hard HEAD^ 回到前一個提交的版本
git branch 分支
git branch 列出所有branch
git branch -a 列出所有本地和遠端的branch
git branch <new-branch> 依目前branch,建立新的branch
git checkout 切換
git checkout <branch> 切換branch
git checkout master 切換到master
git checkout <new-branch> master 從master建立新的branch,並切換到新的branch
git checkout -b <new-branch> 依目前branch建立新的branch,並切換到新的branch
git checkout <file> 重新checkout此檔案
git log
git log 列出所有log
git pull
git pull 將遠端的檔案更新到本地
git push
git push 將本地檔案更新到遠端
參考來源
https://www.atlassian.com/git/tutorials/setting-up-a-repository
https://git-scm.com/doc
https://backlogtool.com/git-guide/tw/

GCC使用方法

1. Compiler a source file
$ gcc -o hello hello.c
2. Compiler many source files
$ gcc -o hello hello.c func.c
3. Compiler source file with debug flag
$ gcc -g -o hello hello.c
4. 編譯動態連結
$ arm-linux-gcc -Wall -c -o hrt_timer.o hrt_timer.c
$ arm-linux-gcc -Wall -o hrt_timer hrt_timer.o -lrt
5. 編譯靜態連結
$ arm-linux-gcc -Wall -c -o hrt_timer.o hrt_timer.c
$ arm-linux-gcc -Wall -static -o hrt_timer hrt_timer.o -lrt
6. preprofessor
$ gcc -E

Linux find 命令

1. find,配合萬用元字找尋檔案
$ find -name *.c -o -name *.h
2.1. find,配合萬用元字找尋檔案
2.2. xargs,將上一個程式輸出的結果轉成下一個程式的參數輸入
2.3. grep,找尋包含DEBUG的字串
$ find . -name "*.h" | xargs grep -r DEBUG
3.1. find,配合萬用元字找尋檔案
3.2. -exec,是表示找到檔案後要執行的指令
3.3. rm -rf,強制刪除檔案
3.4. {},代表指令接檔案名稱的地方,find會將找到檔案的檔名與路徑帶入到{}
3.5. \;,代表指令結束,不可以省略
$ find . -name .svn -exec rm -rf {} \;
4.1. find,配合萬用元字找尋檔案
4.2. xargs,將上一個程式輸出的結果轉成下一個程式的參數輸入
4.3. rm -rf,強制刪除檔案
$ find . -name .svn | xargs rm -rf

Linux diff 和 patch 命令

1. Create a patch
$ diff -urN a/trunk b/trunk > c.patch

-a --text Treat all files as text.
-u -U NUM --unified[=NUM] Output NUM (default 3) lines of unified context.
-r --recursive Recursively compare any subdirectories found.
-N --new-file Treat absent files as empty.
2. Apply patch
$ patch -p1 < c.patch

-p0,Do not get rid of any level
-p1,Get rid of first level
-p2,Get rid of secondary level
-p NUM --strip=NUM Strip NUM leading components from file names.
-R --reverse Assume patches were created with old and new files swapped.
3. Restore the patch file
$ patch -R -p1 < c.patch

ethtool使用方法

1. Printf the offload setting
$ ethtool -k eth0

Offload parameters for eth0: 
rx-checksumming: on 
tx-checksumming: on 
scatter-gather: on 
tcp-segmentation-offload: on 
udp-fragmentation-offload: off 
generic-segmentation-offload: on 
generic-receive-offload: on 
large-receive-offload: off
這些offload特性都是為了提升網路收/發效能。TSO、UFO和GSO是對應網路發送,在接收方向上對應的是LRO、GRO。

2. TSO (TCP Segmentation Offload)
$ ethtool -K eth0 tso on
$ ethtool -K eth0 tso off
TSO是一種利用網卡對TCP封包分裝,減輕CPU負荷的一種技術,有時也被叫做LSO (Large Segment Offload),TSO是針對TCP的,UFO是針對UDP的。如果硬體支援TSO功能,同時也需要硬體支援的TCP checksum計算和分散/聚集(Scatter Gather)功能。

3. UFO (UDP Fragmentation Offload)
$ ethtool -K eth0 ufo on
$ ethtool -K eth0 ufo off
4. GSO (Generic Segmentation Offload)
$ ethtool -K eth0 gso on
$ ethtool -K eth0 gso off
GSO比TSO更通用,基本思想就是盡可能的將大封包分裝直至發送到網卡驅動之前,此時會檢查網卡是否支援分裝功能(如TSO、UFO),如果支援直接發送到網卡,如果不支援就進行分片後再發往網卡。這樣大封包只需走一次protocol stack,而不是被分割成幾個小封包分別走,這就提高了效率。

5. LRO (Large Receive Offload)
$ ethtool -K eth0 lro on
$ ethtool -K eth0 lro off
LRO通過將接收到的多個TCP封包組合成一個大的封包,然後傳遞給protocol stack處理,以減少上層protocol stack的處理,提高系統接收TCP封包的能力。

6. GRO (Generic Receive Offload)
$ ethtool -K eth0 gro on
$ ethtool -K eth0 gro off
GRO基本思想跟LRO類似,克服了LRO的一些缺點,更通用。後續的驅動都使用GRO的接口,而不是LRO。

7. RSS (Receive Side Scaling)
RSS是一項網卡的新特性,俗稱多隊列。具備多個RSS隊列的網卡,可以將不同的網絡流分成不同的隊列,再分別將這些隊列分配到多個CPU核心上進行處理,從而將負荷分散,充分利用多核處理器的能力。

網路封包長度整理

- Ethernet Packet
Ethernet Header = ( 6 SRCMAC + 6 DSTMAC + 2 TYPE ) = 14 B
Ethernet Packet MinSize = ( 6 SRCMAC + 6 DSTMAC + 2 TYPE + 4 CRC + 46 PAYLOAD ) = 64 B
Ethernet Packet MaxSize = ( 6 SRCMAC + 6 DSTMAC + 2 TYPE + 4 CRC + 1500 PAYLOAD ) = 1518 B

- TCP Packet
Ethernet Header = 14 B
IP Header = 20 B
TCP Header MinSize = 20
TCP Header MaxSize = (20 TCP_HEADER + 40 OPTION) = 60 B

TCP Packet MinSize = ( 14 ETHERNET_HEADER + 20 IP_HEADER + 20 TCP_HEADER ) = 54 B
TCP Packet MaxSize = ( 14 ETHERNET_HEADER + 20 IP_HEADER + 60 TCP_HEADERP ) = 94 B

- UDP Packet
Ethernet Header = 14 B
IP Header = 20 B
UDP Header = 8 B

UDP Packet = ( 14 ETHERNET_HEADER + 20 IP_HEADER + 8 UDP_HEADER ) = 42 B

- PPPoE packet PPP Header = 2 B PPPoE Session = 6 B TCP over PPPoE MinSize = ( 14 ETHERNET_HEADER + 6 PPPoE_SESSION + 2 PPP_HEADER + 20 IP_HEADER + 20 TCP_HEADER ) = 62 B TCP over PPPoE MaxSize = ( 14 ETHERNET_HEADER + 6 PPPoE_SESSION + 2 PPP_HEADER + 20 IP_HEADER + 60 TCP_HEADER ) = 102 B UDP over PPPoE = ( 14 ETHERNET_HEADER + 6 PPPoE_SESSION + 2 PPP_HEADER + 20 IP_HEADER + 8 UDP_HEADER ) = 50 B Ethernet IP MTU = 1518 - 18 ( 6 SRCMAC+ 6 DSTMAC+ 2 TYPE+ 4 CRC) = 1500 B Ethernet IP TCP MSS = 1500 - 40 ( 20 IP_HEADER + 20 TCP_HEADER) = 1460 B Ethernet IP UDP MTU/MRU = 1500 - 28 ( 20 IP_HEADER + 8 UDP_HEADER ) = 1472 B PPPoE MTU/MRU = 1500 - 8 ( 6 PPPoE_SESSION + 2 PPP_HEADER ) = 1492 B TCP over PPPoE MSS = 1492 ( PPPoE MTU/MRU ) - 40 ( 20 IP_HEADER + 20 TCP_HEADER) = 1452 UDP over PPPoE MTU/MRU = 1492 ( PPPoE MTU/MRU ) - 28 ( 20 IP_HEADER + 8 UDP_HEADER ) = 1464 PPTP MTU/MRU = 1500 - 56 ( 20 IP_HEADER + 20 TCP_HEADER + 12 GRE_HEADER + 4 PPP_HEADER ) = 1444 B TCP over PPTP MSS = 1444 ( PPTP MTU/MRU ) - 40 ( 20 IP_HEADER + 20 TCP_HEADER) = 1404 L2TP MTU/MRU = 1500 - 40 ( 20 IP_HEADER +8 UDP_HEADER + 8 L2TP_HEADER + 4 PPP_HEADER ) = 1460 B TCP over L2TP MSS = 1460 ( L2TP MTU/MRU ) - 40 ( 20 IP_HEADER + 20 TCP_HEADER) = 1420 B PPTP over PPPoE MTU/MRU = 1492 ( PPPoE MTU/MRU ) - 56 ( 20 IP_HEADER + 20 TCP_HEADER + 12 GRE_HEADER + 4 PPP_HEADER ) = 1436 B PPTP over PPTP MTU/MRU = 1444 ( PPTP MTU/MRU ) - 56 ( 20 IP_HEADER + 20 TCP_HEADER + 12 GRE_HEADER + 4 PPP_HEADER ) = 1388 B PPTP over L2TP MTU/MRU = 1460 ( L2TP MTU/MRU ) - 56 ( 20 IP_HEADER + 20 TCP_HEADER + 12 GRE_HEADER + 4 PPP_HEADER ) = 1404 B L2TP over PPPoE MTU/MRU = 1492 ( PPPoE MTU/MRU ) - 40 ( 20 IP_HEADER +8 UDP_HEADER + 8 L2TP_HEADER + 4 PPP_HEADER ) = 1452 B L2TP over PPTP MTU/MRU = 1444 ( PPTP MTU/MRU ) - 40 ( 20 IP_HEADER +8 UDP_HEADER + 8 L2TP_HEADER + 4 PPP_HEADER ) = 1404 B L2TP over L2TP MTU/MRU = 1460 ( L2TP MTU/MRU ) - 40 ( 20 IP_HEADER +8 UDP_HEADER + 8 L2TP_HEADER + 4 PPP_HEADER ) = 1420 B

在Windows透過SSH掛載Linux上的硬碟 SFTP Net Drive

1. 下載最新版本SFTP Net Drive
https://www.eldos.com/sftp-net-drive/

2. 安裝SFTP Net Drive
3. 執行SFTP Net Drive,建立一個新的profile,並鍵入帳號密碼,然後按下連線
4. 參考來源
https://www.eldos.com/sftp-net-drive/