ebtables的使用

來源:互聯網
上載者:User

http://blog.163.com/dyc_888@126/blog/static/10044335120111255249503/

http://blog.chinaunix.net/uid-10694051-id-2935976.html

一:ebtables簡介
ebtables和iptables類似,都是Linux系統下網路資料包過濾的組態工具。既然稱之為組態工具,就是說過濾功能是由核心底層提供支援的,這兩個工具只是負責制定過濾的rules.

ebtables即是乙太網路橋防火牆,乙太網路橋工作在資料連結層,ebtables來過濾資料連結層資料包。 2.6核心內建了ebtables,要使用它必須先安裝ebtables的使用者空間工具(ebtables-v2.0.8-2),安裝完成後就可以使用ebtables來過濾橋接器的資料包。

二、ebtables配置
ebtables的配置分為表、鏈和規則三級。
1. 表
表是內建且固定的,共有三種: filter, nat, broute,用-t選項指定。最常用的就是filter了,所以不設-t時預設就是這個表。nat用於地址轉換,broute用於乙太網路橋。
2.  鏈
鏈有內建和自訂兩種 。不同的表內建的鏈不同,這個從資料包的流程圖中就可以看出來。所謂自訂的鏈也是掛接在對應的內建鏈內的,使用-j讓其跳轉到新的鏈中。
3. 規則
每個鏈中有一系列規則,每個規則定義了一些過濾選項。每個資料包都會匹配這些項,一但匹配成功就會執行對應的動作。

所謂動作,就是過濾的行為了。有四種,ACCEPT,DROP,RETURN和CONTINUE。常用的就是ACCEPT和DROP,

Ebtables使用規則如下:

ebtables [-t table] -[ADI] chain rule-specification [match-extensions] [watcher-extensions]

-t table :一般為FORWARD鏈。

-ADI:A添加到現有鏈的末尾;D刪除規則鏈(必須指明規則鏈號);I插入新的規則鏈(必須指明規則鏈號)。

-P:規則表的預設規則的設定。不同的表有不同的規則。

-F:對所有的規則表的規則鏈清空。

-L:指明規則表。可加參數,--Lc,--Ln

-p:指明使用的協議類型,ipv4,arp等可選(使用時必選)詳情見/etc/ethertypes

--ip-proto:IP包的類型,1為ICMP包,6為TCP包,17為UDP包,在/etc/protocols下有詳細說明

--ip-src:IP包的源地址

--ip-dst:IP包的目的地址

--ip-sport:IP包的源連接埠

--ip-dport:IP包的目的連接埠

-i:指明從那片網卡進入

-o:指明從那片網卡出去
3:執行個體
這個執行個體來自ebtables的官網,ebtables執行個體。這個執行個體主要是結合ebtables和TC來實現對使用者進行流量控制。
在實際的應用中,這種情境較常見。
基於MAC地址來對不同的網路寬頻使用者進行流量控制。

Bridge configuration
------------- ifcfg-br0 -------------DEVICE=br0ONBOOT=noBOOTPROTO=staticIPADDR=192.168.111.11NETMASK=255.255.255.0----------- bridge_up.sh --------------------#!/bin/bashifdown eth0ifdown eth1  //關閉網路中的網卡eth0和eth1.ifconfig eth0 0.0.0.0 upifconfig eth1 0.0.0.0 up //開啟eth0,eth1,但是沒有給有效IP地址。即沒有IP地址。brctl addbr br0 //使用brctl命令建立橋接器br0brctl addif br0 eth0brctl addif br0 eth1 //將網路介面添加進橋接器br0ifconfig br0 up----------- bridge_down.sh --------------------#!/bin/bash ifdown eth0ifdown eth1ifconfig br0 downbrctl delbr br0
The rate shaping part

We're usingTC to do the deed. This is my first attempt at this, so I may be doing some things wrong,
especially with the TCcommands - BUT IT WORKS - so I figure, I'll fix it later. You can use ebtables
-L --Lc
 to see your customer's usage. I dump this out hourly, adding the -Z option to zero the counters out, then have a perl script parse that output and dump it into a mysql table where I can make better use of it.

--------------------------- rateshape -----------------------#!/bin/bash##  All Rates are in Kbits, so in order to gets Bytes divide by 8#  e.g. 25Kbps == 3.125KB/s#TC=/sbin/tcEBTABLES=/sbin/ebtables   # Location of ebtables cd /usr/local/bridge tc_start() {    $TC qdisc add dev eth0 root handle 1:0 cbq bandwidth 100Mbit avpkt 1000 mpu 64    $TC qdisc add dev eth1 root handle 1:0 cbq bandwidth 100Mbit avpkt 1000 mpu 64 

在TC 中, 使用"major:minor"這樣的控制代碼來標識隊列和類別,其中major和minor都是數字。對於隊列來說,minor總是為0,即"major:0"這樣的形式,也可以簡寫為"major: 比如,隊列1:0可以簡寫為1:。需要注意的是,major在一個網卡的所有隊列中必須是惟一的。對於類別來說,其major必須和它的父系類別或父隊列的major相同,而minor在一個隊列內部則必須是惟一的(因為類別肯定是包含在某個隊列中的)。舉個例子,如果隊列2:包含兩個類別,則這兩個類別的控制代碼必須是2:x這樣的形式,並且它們的x不能相同, 比如2:1和2:2。這裡,命令中的"add 表示要添加,"dev eth0 表示要操作的網卡為eth0。"root 表示為網卡eth0添加的是一個根隊列。"handle 1: 表示隊列的控制代碼為1:。"cbq 表示要添加的隊列為cbq隊列。

#Customer A#Two MACs: 00:0D:BD:A4:E1:C8 and 00:20:78:B0:25:7D#256kbps download speed${TC} class add dev eth0 parent 1:0 classid 1:1 cbq rate 256KBit allot 1514 prio 1 avpkt 1000 bounded${TC} filter add dev eth0 parent 1:0 protocol ip handle 1 fw flowid 1:1${EBTABLES} -A FORWARD -d 00:0D:BD:A4:E1:C8 -j mark --set-mark 1 --mark-target ACCEPT${EBTABLES} -A FORWARD -d 00:20:78:B0:25:7D -j mark --set-mark 1 --mark-target ACCEPT#128kbps upload speed${TC} class add dev eth1 parent 1:0 classid 1:1 cbq rate 128KBit allot 1514 prio 1 avpkt 1000 bounded${TC} filter add dev eth1 parent 1:0 protocol ip handle 1 fw flowid 1:1${EBTABLES} -A FORWARD -s 00:0D:BD:A4:E1:C8 -j mark --set-mark 1 --mark-target ACCEPT${EBTABLES} -A FORWARD -s 00:20:78:B0:25:7D -j mark --set-mark 1 --mark-target ACCEPT#Customer B#MAC Address: 00:0D:BD:A4:D6:54#800kbps download speed${TC} class add dev eth0 parent 1:0 classid 1:2 cbq rate 800KBit allot 1514 prio 1 avpkt 1000 bounded${TC} filter add dev eth0 parent 1:0 protocol ip handle 2 fw flowid 1:2${EBTABLES} -A FORWARD -d 00:0D:BD:A4:D6:54 -j mark --set-mark 2 --mark-target ACCEPT#64kbps upload speed${TC} class add dev eth1 parent 1:0 classid 1:2 cbq rate 64KBit allot 1514 prio 1 avpkt 1000 bounded${TC} filter add dev eth1 parent 1:0 protocol ip handle 2 fw flowid 1:2${EBTABLES} -A FORWARD -s 00:0D:BD:A4:D6:54 -j mark --set-mark 2 --mark-target ACCEPT#Customer C#MAC Address: 00:0A:5E:22:D1:A3#do not rate shape!${EBTABLES} -A FORWARD -s 00:0A:5E:22:D1:A3 -j ACCEPT${EBTABLES} -A FORWARD -d 00:0A:5E:22:D1:A3 -j ACCEPT#Block anything we didn't specify above.${EBTABLES} -A FORWARD -j DROP --log#<my config has over 500 customers and over 1100 MAC addresses>#Just keep incrementing the classid, handle, flowid, and mark values for each customer's#individual speed queues.} tc_stop() {     ./save_and_reset_counters     ${EBTABLES} -F //將刪除ebtables中定義的規則     $TC qdisc del dev eth0 root    $TC qdisc del dev eth1 root //刪除在eth0和eth1上面定義的隊列} tc_restart() {     tc_stop    sleep 1    tc_start} tc_show() { //分別查看eth0和eth1上面定義的隊列,類及過濾器。    echo ""    echo "eth0"    $TC qdisc show dev eth0    $TC class show dev eth0    $TC filter show dev eth0    echo ""    echo "eth1"    $TC qdisc show dev eth1    $TC class show dev eth1
    $TC filter show dev eth1

}//下面是shell指令碼的case語句。

case "$1" in   start)     echo -n "Starting bandwidth shaping: "    tc_start    echo "done"    ;;   stop)     echo -n "Stopping bandwidth shaping: "    tc_stop    echo "done"    ;;   restart)     echo -n "Restarting bandwidth shaping: "    tc_restart    echo "done"    ;;   show)     tc_show    ;;   *)     echo "Usage: rateshape {start|stop|restart|show}"    ;; esac

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.