admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #/usr/bin/env python |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 26-Nov-2012 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 4 | |
| 5 | author:: Raghav Kashyap( raghavkashyap@paxterrasolutions.com ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 6 | |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 11 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | |
| 23 | DPCTL driver class provides the basic functions of DPCTL controller |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 24 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | import pexpect |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 26 | import struct |
| 27 | import fcntl |
| 28 | import os |
| 29 | import sys |
| 30 | import signal |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 31 | import sys |
| 32 | from drivers.common.cli.toolsdriver import Tools |
| 33 | import pydoc |
| 34 | from drivers.common.clidriver import CLI |
| 35 | import re |
| 36 | import os |
| 37 | import sys |
| 38 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 39 | |
| 40 | class DpctlCliDriver( Tools ): |
| 41 | |
| 42 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 43 | DpctlCliDriver driver class provides the basic functions of DPCTL controller |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 44 | """ |
| 45 | def __init__( self ): |
| 46 | super( DpctlCliDriver, self ).__init__() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 47 | self.handle = self |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 48 | self.wrapped = sys.modules[ __name__ ] |
| 49 | |
| 50 | def connect( self, **connectargs ): |
| 51 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 53 | vars( self )[ key ] = connectargs[ key ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 55 | self.name = self.options[ 'name' ] |
| 56 | |
| 57 | self.handle = super( |
| 58 | DpctlCliDriver, self ).connect( user_name=self.user_name, |
| 59 | ip_address=self.ip_address, |
| 60 | port=None, |
| 61 | pwd=self.pwd ) |
| 62 | if self.handle: |
| 63 | main.log.info( "Connected to the host" ) |
| 64 | return main.TRUE |
| 65 | else: |
| 66 | main.log.error( |
| 67 | "Connection failed to the host " + |
| 68 | self.user_name + |
| 69 | "@" + |
| 70 | self.ip_address ) |
| 71 | return main.FALSE |
| 72 | |
| 73 | def addFlow( self, **flowParameters ): |
| 74 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 75 | addFlow create a new flow entry into flow table using "dpctl" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 76 | """ |
| 77 | args = utilities.parse_args( [ |
| 78 | "TCPIP", |
| 79 | "TCPPORT", |
| 80 | "INPORT", |
| 81 | "ACTION", |
| 82 | "TIMEOUT" ], |
| 83 | **flowParameters ) |
| 84 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 85 | cmd = "dpctl add-flow tcp:" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 86 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 87 | tcpPort = args[ "TCPPORT" ] if args[ |
| 88 | "TCPPORT" ] is not None else "6634" |
| 89 | timeOut = args[ "TIMEOUT" ] if args[ "TIMEOUT" ] is not None else 120 |
| 90 | cmd = cmd + tcpIP + ":" + tcpPort + " in_port=" + \ |
| 91 | str( args[ "INPORT" ] ) + ",idle_timeout=" + str( |
| 92 | args[ "TIMEOUT" ] ) + ",actions=" + args[ "ACTION" ] |
| 93 | response = self.execute( cmd=cmd, prompt="\~\$", timeout=60 ) |
| 94 | if utilities.assert_matches( expect="openflow", actual=response, onpass="Flow Added Successfully", onfail="Adding Flow Failed!!!" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 95 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 96 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 97 | return main.FALSE |
| 98 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 99 | def showFlow( self, **flowParameters ): |
| 100 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 101 | showFlow dumps the flow entries of flow table using "dpctl" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 102 | """ |
| 103 | args = utilities.parse_args( [ "TCPIP", "TCPPORT" ], **flowParameters ) |
| 104 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 105 | tcpPort = args[ "TCPPORT" ] if args[ |
| 106 | "TCPPORT" ] is not None else "6634" |
| 107 | command = "dpctl show tcp:" + str( tcpIP ) + ":" + str( tcpPort ) |
| 108 | response = self.execute( |
| 109 | cmd=command, |
| 110 | prompt="get_config_reply", |
| 111 | timeout=240 ) |
| 112 | if utilities.assert_matches( expect='features_reply', actual=response, onpass="Show flow executed", onfail="Show flow execution Failed" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 113 | main.last_result = main.TRUE |
| 114 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 115 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 116 | main.last_result = main.FALSE |
| 117 | return main.FALSE |
| 118 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 119 | def dumpFlow( self, **flowParameters ): |
| 120 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 121 | dumpFlow gives installed flow information |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 122 | """ |
| 123 | args = utilities.parse_args( [ "TCPIP", "TCPPORT" ], **flowParameters ) |
| 124 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 125 | tcpPort = args[ "TCPPORT" ] if args[ |
| 126 | "TCPPORT" ] is not None else "6634" |
| 127 | command = "dpctl dump-flows tcp:" + str( tcpIP ) + ":" + str( tcpPort ) |
| 128 | response = self.execute( cmd=command, prompt="type=", timeout=240 ) |
| 129 | if utilities.assert_matches( expect='stats_reply', actual=response, onpass="Dump flow executed", onfail="Dump flow execution Failed" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 130 | main.last_result = main.TRUE |
| 131 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 132 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 133 | main.last_result = main.FALSE |
| 134 | return main.FALSE |
| 135 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 136 | def dumpTables( self, **flowParameters ): |
| 137 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 138 | dumpTables gives statistics for each of the flow tables used by datapath switch. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 139 | """ |
| 140 | args = utilities.parse_args( [ "TCPIP", "TCPPORT" ], **flowParameters ) |
| 141 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 142 | tcpPort = args[ "TCPPORT" ] if args[ |
| 143 | "TCPPORT" ] is not None else "6634" |
| 144 | command = "dpctl dump-tables tcp:" + \ |
| 145 | str( tcpIP ) + ":" + str( tcpPort ) |
| 146 | response = self.execute( cmd=command, prompt="matched", timeout=240 ) |
| 147 | if utilities.assert_matches( expect='lookup=3', actual=response, onpass="Dump Tables executed", onfail="Dump Tables execution Failed" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 148 | main.last_result = main.TRUE |
| 149 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 150 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 151 | main.last_result = main.FALSE |
| 152 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 153 | |
| 154 | def dumpPorts( self, **flowParameters ): |
| 155 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 156 | dumpPorts gives ports information |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 157 | """ |
| 158 | args = utilities.parse_args( [ "TCPIP", "TCPPORT" ], **flowParameters ) |
| 159 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 160 | tcpPort = args[ "TCPPORT" ] if args[ |
| 161 | "TCPPORT" ] is not None else "6634" |
| 162 | command = "dpctl dump-ports tcp:" + str( tcpIP ) + ":" + str( tcpPort ) |
| 163 | response = self.execute( cmd=command, prompt="rx pkts", timeout=240 ) |
| 164 | if utilities.assert_matches( expect='ports', actual=response, onpass="Dump Ports executed", onfail="Dump Ports execution Failed" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 165 | main.last_result = main.TRUE |
| 166 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 167 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 168 | main.last_result = main.FALSE |
| 169 | return main.FALSE |
| 170 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 171 | def dumpAggregate( self, **flowParameters ): |
| 172 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 173 | dumpAggregate gives installed flow information.ggregate statistics for flows in datapath WITCH's tables that match flows. |
| 174 | If flows is omitted, the statistics are aggregated across all flows in the datapath's flow tables |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 175 | """ |
| 176 | args = utilities.parse_args( |
| 177 | [ "TCPIP", "TCPPORT", "FLOW" ], **flowParameters ) |
| 178 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 179 | tcpPort = args[ "TCPPORT" ] if args[ |
| 180 | "TCPPORT" ] is not None else "6634" |
| 181 | flow = args[ "FLOW" ] if args[ "FLOW" ] is not None else "" |
| 182 | command = "dpctl dump-aggregate tcp:" + \ |
| 183 | str( tcpIP ) + ":" + str( tcpPort ) + " " + str( flow ) |
| 184 | response = self.execute( |
| 185 | cmd=command, |
| 186 | prompt="flow_count=", |
| 187 | timeout=240 ) |
| 188 | if utilities.assert_matches( expect='stats_reply', actual=response, onpass="Dump Aggregate executed", onfail="Dump Aggregate execution Failed" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | main.last_result = main.TRUE |
| 190 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 191 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 192 | main.last_result = main.FALSE |
| 193 | return main.FALSE |
| 194 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 195 | def delFlow( self, **flowParameters ): |
| 196 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 197 | delFlow Deletes entries from the datapath switch's tables that match flow |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 198 | """ |
| 199 | args = utilities.parse_args( |
| 200 | [ "TCPIP", "TCPPORT", "FLOW" ], **flowParameters ) |
| 201 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 202 | tcpPort = args[ "TCPPORT" ] if args[ |
| 203 | "TCPPORT" ] is not None else "6634" |
| 204 | flow = args[ "FLOW" ] if args[ "FLOW" ] is not None else "" |
| 205 | command = "dpctl del-flows tcp:" + \ |
| 206 | str( tcpIP ) + ":" + str( tcpPort ) + " " + str( flow ) |
| 207 | response = self.execute( |
| 208 | cmd=command, |
| 209 | prompt="ETH-Tutorial", |
| 210 | timeout=240 ) |
| 211 | if utilities.assert_matches( expect='@', actual=response, onpass="Delete flow executed", onfail="Delete flow execution Failed" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 212 | main.last_result = main.TRUE |
| 213 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 214 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 215 | main.last_result = main.FALSE |
| 216 | return main.FALSE |
| 217 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 218 | def show( self, **flowParameters ): |
| 219 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 220 | show gives information on datapath switch including information on its flow tables and ports. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 221 | """ |
| 222 | args = utilities.parse_args( [ "TCPIP", "TCPPORT" ], **flowParameters ) |
| 223 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 224 | tcpPort = args[ "TCPPORT" ] if args[ |
| 225 | "TCPPORT" ] is not None else "6634" |
| 226 | command = "dpctl show tcp:" + str( tcpIP ) + ":" + str( tcpPort ) |
| 227 | response = self.execute( |
| 228 | cmd=command, |
| 229 | prompt="miss_send_len=", |
| 230 | timeout=240 ) |
| 231 | if utilities.assert_matches( expect='get_config_reply', actual=response, onpass="show command executed", onfail="show command execution Failed" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 232 | main.last_result = main.TRUE |
| 233 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 234 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 235 | main.last_result = main.FALSE |
| 236 | return main.FALSE |
| 237 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 238 | def showStatus( self, **flowParameters ): |
| 239 | """ |
| 240 | showStatus gives a series of key-value pairs that report the status of switch. |
| 241 | If key is specified, only the key-value pairs whose key names begin with key are printed. |
| 242 | """ |
| 243 | args = utilities.parse_args( |
| 244 | [ "TCPIP", "TCPPORT", "KEY" ], **flowParameters ) |
| 245 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 246 | tcpPort = args[ "TCPPORT" ] if args[ |
| 247 | "TCPPORT" ] is not None else "6634" |
| 248 | key = args[ "KEY" ] if args[ "KEY" ] is not None else "" |
| 249 | command = "dpctl status tcp:" + \ |
| 250 | str( tcpIP ) + ":" + str( tcpPort ) + " " + key |
| 251 | response = self.execute( cmd=command, prompt="(.*)", timeout=240 ) |
| 252 | if utilities.assert_matches( expect='(.*)', actual=response, onpass="show command executed", onfail="show command execution Failed" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 253 | main.last_result = main.TRUE |
| 254 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 255 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 256 | main.last_result = main.FALSE |
| 257 | return main.FALSE |
| 258 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 259 | def desc_set( self, **flowParameters ): |
| 260 | """ |
| 261 | desc_set Sets the switch description ( as returned in ofp_desc_stats ) to string ( max length is DESC_STR_LEN ) |
| 262 | """ |
| 263 | args = utilities.parse_args( [ |
| 264 | "TCPIP", |
| 265 | "TCPPORT", |
| 266 | "STRING" ], |
| 267 | **flowParameters ) |
| 268 | |
| 269 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 270 | tcpPort = args[ "TCPPORT" ] if args[ |
| 271 | "TCPPORT" ] is not None else "6634" |
| 272 | string = " " + args[ "STRING" ] if args[ |
| 273 | "STRING" ] is not None else " DESC_STR_LEN" |
| 274 | command = "dpctl desc tcp:" + \ |
| 275 | str( tcpIP ) + ":" + str( tcpPort ) + str( string ) |
| 276 | response = self.execute( |
| 277 | cmd=command, |
| 278 | prompt="ETH-Tutorial", |
| 279 | timeout=240 ) |
| 280 | if utilities.assert_matches( expect='@', actual=response, onpass="desc command executed", onfail="desc command execution Failed" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 281 | main.last_result = main.TRUE |
| 282 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 283 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 284 | main.last_result = main.FALSE |
| 285 | return main.FALSE |
| 286 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 287 | def dumpDesc( self, **flowParameters ): |
| 288 | """ |
| 289 | dumpDesc Sets the switch description ( as returned in ofp_desc_stats ) to string ( max length is DESC_STR_LEN ) |
| 290 | """ |
| 291 | args = utilities.parse_args( [ |
| 292 | "TCPIP", |
| 293 | "TCPPORT", |
| 294 | "STRING" ], |
| 295 | **flowParameters ) |
| 296 | |
| 297 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 298 | tcpPort = args[ "TCPPORT" ] if args[ |
| 299 | "TCPPORT" ] is not None else "6634" |
| 300 | command = "dpctl dump-desc tcp:" + str( tcpIP ) + ":" + str( tcpPort ) |
| 301 | response = self.execute( |
| 302 | cmd=command, |
| 303 | prompt="Serial Num:", |
| 304 | timeout=240 ) |
| 305 | if utilities.assert_matches( expect='stats_reply', actual=response, onpass="desc command executed", onfail="desc command execution Failed" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 306 | main.last_result = main.TRUE |
| 307 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 308 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 309 | main.last_result = main.FALSE |
| 310 | return main.FALSE |
| 311 | |
| 312 | if __name__ != "__main__": |
| 313 | import sys |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 314 | sys.modules[ __name__ ] = DpctlCliDriver() |
| 315 | |