Lab 4 : OpenFlow

實驗描述

OpenFlow is a communications protocol that gives access to the forwarding plane of a network switch or router over the network.

利用Controller 控制其底下的 OpenFlow switches,達成資料中心所需之客製化網路環境。

本次實驗將讓大家進行:

1. Open vSwitch 的安裝。

2. Install flows in Open vSwitch switches.

3. 使用 POX Controller.




實習環境

理學大樓 1002 教室
  Cloud-A01 ~ Cloud-D12
CPU AMD Phenom™ II X6 1065T Processor
Memory 8G
Disk spaces 500G、500G
O.S. Debian wheezy
理學大樓 821 機房
  CSIE-Cloud01 ~ CSIE-Cloud06
CPU AMD Opteron™ Processor 6128 * 2
(total 16 cpu cores)
Memory 32G
Disk spaces 500G、500G、1T
O.S. Debian wheezy
  CSIE-Cloud07、CSIE-Cloud08
CPU AMD Opteron™ Processor 6234 * 2
(total 24 cpu cores)
Memory 32G
Disk spaces 500G、500G、1T
O.S. Debian wheezy
虛擬機環境
  Virtual Machine
Location 821 機房伺服器
Memory 4G
Disk spaces 50G (QCOW2 Format)
O.S. Ubuntu 12.04 Server Edition

實習之前

由於大家花費好大力氣才建立OpenStack,所以我們會利用 OpenStack 建立三台 CirrOS VMs,並且進行這一次的實習。

用 admin 帳號登入,刪除所有的 instances

建立兩台 Cirros VMs。


實習內容

登入虛擬機

ssh 140.120.16.67 -p [YourVM port]

安裝Open vSwitch

sudo aptitude install openvswitch-switch
sudo ovs-vsctl show
3846d695-0234-44a8-b36c-82ba3906eff8 ovs_version: "1.4.0+build0”

基本操作

新增一個 switch

sudo ovs-vsctl add-br br2
sudo ovs-vsctl show
b84370b6-0c00-40ea-b1ce-a4682dfa3b73 Bridge "br2" Port "br2" Interface "br2" type: internal ovs_version: "1.4.0+build0"
sudo ifconfig br2 192.168.100.1 up
ifconfig br2
br2 Link encap:Ethernet HWaddr f6:af:a8:3b:1a:48 inet addr:192.168.100.1 Bcast:192.168.100.255 Mask:255.255.255.0 inet6 addr: fe80::f4af:a8ff:fe3b:1a48/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:754 errors:0 dropped:0 overruns:0 frame:0 TX packets:746 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:70654 (70.6 KB) TX bytes:72862 (72.8 KB)

將虛擬網卡從 Linux Bridge 上移除

sudo brctl show
bridge name bridge id STP enabled interfaces br1 8000.fe163e1ea1b2 no vnet0 vnet1 vnet2 br2 0000.f6afa83b1a48 no
sudo brctl delif br1 vnet0
sudo brctl delif br1 vnet1
bridge name bridge id STP enabled interfaces br1 8000.fe163e1ea1b2 no br2 0000.f6afa83b1a48 no

將虛擬網卡插入 Open vSwitch

sudo ovs-vsctl add-port br2 vnet0
sudo ovs-vsctl add-port br2 vnet1
sudo ovs-vsctl show
b84370b6-0c00-40ea-b1ce-a4682dfa3b73 Bridge "br2" Port "vnet1" Interface "vnet1" Port "vnet2" Interface "vnet2" Port "vnet0" Interface "vnet0" Port "br2" Interface "br2" type: internal ovs_version: "1.4.0+build0"

利用指令插入flow

原本的狀態為正常的 Switch forwarding。

sudo ovs-ofctl dump-flows br2
NXST_FLOW reply (xid=0x4): cookie=0x0, duration=3.33s, table=0, n_packets=0, n_bytes=0, priority=0 actions=NORMAL

阻擋所有封包

接下來,將原本的規則刪除,然後新增一個 Flow,單純定義 actions=drop。

sudo ovs-ofctl del-flows br2
sudo ovs-ofctl dump-flows br2
NXST_FLOW reply (xid=0x4):

利用虛擬機 ping 192.168.100.1,應該會沒有回應。

允許 ICMP 封包

sudo ovs-ofctl add-flow br2 "table=0,arp,actions=normal"
sudo ovs-ofctl add-flow br2 "table=0,icmp,actions=normal"
sudo ovs-ofctl dump-flows br2
NXST_FLOW reply (xid=0x4): cookie=0x0, duration=116.622s, table=0, n_packets=6, n_bytes=252, arp actions=NORMAL cookie=0x0, duration=6.691s, table=0, n_packets=0, n_bytes=0, icmp actions=NORMAL

1. 利用虛擬機 ping 192.168.100.1,icmp 可正常回應。

2. 利用虛擬機 ssh 192.168.100.1,icmp 無法登入。

允許 ssh(port 22) 連線

sudo ovs-ofctl add-flow br2 "table=0,tcp,tp_src=22,actions=normal"
sudo ovs-ofctl add-flow br2 "table=0,tcp,tp_dst=22,actions=normal"
sudo ovs-ofctl dump-flows br2
NXST_FLOW reply (xid=0x4): cookie=0x0, duration=748.124s, table=0, n_packets=10, n_bytes=420, arp actions=NORMAL cookie=0x0, duration=5.379s, table=0, n_packets=0, n_bytes=0, tcp,tp_dst=22 actions=NORMAL cookie=0x0, duration=16.384s, table=0, n_packets=0, n_bytes=0, tcp,tp_src=22 actions=NORMAL cookie=0x0, duration=638.193s, table=0, n_packets=8, n_bytes=784, icmp actions=NORMALMAL

恢復原本規則

sudo ovs-ofctl del-flows br2
sudo ovs-ofctl add-flow br2 "table=0,priority=0,actions=normal"
sudo ovs-ofctl dump-flows br2
NXST_FLOW reply (xid=0x4): cookie=0x0, duration=18.533s, table=0, n_packets=30, n_bytes=2716, priority=0 actions=NORMAL

利用虛擬機 ssh 192.168.100.1,則可以正常登入。

安裝 POX controller

sudo aptitude update;sudo aptitude install git
cd ~;git clone http://github.com/noxrepo/pox

設定 Open vSwitch 要連接的controller 位置。

sudo ovs-vsctl set-controller br2 "tcp:[Ubuntu IP]"
sudo ovs-vsctl show
b84370b6-0c00-40ea-b1ce-a4682dfa3b73 Bridge "br2" Controller "tcp:[Ubuntu IP]" Port "vnet1" Interface "vnet1" Port "vnet2" Interface "vnet2" Port "vnet0" Interface "vnet0" Port "br2" Interface "br2" type: internal ovs_version: "1.4.0+build0"

利用 POX controller 執行簡單的網路路由

POX controller 提供簡單的範例,讓我們的網路運作。

cd ~/pox
./pox.py forwarding.l2_learning
POX 0.2.0 (carp) / Copyright 2011-2013 James McCauley, et al. INFO:core:POX 0.2.0 (carp) is up. INFO:openflow.of_01:[f6-af-a8-3b-1a-48 1] connected

我們可以在執行完 controller 後,在 VM 中進行網路操作,再觀察 Flow table 的變化。

sudo ovs-ofctl dump-flows br2
NXST_FLOW reply (xid=0x4): cookie=0x0, duration=8.718s, table=0, n_packets=1, n_bytes=98, idle_timeout=10,hard_timeout=30,priority=65535,icmp,in_port=6,vlan_tci=0x0000,dl_src=fa:16:3e:ac:71:fb,dl_dst=f6:af:a8:3b:1a:48,nw_src=192.168.100.4,nw_dst=192.168.100.1,nw_tos=0,icmp_type=8,icmp_code=0 actions=LOCAL cookie=0x0, duration=8.715s, table=0, n_packets=1, n_bytes=98, idle_timeout=10,hard_timeout=30,priority=65535,icmp,in_port=65534,vlan_tci=0x0000,dl_src=f6:af:a8:3b:1a:48,dl_dst=fa:16:3e:ac:71:fb,nw_src=192.168.100.1,nw_dst=192.168.100.4,nw_tos=0,icmp_type=0,icmp_code=0 actions=output:6

利用 POX controller 執行防火牆規則

我們會利用剛才使用的 l2_learning 程式進行修改。

cd ~/pox/pox/forwarding
ls
hub.py l2_flowvisor.py l2_multi.py l2_pairs.py __init__.py l2_learning.py l2_nx.py l3_learning.py __init__.pyc l2_learning.pyc l2_nx_self_learning.py topo_proactive.py
cp l2_learning.py l2_icmp_block.py
cp l2_learning.py l2_ip_block.py
cp l2_learning.py l2_mac_block.py

阻擋 ICMP 封包

程式範例

當我們發現封包是 ICMP packets 時,我們會將封包進行阻擋。

nano l2_icmp_block.py
# 147 行新增 icmpPkt = packet.find('icmp') if icmpPkt is not None: log.info("Receive ICMP Packet!!") drop() return
~/pox/pox.py forwarding.l2_icmp_block

阻擋特定 IP 封包

程式範例

nano l2_ip_block.py
# 147 行新增 IPPkt = packet.find('ipv4') if IPPkt is not None: log.info("Receive %s Packet!!",IPPkt.srcip) if IPPkt.srcip == '192.168.100.3': log.info("Drop %s Packet...",IPPkt.srcip) drop() return
~/pox/pox.py forwarding.l2_ip_block

阻擋特定 mac address 之封包。

程式範例

nano l2_mac_block.py
# 27 行新增 from pox.lib.addresses import EthAddr # 147 行新增 MacAddr = 'FA:16:3E:AC:71:FB' EthPkt = packet.find('ethernet') if EthPkt is not None: log.info("%s",EthPkt.src) if EthPkt.src == EthAddr(MacAddr): log.info("Match") drop() return
~/pox/pox.py forwarding.l2_mac_block

Assignment#4(Deadline: 2014/01/24 00:00)

攥寫一 POX controller program,執行功能如下:
1. 阻擋所有的 ping 192.168.100.1 的 ICMP 封包。
2. 建立一台 mac to IP 的唯一機器。
3. 阻擋特定兩個 IP 互相連線。
4. 其他的網路功能則維持 L2 learning 之狀態。

完成後請寄信至助教信箱:keuu2002@cs.nchu.edu.tw
信件內容如下:
a. 學號、姓名,使用的Ubuntu 內網IP
b. mac to IP 的 mac address 與 IP address
c. 阻擋特定兩個 IP 的 IP address
d. OpenFlow 程式的檔名

原文地址:https://www.cnblogs.com/dream397/p/13150870.html