diff mbox series

[net-next,5/7] netdevsim: Simulate port function state for a PCI port

Message ID 20210206125551.8616-6-parav@nvidia.com
State Superseded
Headers show
Series netdevsim port add, delete support | expand

Commit Message

Parav Pandit Feb. 6, 2021, 12:55 p.m. UTC
Simulate port function state of a PCI port.
This enables users to get the state of the PCI port function.

Example of a PCI SF port which supports a port function hw_addr set:
Create a device with ID=10 and one physical port.
$ echo "10 1" > /sys/bus/netdevsim/new_device

Add PCI PF port:
$ devlink port add netdevsim/netdevsim10 flavour pcipf pfnum 2
netdevsim/netdevsim10/1: type eth netdev eth1 flavour pcipf controller 0 pfnum 2 external false splittable false
  function:
    hw_addr 00:00:00:00:00:00

$ devlink port add netdevsim/netdevsim10 flavour pcisf pfnum 2
netdevsim/netdevsim10/2: type eth netdev eth2 flavour pcisf controller 0 pfnum 2 sfnum 0 splittable false
  function:
    hw_addr 00:00:00:00:00:00

Show devlink port:
$ devlink port show netdevsim/netdevsim10/2
netdevsim/netdevsim10/2: type eth netdev eth2 flavour pcisf controller 0 pfnum 2 sfnum 0 splittable false
  function:
    hw_addr 00:00:00:00:00:00 state inactive opstate detached

Show the port and function attributes in JSON format:
$ devlink port show netdevsim/netdevsim10/2 -jp
{
    "port": {
        "netdevsim/netdevsim10/2": {
            "type": "eth",
            "netdev": "eth2",
            "flavour": "pcisf",
            "controller": 0,
            "pfnum": 2,
            "sfnum": 0,
            "splittable": false,
            "function": {
                "hw_addr": "00:00:00:00:00:00",
                "state": "inactive",
                "opstate": "detached"
            }
        }
    }
}

Signed-off-by: Parav Pandit <parav@nvidia.com>
---
 drivers/net/netdevsim/dev.c           |  1 +
 drivers/net/netdevsim/netdevsim.h     |  5 +++++
 drivers/net/netdevsim/port_function.c | 22 ++++++++++++++++++++++
 3 files changed, 28 insertions(+)
diff mbox series

Patch

diff --git a/drivers/net/netdevsim/dev.c b/drivers/net/netdevsim/dev.c
index ab15b5f7e955..9f2164ea89cd 100644
--- a/drivers/net/netdevsim/dev.c
+++ b/drivers/net/netdevsim/dev.c
@@ -909,6 +909,7 @@  static const struct devlink_ops nsim_dev_devlink_ops = {
 	.port_del = nsim_dev_devlink_port_del,
 	.port_function_hw_addr_get = nsim_dev_port_fn_hw_addr_get,
 	.port_function_hw_addr_set = nsim_dev_port_fn_hw_addr_set,
+	.port_fn_state_get = nsim_dev_port_fn_state_get,
 };
 
 #define NSIM_DEV_MAX_MACS_DEFAULT 32
diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h
index aafe2027a112..c0544d93e1e8 100644
--- a/drivers/net/netdevsim/netdevsim.h
+++ b/drivers/net/netdevsim/netdevsim.h
@@ -328,3 +328,8 @@  int nsim_dev_port_fn_hw_addr_set(struct devlink *devlink,
 				 struct devlink_port *port,
 				 const u8 *hw_addr, int hw_addr_len,
 				 struct netlink_ext_ack *extack);
+int nsim_dev_port_fn_state_get(struct devlink *devlink,
+			       struct devlink_port *port,
+			       enum devlink_port_fn_state *state,
+			       enum devlink_port_fn_opstate *opstate,
+			       struct netlink_ext_ack *extack);
diff --git a/drivers/net/netdevsim/port_function.c b/drivers/net/netdevsim/port_function.c
index d47cdf44770a..10c692b8aea2 100644
--- a/drivers/net/netdevsim/port_function.c
+++ b/drivers/net/netdevsim/port_function.c
@@ -17,6 +17,7 @@  struct nsim_port_fn {
 	u32 sfnum;
 	u16 pfnum;
 	u8 hw_addr[ETH_ALEN];
+	u8 state; /* enum devlink_port_fn_state */
 };
 
 static struct devlink_port *
@@ -248,6 +249,7 @@  static int nsim_devlink_port_fn_add(struct devlink *devlink,
 		goto pf_err;
 	}
 
+	port->state = DEVLINK_PORT_FN_STATE_INACTIVE;
 	err = devlink_port_register(devlink, &port->dl_port, port->port_index);
 	if (err)
 		goto reg_err;
@@ -482,3 +484,23 @@  int nsim_dev_port_fn_hw_addr_set(struct devlink *devlink,
 	memcpy(port->hw_addr, hw_addr, ETH_ALEN);
 	return 0;
 }
+
+int nsim_dev_port_fn_state_get(struct devlink *devlink,
+			       struct devlink_port *dl_port,
+			       enum devlink_port_fn_state *state,
+			       enum devlink_port_fn_opstate *opstate,
+			       struct netlink_ext_ack *extack)
+{
+	struct nsim_dev *nsim_dev = devlink_priv(devlink);
+	struct nsim_port_fn *port;
+
+	port = nsim_dev_to_port_fn(nsim_dev, dl_port, extack);
+	if (IS_ERR(port))
+		return PTR_ERR(port);
+	*state = port->state;
+	if (port->state == DEVLINK_PORT_FN_STATE_INACTIVE)
+		*opstate = DEVLINK_PORT_FN_OPSTATE_DETACHED;
+	else
+		*opstate = DEVLINK_PORT_FN_OPSTATE_ATTACHED;
+	return 0;
+}