Ejemplo n.º 1
0
def extract_RJ45_ports_index():
    # Cross check 'platform.json' and 'hwsku.json' to extract the RJ45 port index if exists.
    hwsku_path = device_info.get_path_to_hwsku_dir()
    platform_file = device_info.get_path_to_port_config_file()
    platform_dict = load_json_file(platform_file)['interfaces']
    hwsku_file = os.path.join(hwsku_path, HWSKU_JSON)
    hwsku_dict = load_json_file(hwsku_file)['interfaces']
    port_name_to_index_map_dict = {}
    RJ45_port_index_list = []

    # Compose a interface name to index mapping from 'platform.json'
    for i, (key, value) in enumerate(platform_dict.items()):
        if PORT_INDEX_KEY in value:
            index_raw = value[PORT_INDEX_KEY]
            # The index could be "1" or "1, 1, 1, 1"
            index = index_raw.split(',')[0]
            port_name_to_index_map_dict[key] = index

    if not bool(port_name_to_index_map_dict):
        return None

    # Check if "port_type" specified as "RJ45", if yes, add the port index to the list.
    for i, (key, value) in enumerate(hwsku_dict.items()):
        if key in port_name_to_index_map_dict and PORT_TYPE_KEY in value and value[
                PORT_TYPE_KEY] == RJ45_PORT_TYPE:
            RJ45_port_index_list.append(
                int(port_name_to_index_map_dict[key]) - 1)

    return RJ45_port_index_list if bool(RJ45_port_index_list) else None
Ejemplo n.º 2
0
def profile(profile):
    # Check if profile can be changed
    completed_process = subprocess.run(
        ['docker', 'exec', '-it', 'syncd', 'test', '-h', '/opt/bfn/install'])
    if completed_process.returncode != 0:
        click.echo('Cannot change profile: default one is in use')
        raise click.Abort()

    # Get chip family
    hwsku_dir = device_info.get_path_to_hwsku_dir()
    with open(hwsku_dir + '/switch-tna-sai.conf') as file:
        chip_family = json.load(file)['chip_list'][0]['chip_family'].lower()

    # Check if profile is supported
    if chip_family == 'tofino' and profile[0] == 'y' or \
        chip_family == 'tofino2' and profile[0] == 'x':
        click.echo('Specified profile is unsupported on the system')
        raise click.Abort()

    # Check if profile exists
    completed_process = subprocess.run([
        'docker', 'exec', '-it', 'syncd', 'test', '-d',
        '/opt/bfn/install_' + profile + '_profile'
    ])
    if completed_process.returncode != 0:
        click.echo('No profile with the provided name found')
        raise click.Abort()

    # Update configuration
    config_db = ConfigDBConnector()
    config_db.connect()
    config_db.mod_entry('DEVICE_METADATA', 'localhost',
                        {'p4_profile': profile + '_profile'})
    subprocess.run(['systemctl', 'restart', 'swss'], check=True)
Ejemplo n.º 3
0
def profile():
    # Check if profile can be changed
    completed_process = subprocess.run(
        ['docker', 'exec', '-it', 'syncd', 'test', '-h', '/opt/bfn/install'])
    if completed_process.returncode != 0:
        click.echo('Current profile: default')
        return

    # Get chip family
    hwsku_dir = device_info.get_path_to_hwsku_dir()
    with open(hwsku_dir + '/switch-tna-sai.conf') as file:
        chip_family = json.load(file)['chip_list'][0]['chip_family'].lower()

    # Print current profile
    click.echo('Current profile: ', nl=False)
    subprocess.run(
        'docker exec -it syncd readlink /opt/bfn/install | sed '
        r's/install_\\\(.\*\\\)_profile/\\1/',
        check=True,
        shell=True)

    # Exclude current and unsupported profiles
    opts = ''
    if chip_family == 'tofino':
        opts = r'\! -name install_y\*_profile '
    elif chip_family == 'tofino2':
        opts = r'\! -name install_x\*_profile '

    # Print profile list
    click.echo('Available profile(s):')
    subprocess.run('docker exec -it syncd find /opt/bfn -mindepth 1 '
                   r'-maxdepth 1 -type d -name install_\*_profile ' + opts +
                   '| sed '
                   r's%/opt/bfn/install_\\\(.\*\\\)_profile%\\1%',
                   shell=True)
Ejemplo n.º 4
0
def breakout(ctx):
    """Show Breakout Mode information by interfaces"""
    # Reading data from Redis configDb
    config_db = ConfigDBConnector()
    config_db.connect()
    ctx.obj = {'db': config_db}

    try:
        cur_brkout_tbl = config_db.get_table('BREAKOUT_CFG')
    except Exception as e:
        click.echo("Breakout table is not present in Config DB")
        raise click.Abort()

    if ctx.invoked_subcommand is None:
        # Get port capability from platform and hwsku related files
        hwsku_path = device_info.get_path_to_hwsku_dir()
        platform_file = device_info.get_path_to_port_config_file()
        platform_dict = readJsonFile(platform_file)['interfaces']
        hwsku_file = os.path.join(hwsku_path, HWSKU_JSON)
        hwsku_dict = readJsonFile(hwsku_file)['interfaces']

        if not platform_dict or not hwsku_dict:
            click.echo("Can not load port config from {} or {} file".format(
                platform_file, hwsku_file))
            raise click.Abort()

        for port_name in platform_dict:
            cur_brkout_mode = cur_brkout_tbl[port_name]["brkout_mode"]

            # Update deafult breakout mode and current breakout mode to platform_dict
            platform_dict[port_name].update(hwsku_dict[port_name])
            platform_dict[port_name]["Current Breakout Mode"] = cur_brkout_mode

            # List all the child ports if present
            child_port_dict = get_child_ports(port_name, cur_brkout_mode,
                                              platform_file)
            if not child_port_dict:
                click.echo(
                    "Cannot find ports from {} file ".format(platform_file))
                raise click.Abort()

            child_ports = natsorted(list(child_port_dict.keys()))

            children, speeds = [], []
            # Update portname and speed of child ports if present
            for port in child_ports:
                speed = config_db.get_entry('PORT', port).get('speed')
                if speed is not None:
                    speeds.append(str(int(speed) // 1000) + 'G')
                    children.append(port)

            platform_dict[port_name]["child ports"] = ",".join(children)
            platform_dict[port_name]["child port speeds"] = ",".join(speeds)

        # Sorted keys by name in natural sort Order for human readability
        parsed = OrderedDict((k, platform_dict[k])
                             for k in natsorted(list(platform_dict.keys())))
        click.echo(json.dumps(parsed, indent=4))