Esempio n. 1
0
 def getPortDataConfig(cls,pid):
     try:
         isIp = False if pid < 256 else Converter.intIp2string(pid)
         searchName = 'slan0'+str(pid) if not isIp else cls._portNameByIP(isIp)
         ostPortInfo = list(filter(lambda ostPrt: searchName in ostPrt.name.lower() ,cls._PortConfigList.port[:]))[0]
         pIdList = ost_pb.PortIdList()
         pIdList.port_id.add().id = ostPortInfo.port_id.id
         pConfig = cls._drone.getPortConfig(pIdList)
     except Exception as e:
         raise Exception("Failed to assotiate ip to Nic... maybe wrong ip?\n"  + "\n" + str(e))
     return pIdList,pConfig
Esempio n. 2
0
 def _getPortDataConfigDebugLoopback(cls, pid):
     try:
         for p1 in cls._PortConfigList.port[:]:
             res = [p for p in cls._PortConfigList.port[:] if p.name == p1.name]
             if len(res) > 1:
                 continue
         ostPortInfo = res[pid]
         pIdList = ost_pb.PortIdList()
         pIdList.port_id.add().id = ostPortInfo.port_id.id
         pConfig = cls._drone.getPortConfig(pIdList)
     except Exception as e:
         raise Exception("Failed to assotiate ip to Nic... maybe wrong ip?\n"  + "\n" + str(e))
     return pIdList,pConfig
Esempio n. 3
0
    def getPortDataByIP(cls, ip):
        try:
            nicInfo = list(filter(lambda p : p.ips[0].ip == ip, cls._adaptersInfo))[0]

            PY3K = sys.version_info >= (3, 0)
            if PY3K:
                searchName = nicInfo.name.decode('utf-8')
            else:
                searchName = nicInfo.name

            ostPortInfo = list(filter(lambda ostPrt: searchName in ostPrt.name ,cls._PortConfigList.port[:]))[0]
            pIdList = ost_pb.PortIdList()
            pIdList.port_id.add().id = ostPortInfo.port_id.id
            pConfig = cls._drone.getPortConfig(pIdList)
        except Exception as e:
            raise Exception("Failed to assotiate ip to Nic... maybe wrong ip?\n" + ip + "\n" + str(e))
        return pIdList,pConfig
Esempio n. 4
0
class EthernetTrafficGenerator(TrafficGenerator):

    def __init__(self, drone_hostname, drone_ifname, src_mac, dest_mac):

        self.drone_ifname = drone_ifname
        self.src_mac = src_mac
        self.dest_mac = dest_mac
        self.stream_id = 0
        self.streams = []

        self.drone = DroneProxy(drone_hostname)
        try:
            self.drone.connect()
        except socket.error, e:
            if e[0] == errno.ECONNREFUSED:
                raise ConnectionRefusedException(
                    "Connection Refused - make sure that 'drone' is running at '%s' and the interface '%s' is up."
                % (drone_hostname, drone_ifname))
        time.sleep(1)
        self.tx_port = ost_pb.PortIdList()
        self.tx_port.port_id.add().id = self._get_portid_for_ifname(self.drone_ifname)
rx_port_number = 7

# setup logging
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

drone = DroneProxy(host_name, port_number)

try:
    # connect to drone
    log.info('connecting to drone(%s:%d)' %
             (drone.hostName(), drone.portNumber()))
    drone.connect()

    # setup tx port list
    tx_port = ost_pb.PortIdList()
    tx_port.port_id.add().id = tx_port_number
    rx_port = ost_pb.PortIdList()
    rx_port.port_id.add().id = rx_port_number

    stream_id = ost_pb.StreamIdList()
    stream_id.port_id.id = tx_port_number
    stream_id.stream_id.add().id = 1
    drone.addStream(stream_id)

    stream_cfg = ost_pb.StreamConfigList()
    stream_cfg.port_id.id = tx_port_number

    # stream 2 test
    # rx-port buff = drone.getCaptureBuffer(rx_port.port_id[0])
    stream_cfg = ost_pb.StreamConfigList()
Esempio n. 6
0
def main():
    # initialize defaults
    use_defaults = False
    host_name = '127.0.0.1'  # default drone host name
    tx_port_number = 0
    src_mac = 0x000c292eba20  # pkt src mac, modify it
    dst_mac = 0x18b169091010  # pkt dst mac, modify it
    src_ip = ipstr2long('192.168.168.2')  # pkt scr ip, modify it
    dst_ip = ipstr2long('192.168.166.2')  # pkt dst ip, modify it

    # compute total num of streams
    total_streams = sum([x[1] for x in ICMP_TYPE_CODE])
    print 'Total number of streams: %d' % total_streams

    # setup logging
    log = logging.getLogger(__name__)
    logging.basicConfig(level=logging.INFO)

    # command-line option/arg processing
    if len(sys.argv) > 1:
        if sys.argv[1] in ('-d', '--use-defaults'):
            use_defaults = True
        if sys.argv[1] in ('-h', '--help'):
            print('%s [OPTION]...' % (sys.argv[0]))
            print('Options:')
            print(' -d --use-defaults   run using default values')
            print(' -h --help           show this help')
            sys.exit(0)

    if not use_defaults:
        s = raw_input('Drone\'s Hostname/IP [%s]: ' % (host_name))
        host_name = s or host_name

    drone = DroneProxy(host_name)
    drone.TransmitMode = 'sequential'

    try:
        # connect to drone
        log.info('connecting to drone(%s:%d)' %
                 (drone.hostName(), drone.portNumber()))
        drone.connect()

        # retreive port id list
        log.info('retreiving port list')
        port_id_list = drone.getPortIdList()

        # retreive port config list
        log.info('retreiving port config for all ports')
        port_config_list = drone.getPortConfig(port_id_list)

        if len(port_config_list.port) == 0:
            log.warning('drone has no ports!')
            sys.exit(1)

        print('Port List')
        print('---------')
        print(port_config_list)
        for port in port_config_list.port:
            print('%d.%s (%s)' %
                  (port.port_id.id, port.name, port.description))
            # use a loopback port as default tx/rx port
            if ('lo' in port.name or 'loopback' in port.description.lower()):
                tx_port_number = port.port_id.id

        if not use_defaults:
            p = raw_input('Tx Port Id [%d]: ' % (tx_port_number))
            if p:
                tx_port_number = int(p)

        tx_port = ost_pb.PortIdList()
        tx_port.port_id.add().id = tx_port_number

        sid = 1
        for icmp_stream in ICMP_TYPE_CODE:
            stype = icmp_stream[0]
            for scode in range(icmp_stream[1]):
                # add a stream
                print 'icmp (type, code) = (%d, %d)' % (stype, scode)
                stream_id = ost_pb.StreamIdList()
                stream_id.port_id.CopyFrom(tx_port.port_id[0])
                stream_id.stream_id.add().id = sid
                log.info('adding tx_stream %d' % stream_id.stream_id[0].id)
                drone.addStream(stream_id)

                # configure the stream
                stream_cfg = ost_pb.StreamConfigList()
                stream_cfg.port_id.CopyFrom(tx_port.port_id[0])
                s = stream_cfg.stream.add()
                s.stream_id.id = stream_id.stream_id[0].id
                s.core.is_enabled = True
                s.control.num_packets = 1

                # setup stream protocols as mac:eth2:ip4:icmp
                # setup mac header
                p = s.protocol.add()
                p.protocol_id.id = ost_pb.Protocol.kMacFieldNumber
                p.Extensions[mac].dst_mac = dst_mac
                p.Extensions[mac].src_mac = src_mac

                p = s.protocol.add()
                p.protocol_id.id = ost_pb.Protocol.kEth2FieldNumber

                #setup ip header
                p = s.protocol.add()
                p.protocol_id.id = ost_pb.Protocol.kIp4FieldNumber
                ip = p.Extensions[ip4]
                ip.src_ip = src_ip
                ip.dst_ip = dst_ip
                ip.dst_ip_mode = Ip4.e_im_fixed

                # setup icmp header
                p = s.protocol.add()
                p.protocol_id.id = ost_pb.Protocol.kIcmpFieldNumber
                icmp_va = p.Extensions[icmp]
                icmp_va.icmp_version = Icmp.kIcmp4  # icmp version
                icmp_va.type = stype  # icmp type
                icmp_va.code = scode  # icmp code

                s.protocol.add(
                ).protocol_id.id = ost_pb.Protocol.kPayloadFieldNumber

                s.control.unit = ost_pb.StreamControl.e_su_packets  # Send: Packets
                s.control.mode = ost_pb.StreamControl.e_sm_fixed  # Mode: Fixed

                if sid < total_streams:
                    s.control.next = ost_pb.StreamControl.e_nw_goto_next
                else:
                    s.control.next = ost_pb.StreamControl.e_nw_goto_id  # e_nw_stop

                s.core.ordinal = sid - 1  # keep stream transmitted in order
                sid += 1
                s.control.packets_per_sec = 20  # transmit rate: pkts per second

                log.info('configuring tx_stream %d' %
                         stream_id.stream_id[0].id)
                drone.modifyStream(stream_cfg)

        log.info('clearing tx stats')
        drone.clearStats(tx_port)

        # start transmit
        log.info('starting transmit')
        drone.startTransmit(tx_port)

        # wait for transmit to finish
        log.info('waiting for transmit to finish ...')
        while True:
            try:
                time.sleep(5)
                tx_stats = drone.getStats(tx_port)
                if tx_stats.port_stats[0].state.is_transmit_on == False:
                    break
            except KeyboardInterrupt:
                log.info('Transmit terminated by user!!!')
                break

        # stop transmit and capture
        log.info('stopping transmit')
        drone.stopTransmit(tx_port)

        # get tx/rx stats
        log.info('retreiving stats')
        tx_stats = drone.getStats(tx_port)

        log.info('tx pkts = %d' % (tx_stats.port_stats[0].tx_pkts))

        # retrieve and dump received packets
        #log.info('getting Rx capture buffer')
        #drone.saveCaptureBuffer(buff, 'capture.pcap')
        #log.info('dumping Rx capture buffer')
        #os.system('tshark -r capture.pcap')
        #os.remove('capture.pcap')

        # delete streams
        log.info('deleting tx_stream %d' % stream_id.stream_id[0].id)
        drone.deleteStream(stream_id)
        # desconnect drone
        drone.disconnect()

    except Exception as ex:
        log.exception(ex)
        sys.exit(1)
Esempio n. 7
0
 def _fetch_stream_ids(self):
     o_port_ids = ost_pb.PortIdList()
     o_port_id = o_port_ids.port_id.add()
     o_port_id.id = self.port_id
     return self._drone.getStreamIdList(o_port_id)
Esempio n. 8
0
 def _get_o_port_id_list(self):
     o_port_ids = ost_pb.PortIdList()
     o_port_ids.port_id.add().id = self.port_id
     return o_port_ids