In a virtual environment, the network controller may have to configure
some SR-IOV VF parameters for security reasons.

When the PF (host port) is driven by DPDK (OVS-DPDK case),
we face two different cases:
        - driver is bifurcated (Mellanox case),
          so the VF can be configured via the kernel.
        - driver is on top of UIO or VFIO, so DPDK API is required,
          and PMD-specific APIs were used.
This new generic API will avoid vendors fragmentation.

In order to target a VF (which has no port ID in the host),
the higher bit of port ID is reserved to be used
with port representor ID in existing functions.
Summary:
        representor ID + VF bit == VF ID

If a function is not expected to do VF configuration,
or if the port does not control any VF configuration,
it returns -EINVAL or -ENODEV.
If a function can do VF configuration,
but the PMD does not support it, then -ENOTSUP should be returned.
The port can allow the use of the VF bit per function
by adding the implementation to its vf_ops.

The new macro RTE_ETH_VALID_ID_OR_ERR_RET must be called
instead of RTE_ETH_VALID_PORTID_OR_ERR_RET
to allow the use of the VF bit.

The new macro CALL_OP_OR_ERR_RET can be used to help
calling the right function in dev_ops or vf_ops,
depending on the VF bit.

No feature is enabled in this commit.

Signed-off-by: Thomas Monjalon <tho...@monjalon.net>
---
 lib/librte_ethdev/rte_ethdev.c           | 44 ++++++++++++++++++++++--
 lib/librte_ethdev/rte_ethdev.h           | 38 ++++++++++++++++++++
 lib/librte_ethdev/rte_ethdev_core.h      |  1 +
 lib/librte_ethdev/rte_ethdev_version.map |  1 +
 4 files changed, 81 insertions(+), 3 deletions(-)

diff --git a/lib/librte_ethdev/rte_ethdev.c b/lib/librte_ethdev/rte_ethdev.c
index 7743205d38..fb3da4dcc3 100644
--- a/lib/librte_ethdev/rte_ethdev.c
+++ b/lib/librte_ethdev/rte_ethdev.c
@@ -593,11 +593,36 @@ rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
 int
 rte_eth_dev_is_valid_port(uint16_t port_id)
 {
+       /* legacy behaviour - without VF flag */
+       return rte_eth_dev_is_valid(port_id, 0);
+}
+
+static uint16_t
+port_id_parse(uint16_t port_id, bool *is_vf)
+{
+       *is_vf = (port_id & RTE_ETH_PORT_VF_FLAG) != 0;
+       return port_id & RTE_ETH_PORT_ID_MASK;
+}
+
+int
+rte_eth_dev_is_valid(uint16_t port_id, char allow_vf)
+{
+       bool is_vf;
+
+       port_id = port_id_parse(port_id, &is_vf);
+       if (is_vf && !allow_vf)
+               return 0; /* unallowed VF */
+
        if (port_id >= RTE_MAX_ETHPORTS ||
            (rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED))
-               return 0;
-       else
-               return 1;
+               return 0; /* invalid port */
+
+       if (!is_vf)
+               return 1; /* valid port */
+
+       if (rte_eth_devices[port_id].vf_ops == NULL)
+               return 0; /* VF flag applies only to port controlling a VF */
+       return 2; /* VF connected to a valid port on the host */
 }
 
 static int
@@ -851,6 +876,19 @@ eth_err(uint16_t port_id, int ret)
        return ret;
 }
 
+static inline const struct eth_dev_ops *
+eth_dev_ops_get(const struct rte_eth_dev *dev, bool is_vf)
+{
+       if (is_vf)
+               return dev->vf_ops;
+       return dev->dev_ops;
+}
+
+#define ETH_DEV_OP_CALL(dev, vf, op, ...) ({ \
+       RTE_FUNC_PTR_OR_ERR_RET((eth_dev_ops_get(dev, vf)->op), -ENOTSUP); \
+       (eth_dev_ops_get(dev, vf)->op)(dev, ## __VA_ARGS__); \
+})
+
 static int
 rte_eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
 {
diff --git a/lib/librte_ethdev/rte_ethdev.h b/lib/librte_ethdev/rte_ethdev.h
index c36c1b631f..a410a195ce 100644
--- a/lib/librte_ethdev/rte_ethdev.h
+++ b/lib/librte_ethdev/rte_ethdev.h
@@ -1345,6 +1345,13 @@ struct rte_eth_dcb_info {
 #define RTE_ETH_ALL RTE_MAX_ETHPORTS
 
 /* Macros to check for valid port */
+#define RTE_ETH_VALID_ID_OR_ERR_RET(port_id, retval) do { \
+       if (!rte_eth_dev_is_valid(port_id, 1)) { \
+               RTE_ETHDEV_LOG(ERR, "Invalid port_id=%u\n", port_id); \
+               return retval; \
+       } \
+} while (0)
+
 #define RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, retval) do { \
        if (!rte_eth_dev_is_valid_port(port_id)) { \
                RTE_ETHDEV_LOG(ERR, "Invalid port_id=%u\n", port_id); \
@@ -1468,6 +1475,17 @@ struct rte_eth_dev_owner {
 /** Device does not support MAC change after started */
 #define RTE_ETH_DEV_NOLIVE_MAC_ADDR  0x0020
 
+/**
+ * Highest bit of port ID is reserved for targeting controlled VF.
+ * This bit can be combined with the port ID of a representor
+ * which implements some vf_ops.
+ * The meaning is to target the VF connected with the representor port
+ * instead of the representor port itself.
+ */
+#define RTE_ETH_PORT_VF_FLAG (1 << 15)
+/** Mask to get representor port ID from VF ID, excluding VF flag. */
+#define RTE_ETH_PORT_ID_MASK (RTE_ETH_PORT_VF_FLAG - 1)
+
 /**
  * Iterates over valid ethdev ports owned by a specific owner.
  *
@@ -1909,6 +1927,26 @@ int rte_eth_dev_socket_id(uint16_t port_id);
  */
 int rte_eth_dev_is_valid_port(uint16_t port_id);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Check if port_id of device is attached.
+ * The port_id can represent a VF connected to port
+ * implementing some vf_ops.
+ *
+ * @param port_id
+ *   The port identifier of the Ethernet device.
+ * @param allow_vf
+ *   The bit RTE_ETH_PORT_VF_FLAG is considered valid.
+ * @return
+ *   - 0 if port is not attached or unallowed VF
+ *   - 1 if device is attached and not representing a VF
+ *   - 2 if is a remote VF connected to a port implementing vf_ops
+ */
+__rte_experimental
+int rte_eth_dev_is_valid(uint16_t port_id, char allow_vf);
+
 /**
  * Start specified RX queue of a port. It is used when rx_deferred_start
  * flag of the specified queue is true.
diff --git a/lib/librte_ethdev/rte_ethdev_core.h 
b/lib/librte_ethdev/rte_ethdev_core.h
index 392aea8e6b..46bc01926d 100644
--- a/lib/librte_ethdev/rte_ethdev_core.h
+++ b/lib/librte_ethdev/rte_ethdev_core.h
@@ -682,6 +682,7 @@ struct rte_eth_dev {
        struct rte_eth_dev_data *data;  /**< Pointer to device data. */
        void *process_private; /**< Pointer to per-process device data. */
        const struct eth_dev_ops *dev_ops; /**< Functions exported by PMD */
+       const struct eth_dev_ops *vf_ops; /**< Functions for VF control */
        struct rte_device *device; /**< Backing device */
        struct rte_intr_handle *intr_handle; /**< Device interrupt handle */
        /** User application callbacks for NIC interrupts */
diff --git a/lib/librte_ethdev/rte_ethdev_version.map 
b/lib/librte_ethdev/rte_ethdev_version.map
index e59d51648f..09670d4bb3 100644
--- a/lib/librte_ethdev/rte_ethdev_version.map
+++ b/lib/librte_ethdev/rte_ethdev_version.map
@@ -285,6 +285,7 @@ EXPERIMENTAL {
        rte_eth_read_clock;
 
        # added in 19.11
+       rte_eth_dev_is_valid;
        rte_eth_rx_burst_mode_get;
        rte_eth_tx_burst_mode_get;
        rte_eth_burst_mode_option_name;
-- 
2.23.0

Reply via email to