basic-usage-for-openvswitch

In this post I show the basic commands for the openvswitch. You should have at least openvswitch 1.9 installed. This is the case, if you are running Ubuntu 13.04.

Preparation

In this blog, I show more than the basic commands. We use Linux network namespaces to show also the usage on the ip layer. The first step is to create two linux network namespaces (you must be root):

# create two network namespaces
ip netns add ns1
ip netns add ns2

# bring up the loopback interfaces of the namespaces
ip netns exec ns1 ip link set dev lo up
ip netns exec ns2 ip link set dev lo up</pre>

Next, we need three console sessions. One for each network namespace (use ip netns exec ns <x> bash to get into the namespace) and one for the non namespace config. I recommend, that you change the prompt for each session – it helps a lot, that you always knows, where you are.

#
# MAIN bash 
#
export PS1="Main>"
#
# Console for Network Namespace ns1
#
ip netns exec ns1 bash
export PS1="NS 1>"
#
# Console in Network Namespace ns2
#
ip netns exec ns2 bash
export PS1="NS 2>"</pre>

Create a Switch (bridge)

The next step is to create a new virtual switch (or bridge). Use the following command in the Main console:

# create a new virtual switch
ovs-vsctl add-br testsw1

# check using
ovs-vsctl show

# the output will look like
9ddae96d-0cc4-4c17-98c2-7ffe3f4f6226             <==== this is the UUID of the running openvswitch
    Bridge testsw1                               <==== this is a running virtual switch instance
        Port testsw1                             
            Interface testsw1                    <==== By default, each virtual switch gets an internal interface      
                type: internal                         internal interface == this is a interface seen by Linux
    ovs_version: "1.9.0"                         <==== the version of the openvswitch software running</pre>

By default, each virtual switch (=bridge), which has been created, gets one „internal port“ assigned. These internal ports are seen by the Linux OS. Type „ip link“ in the Main console and you get:

19: testsw1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT 
    link/ether ae:fd:bb:27:2b:43 brd ff:ff:ff:ff:ff:ff</pre>

You can use this interface in the same way as all other Linux interfaces. Assign IP addresses to it or put IP access lists on it (iptables). We will ignore this interface.

An openvswitch bridge is not only a simple bridge, it’s a full blown software switch with

  • 4 k Vlans – nothing to do to create them – they are just there
  • it’s own MAC table

Create two ports in the bridge

The next step is to create two ports in the switch testsw1 and assign these to the two network namespaces. Run this in the Main console.

# create an internal ovs port
ovs-vsctl add-port testsw1 ns1iface1 -- set Interface ns1iface1 type=internal
# attach it to namespace
ip link set ns1iface1 netns ns1

# create an internal ovs port
ovs-vsctl add-port testsw1 ns2iface1 -- set Interface ns2iface1 type=internal
# attach it to namespace
ip link set ns2iface1 netns ns2</pre>

Check now in the Main console the openvswitch:

# show all switches
ovs-vsctl show

    Bridge "testsw1"
        Port "ns2iface1"
            Interface "ns2iface1"
                type: internal
        Port "testsw1"
            Interface "testsw1"
                type: internal
        Port "ns1iface1"
            Interface "ns1iface1"
                type: internal</pre>

We see the two ports in the list. A shorter command to list only the ports is

# list all ports of a bridge - here testsw1
ovs-vsctl list-ports testsw1
ns1iface1
ns2iface1

Now look into the namespace windows. Type ip link in both windows and the new interfaces appeared.

In ns1:

ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
20: ns1iface1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT 
    link/ether 5e:e7:51:a5:10:55 brd ff:ff:ff:ff:ff:ff
#
# bring up ns1iface1
#
ip link set dev ns1iface1 up
# 
# assign an ip address
#
ip addr add dev ns1iface1 10.0.0.1/24

In ns2:

ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
21: ns2iface1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT 
    link/ether 8a:42:b3:91:db:c2 brd ff:ff:ff:ff:ff:ff
#
# bring up ns2iface1
#
ip link set dev ns2iface1 up
# 
# assign an ip address
#
ip addr add dev ns2iface1 10.0.0.2/24</pre>

Now it should be possible to ping the other side.

In the Main console check the MAC address table of testsw1:

#
# list all mac addresses of testsw1 and the ports where the MAC addresses are located
#
ovs-appctl fdb/show testsw1
 port  VLAN  MAC                Age
    1     0  5e:e7:51:a5:10:55   58
    2     0  8a:42:b3:91:db:c2   58</pre>

The MAC addresses match those of the interfaces, we previously created. You might notice, that both MAC addresses are attached to Vlan 0. So – what is Vlan 0?

By default, ALL ports/interfaces, which are created on the openvswitch are able to transport dot1q tagged traffic unless there is a restriction added when the port is created. We made no restriction about this when we created the ports. Since both ip interfaces used in the network namespaces do not send dot1q tagged traffic, the openvswitch places this traffic in Vlan 0.

This Vlan 0 means: the openvswitch got untagged traffic on a link, where dot1q transport is enabled.

Create a dot1q tagged link

Now we create a dot1q tagged link in both network namespaces on top of the existing link. the command to do this is ip.

Example for network namespace ns1.

#
# create a Vlan subinterface on a interface
#
# here (example with the interface in namespace ns1)
# Vlan tag: 22
#
ip link add link ns1iface1 name ns1iface1.22 type vlan id 22
#
# it's a good practice to code the vlan number and the interface in the interface name for the new interface
#
# check the existing interfaces using ip link
#
NS 1>ip l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ns1iface1.22@ns1iface1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT 
    link/ether 5e:e7:51:a5:10:55 brd ff:ff:ff:ff:ff:ff
20: ns1iface1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN mode DEFAULT 
    link/ether 5e:e7:51:a5:10:55 brd ff:ff:ff:ff:ff:ff</pre>

Do the same for network namespace ns2 (do not forget to change the interface name!)

Then bring the interface up and assign an ip address.

ns1 and ns2:

#
#### N S 1 ######
#
# bring the interface up
ip link set dev ns1iface1.22 up
#
# assign an ip address
ip addr add dev ns1iface1.22 10.1.0.1/24
#
#### N S 2 ######
#
# bring the interface up
ip link set dev ns1iface1.22 up
#
# assign an ip address
ip addr add dev ns1iface1.22 10.1.0.1/24
#
#### N S 2 #####
#
# ping the other side (from ns2 to ns1) in vlan 22 
ping 10.1.0.1 
#
#### N S 1 ######
#
# ping the other side (from ns1 to ns2) on the untagged link 
ping 10.1.0.2

Look now on the MAC table of the ovenvswitch in the Main window:

ovs-appctl fdb/show testsw1
 port  VLAN  MAC                Age
    1    22  5e:e7:51:a5:10:55   87
    2    22  8a:42:b3:91:db:c2   87
    2     0  8a:42:b3:91:db:c2    1
    1     0  5e:e7:51:a5:10:55    1

You see now two Vlans with active MAC addresses. Yes they are the same in both Vlans. This is OK, since each Vlan has it’s own layer 2 forwarding table.

If the MAC entries disappear – don’t panic. This is a normal behaviour. As soon as traffic flows, they are populated again.

We have now the following setup:

By default a port created without restrictions will transport all vlan tags

Create a untagged port

The next step is to create ports on the openvswitch for the network namespaces, which are untagged by default. This is the case in 99.9% of all cases, when you are attaching LXC or KVM instances to an openvswitch. We attach these ports to vlan 33. This is done using the commands in the Main console:

# create an internal ovs port
ovs-vsctl add-port testsw1 ns1iface2 tag=33 -- set Interface ns1iface2 type=internal
# attach it to namespace
ip link set ns1iface2 netns ns1

# create an internal ovs port
ovs-vsctl add-port testsw1 ns2iface2 tag=33 -- set Interface ns2iface2 type=internal
# attach it to namespace
ip link set ns2iface2 netns ns2

By adding „tag=33“ while using the add-port the port is by default untagged (no dot1q tags are accepted) and the port is attached to Vlan 33 on the openvswitch.

A ovs-vsctl show executed in the Main console outputs now:

   Bridge "testsw1"
        Port "ns2iface1"
            Interface "ns2iface1"
                type: internal
        Port "ns1iface2"
            tag: 33
            Interface "ns1iface2"
                type: internal
        Port "testsw1"
            Interface "testsw1"
                type: internal
        Port "ns2iface2"
            tag: 33
            Interface "ns2iface2"
                type: internal
        Port "ns1iface1"
            Interface "ns1iface1"
                type: internal</pre>

The ports ns1iface2 and ns2iface2 have now one additional line: „tag :33“. This means. The port is untagged and attached to Vlan 33. It does not mean, that only the Vlan tag 33 is accepted on a tagged link.

Next task: Bring the new interfaces up in the network namespaces, assign the ip addresses 10.2.0.1 for ns1 and 10.2.0.2 for ns2 and try to ping. Look at the MAC table on the openvswitch (from the Main console). You should see MAC addresses in Vlan 33.

We have now the following setup:

A port with tag= will be untagged on the link and will be attached to a fixed vlan on the openvswitch

Create a dot1q trunking port with a restricted vlan list

The next step is to create a new interface, which is dot1q tagged, but allows only a limited list of tags (=vlans) on the port.

The commands for the Main console are:

# create an internal ovs port
ovs-vsctl add-port testsw1 ns1iface3 trunk=44,55,66 -- set Interface ns1iface3 type=internal
# attach it to namespace
ip link set ns1iface3 netns ns1

# create an internal ovs port
ovs-vsctl add-port testsw1 ns2iface3 trunk=44,55,66 -- set Interface ns2iface3 type=internal
# attach it to namespace
ip link set ns2iface3 netns ns2

The keyword trunk tell the ovs to allow dot1q on this port. We allow the tags (=vlans) 44, 55 and 66.

The command „ovs-vsctl show“ shows now:

   Bridge "testsw1"
        Port "ns1iface3"
            trunks: [44, 55, 66]
            Interface "ns1iface3"
                type: internal
        Port "ns2iface1"
            Interface "ns2iface1"
                type: internal
        Port "ns1iface2"
            tag: 33
            Interface "ns1iface2"
                type: internal
        Port "ns2iface3"
            trunks: [44, 55, 66]
            Interface "ns2iface3"
                type: internal
        Port "testsw1"
            Interface "testsw1"
                type: internal
        Port "ns2iface2"
            tag: 33
            Interface "ns2iface2"
                type: internal
        Port "ns1iface1"
            Interface "ns1iface1"
                type: internal

The new created ports have one line stating with trunks:. This means: The port is dot1q tagged and allows the transport of the tags 44, 55 and 66. All other tags are dropped.

Check if the 3 vlans are working on the new link. Configure also Vlan 77 on the link and verify, that the traffic is dropped at the openvswitch – reason: Vlan 77 ist not allowed on the port. Configure an IP address on the native interface nsxiface3 and verify, that this traffic is also dropped.

We have now the following setup:

NOTE: After adding a „trunking“ port with a restricted vlan list

end.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,539评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,911评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,337评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,723评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,795评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,762评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,742评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,508评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,954评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,247评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,404评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,104评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,736评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,352评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,557评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,371评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,292评论 2 352

推荐阅读更多精彩内容