更新时间:2021-08-26 GMT+08:00
分享

新建组网脚本

组网固定后,配置命令相应固定了,配置命令多且重复度高,遂提供脚本配置组网,脚本如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
local_ip=172.16.1.1
remote_ip=172.16.1.2
tap_num=8

setup_vxlan(){
    #add br-int
    ovs-vsctl add-br br-int -- set bridge br-int datapath_type=netdev
    ovs-vsctl add-port br-int vxlan0 --  set Interface vxlan0 type=vxlan options:local_ip="$local_ip" options:remote_ip="$remote_ip"

    #add br-ply
    for i in `seq 1 $tap_num`
    do
        ovs-vsctl add-br br-ply$i -- set bridge br-ply$i datapath_type=netdev
        ovs-vsctl add-port br-ply$i tap$i --  set Interface tap$i type=dpdkvhostuserclient options:vhost-server-path=/var/run/openvswitch/tap$i
        ovs-vsctl add-port br-ply$i p-tap$i-int --  set Interface p-tap$i-int type=patch options:peer=p-tap$i
        ovs-vsctl add-port br-int p-tap$i --  set Interface p-tap$i type=patch options:peer=p-tap$i-int
    done

    #add dpdkbond
    ovs-vsctl add-br br-dpdk -- set bridge br-dpdk datapath_type=netdev
    ovs-vsctl add-bond br-dpdk dpdk-bond p0 p1 -- set Interface p0 type=dpdk options:dpdk-devargs=0000:01:00.0,txq_mpw_en=1 options:n_rxq=8 \
    -- set Interface p1 type=dpdk options:dpdk-devargs=0000:01:00.1,txq_mpw_en=1 options:n_rxq=8
    ovs-vsctl set port dpdk-bond bond_mode=balance-tcp
    ovs-vsctl set port dpdk-bond lacp=active
    ifconfig br-dpdk "$local_ip"/24 up
}

get_ofport_by_name(){
    local name=$1
    local extra_filter="$2"

    [ -n "$extra_filter" ] && echo $(ovs-appctl dpif/show |grep $name |grep "$extra_filter" |cut -d '/' -f 1 |awk '{printf $2 "\n"}')
    [ -z "$extra_filter" ] && echo $(ovs-appctl dpif/show |grep $name |cut -d '/' -f 1 |awk '{printf $2 "\n"}')
}

ct(){
    for i in `seq 1 $tap_num`
    do
        VM=$(get_ofport_by_name tap$i "dpdkvhostuserclient")
        PATCH_TAP_INT=$(get_ofport_by_name p-tap$i-int "(patch: peer=p-tap$i)")

        echo "setup_ct_$i: VM=$VM, PATCH_TAP_INT=$PATCH_TAP_INT"

        ovs-ofctl add-flow br-ply$i "arp,actions=normal"
        ovs-ofctl add-flow br-ply$i "table=0,priority=50,in_port=$VM,ip,ct_state=-trk,actions=ct(table=0,zone=$i)"
        ovs-ofctl add-flow br-ply$i "table=0,priority=50,in_port=$VM,ip,ct_state=+trk+new,ct_zone=$i,actions=ct(zone=$i,commit)$PATCH_TAP_INT"
        ovs-ofctl add-flow br-ply$i "table=0,priority=50,in_port=$PATCH_TAP_INT,ip,ct_state=-trk,actions=ct(table=0,zone=$i)"
        ovs-ofctl add-flow br-ply$i "table=0,priority=50,in_port=$PATCH_TAP_INT,ip,ct_state=+trk+new,ct_zone=$i,actions=ct(zone=$i,commit)$VM"
        ovs-ofctl add-flow br-ply$i "table=0,priority=50,in_port=$VM,ip,ct_state=+est+trk,ct_zone=$i,actions=output:$PATCH_TAP_INT"
        ovs-ofctl add-flow br-ply$i "table=0,priority=50,in_port=$PATCH_TAP_INT,ip,ct_state=+est+trk,ct_zone=$i,actions=output:$VM"
    done
}

ct_clean(){
    for i in `seq 1 $tap_num`
    do
        VM=$(get_ofport_by_name tap$i "dpdkvhostuserclient")
        PATCH_TAP_INT=$(get_ofport_by_name p-tap$i-int "(patch: peer=p-tap$i)")

        echo "clean_ct_$i: VM=$VM, PATCH_TAP_INT=$PATCH_TAP_INT"

        ovs-ofctl del-flows br-ply$i "arp"
        ovs-ofctl del-flows br-ply$i "table=0,in_port=$VM,ip,ct_state=-trk"
        ovs-ofctl del-flows br-ply$i "table=0,in_port=$VM,ip,ct_state=+trk+new,ct_zone=$i"
        ovs-ofctl del-flows br-ply$i "table=0,in_port=$PATCH_TAP_INT,ip,ct_state=-trk"
        ovs-ofctl del-flows br-ply$i "table=0,in_port=$PATCH_TAP_INT,ip,ct_state=+trk+new,ct_zone=$i"
        ovs-ofctl del-flows br-ply$i "table=0,in_port=$VM,ip,ct_state=+est+trk,ct_zone=$i"
        ovs-ofctl del-flows br-ply$i "table=0,in_port=$PATCH_TAP_INT,ip,ct_state=+est+trk,ct_zone=$i"
    done
}

clean(){
    for i in `ovs-vsctl list-br`
    do
        ovs-vsctl del-br $i
    done
}

case $1 in
clean)
    clean
    ;;
setvxlan)
    setup_vxlan
    ;;
ct)
    ct
    ;;
ct_clean)
    ct_clean
    ;;
*)
    ;;
esac
  • 实例环境中,脚本被命名为topology_all.sh。
  • Host2和Host1的脚本基本一致,但在配置Host2的脚本的时候需将local_ip和remote_ip值进行对换。
分享:

    相关文档

    相关产品

close