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 sys |
| 26 | from drivers.common.cli.toolsdriver import Tools |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 27 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 28 | |
| 29 | class DpctlCliDriver( Tools ): |
| 30 | |
| 31 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 32 | DpctlCliDriver driver class provides the basic functions of DPCTL controller |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 33 | """ |
| 34 | def __init__( self ): |
| 35 | super( DpctlCliDriver, self ).__init__() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 36 | self.handle = self |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 37 | self.wrapped = sys.modules[ __name__ ] |
| 38 | |
| 39 | def connect( self, **connectargs ): |
| 40 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 42 | vars( self )[ key ] = connectargs[ key ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 43 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 44 | self.name = self.options[ 'name' ] |
| 45 | |
Jon Hall | fd8ce43 | 2015-07-21 13:07:53 -0700 | [diff] [blame] | 46 | self.handle = super( |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 47 | DpctlCliDriver, self ).connect( user_name=self.user_name, |
| 48 | ip_address=self.ip_address, |
| 49 | port=None, |
| 50 | pwd=self.pwd ) |
| 51 | if self.handle: |
| 52 | main.log.info( "Connected to the host" ) |
| 53 | return main.TRUE |
| 54 | else: |
| 55 | main.log.error( |
| 56 | "Connection failed to the host " + |
| 57 | self.user_name + |
| 58 | "@" + |
| 59 | self.ip_address ) |
| 60 | return main.FALSE |
| 61 | |
| 62 | def addFlow( self, **flowParameters ): |
| 63 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 64 | addFlow create a new flow entry into flow table using "dpctl" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 65 | """ |
| 66 | args = utilities.parse_args( [ |
| 67 | "TCPIP", |
| 68 | "TCPPORT", |
| 69 | "INPORT", |
| 70 | "ACTION", |
| 71 | "TIMEOUT" ], |
| 72 | **flowParameters ) |
| 73 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 74 | cmd = "dpctl add-flow tcp:" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 75 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 76 | tcpPort = args[ "TCPPORT" ] if args[ |
| 77 | "TCPPORT" ] is not None else "6634" |
| 78 | timeOut = args[ "TIMEOUT" ] if args[ "TIMEOUT" ] is not None else 120 |
| 79 | cmd = cmd + tcpIP + ":" + tcpPort + " in_port=" + \ |
| 80 | str( args[ "INPORT" ] ) + ",idle_timeout=" + str( |
| 81 | args[ "TIMEOUT" ] ) + ",actions=" + args[ "ACTION" ] |
| 82 | response = self.execute( cmd=cmd, prompt="\~\$", timeout=60 ) |
| 83 | 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] | 84 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 85 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 86 | return main.FALSE |
| 87 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 88 | def showFlow( self, **flowParameters ): |
| 89 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 90 | showFlow dumps the flow entries of flow table using "dpctl" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 91 | """ |
| 92 | args = utilities.parse_args( [ "TCPIP", "TCPPORT" ], **flowParameters ) |
| 93 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 94 | tcpPort = args[ "TCPPORT" ] if args[ |
| 95 | "TCPPORT" ] is not None else "6634" |
| 96 | command = "dpctl show tcp:" + str( tcpIP ) + ":" + str( tcpPort ) |
| 97 | response = self.execute( |
| 98 | cmd=command, |
| 99 | prompt="get_config_reply", |
| 100 | timeout=240 ) |
| 101 | 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] | 102 | main.last_result = main.TRUE |
| 103 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 104 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 105 | main.last_result = main.FALSE |
| 106 | return main.FALSE |
| 107 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 108 | def dumpFlow( self, **flowParameters ): |
| 109 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 110 | dumpFlow gives installed flow information |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 111 | """ |
| 112 | args = utilities.parse_args( [ "TCPIP", "TCPPORT" ], **flowParameters ) |
| 113 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 114 | tcpPort = args[ "TCPPORT" ] if args[ |
| 115 | "TCPPORT" ] is not None else "6634" |
| 116 | command = "dpctl dump-flows tcp:" + str( tcpIP ) + ":" + str( tcpPort ) |
| 117 | response = self.execute( cmd=command, prompt="type=", timeout=240 ) |
| 118 | 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] | 119 | main.last_result = main.TRUE |
| 120 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 121 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 122 | main.last_result = main.FALSE |
| 123 | return main.FALSE |
| 124 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 125 | def dumpTables( self, **flowParameters ): |
| 126 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 127 | dumpTables gives statistics for each of the flow tables used by datapath switch. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 128 | """ |
| 129 | args = utilities.parse_args( [ "TCPIP", "TCPPORT" ], **flowParameters ) |
| 130 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 131 | tcpPort = args[ "TCPPORT" ] if args[ |
| 132 | "TCPPORT" ] is not None else "6634" |
| 133 | command = "dpctl dump-tables tcp:" + \ |
| 134 | str( tcpIP ) + ":" + str( tcpPort ) |
| 135 | response = self.execute( cmd=command, prompt="matched", timeout=240 ) |
| 136 | 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] | 137 | main.last_result = main.TRUE |
| 138 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 139 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 140 | main.last_result = main.FALSE |
| 141 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 142 | |
| 143 | def dumpPorts( self, **flowParameters ): |
| 144 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 145 | dumpPorts gives ports information |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 146 | """ |
| 147 | args = utilities.parse_args( [ "TCPIP", "TCPPORT" ], **flowParameters ) |
| 148 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 149 | tcpPort = args[ "TCPPORT" ] if args[ |
| 150 | "TCPPORT" ] is not None else "6634" |
| 151 | command = "dpctl dump-ports tcp:" + str( tcpIP ) + ":" + str( tcpPort ) |
| 152 | response = self.execute( cmd=command, prompt="rx pkts", timeout=240 ) |
| 153 | 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] | 154 | main.last_result = main.TRUE |
| 155 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 156 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 157 | main.last_result = main.FALSE |
| 158 | return main.FALSE |
| 159 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 160 | def dumpAggregate( self, **flowParameters ): |
| 161 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 162 | dumpAggregate gives installed flow information.ggregate statistics for flows in datapath WITCH's tables that match flows. |
| 163 | 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] | 164 | """ |
| 165 | args = utilities.parse_args( |
| 166 | [ "TCPIP", "TCPPORT", "FLOW" ], **flowParameters ) |
| 167 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 168 | tcpPort = args[ "TCPPORT" ] if args[ |
| 169 | "TCPPORT" ] is not None else "6634" |
| 170 | flow = args[ "FLOW" ] if args[ "FLOW" ] is not None else "" |
| 171 | command = "dpctl dump-aggregate tcp:" + \ |
| 172 | str( tcpIP ) + ":" + str( tcpPort ) + " " + str( flow ) |
| 173 | response = self.execute( |
| 174 | cmd=command, |
| 175 | prompt="flow_count=", |
| 176 | timeout=240 ) |
| 177 | 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] | 178 | main.last_result = main.TRUE |
| 179 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 180 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 181 | main.last_result = main.FALSE |
| 182 | return main.FALSE |
| 183 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 184 | def delFlow( self, **flowParameters ): |
| 185 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 186 | delFlow Deletes entries from the datapath switch's tables that match flow |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 187 | """ |
| 188 | args = utilities.parse_args( |
| 189 | [ "TCPIP", "TCPPORT", "FLOW" ], **flowParameters ) |
| 190 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 191 | tcpPort = args[ "TCPPORT" ] if args[ |
| 192 | "TCPPORT" ] is not None else "6634" |
| 193 | flow = args[ "FLOW" ] if args[ "FLOW" ] is not None else "" |
| 194 | command = "dpctl del-flows tcp:" + \ |
| 195 | str( tcpIP ) + ":" + str( tcpPort ) + " " + str( flow ) |
| 196 | response = self.execute( |
| 197 | cmd=command, |
| 198 | prompt="ETH-Tutorial", |
| 199 | timeout=240 ) |
| 200 | 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] | 201 | main.last_result = main.TRUE |
| 202 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 203 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 204 | main.last_result = main.FALSE |
| 205 | return main.FALSE |
| 206 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 207 | def show( self, **flowParameters ): |
| 208 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 209 | 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] | 210 | """ |
| 211 | args = utilities.parse_args( [ "TCPIP", "TCPPORT" ], **flowParameters ) |
| 212 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 213 | tcpPort = args[ "TCPPORT" ] if args[ |
| 214 | "TCPPORT" ] is not None else "6634" |
| 215 | command = "dpctl show tcp:" + str( tcpIP ) + ":" + str( tcpPort ) |
| 216 | response = self.execute( |
| 217 | cmd=command, |
| 218 | prompt="miss_send_len=", |
| 219 | timeout=240 ) |
| 220 | 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] | 221 | main.last_result = main.TRUE |
| 222 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 223 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 224 | main.last_result = main.FALSE |
| 225 | return main.FALSE |
| 226 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 227 | def showStatus( self, **flowParameters ): |
| 228 | """ |
| 229 | showStatus gives a series of key-value pairs that report the status of switch. |
| 230 | If key is specified, only the key-value pairs whose key names begin with key are printed. |
| 231 | """ |
| 232 | args = utilities.parse_args( |
| 233 | [ "TCPIP", "TCPPORT", "KEY" ], **flowParameters ) |
| 234 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 235 | tcpPort = args[ "TCPPORT" ] if args[ |
| 236 | "TCPPORT" ] is not None else "6634" |
| 237 | key = args[ "KEY" ] if args[ "KEY" ] is not None else "" |
| 238 | command = "dpctl status tcp:" + \ |
| 239 | str( tcpIP ) + ":" + str( tcpPort ) + " " + key |
| 240 | response = self.execute( cmd=command, prompt="(.*)", timeout=240 ) |
| 241 | 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] | 242 | main.last_result = main.TRUE |
| 243 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 244 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 245 | main.last_result = main.FALSE |
| 246 | return main.FALSE |
| 247 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 248 | def desc_set( self, **flowParameters ): |
| 249 | """ |
| 250 | desc_set Sets the switch description ( as returned in ofp_desc_stats ) to string ( max length is DESC_STR_LEN ) |
| 251 | """ |
| 252 | args = utilities.parse_args( [ |
| 253 | "TCPIP", |
| 254 | "TCPPORT", |
| 255 | "STRING" ], |
| 256 | **flowParameters ) |
| 257 | |
| 258 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 259 | tcpPort = args[ "TCPPORT" ] if args[ |
| 260 | "TCPPORT" ] is not None else "6634" |
| 261 | string = " " + args[ "STRING" ] if args[ |
| 262 | "STRING" ] is not None else " DESC_STR_LEN" |
| 263 | command = "dpctl desc tcp:" + \ |
| 264 | str( tcpIP ) + ":" + str( tcpPort ) + str( string ) |
| 265 | response = self.execute( |
| 266 | cmd=command, |
| 267 | prompt="ETH-Tutorial", |
| 268 | timeout=240 ) |
| 269 | 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] | 270 | main.last_result = main.TRUE |
| 271 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 272 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 273 | main.last_result = main.FALSE |
| 274 | return main.FALSE |
| 275 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 276 | def dumpDesc( self, **flowParameters ): |
| 277 | """ |
| 278 | dumpDesc Sets the switch description ( as returned in ofp_desc_stats ) to string ( max length is DESC_STR_LEN ) |
| 279 | """ |
| 280 | args = utilities.parse_args( [ |
| 281 | "TCPIP", |
| 282 | "TCPPORT", |
| 283 | "STRING" ], |
| 284 | **flowParameters ) |
| 285 | |
| 286 | tcpIP = args[ "TCPIP" ] if args[ "TCPIP" ] is not None else "127.0.0.1" |
| 287 | tcpPort = args[ "TCPPORT" ] if args[ |
| 288 | "TCPPORT" ] is not None else "6634" |
| 289 | command = "dpctl dump-desc tcp:" + str( tcpIP ) + ":" + str( tcpPort ) |
| 290 | response = self.execute( |
| 291 | cmd=command, |
| 292 | prompt="Serial Num:", |
| 293 | timeout=240 ) |
| 294 | 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] | 295 | main.last_result = main.TRUE |
| 296 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 297 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 298 | main.last_result = main.FALSE |
| 299 | return main.FALSE |
| 300 | |
| 301 | if __name__ != "__main__": |
| 302 | import sys |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 303 | sys.modules[ __name__ ] = DpctlCliDriver() |
| 304 | |