Performance adjustments for OpenFlow
[integration/test.git] / csit / libraries / MininetKeywords.robot
1 *** Settings ***
2 Documentation     Mininet library. This library is useful for tests using mininet tool to simulate devices.
3 Library           SSHLibrary
4 Resource          SSHKeywords.robot
5 Resource          Utils.robot
6 Resource          OVSDB.robot
7 Resource          ClusterManagement.robot
8 Variables         ../variables/Variables.py
9
10 *** Variables ***
11 ${switch_private_key}    switch.key
12 ${switch_certificate}    switch.crt
13 ${controller_ca_list}    cachain.crt
14 ${topology_file}    create_fullymesh.py
15 ${topology_file_path}    MininetTopo/${topology_file}
16
17 *** Keywords ***
18 Start Mininet Single Controller
19     [Arguments]    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${options}=--topo tree,1    ${custom}=${EMPTY}    ${ofversion}=13    ${ofport}=${ODL_OF_PORT}
20     ...    ${timeout}=${DEFAULT_TIMEOUT}
21     [Documentation]    Start Mininet with custom topology and connect to controller.
22     Log    Clear any existing mininet
23     Utils.Clean Mininet System    ${mininet}
24     ${mininet_conn_id}=    SSHKeywords.Open_Connection_To_Tools_System    ip_address=${mininet}    timeout=${timeout}
25     Set Suite Variable    ${mininet_conn_id}
26     Run Keyword If    '${custom}' != '${EMPTY}'    Put File    ${custom}
27     Log    Start mininet ${options} to ${controller}
28     SSHLibrary.Write    sudo mn --controller 'remote,ip=${controller},port=${ofport}' ${options} --switch ovsk,protocols=OpenFlow${ofversion}
29     SSHLibrary.Read Until    mininet>
30     Log    Check OVS configuratiom
31     SSHLibrary.Write    sh ovs-vsctl show
32     SSHLibrary.Read Until    mininet>
33     [Return]    ${mininet_conn_id}
34
35 Start Mininet Multiple Controllers
36     [Arguments]    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller_index_list}=${EMPTY}    ${options}=--topo tree,1    ${custom}=${EMPTY}    ${ofversion}=13    ${ofport}=${ODL_OF_PORT}
37     ...    ${protocol}=tcp    ${timeout}=${DEFAULT_TIMEOUT}
38     [Documentation]    Start Mininet with custom topology and connect to list of controllers in ${controller_index_list} or all if no list is provided.
39     ${index_list} =    ClusterManagement.List Indices Or All    given_list=${controller_index_list}
40     Log    Clear any existing mininet
41     Utils.Clean Mininet System    ${mininet}
42     ${mininet_conn_id}=    SSHKeywords.Open_Connection_To_Tools_System    ip_address=${mininet}    timeout=${timeout}
43     Set Suite Variable    ${mininet_conn_id}
44     Run Keyword If    '${custom}' != '${EMPTY}'    Put File    ${custom}
45     Run Keyword If    '${protocol}' == 'ssl'    Install Certificates In Mininet
46     Log    Start mininet ${options}
47     SSHLibrary.Write    sudo mn ${options}
48     SSHLibrary.Read Until    mininet>
49     Log    Create controller configuration
50     ${controller_opt}=    Set Variable
51     : FOR    ${index}    IN    @{index_list}
52     \    ${controller_opt}=    Catenate    ${controller_opt}    ${SPACE}${protocol}:${ODL_SYSTEM_${index}_IP}:${ofport}
53     \    Log    ${controller_opt}
54     Log    Open extra SSH connection to configure the OVS bridges
55     SSHKeywords.Open_Connection_To_Tools_System
56     ${num_bridges}    SSHLibrary.Execute Command    sudo ovs-vsctl show | grep Bridge | wc -l
57     ${num_bridges}=    Convert To Integer    ${num_bridges}
58     Log    Configure OVS controllers ${controller_opt} in all bridges
59     : FOR    ${i}    IN RANGE    1    ${num_bridges+1}
60     \    ${bridge}=    SSHLibrary.Execute Command    sudo ovs-vsctl show | grep Bridge | cut -c 12- | sort | head -${i} | tail -1
61     \    SSHLibrary.Execute Command    sudo ovs-vsctl set bridge ${bridge} protocols=OpenFlow${ofversion}
62     \    SSHLibrary.Execute Command    sudo ovs-vsctl set-controller ${bridge} ${controller_opt}
63     Log    Check OVS configuratiom
64     ${output}=    SSHLibrary.Execute Command    sudo ovs-vsctl show
65     Log    ${output}
66     SSHLibrary.Close Connection
67     [Return]    ${mininet_conn_id}
68
69 Start Mininet Multiple Hosts
70     [Arguments]    ${hosts}    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${ofversion}=13    ${ofport}=${ODL_OF_PORT}    ${mininet_timeout}=${DEFAULT_TIMEOUT}
71     [Documentation]    Start mininet 1 switch with ${hosts} hosts attached.
72     Log    Start Mininet Linear
73     MininetKeywords.StartMininet Single Controller    options=--topo single,${hosts} --mac    ofversion=${ofversion}    ofport=${ofport}    timeout=${mininet_timeout}
74
75 Start Mininet Linear
76     [Arguments]    ${switches}    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${ofversion}=13    ${ofport}=${ODL_OF_PORT}    ${mininet_timeout}=${DEFAULT_TIMEOUT}
77     [Documentation]    Start mininet linear topology with ${switches} nodes.
78     Log    Start Mininet Linear
79     MininetKeywords.StartMininet Single Controller    options=--topo linear,${switches}    ofversion=${ofversion}    ofport=${ofport}    timeout=${mininet_timeout}
80
81 Start Mininet Full Mesh
82     [Arguments]    ${switches}    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${ofversion}=13    ${ofport}=${ODL_OF_PORT}    ${hosts}=0
83     ...    ${mininet_timeout}=${DEFAULT_TIMEOUT}
84     [Documentation]    Start a custom mininet topology.
85     ${mininet_conn_id}=    SSHLibrary.Open Connection    ${mininet}    prompt=${TOOLS_SYSTEM_PROMPT}    timeout=${mininet_timeout}
86     Set Suite Variable    ${mininet_conn_id}
87     SSHKeywords.Flexible_Mininet_Login
88     Log    Copying ${topology_file_path} file to Mininet VM and Creating Full Mesh topology
89     SSHLibrary.Put File    ${CURDIR}/${topology_file_path}
90     SSHLibrary.Write    python ${topology_file} ${switches} ${hosts} 00:00:00:00:00:00 10.0.0.0
91     SSHLibrary.Read Until    ${TOOLS_SYSTEM_PROMPT}
92     Log    Start Mininet Full Mesh
93     SSHLibrary.Write    sudo mn --controller=remote,ip=${controller},port=${ofport} --custom switch.py --topo demotopo --switch ovsk,protocols=OpenFlow${ofversion}
94     SSHLibrary.Read Until    mininet>
95     Log    Check OVS configuratiom
96     SSHLibrary.Write    sh ovs-vsctl show
97     ${output}=    Read Until    mininet>
98     # Ovsdb connection is sometimes lost after mininet is started. Checking if the connection is alive before proceeding.
99     Should Not Contain    ${output}    database connection failed
100
101 Send Mininet Command
102     [Arguments]    ${mininet_conn}=${EMPTY}    ${cmd}=help
103     [Documentation]    Sends Command ${cmd} to Mininet session ${mininet_conn} and returns read buffer response.
104     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
105     SSHLibrary.Write    ${cmd}
106     ${output}=    SSHLibrary.Read Until    mininet>
107     [Return]    ${output}
108
109 Send Mininet Command Multiple Sessions
110     [Arguments]    ${mininet_conn_list}    ${cmd}=help
111     [Documentation]    Sends Command ${cmd} to Mininet sessions in ${mininet_conn_list} and returns list of read buffer responses.
112     ${output_list}=    Create List
113     : FOR    ${mininet_conn}    IN    @{mininet_conn_list}
114     \    ${output}=    Utils.Send Mininet Command    ${mininet_conn}    ${cmd}
115     \    Append To List    ${output_list}    ${output}
116     [Return]    ${output_list}
117
118 Stop Mininet And Exit
119     [Arguments]    ${mininet_conn}=${EMPTY}
120     [Documentation]    Stops Mininet and exits session ${mininet_conn}
121     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
122     SSHLibrary.Write    exit
123     SSHLibrary.Read Until    ${TOOLS_SYSTEM_PROMPT}
124     SSHLibrary.Close Connection
125
126 Stop Mininet And Exit Multiple Sessions
127     [Arguments]    ${mininet_conn_list}
128     [Documentation]    Stops Mininet and exits sessions in ${mininet_conn_list}.
129     : FOR    ${mininet_conn}    IN    @{mininet_conn_list}
130     \    MininetKeywords.Stop Mininet And Exit    ${mininet_conn}
131
132 Disconnect Cluster Mininet
133     [Arguments]    ${action}=break    ${member_index_list}=${EMPTY}
134     [Documentation]    Break and restore controller to mininet connection via iptables.
135     ${index_list} =    ClusterManagement.List_Indices_Or_All    given_list=${member_index_list}
136     : FOR    ${index}    IN    @{index_list}
137     \    ${rule} =    BuiltIn.Set Variable    OUTPUT -p all --source ${ODL_SYSTEM_${index}_IP} --destination ${TOOLS_SYSTEM_IP} -j DROP
138     \    ${command} =    BuiltIn.Set Variable If    '${action}'=='restore'    sudo /sbin/iptables -D ${rule}    sudo /sbin/iptables -I ${rule}
139     \    Log To Console    ${ODL_SYSTEM_${index}_IP}
140     \    Utils.Run Command On Controller    ${ODL_SYSTEM_${index}_IP}    cmd=${command}
141     \    ${command} =    BuiltIn.Set Variable    sudo /sbin/iptables -L -n
142     \    ${output} =    Utils.Run Command On Controller    cmd=${command}
143     \    BuiltIn.Log    ${output}
144
145 Verify Aggregate Flow From Mininet Session
146     [Arguments]    ${mininet_conn}=${EMPTY}    ${flow_count}=0    ${time_out}=0s
147     [Documentation]    Verify flow count per switch
148     Wait Until Keyword Succeeds    ${time_out}    2s    MininetKeywords.Check Flows In Mininet    ${mininet_conn}    ${flow_count}
149
150 Check Flows In Mininet
151     [Arguments]    ${mininet_conn}=${EMPTY}    ${flow_count}=0
152     [Documentation]    Sync with mininet to match exact number of flows
153     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
154     ${cmd} =    Set Variable    dpctl dump-aggregate -O OpenFlow13
155     ${output}=    MininetKeywords.Send Mininet Command    ${mininet_conn}    ${cmd}
156     ${flows}=    String.Get RegExp Matches    ${output}    (?<=flow_count\=).*?(?=\r)
157     ${total_flows}=    BuiltIn.Evaluate    sum(map(int, ${flows}))
158     Should Be Equal As Numbers    ${total_flows}    ${flow_count}
159
160 Verify Mininet Ping
161     [Arguments]    ${host1}    ${host2}
162     [Documentation]    Send ping from mininet and verify connectivity.
163     SSHLibrary.Write    ${host1} ping -w 3 ${host2}
164     ${result}=    SSHLibrary.Read Until    mininet>
165     Should Contain    ${result}    64 bytes
166
167 Verify Mininet No Ping
168     [Arguments]    ${host1}    ${host2}
169     [Documentation]    Send ping from mininet and verify no conectivity.
170     SSHLibrary.Write    ${host1} ping -w 3 ${host2}
171     ${result}=    SSHLibrary.Read Until    mininet>
172     Should Contain    ${result}    100% packet loss
173
174 Ping All Hosts
175     [Arguments]    @{host_list}
176     [Documentation]    Do one round of ping from one host to all other hosts in mininet.
177     ...    Note that a single ping failure will exit the loop and return a non zero value.
178     ${source}=    Get From List    ${host_list}    ${0}
179     : FOR    ${h}    IN    @{host_list}
180     \    ${status}=    Ping Two Hosts    ${source}    ${h}    1
181     \    Exit For Loop If    ${status}!=${0}
182     [Return]    ${status}
183
184 Ping Two Hosts
185     [Arguments]    ${host1}    ${host2}    ${pingcount}=2
186     [Documentation]    Ping between mininet hosts. Must be used only after a mininet session is in place.
187     ...    Returns non zero value if there is 100% packet loss.
188     SSHLibrary.Write    ${host1} ping -c ${pingcount} ${host2}
189     ${out}=    SSHLibrary.Read Until    mininet>
190     ${ret}=    String.Get Lines Matching Regexp    ${out}    .*100% packet loss.*
191     ${len}=    Get Length    ${ret}
192     [Return]    ${len}
193
194 Get Mininet Hosts
195     [Documentation]    Get all the hosts from mininet
196     ${host_list}=    Create List
197     SSHLibrary.Write    nodes
198     ${out}=    SSHLibrary.Read Until    mininet>
199     @{words}=    String.Split String    ${out}    ${SPACE}
200     : FOR    ${item}    IN    @{words}
201     \    ${h}=    String.Get Lines Matching Regexp    ${item}    h[0-9]*
202     \    Run Keyword If    '${h}' != '${EMPTY}'    Append To List    ${host_list}    ${h}
203     [Return]    ${host_list}
204
205 Install Certificates In Mininet
206     [Documentation]    Copy and install certificates in simulator.
207     Comment    Copy Certificates
208     SSHLibrary.Put File    ${CURDIR}/tls/${switch_private_key}    .
209     SSHLibrary.Put File    ${CURDIR}/tls/${switch_certificate}    .
210     SSHLibrary.Put File    ${CURDIR}/tls/${controller_ca_list}    .
211     Comment    Install Certificates
212     SSHLibrary.Execute Command    sudo mv ${switch_private_key} /etc/openvswitch && sudo mv ${switch_certificate} /etc/openvswitch && sudo mv ${controller_ca_list} /etc/openvswitch
213     SSHLibrary.Execute Command    sudo ovs-vsctl set-ssl /etc/openvswitch/${switch_private_key} /etc/openvswitch/${switch_certificate} /etc/openvswitch/${controller_ca_list}
214     ${std_out}=    SSHLibrary.Execute Command    .    sudo ovs-vsctl get-ssl
215     Log    ${std_out}