Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: introduce the build with swig helper #639

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions doc/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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/*
frankdjx marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
62 changes: 62 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# The Python support

IMTL leverage SWIG, found at <https://github.com/swig/swig/tree/master>, 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 <https://github.com/swig/swig/tree/master>.

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
```
6 changes: 6 additions & 0 deletions python/example/sample.py
Original file line number Diff line number Diff line change
@@ -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()}")
7 changes: 7 additions & 0 deletions python/swig/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# generated file
*.c
*.so
pymtl.py
build
dist
pymtl.egg-info
15 changes: 15 additions & 0 deletions python/swig/pymtl.i
Original file line number Diff line number Diff line change
@@ -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"
18 changes: 18 additions & 0 deletions python/swig/setup.py
Original file line number Diff line number Diff line change
@@ -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],
)