Exemple #1
0
    def annotate(self, log_func=None, desc=None, show_status=True):

        if not log_func:
            log_func = self.prn_func

        if desc:
            log_func(format_text('\n{:<60}'.format(desc), 'bold'),
                     newline=False)
        else:
            log_func("")

        if self.bad():
            # print all the errors
            print("")
            for x in self.rc_list:
                if not x.rc:
                    log_func(format_text("\n{0}".format(x.data), 'bold'))

            print("")
            if show_status:
                log_func(format_text("[FAILED]\n", 'red', 'bold'))

        else:
            if show_status:
                log_func(format_text("[SUCCESS]\n", 'green', 'bold'))
Exemple #2
0
    def generate_port_status(self):

        info = self.get_formatted_info()

        return {"driver":           info['driver'],
                "description":      info.get('description', 'N/A')[:18],
                "src MAC":          info['src_mac'],
                "src IPv4":         info['src_ipv4'],
                "Destination":      format_text("{0}".format(info['dest']), 'bold',
                                                'red' if info['dest'] == 'unconfigured' else None),
                "ARP Resolution":   format_text("{0}".format(info['arp']), 'bold',
                                                'red' if info['arp'] == 'unresolved' else None),
                "VLAN":             format_text("{0}".format(info['vlan']), *('bold', 'magenta')
                                                if info['vlan'] != '-' else ''),
                "PCI Address":      info['pci_addr'],
                "NUMA Node":        info['numa'],
                "--": "",
                "---": "",
                "----": "",
                "-----": "",
                "------": "",
                "link speed": "%g Gb/s" % info['speed'],
                "port status": info['status'],
                "link status": info['link'],
                "promiscuous": info['prom'],
                "multicast": info['mult'],
                "flow ctrl": info['fc'],

                "layer mode": format_text(info['layer_mode'], 'green' if info['layer_mode']
                                          == 'IPv4' else 'magenta'),
                "RX Filter Mode": info['rx_filter_mode'],
                "RX Queueing": info['rx_queue'],
                "Grat ARP": info['grat_arp'],

                }
Exemple #3
0
def test_multi_core(r, options):

    for core_count in range(1, 9):
        r.run(input_list=options.input_file,
              outfile='generated/{0}.cap'.format(core_count),
              dp_core_count=core_count,
              is_debug=(not options.release),
              pkt_limit=options.limit,
              mult=options.mult,
              duration=options.duration,
              mode='none',
              silent=True,
              tunables=options.tunables)

    print("")

    for core_count in range(1, 9):
        print(
            (format_text("comparing {0} cores to 1 core:\n".format(core_count),
                         'underline')))
        rc = compare_caps_strict('generated/1.cap',
                                 'generated/{0}.cap'.format(core_count))
        if rc:
            print("[Passed]\n")

    return
Exemple #4
0
def compare_caps(cap1, cap2, max_diff_sec=(5 * 1e-6)):
    pkts1 = list(RawPcapReader(cap1))
    pkts2 = list(RawPcapReader(cap2))

    if len(pkts1) != len(pkts2):
        print(('{0} contains {1} packets vs. {2} contains {3} packets'.format(
            cap1, len(pkts1), cap2, len(pkts2))))
        return False

    # to be less strict we define equality if all packets from cap1 exists and in cap2
    # and vice versa
    # 'exists' means the same packet with abs(TS1-TS2) < 5nsec
    # its O(n^2) but who cares, right ?
    for i, pkt1 in enumerate(pkts1):
        ts1 = float(pkt1[1][0]) + (float(pkt1[1][1]) / 1e6)
        found = None
        for j, pkt2 in enumerate(pkts2):
            ts2 = float(pkt2[1][0]) + (float(pkt2[1][1]) / 1e6)

            if abs(ts1 - ts2) > max_diff_sec:
                break

            if pkt1[0] == pkt2[0]:
                found = j
                break

        if found is None:
            print((format_text(
                "cannot find packet #{0} from {1} in {2}\n".format(
                    i, cap1, cap2), 'bold')))
            return False
        else:
            del pkts2[found]

    return True
Exemple #5
0
 def __str__(self):
     if self.good():
         s = ""
         for x in self.rc_list:
             if x.data:
                 s += format_text("\n{0}".format(x.data), 'bold')
         return s
     else:
         show_count = 10
         err_list = []
         err_count = 0
         for x in filter(len, listify(self.err())):
             err_count += 1
             if len(err_list) < show_count:
                 err_list.append(format_text(x, 'bold'))
         s = ''
         if err_count > show_count:
             s += format_text(
                 'Occurred %s errors, showing first %s:\n' %
                 (err_count, show_count), 'bold')
         s += '\n'.join(err_list)
         return s
Exemple #6
0
def compare_caps_strict(cap1, cap2, max_diff_sec=(5 * 1e-6)):
    pkts1 = list(RawPcapReader(cap1))
    pkts2 = list(RawPcapReader(cap2))

    if len(pkts1) != len(pkts2):
        print(('{0} contains {1} packets vs. {1} contains {2} packets'.format(
            cap1, len(pkts1), cap2, len(pkts2))))
        return False

    # a strict check
    for pkt1, pkt2, i in zip(pkts1, pkts2, list(range(1, len(pkts1)))):
        ts1 = float(pkt1[1][0]) + (float(pkt1[1][1]) / 1e6)
        ts2 = float(pkt2[1][0]) + (float(pkt2[1][1]) / 1e6)

        if abs(ts1 - ts2) > 0.000005:  # 5 nsec
            print((format_text(
                "TS error: cap files '{0}', '{1}' differ in cap #{2} - '{3}' vs. '{4}'\n"
                .format(cap1, cap2, i, ts1, ts2), 'bold')))
            return False

        if pkt1[0] != pkt2[0]:
            print((format_text(
                "RAW error: cap files '{0}', '{1}' differ in cap #{2}\n".
                format(cap1, cap2, i), 'bold')))

            diff_list = hexdiff(pkt1[0], pkt2[0])

            print(("{0} - packet #{1}:\n".format(cap1, i)))
            prettyhex(pkt1[0], diff_list)

            print(("\n{0} - packet #{1}:\n".format(cap2, i)))
            prettyhex(pkt2[0], diff_list)

            print("")
            return False

    return True
Exemple #7
0
    def print_profile(self, mult, duration):
        if not self.get_profile():
            return

        rate = self.get_profile()['rate']
        graph = self.get_profile()['graph']

        print(format_text("Profile Map Per Port\n", 'underline', 'bold'))

        factor = mult_to_factor(
            mult, rate['max_bps_l2'], rate['max_pps'], rate['max_line_util'])

        print("Profile max BPS L2   (base / req):   {:^12} / {:^12}"
              .format(format_num(rate['max_bps_l2'], suffix="bps"),
                      format_num(rate['max_bps_l2'] * factor, suffix="bps")))

        print("Profile max BPS L1   (base / req):   {:^12} / {:^12}"
              .format(format_num(rate['max_bps_l1'], suffix="bps"),
                      format_num(rate['max_bps_l1'] * factor, suffix="bps")))

        print("Profile max PPS      (base / req):   {:^12} / {:^12}"
              .format(format_num(rate['max_pps'], suffix="pps"),
                      format_num(rate['max_pps'] * factor, suffix="pps"),))

        print("Profile line util.   (base / req):   {:^12} / {:^12}"
              .format(format_percentage(rate['max_line_util']),
                      format_percentage(rate['max_line_util'] * factor)))

        # duration
        exp_time_base_sec = graph['expected_duration'] / (1000 * 1000)
        exp_time_factor_sec = exp_time_base_sec / factor

        # user configured a duration
        if duration > 0:
            if exp_time_factor_sec > 0:
                exp_time_factor_sec = min(exp_time_factor_sec, duration)
            else:
                exp_time_factor_sec = duration

        print("Duration             (base / req):   {:^12} / {:^12}"
              .format(format_time(exp_time_base_sec),
                      format_time(exp_time_factor_sec)))
        print("\n")
Exemple #8
0
    def __str__(self):
        self.tb = traceback.extract_tb(sys.exc_info()[2])
        if not self.tb:
            return self.msg

        s = format_text("\n******\n", 'bold')
        s += format_text("\nException stack(most recent call last):\n\n",
                         'underline')

        for i, line in enumerate(self.tb):
            fname, lineno, src = os.path.split(
                line[0])[1], line[1], line[2], line[3]
            s += "#{:<2}    {:<50} - '{}'\n".format(
                len(self.tb) - i - 1,
                format_text(fname, 'bold') + ':' + format_text(lineno, 'bold'),
                format_text(src.strip(), 'bold'))

        s += format_text('\nSummary error message:\n\n', 'underline')
        s += format_text(self.msg + '\n', 'bold')

        return s
Exemple #9
0
    def run(self,
            input_list,
            outfile=None,
            dp_core_count=1,
            dp_core_index=None,
            is_debug=True,
            pkt_limit=5000,
            mult="1",
            duration=-1,
            mode='none',
            silent=False,
            tunables=None):

        if mode not in [
                'none', 'gdb', 'valgrind', 'json', 'yaml', 'pkt', 'native'
        ]:
            raise STLArgumentError('mode', mode)

        # listify
        input_list = input_list if isinstance(input_list,
                                              list) else [input_list]

        # check streams arguments
        if not all([isinstance(i, (STLStream, str)) for i in input_list]):
            raise STLArgumentError('input_list', input_list)

        # split to two type
        input_files = [x for x in input_list if isinstance(x, str)]
        stream_list = [x for x in input_list if isinstance(x, STLStream)]

        # handle YAMLs
        if tunables is None:
            tunables = {}

        for input_file in input_files:
            try:
                if 'direction' not in tunables:
                    tunables['direction'] = self.port_id % 2

                profile = STLProfile.load(input_file, **tunables)

            except STLError as e:
                s = format_text(
                    "\nError while loading profile '{0}'\n".format(input_file),
                    'bold')
                s += "\n" + e.brief()
                raise STLError(s)

            stream_list += profile.get_streams()

        # load streams
        cmds_json = []

        id_counter = 1

        lookup = {}

        # allocate IDs
        for stream in stream_list:
            if stream.get_id() is not None:
                stream_id = stream.get_id()
            else:
                stream_id = id_counter
                id_counter += 1

            name = stream.get_name() if stream.get_name() is not None else id(
                stream)
            if name in lookup:
                raise STLError(
                    "multiple streams with name: '{0}'".format(name))
            lookup[name] = stream_id

        # resolve names
        for stream in stream_list:

            name = stream.get_name() if stream.get_name() is not None else id(
                stream)
            stream_id = lookup[name]

            next_id = -1
            next = stream.get_next()
            if next:
                if next not in lookup:
                    raise STLError(
                        "stream dependency error - unable to find '{0}'".
                        format(next))
                next_id = lookup[next]

            stream_json = stream.to_json()
            stream_json['next_stream_id'] = next_id

            cmd = {
                "id": 1,
                "jsonrpc": "2.0",
                "method": "add_stream",
                "params": {
                    "handler": self.handler,
                    "api_h": self.api_h,
                    "port_id": self.port_id,
                    "stream_id": stream_id,
                    "stream": stream_json
                }
            }

            cmds_json.append(cmd)

        # generate start command
        cmds_json.append(
            self.generate_start_cmd(mult=mult, force=True, duration=duration))

        if mode == 'json':
            print((json.dumps(cmds_json,
                              indent=4,
                              separators=(',', ': '),
                              sort_keys=True)))
            return
        elif mode == 'yaml':
            print((STLProfile(stream_list).dump_to_yaml()))
            return
        elif mode == 'pkt':
            print((STLProfile(stream_list).dump_as_pkt()))
            return
        elif mode == 'native':
            print((STLProfile(stream_list).dump_to_code()))
            return

        # start simulation
        self.outfile = outfile
        self.dp_core_count = dp_core_count
        self.dp_core_index = dp_core_index
        self.is_debug = is_debug
        self.pkt_limit = pkt_limit
        self.mult = mult
        self.duration = duration,
        self.mode = mode
        self.silent = silent

        self.__run(cmds_json)