"Fossies" - the Fresh Open Source Software Archive

Member "openstack-cyborg-9.0.0/cyborg/accelerator/drivers/fpga/inspur/sysinfo.py" (5 Oct 2022, 5979 Bytes) of package /linux/misc/openstack/openstack-cyborg-9.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "sysinfo.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 8.0.0_vs_9.0.0.

    1 # Copyright 2020 Inspur, Inc.
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # a copy of the License at
    6 #
    7 #      http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 
   16 """
   17 Cyborg Inspur FPGA driver implementation.
   18 """
   19 
   20 import re
   21 
   22 from oslo_concurrency import processutils
   23 from oslo_serialization import jsonutils
   24 
   25 from cyborg.accelerator.common import utils
   26 from cyborg.common import constants
   27 from cyborg.conf import CONF
   28 from cyborg.objects.driver_objects import driver_attach_handle
   29 from cyborg.objects.driver_objects import driver_attribute
   30 from cyborg.objects.driver_objects import driver_controlpath_id
   31 from cyborg.objects.driver_objects import driver_deployable
   32 from cyborg.objects.driver_objects import driver_device
   33 import cyborg.privsep
   34 
   35 INSPUR_FPGA_FLAGS = ["Inspur Electronic Information Industry Co., Ltd.",
   36                      "Processing accelerators"]
   37 INSPUR_FPGA_INFO_PATTERN = re.compile(
   38     r"(?P<devices>[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:"
   39     r"[0-9a-fA-F]{2}\.[0-9a-fA-F]) "
   40     r"(?P<controller>.*) [\[].*]: (?P<model>.*) .*"
   41     r"[\[](?P<vendor_id>[0-9a-fA-F]"
   42     r"{4}):(?P<product_id>[0-9a-fA-F]{4})].*")
   43 
   44 VENDOR_ID = "1bd4"
   45 VENDOR_MAPS = {"1bd4": "inspur"}
   46 
   47 
   48 @cyborg.privsep.sys_admin_pctxt.entrypoint
   49 def lspci_privileged():
   50     cmd = ['lspci', '-nnn', '-D']
   51     return processutils.execute(*cmd)
   52 
   53 
   54 def get_pci_devices(pci_flags, vendor_id=None):
   55     device_for_vendor_out = []
   56     all_device_out = []
   57     lspci_out = lspci_privileged()[0].split('\n')
   58     for i in range(len(lspci_out)):
   59         if any(x in lspci_out[i] for x in pci_flags):
   60             all_device_out.append(lspci_out[i])
   61             if vendor_id and vendor_id in lspci_out[i]:
   62                 device_for_vendor_out.append(lspci_out[i])
   63     return device_for_vendor_out if vendor_id else all_device_out
   64 
   65 
   66 def get_traits(vendor_id, product_id):
   67     """Generate traits for FPGAs.
   68     : param vendor_id: vendor_id of FPGA, eg."1bd4"
   69     : param product_id: product_id of FPGA, eg."a115".
   70     Example FPGA traits:
   71     {traits:["CUSTOM_FPGA_INSPUR", "CUSTOM_FPGA_PRODUCT_ID_A115"]}
   72     """
   73     traits = []
   74     traits.append("CUSTOM_FPGA_" + VENDOR_MAPS.get(vendor_id, "").upper())
   75     traits.append("CUSTOM_FPGA_PRODUCT_ID_" + product_id.upper())
   76     # TODO(wenping) Currently we don't support program by Cyborg. The operator
   77     # can bind Inspur FPGA card to guest and then use it in guest. So the BSP
   78     # data reported is ignored here now.
   79     return {"traits": traits}
   80 
   81 
   82 def fpga_tree():
   83     fpga_list = []
   84     fpgas = get_pci_devices(INSPUR_FPGA_FLAGS, vendor_id=VENDOR_ID)
   85     for fpga in fpgas:
   86         m = INSPUR_FPGA_INFO_PATTERN.match(fpga)
   87         if m:
   88             fpga_dict = m.groupdict()
   89             # generate traits info
   90             traits = get_traits(
   91                 fpga_dict["vendor_id"], fpga_dict["product_id"])
   92             fpga_dict["rc"] = constants.RESOURCES["FPGA"]
   93             fpga_dict.update(traits)
   94             fpga_list.append(_generate_driver_device(fpga_dict))
   95     return fpga_list
   96 
   97 
   98 def _generate_driver_device(fpga):
   99     driver_device_obj = driver_device.DriverDevice()
  100     driver_device_obj.vendor = fpga["vendor_id"]
  101     driver_device_obj.model = fpga.get('model', 'miss model info')
  102     std_board_info = {'product_id': fpga.get('product_id', None),
  103                       'controller': fpga.get('controller', None)}
  104     vendor_board_info = {
  105         'vendor_info': fpga.get('vendor_info', 'fpga_vb_info')}
  106     driver_device_obj.std_board_info = jsonutils.dumps(std_board_info)
  107     driver_device_obj.vendor_board_info = jsonutils.dumps(vendor_board_info)
  108     driver_device_obj.type = constants.DEVICE_FPGA
  109     driver_device_obj.stub = fpga.get('stub', False)
  110     driver_device_obj.controlpath_id = _generate_controlpath_id(fpga)
  111     driver_device_obj.deployable_list = _generate_dep_list(fpga)
  112     return driver_device_obj
  113 
  114 
  115 def _generate_controlpath_id(fpga):
  116     driver_cpid = driver_controlpath_id.DriverControlPathID()
  117     driver_cpid.cpid_type = "PCI"
  118     driver_cpid.cpid_info = utils.pci_str_to_json(fpga["devices"])
  119     return driver_cpid
  120 
  121 
  122 def _generate_dep_list(fpga):
  123     dep_list = []
  124     driver_dep = driver_deployable.DriverDeployable()
  125     driver_dep.attribute_list = _generate_attribute_list(fpga)
  126     driver_dep.attach_handle_list = []
  127     # NOTE(wenping) Now simply named as <Compute_hostname>_<Device_address>
  128     # once cyborg needs to support Inspur FPGA devices discovered from a
  129     # baremetal node, we might need to support more formats.
  130     driver_dep.name = CONF.host + '_' + fpga["devices"]
  131     driver_dep.driver_name = VENDOR_MAPS.get(fpga["vendor_id"], '').upper()
  132     driver_dep.num_accelerators = 1
  133     driver_dep.attach_handle_list = [_generate_attach_handle(fpga)]
  134     dep_list.append(driver_dep)
  135     return dep_list
  136 
  137 
  138 def _generate_attribute_list(fpga):
  139     attr_list = []
  140     for k, v in fpga.items():
  141         if k == "rc":
  142             driver_attr = driver_attribute.DriverAttribute()
  143             driver_attr.key, driver_attr.value = k, v
  144             attr_list.append(driver_attr)
  145         if k == "traits":
  146             for index, val in enumerate(v):
  147                 driver_attr = driver_attribute.DriverAttribute(
  148                     key="trait" + str(index), value=val)
  149                 attr_list.append(driver_attr)
  150     return attr_list
  151 
  152 
  153 def _generate_attach_handle(fpga):
  154     driver_ah = driver_attach_handle.DriverAttachHandle()
  155     driver_ah.attach_type = constants.AH_TYPE_PCI
  156     driver_ah.attach_info = utils.pci_str_to_json(fpga["devices"])
  157     driver_ah.in_use = False
  158     return driver_ah