def load_platform_sfputil(): global platform_sfputil # Load platform module from source platform_path, _ = device_info.get_paths_to_platform_and_hwsku_dirs() try: module_file = os.path.join(platform_path, "plugins", PLATFORM_SPECIFIC_MODULE_NAME + ".py") module = imp.load_source(PLATFORM_SPECIFIC_MODULE_NAME, module_file) except IOError as e: log.log_error( "Failed to load platform module '%s': %s" % (PLATFORM_SPECIFIC_MODULE_NAME, str(e)), True) return -1 try: platform_sfputil_class = getattr(module, PLATFORM_SPECIFIC_CLASS_NAME) platform_sfputil = platform_sfputil_class() except AttributeError as e: log.log_error( "Failed to instantiate '%s' class: %s" % (PLATFORM_SPECIFIC_CLASS_NAME, str(e)), True) return -2 return 0
def cli(): """sfputil - Command line utility for managing SFP transceivers""" if os.geteuid() != 0: click.echo("Root privileges are required for this operation") sys.exit(1) # Load platform-specific sfputil class err = load_platform_sfputil() if err != 0: sys.exit(2) # Load port info try: if multi_asic.is_multi_asic(): # For multi ASIC platforms we pass DIR of port_config_file_path and the number of asics (platform_path, hwsku_path) = device_info.get_paths_to_platform_and_hwsku_dirs() # Load platform module from source platform_sfputil.read_all_porttab_mappings( hwsku_path, multi_asic.get_num_asics()) else: # For single ASIC platforms we pass port_config_file_path and the asic_inst as 0 port_config_file_path = device_info.get_path_to_port_config_file() platform_sfputil.read_porttab_mappings(port_config_file_path, 0) except Exception as e: log.log_error("Error reading port info (%s)" % str(e), True) sys.exit(3)
def import_ssd_api(diskdev): """ Loads platform specific or generic ssd_util module from source Raises an ImportError exception if none of above available Returns: Instance of the class with SSD API implementation (vendor or generic) """ # try to load platform specific module try: platform_path, _ = device_info.get_paths_to_platform_and_hwsku_dirs() platform_plugins_path = os.path.join(platform_path, "plugins") sys.path.append(os.path.abspath(platform_plugins_path)) from ssd_util import SsdUtil except ImportError as e: log.log_warning( "Platform specific SsdUtil module not found. Falling down to the generic implementation" ) try: from sonic_platform_base.sonic_ssd.ssd_generic import SsdUtil except ImportError as e: log.log_error( "Failed to import default SsdUtil. Error: {}".format(str(e)), True) raise e return SsdUtil(diskdev)
def load_platform_util(self, module_name, class_name): platform_util = None # Get path to platform and hwsku (platform_path, hwsku_path) = device_info.get_paths_to_platform_and_hwsku_dirs() try: module_file = os.path.join(platform_path, "plugins", module_name + ".py") module = imp.load_source(module_name, module_file) except IOError as e: raise IOError("Failed to load platform module '%s': %s" % (module_name, str(e))) try: platform_util_class = getattr(module, class_name) # board class of eeprom requires 4 paramerters, need special treatment here. if module_name == EEPROM_MODULE_NAME and class_name == EEPROM_CLASS_NAME: platform_util = platform_util_class('', '', '', '') else: platform_util = platform_util_class() except AttributeError as e: raise AttributeError("Failed to instantiate '%s' class: %s" % (class_name, str(e))) return platform_util
def platform_sfputil_read_porttab_mappings(): global platform_porttab_mapping_read if platform_porttab_mapping_read: return 0 try: if multi_asic.is_multi_asic(): # For multi ASIC platforms we pass DIR of port_config_file_path and the number of asics (platform_path, hwsku_path) = device_info.get_paths_to_platform_and_hwsku_dirs() # Load platform module from source platform_sfputil.read_all_porttab_mappings( hwsku_path, multi_asic.get_num_asics()) else: # For single ASIC platforms we pass port_config_file_path and the asic_inst as 0 port_config_file_path = device_info.get_path_to_port_config_file() platform_sfputil.read_porttab_mappings(port_config_file_path, 0) platform_porttab_mapping_read = True except Exception as e: click.echo("Error reading port info (%s)" % str(e)) sys.exit(1) return 0
def init_device_prefix(): platform_path, _ = device_info.get_paths_to_platform_and_hwsku_dirs() PLUGIN_PATH = "/".join([platform_path, "plugins", FILENAME]) if os.path.exists(PLUGIN_PATH): fp = open(PLUGIN_PATH, 'r') line = fp.readlines() SysInfoProvider.DEVICE_PREFIX = "/dev/" + line[0]
def init_device_prefix(): platform_path, _ = device_info.get_paths_to_platform_and_hwsku_dirs() UDEV_PREFIX_CONF_FILE_PATH = os.path.join(platform_path, UDEV_PREFIX_CONF_FILENAME) if os.path.exists(UDEV_PREFIX_CONF_FILE_PATH): fp = open(UDEV_PREFIX_CONF_FILE_PATH, 'r') lines = fp.readlines() SysInfoProvider.DEVICE_PREFIX = "/dev/" + lines[0].rstrip()
def load_platform_pcieutil(): global platform_pcieutil global platform_path # Load platform module from source try: platform_path, _ = device_info.get_paths_to_platform_and_hwsku_dirs() from sonic_platform_base.sonic_pcie.pcie_common import PcieUtil platform_pcieutil = PcieUtil(platform_path) except ImportError as e: log.log_error( "Failed to load default PcieUtil module. Error : {}".format( str(e)), True) raise e
def load_port_config(): try: if multi_asic.is_multi_asic(): # For multi ASIC platforms we pass DIR of port_config_file_path and the number of asics (platform_path, hwsku_path) = device_info.get_paths_to_platform_and_hwsku_dirs() # Load platform module from source platform_sfputil.read_all_porttab_mappings(hwsku_path, multi_asic.get_num_asics()) else: # For single ASIC platforms we pass port_config_file_path and the asic_inst as 0 port_config_file_path = device_info.get_path_to_port_config_file() platform_sfputil.read_porttab_mappings(port_config_file_path, 0) except Exception as e: log.log_error("Error reading port info ({})".format(str(e)), True) return False return True
def load_platform_pcieutil(): global platform_pcieutil global platform_plugins_path # Load platform module from source try: platform_path, _ = device_info.get_paths_to_platform_and_hwsku_dirs() platform_plugins_path = os.path.join(platform_path, "plugins") sys.path.append(os.path.abspath(platform_plugins_path)) from pcieutil import PcieUtil except ImportError as e: log.log_warning( "Failed to load platform-specific PcieUtil module. Falling back to the common implementation" ) try: from sonic_platform_base.sonic_pcie.pcie_common import PcieUtil platform_pcieutil = PcieUtil(platform_plugins_path) except ImportError as e: log.log_error( "Failed to load default PcieUtil module. Error : {}".format( str(e)), True) raise e
def init(self): global platform_sfputil global platform_chassis self.log_info("Start daemon init...") config_db, metadata_tbl, metadata_dict = {}, {}, {} is_vs = False namespaces = multi_asic.get_front_end_namespaces() for namespace in namespaces: asic_id = multi_asic.get_asic_index_from_namespace(namespace) config_db[asic_id] = daemon_base.db_connect("CONFIG_DB", namespace) metadata_tbl[asic_id] = swsscommon.Table(config_db[asic_id], "DEVICE_METADATA") (status, fvs) = metadata_tbl[0].get("localhost") if status is False: helper_logger.log_debug( "Could not retreive fieldvalue pairs for {}, inside config_db table {}" .format('localhost', metadata_tbl[0].getTableName())) return else: # Convert list of tuples to a dictionary metadata_dict = dict(fvs) if "platform" in metadata_dict: val = metadata_dict.get("platform", None) if val == "x86_64-kvm_x86_64-r0": is_vs = True # Load new platform api class try: if is_vs is False: import sonic_platform.platform platform_chassis = sonic_platform.platform.Platform( ).get_chassis() self.log_info("chassis loaded {}".format(platform_chassis)) # we have to make use of sfputil for some features # even though when new platform api is used for all vendors. # in this sense, we treat it as a part of new platform api. # we have already moved sfputil to sonic_platform_base # which is the root of new platform api. import sonic_platform_base.sonic_sfp.sfputilhelper platform_sfputil = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper( ) except Exception as e: self.log_warning("Failed to load chassis due to {}".format( repr(e))) # Load platform specific sfputil class if platform_chassis is None or platform_sfputil is None: if is_vs is False: try: platform_sfputil = self.load_platform_util( PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME) except Exception as e: self.log_error("Failed to load sfputil: {}".format(str(e)), True) sys.exit(SFPUTIL_LOAD_ERROR) if multi_asic.is_multi_asic(): # Load the namespace details first from the database_global.json file. swsscommon.SonicDBConfig.initializeGlobalConfig() # Load port info try: if multi_asic.is_multi_asic(): # For multi ASIC platforms we pass DIR of port_config_file_path and the number of asics (platform_path, hwsku_path ) = device_info.get_paths_to_platform_and_hwsku_dirs() platform_sfputil.read_all_porttab_mappings( hwsku_path, self.num_asics) else: # For single ASIC platforms we pass port_config_file_path and the asic_inst as 0 port_config_file_path = device_info.get_path_to_port_config_file( ) platform_sfputil.read_porttab_mappings(port_config_file_path, 0) except Exception as e: self.log_error("Failed to read port info: {}".format(str(e)), True) sys.exit(PORT_CONFIG_LOAD_ERROR) # Connect to STATE_DB and create ycable tables state_db = {} # Get the namespaces in the platform namespaces = multi_asic.get_front_end_namespaces() for namespace in namespaces: asic_id = multi_asic.get_asic_index_from_namespace(namespace) state_db[asic_id] = daemon_base.db_connect("STATE_DB", namespace) """ # TODO need to decide if we need warm start capability in this ycabled daemon warmstart = swsscommon.WarmStart() warmstart.initialize("ycabled", "pmon") warmstart.checkWarmStart("ycabled", "pmon", False) is_warm_start = warmstart.isWarmStart() """ # Make sure this daemon started after all port configured self.log_info("Wait for port config is done") # Init port y_cable status table y_cable_helper.init_ports_status_for_y_cable(platform_sfputil, platform_chassis, self.y_cable_presence, self.stop_event, is_vs)
FLOW_KEY = "flow_control" DEFAULT_BAUD = "9600" FILENAME = "udevprefix.conf" # QUIET == True => picocom will not output any messages, and pexpect will wait for console # switch login or command line to let user interact with shell # Downside: if console switch output ever does not match DEV_READY_MSG, program will think connection failed # QUIET == False => picocom will output messages - welcome message is caught by pexpect, so successful # connection will always lead to user interacting with shell # Downside: at end of session, picocom will print exit message, exposing picocom to user QUIET = False DEV_READY_MSG = r"([Ll]ogin:|[Pp]assword:|[$>#])" # login prompt or command line prompt TIMEOUT_SEC = 0.2 platform_path, _ = device_info.get_paths_to_platform_and_hwsku_dirs() PLUGIN_PATH = "/".join([platform_path, "plugins", FILENAME]) if os.path.exists(PLUGIN_PATH): fp = open(PLUGIN_PATH, 'r') line = fp.readlines() DEVICE_PREFIX = "/dev/" + line[0] # runs command, exit if stderr is written to and abort argument is ture, returns stdout, stderr otherwise # input: cmd (str, bool), output: output of cmd (str) and error of cmd (str) if abort is not true def run_command(cmd, abort=True): proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)