Skip to content

Commit

Permalink
opae.io: add vf_token subcommand
Browse files Browse the repository at this point in the history
Add the vf_token subcommand to set the PCI Virtual Function (VF)
token when the vfio-pci driver is bound to the Physical Function
(PF).

Signed-off-by: Matthew Gerlach <matthew.gerlach@linux.intel.com>
  • Loading branch information
matthew-gerlach committed Jul 12, 2023
1 parent 3b20a50 commit 41c854e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
24 changes: 24 additions & 0 deletions binaries/opae.io/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#pragma once

#include <iostream>
#include <sys/ioctl.h>
#include <opae/vfio.h>


Expand Down Expand Up @@ -215,6 +217,28 @@ struct vfio_device {

return b;
}

#define VFIO_VF_TOKEN_LEN (16)
int set_vf_token(const char *vf_token)
{
struct vfio_device_feature *df;
int ret;

df = (struct vfio_device_feature *)
new uint8_t[sizeof(struct vfio_device_feature) + VFIO_VF_TOKEN_LEN];

df->argsz = sizeof(df) + VFIO_VF_TOKEN_LEN;
df->flags = VFIO_DEVICE_FEATURE_SET | VFIO_DEVICE_FEATURE_PCI_VF_TOKEN;
memcpy(df->data, vf_token, VFIO_VF_TOKEN_LEN);

ret = ioctl(v_->device.device_fd, VFIO_DEVICE_FEATURE, df);
delete df;

if (ret)
std::cerr << "ioctl failed " << errno << std::endl;

return ret;
}
private:
opae_vfio *v_;
vfio_device(opae_vfio *v)
Expand Down
26 changes: 26 additions & 0 deletions binaries/opae.io/pymain.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import os
import pdb
import sys
import libvfio
import uuid
from opae.io import utils, pci
from opae.io.utils import Path
Expand Down Expand Up @@ -148,6 +149,29 @@ class poke_action(base_action):
wr(args.offset, args.value)
raise SystemExit(0)
class vf_token_action(base_action):
open_device = True
def add_args(self):
self.parser.add_argument('vftoken', default=None)
def execute(self, args):
if not self.device:
raise SystemExit('Need device for poke.')
try:
token_uuid = uuid.UUID(args.vftoken)
ret = self.device.set_vf_token(token_uuid.bytes)
if ret:
print('Failed to set token')
raise SystemExit(1)
print(f'Successfully set vf token to {str(token_uuid)}')
except ValueError:
print('Invalid vf_token')
raise SystemExit(1)
raise SystemExit(0)
class script_action(base_action):
open_device = True
Expand Down Expand Up @@ -218,6 +242,7 @@ actions = {
'poke': poke_action,
'walk': walk_action,
'dump': dump_action,
'vf_token': vf_token_action,
}
def do_action(action, args):
Expand All @@ -244,6 +269,7 @@ def show_help():
"opae.io init [-d <PCI_ADDRESS>] <USER>[:<GROUP>]"
"opae.io release [-d <PCI_ADDRESS>]"
"opae.io [-d <PCI_ADDRESS>]"
"opae.io [-d <PCI_ADDRESS>] vf_token <GUID>"
"opae.io [-d <PCI_ADDRESS>] [-r <REGION_NUMBER>] [-a <ACCESS_MODE>]"
"opae.io [-d <PCI_ADDRESS>] [-r <REGION_NUMBER>] [-a <ACCESS_MODE>] walk [<OFFSET>] [-u | --show-uuid]"
"opae.io [-d <PCI_ADDRESS>] [-r <REGION_NUMBER>] [-a <ACCESS_MODE>] dump [<OFFSET>] [-o | --output <FILE>] [-f | --format (hex, bin)] [ -c | --count <WORD COUNT>]
Expand Down
1 change: 1 addition & 0 deletions binaries/opae.io/vfiobindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ PYBIND11_MODULE(libvfio, m)
.def("config_write8", &vfio_device::config_write<uint8_t>)
.def("__repr__", &vfio_device::address)
.def("allocate", &vfio_device::buffer_allocate)
.def("set_vf_token", &vfio_device::set_vf_token)
.def_property_readonly("pci_address", &vfio_device::address)
.def_property_readonly("num_regions", &vfio_device::num_regions)
.def_property_readonly("regions", &vfio_device::regions);
Expand Down

0 comments on commit 41c854e

Please sign in to comment.