From b9984536aa5214c786e3510b61bf1cf0ccfe02dd Mon Sep 17 00:00:00 2001 From: Frank Du Date: Tue, 12 Dec 2023 16:52:47 +0800 Subject: [PATCH 1/3] python: introduce the build with swig helper Signed-off-by: Frank Du --- doc/run.md | 35 ++++++++++++++++++----- python/README.md | 62 ++++++++++++++++++++++++++++++++++++++++ python/example/sample.py | 6 ++++ python/swig/.gitignore | 7 +++++ python/swig/pymtl.i | 15 ++++++++++ python/swig/setup.py | 18 ++++++++++++ 6 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 python/README.md create mode 100644 python/example/sample.py create mode 100644 python/swig/.gitignore create mode 100644 python/swig/pymtl.i create mode 100644 python/swig/setup.py diff --git a/doc/run.md b/doc/run.md index 354b6c60e..070a3c274 100644 --- a/doc/run.md +++ b/doc/run.md @@ -14,15 +14,13 @@ ls -l /sys/kernel/iommu_groups/ The steps to enable IOMMU in your BIOS/UEFI may vary depending on the manufacturer and model of your motherboard. Here are general steps that should guide you: -```bash - 1. Restart your computer. During the boot process, you'll need to press a specific key to enter the BIOS/UEFI setup. This key varies depending on your system's manufacturer. It's often one of the function keys (like F2, F10, F12), the ESC key, or the DEL key. +1. Restart your computer. During the boot process, you'll need to press a specific key to enter the BIOS/UEFI setup. This key varies depending on your system's manufacturer. It's often one of the function keys (like F2, F10, F12), the ESC key, or the DEL key. - 2. Navigate to the advanced settings. Once you're in the BIOS/UEFI setup menu, look for a section with a name like "Advanced", "Advanced Options", or "Advanced Settings". +2. Navigate to the advanced settings. Once you're in the BIOS/UEFI setup menu, look for a section with a name like "Advanced", "Advanced Options", or "Advanced Settings". - 3. Look for IOMMU setting. Within the advanced settings, look for an option related to IOMMU. It might be listed under CPU Configuration or Chipset Configuration, depending on your system. For Intel systems, it's typically labeled as "VT-d" (Virtualization Technology for Directed I/O). Once you've located the appropriate option, change the setting to "Enabled". +3. Look for IOMMU setting. Within the advanced settings, look for an option related to IOMMU. It might be listed under CPU Configuration or Chipset Configuration, depending on your system. For Intel systems, it's typically labeled as "VT-d" (Virtualization Technology for Directed I/O). Once you've located the appropriate option, change the setting to "Enabled". - 4. Save your changes and exit. There will typically be an option to "Save & Exit" or "Save Changes and Reset". Select this to save your changes and restart the computer. -``` +4. Save your changes and exit. There will typically be an option to "Save & Exit" or "Save Changes and Reset". Select this to save your changes and restart the computer. ### 1.2 Enable IOMMU in kernel @@ -101,7 +99,12 @@ getent group 2110 || sudo groupadd -g 2110 vfio sudo usermod -aG vfio $USER ``` -Re-login and check the group successfully added using `groups`. +Re-login and check the group `vfio` successfully added using the command `groups`. + +```bash +groups +xxx sudo docker libvirt vfio +``` Create or edit a udev rules file, for example, /etc/udev/rules.d/10-vfio.rules, with your preferred text editor. For instance, using vim: @@ -168,6 +171,24 @@ Bind 0000:af:01.5(enp175s0f0v5) to vfio-pci success Create VFs on PF bdf: 0000:af:00.0 enp175s0f0 succ ``` +And please verify that the newly created VFIO device is correctly assigned to the vfio group as specified by your udev rules from section `### 3.1 Allow current user to access /dev/vfio/* devices`, use the `ls -l /dev/vfio/*` command and below is sample output: + +```bash +ls -l /dev/vfio/* +crw-rw---- 1 root vfio 235, 0 12月 12 09:34 /dev/vfio/162 +crw-rw---- 1 root vfio 235, 2 12月 12 09:34 /dev/vfio/163 +crw-rw---- 1 root vfio 235, 3 12月 12 09:34 /dev/vfio/164 +crw-rw---- 1 root vfio 235, 4 12月 12 09:34 /dev/vfio/165 +crw-rw---- 1 root vfio 235, 5 12月 12 09:34 /dev/vfio/166 +crw-rw---- 1 root vfio 235, 6 12月 12 09:34 /dev/vfio/167 +crw-rw---- 1 root vfio 235, 1 12月 12 09:35 /dev/vfio/168 +crw-rw---- 1 root vfio 235, 7 12月 12 09:35 /dev/vfio/169 +crw-rw---- 1 root vfio 235, 8 12月 12 09:35 /dev/vfio/170 +crw-rw---- 1 root vfio 235, 9 12月 12 09:35 /dev/vfio/171 +crw-rw---- 1 root vfio 235, 10 12月 12 09:35 /dev/vfio/172 +crw-rw---- 1 root vfio 235, 11 12月 12 09:35 /dev/vfio/173 +``` + If the creation of VF BDFs fails, you can check the kernel dmesg log to find possible reasons for the failure. The dmesg log contains valuable information that can help identify any issues or errors related to the VF creation process. Please review the dmesg log for any relevant messages or error codes that can provide insights into why the creation of VF BDFs was unsuccessful. ```bash diff --git a/python/README.md b/python/README.md new file mode 100644 index 000000000..db8ddc6cf --- /dev/null +++ b/python/README.md @@ -0,0 +1,62 @@ +# The Python support + +IMTL leverage SWIG, found at , to transform C APIs into a binding layer that Python can utilize. + +## 1. Build and install swig + +Following the build and installation guide from the SWIG GitHub repository at . + +Below are the example steps to build the `v4.1.1` release. Replace the tag with a newer one if there is a more recent release of SWIG available: + +```bash +git clone https://github.com/swig/swig.git +cd swig/ +git checkout v4.1.1 +git log +./autogen.sh +./configure +make +sudo make install +``` + +## 2. Build and install IMTL python binding layer + +### 2.1 Create IMTL binding layer code based on swig + +```bash +cd $imtl_source_code/python/swig/ +swig -python -I/usr/local/include pymtl.i +``` + +If you encounter the error `mtl.i:15: Error: Unable to find 'mtl/mtl_api.h'`, this is typically due to an incorrect include path. Use the following command to locate the correct path for `mtl_api.h`: + +```bash +find /usr/ -name mtl_api.h +``` + +Once you have obtained the correct path, you can update your SWIG interface file or your build configuration to reference the correct location of `mtl_api.h`. + +### 2.2 Build + +```bash +python3 setup.py build_ext --inplace +``` + +### 2.3 Install + +```bash +sudo python3 setup.py install +``` + +Checking the log to see the path installed. + +```bash +creating /usr/local/lib/python3.10/dist-packages/pymtl-0.1-py3.10-linux-x86_64.egg +``` + +## 3. Run python example code + +```bash +cd $imtl_source_code/python/example/ +python3 ../example/sample.py +``` diff --git a/python/example/sample.py b/python/example/sample.py new file mode 100644 index 000000000..0bb4f1e16 --- /dev/null +++ b/python/example/sample.py @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright 2023 Intel Corporation + +import _pymtl as pymtl + +print(f"mtl_version: {pymtl.mtl_version()}") diff --git a/python/swig/.gitignore b/python/swig/.gitignore new file mode 100644 index 000000000..d701a8450 --- /dev/null +++ b/python/swig/.gitignore @@ -0,0 +1,7 @@ +# generated file +*.c +*.so +pymtl.py +build +dist +pymtl.egg-info diff --git a/python/swig/pymtl.i b/python/swig/pymtl.i new file mode 100644 index 000000000..dc9016ca3 --- /dev/null +++ b/python/swig/pymtl.i @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright 2023 Intel Corporation + +// Usage: swig -python -I/usr/local/include mtl.i + +%module pymtl + +%{ +#include "mtl/mtl_api.h" +%} + +%define __MTL_LIB_BUILD__ +%enddef + +%include "mtl/mtl_api.h" \ No newline at end of file diff --git a/python/swig/setup.py b/python/swig/setup.py new file mode 100644 index 000000000..fee345374 --- /dev/null +++ b/python/swig/setup.py @@ -0,0 +1,18 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright 2023 Intel Corporation + +from setuptools import setup, Extension + +mtl_module = Extension( + '_pymtl', + sources=['pymtl_wrap.c'], + # include_dirs=['/path/to/include'], + # library_dirs=['/path/to/lib'], + libraries=['mtl'] +) + +setup( + name='pymtl', + version='0.1', + ext_modules=[mtl_module], +) \ No newline at end of file From 67955f7e57b28b4c9d737fc0c2c02bedd388f4da Mon Sep 17 00:00:00 2001 From: Frank Du Date: Wed, 13 Dec 2023 09:00:12 +0800 Subject: [PATCH 2/3] lint Signed-off-by: Frank Du --- README.md | 16 ++++++++++++++++ doc/run.md | 24 ++++++++---------------- python/swig/setup.py | 12 ++++++------ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index a4fa9e165..f771a0193 100644 --- a/README.md +++ b/README.md @@ -128,12 +128,28 @@ If you do not want the main branch automatically synced to the upstream, please We use the super-linter action for style checks. +#### 7.2.1 C/C++ + For C/C++ coding, you can run the following command to quickly fix the style: ```bash ./format-coding.sh ``` +#### 7.2.2 Python + +For Python, `black` formatter is used. + +```bash +sudo pip install black +``` + +```bash +black python/ +``` + +#### 7.2.3 Others + For other languages, please check with the following example command inside the Docker container: ```bash diff --git a/doc/run.md b/doc/run.md index 070a3c274..eb831a10e 100644 --- a/doc/run.md +++ b/doc/run.md @@ -171,22 +171,14 @@ Bind 0000:af:01.5(enp175s0f0v5) to vfio-pci success Create VFs on PF bdf: 0000:af:00.0 enp175s0f0 succ ``` -And please verify that the newly created VFIO device is correctly assigned to the vfio group as specified by your udev rules from section `### 3.1 Allow current user to access /dev/vfio/* devices`, use the `ls -l /dev/vfio/*` command and below is sample output: - -```bash -ls -l /dev/vfio/* -crw-rw---- 1 root vfio 235, 0 12月 12 09:34 /dev/vfio/162 -crw-rw---- 1 root vfio 235, 2 12月 12 09:34 /dev/vfio/163 -crw-rw---- 1 root vfio 235, 3 12月 12 09:34 /dev/vfio/164 -crw-rw---- 1 root vfio 235, 4 12月 12 09:34 /dev/vfio/165 -crw-rw---- 1 root vfio 235, 5 12月 12 09:34 /dev/vfio/166 -crw-rw---- 1 root vfio 235, 6 12月 12 09:34 /dev/vfio/167 -crw-rw---- 1 root vfio 235, 1 12月 12 09:35 /dev/vfio/168 -crw-rw---- 1 root vfio 235, 7 12月 12 09:35 /dev/vfio/169 -crw-rw---- 1 root vfio 235, 8 12月 12 09:35 /dev/vfio/170 -crw-rw---- 1 root vfio 235, 9 12月 12 09:35 /dev/vfio/171 -crw-rw---- 1 root vfio 235, 10 12月 12 09:35 /dev/vfio/172 -crw-rw---- 1 root vfio 235, 11 12月 12 09:35 /dev/vfio/173 +And please verify that the newly created VFIO device is correctly assigned to the vfio group as specified by your udev rules from section `### 3.1 Allow current user to access /dev/vfio/* devices`, use the `ls -lg /dev/vfio/*` command and below is sample output: + +```bash +ls -lg /dev/vfio/* +crw-rw---- 1 vfio 235, 0 12月 12 09:34 /dev/vfio/162 +crw-rw---- 1 vfio 235, 2 12月 12 09:34 /dev/vfio/163 +crw-rw---- 1 vfio 235, 3 12月 12 09:34 /dev/vfio/164 +crw-rw---- 1 vfio 235, 4 12月 12 09:34 /dev/vfio/165 ``` If the creation of VF BDFs fails, you can check the kernel dmesg log to find possible reasons for the failure. The dmesg log contains valuable information that can help identify any issues or errors related to the VF creation process. Please review the dmesg log for any relevant messages or error codes that can provide insights into why the creation of VF BDFs was unsuccessful. diff --git a/python/swig/setup.py b/python/swig/setup.py index fee345374..237d1406f 100644 --- a/python/swig/setup.py +++ b/python/swig/setup.py @@ -4,15 +4,15 @@ from setuptools import setup, Extension mtl_module = Extension( - '_pymtl', - sources=['pymtl_wrap.c'], + "_pymtl", + sources=["pymtl_wrap.c"], # include_dirs=['/path/to/include'], # library_dirs=['/path/to/lib'], - libraries=['mtl'] + libraries=["mtl"], ) setup( - name='pymtl', - version='0.1', + name="pymtl", + version="0.1", ext_modules=[mtl_module], -) \ No newline at end of file +) From 9bdc44d4df664b443d0d41d71c5bff27415ed78a Mon Sep 17 00:00:00 2001 From: Frank Du Date: Wed, 13 Dec 2023 09:12:59 +0800 Subject: [PATCH 3/3] lint for isort Signed-off-by: Frank Du --- README.md | 4 +++- python/swig/setup.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f771a0193..7372beca1 100644 --- a/README.md +++ b/README.md @@ -138,14 +138,16 @@ For C/C++ coding, you can run the following command to quickly fix the style: #### 7.2.2 Python -For Python, `black` formatter is used. +For Python, `black` and `isort` formatter is used. ```bash sudo pip install black +sudo pip install isort ``` ```bash black python/ +isort python/ ``` #### 7.2.3 Others diff --git a/python/swig/setup.py b/python/swig/setup.py index 237d1406f..0572b10de 100644 --- a/python/swig/setup.py +++ b/python/swig/setup.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: BSD-3-Clause # Copyright 2023 Intel Corporation -from setuptools import setup, Extension +from setuptools import Extension, setup mtl_module = Extension( "_pymtl",