kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1 | """ |
| 2 | Driver for blank dataplane VMs. Created for SDNIP test. |
| 3 | """ |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 4 | import time |
| 5 | import pexpect |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 6 | import struct |
| 7 | import fcntl |
| 8 | import os |
| 9 | import sys |
| 10 | import signal |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 11 | import sys |
| 12 | import re |
| 13 | import json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 14 | sys.path.append( "../" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 15 | from drivers.common.clidriver import CLI |
| 16 | |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 17 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 18 | class DPCliDriver( CLI ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 19 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 20 | def __init__( self ): |
| 21 | super( CLI, self ).__init__() |
| 22 | |
| 23 | def connect( self, **connectargs ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 24 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 25 | vars( self )[ key ] = connectargs[ key ] |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 26 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 27 | self.name = self.options[ 'name' ] |
| 28 | self.handle = super( DPCliDriver, self ).connect( user_name=self.user_name, |
| 29 | ip_address=self.ip_address, |
| 30 | port=self.port, |
| 31 | pwd=self.pwd ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 32 | |
| 33 | if self.handle: |
| 34 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 35 | else: |
| 36 | main.log.info( "NO HANDLE" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 37 | return main.FALSE |
| 38 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 39 | def create_interfaces( self, net, number, start ): |
| 40 | """ |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 41 | Creates a number,specified by 'number,' of subinterfaces on eth0. Ip addresses start at 'net'.'start'.1.1 with a 24 bit netmask. Addresses increment sequentially in the third quad, |
| 42 | therefore all interfaces are in different subnets on the same machine. When the third quad reaches 255, it is reset to 1 and the second quad is incremented. |
| 43 | Every single ip address is placed in a file in /tmp titled 'ip_table{net}.txt' |
| 44 | The file is used by 'pingall_interfaces()' as a fping argument |
| 45 | This method returns true if all interfaces are created without a hitch, and false if a single interface has issues |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 46 | """ |
| 47 | self.handle.sendline( "" ) |
| 48 | self.handle.expect( "\$" ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 49 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 50 | self.handle.sendline( "rm /tmp/local_ip.txt" ) |
| 51 | self.handle.expect( "\$" ) |
| 52 | self.handle.sendline( "touch /tmp/local_ip.txt" ) |
| 53 | self.handle.expect( "\$" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 54 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 55 | main.log.info( "Creating interfaces" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 56 | k = 0 |
| 57 | intf = 0 |
| 58 | while number != 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 59 | k = k + 1 |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 60 | if k == 256: |
| 61 | k = 1 |
| 62 | start = start + 1 |
| 63 | number = number - 1 |
| 64 | intf = intf + 1 |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 65 | ip = net + "." + str( start ) + "." + str( k ) + ".1" |
| 66 | self.handle.sendline( |
| 67 | "sudo ifconfig eth0:" + str( |
| 68 | intf ) + " " + ip + " netmask 255.255.255.0" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 69 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 70 | i = self.handle.expect( [ |
| 71 | "\$", |
| 72 | "password", |
| 73 | pexpect.TIMEOUT, |
| 74 | pexpect.EOF ], |
| 75 | timeout=120 ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 76 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 77 | if i == 0: |
| 78 | self.handle.sendline( |
| 79 | "echo " + str( ip ) + " >> /tmp/local_ip.txt" ) |
| 80 | self.handle.expect( "\$" ) |
| 81 | elif i == 1: |
| 82 | main.log.info( "Sending sudo password" ) |
| 83 | self.handle.sendline( self.pwd ) |
| 84 | self.handle.expect( "\$" ) |
| 85 | else: |
| 86 | main.log.error( "INTERFACES NOT CREATED" ) |
| 87 | return main.FALSE |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 88 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 89 | def pingall_interfaces( self, netsrc, netstrt, netdst, destlogin, destip ): |
| 90 | """ |
| 91 | Copies the /tmp/ip_table{ net }.txt file from the machine you wish to ping, then runs fping with a source address of { netsrc }.{ netstrt }.1.1 on the copied file. |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 92 | Check every single response for reachable or unreachable. If all are reachable, function returns true. If a SINGLE host is unreachable, then the function stops and returns false |
| 93 | If fping is not installed, this function will install fping then run the same command |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 94 | """ |
| 95 | self.handle.sendline( "" ) |
| 96 | self.handle.expect( "\$" ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 97 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 98 | self.handle.sendline( "scp " + str( destlogin ) + "@" + |
| 99 | str( destip ) + ":/tmp/local_ip.txt /tmp/ip_table" + |
| 100 | str( net ) + ".txt" ) |
| 101 | |
| 102 | i = self.handle.expect( [ |
| 103 | "100%", |
| 104 | "password", |
| 105 | pexpect.TIMEOUT ], |
| 106 | timeout=30 ) |
| 107 | |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 108 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 109 | main.log.info( "Copied ping file successfully" ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 110 | elif i == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 111 | self.handle.sendline( self.pwd ) |
| 112 | self.handle.expect( "100%" ) |
| 113 | main.log.info( "Copied ping file successfully" ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 114 | elif i == 2: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 115 | main.log.error( "COULD NOT COPY PING FILE FROM " + str( destip ) ) |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 116 | result = main.FALSE |
| 117 | return result |
SeanCorcoran | f580d10 | 2013-09-27 15:13:21 -0700 | [diff] [blame] | 118 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 119 | self.handle.sendline( "" ) |
| 120 | self.handle.expect( "\$" ) |
| 121 | |
| 122 | main.log.info( "Pinging interfaces on the " + str( netdst ) + |
| 123 | " network from " + str( netsrc ) + "." + |
| 124 | str( netstrt ) + ".1.1" ) |
| 125 | self.handle.sendline( "sudo fping -S " + str( netsrc ) + "." + |
| 126 | str( netstrt ) + ".1.1 -f /tmp/ip_table" + |
| 127 | str( netdst ) + ".txt" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 128 | while 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 129 | i = self.handle.expect( [ |
| 130 | "reachable", |
| 131 | "unreachable", |
| 132 | "\$", |
| 133 | "password", |
| 134 | pexpect.TIMEOUT, |
| 135 | "not installed" ], |
| 136 | timeout=45 ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 137 | if i == 0: |
| 138 | result = main.TRUE |
| 139 | elif i == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 140 | main.log.error( "An interface was unreachable" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 141 | result = main.FALSE |
| 142 | return result |
| 143 | elif i == 2: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 144 | main.log.info( "All interfaces reachable" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 145 | return result |
| 146 | elif i == 3: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 147 | self.handle.sendline( self.pwd ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 148 | elif i == 4: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 149 | main.log.error( "Unable to fping" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 150 | result = main.FALSE |
| 151 | return result |
| 152 | elif i == 5: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 153 | main.log.info( "fping not installed, installing fping" ) |
| 154 | self.handle.sendline( "sudo apt-get install fping" ) |
| 155 | i = self.handle.expect( |
| 156 | [ "password", |
| 157 | "\$", |
| 158 | pexpect.TIMEOUT ], |
| 159 | timeout=60 ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 160 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 161 | self.handle.sendline( self.pwd ) |
| 162 | self.handle.expect( "\$", timeout=30 ) |
| 163 | main.log.info( "fping installed, now pinging interfaces" ) |
| 164 | self.handle.sendline( |
| 165 | "sudo fping -S " + str( |
| 166 | netsrc ) + "." + str( |
| 167 | netstrt ) + ".1.1 -f /tmp/ip_table" + str( |
| 168 | netdst ) + ".txt" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 169 | elif i == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 170 | main.log.info( "fping installed, now pinging interfaces" ) |
| 171 | self.handle.sendline( |
| 172 | "sudo fping -S " + str( |
| 173 | netsrc ) + "." + str( |
| 174 | netstrt ) + ".1.1 -f /tmp/ip_table" + str( |
| 175 | netdst ) + ".txt" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 176 | elif i == 2: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 177 | main.log.error( "Could not install fping" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 178 | result = main.FALSE |
| 179 | return result |
| 180 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 181 | def disconnect( self ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 182 | response = '' |
| 183 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 184 | self.handle.sendline( "exit" ) |
| 185 | self.handle.expect( "closed" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 186 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 187 | main.log.error( "Connection failed to the host" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 188 | response = main.FALSE |
| 189 | return response |
| 190 | |