Skip to content

Commit b9a17ab

Browse files
jacob-kellerdavem330
authored andcommitted
devlink: implement DEVLINK_CMD_REGION_NEW
Implement support for the DEVLINK_CMD_REGION_NEW command for creating snapshots. This new command parallels the existing DEVLINK_CMD_REGION_DEL. In order for DEVLINK_CMD_REGION_NEW to work for a region, the new ".snapshot" operation must be implemented in the region's ops structure. The desired snapshot id must be provided. This helps avoid confusion on the purpose of DEVLINK_CMD_REGION_NEW, and keeps the API simpler. The requested id will be inserted into the xarray tracking the number of snapshots using each id. If this id is already used by another snapshot on any region, an error will be returned. Signed-off-by: Jacob Keller <jacob.e.keller@intel.com> Reviewed-by: Jiri Pirko <jiri@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1210243 commit b9a17ab

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

‎Documentation/networking/devlink/devlink-region.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ address regions that are otherwise inaccessible to the user.
2020
Regions may also be used to provide an additional way to debug complex error
2121
states, but see also :doc:`devlink-health`
2222

23+
Regions may optionally support capturing a snapshot on demand via the
24+
``DEVLINK_CMD_REGION_NEW`` netlink message. A driver wishing to allow
25+
requested snapshots must implement the ``.snapshot`` callback for the region
26+
in its ``devlink_region_ops`` structure.
27+
2328
example usage
2429
-------------
2530

@@ -40,6 +45,9 @@ example usage
4045
# Delete a snapshot using:
4146
$ devlink region del pci/0000:00:05.0/cr-space snapshot 1
4247
48+
# Request an immediate snapshot, if supported by the region
49+
$ devlink region new pci/0000:00:05.0/cr-space snapshot 5
50+
4351
# Dump a snapshot:
4452
$ devlink region dump pci/0000:00:05.0/fw-health snapshot 1
4553
0000000000000000 0014 95dc 0014 9514 0035 1670 0034 db30

‎include/net/devlink.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,16 @@ struct devlink_info_req;
501501
* struct devlink_region_ops - Region operations
502502
* @name: region name
503503
* @destructor: callback used to free snapshot memory when deleting
504+
* @snapshot: callback to request an immediate snapshot. On success,
505+
* the data variable must be updated to point to the snapshot data.
506+
* The function will be called while the devlink instance lock is
507+
* held.
504508
*/
505509
struct devlink_region_ops {
506510
const char *name;
507511
void (*destructor)(const void *data);
512+
int (*snapshot)(struct devlink *devlink, struct netlink_ext_ack *extack,
513+
u8 **data);
508514
};
509515

510516
struct devlink_fmsg;

‎net/core/devlink.c‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3845,6 +3845,33 @@ static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
38453845
}
38463846
}
38473847

3848+
/**
3849+
* __devlink_snapshot_id_insert - Insert a specific snapshot ID
3850+
* @devlink: devlink instance
3851+
* @id: the snapshot id
3852+
*
3853+
* Mark the given snapshot id as used by inserting a zero value into the
3854+
* snapshot xarray.
3855+
*
3856+
* This must be called while holding the devlink instance lock. Unlike
3857+
* devlink_snapshot_id_get, the initial reference count is zero, not one.
3858+
* It is expected that the id will immediately be used before
3859+
* releasing the devlink instance lock.
3860+
*
3861+
* Returns zero on success, or an error code if the snapshot id could not
3862+
* be inserted.
3863+
*/
3864+
static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
3865+
{
3866+
lockdep_assert_held(&devlink->lock);
3867+
3868+
if (WARN_ON(xa_load(&devlink->snapshot_ids, id)))
3869+
return -EEXIST;
3870+
3871+
return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
3872+
GFP_KERNEL));
3873+
}
3874+
38483875
/**
38493876
* __devlink_region_snapshot_id_get - get snapshot ID
38503877
* @devlink: devlink instance
@@ -4038,6 +4065,71 @@ static int devlink_nl_cmd_region_del(struct sk_buff *skb,
40384065
return 0;
40394066
}
40404067

4068+
static int
4069+
devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
4070+
{
4071+
struct devlink *devlink = info->user_ptr[0];
4072+
struct devlink_region *region;
4073+
const char *region_name;
4074+
u32 snapshot_id;
4075+
u8 *data;
4076+
int err;
4077+
4078+
if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) {
4079+
NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
4080+
return -EINVAL;
4081+
}
4082+
4083+
if (!info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
4084+
NL_SET_ERR_MSG_MOD(info->extack, "No snapshot id provided");
4085+
return -EINVAL;
4086+
}
4087+
4088+
region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4089+
region = devlink_region_get_by_name(devlink, region_name);
4090+
if (!region) {
4091+
NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
4092+
return -EINVAL;
4093+
}
4094+
4095+
if (!region->ops->snapshot) {
4096+
NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
4097+
return -EOPNOTSUPP;
4098+
}
4099+
4100+
if (region->cur_snapshots == region->max_snapshots) {
4101+
NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
4102+
return -ENOSPC;
4103+
}
4104+
4105+
snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4106+
4107+
if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
4108+
NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
4109+
return -EEXIST;
4110+
}
4111+
4112+
err = __devlink_snapshot_id_insert(devlink, snapshot_id);
4113+
if (err)
4114+
return err;
4115+
4116+
err = region->ops->snapshot(devlink, info->extack, &data);
4117+
if (err)
4118+
goto err_snapshot_capture;
4119+
4120+
err = __devlink_region_snapshot_create(region, data, snapshot_id);
4121+
if (err)
4122+
goto err_snapshot_create;
4123+
4124+
return 0;
4125+
4126+
err_snapshot_create:
4127+
region->ops->destructor(data);
4128+
err_snapshot_capture:
4129+
__devlink_snapshot_id_decrement(devlink, snapshot_id);
4130+
return err;
4131+
}
4132+
40414133
static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
40424134
struct devlink *devlink,
40434135
u8 *chunk, u32 chunk_size,
@@ -6446,6 +6538,13 @@ static const struct genl_ops devlink_nl_ops[] = {
64466538
.flags = GENL_ADMIN_PERM,
64476539
.internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
64486540
},
6541+
{
6542+
.cmd = DEVLINK_CMD_REGION_NEW,
6543+
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
6544+
.doit = devlink_nl_cmd_region_new,
6545+
.flags = GENL_ADMIN_PERM,
6546+
.internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
6547+
},
64496548
{
64506549
.cmd = DEVLINK_CMD_REGION_DEL,
64516550
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,

0 commit comments

Comments
 (0)