All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH iproute2-next v2] devlink: support kernel-side snapshot id allocation
@ 2020-04-29 23:38 Jakub Kicinski
  0 siblings, 0 replies; only message in thread
From: Jakub Kicinski @ 2020-04-29 23:38 UTC (permalink / raw)
  To: dsahern, stephen, jiri
  Cc: netdev, kernel-team, jacob.e.keller, Jakub Kicinski

Make ID argument optional and read the snapshot info
that kernel sends us.

  $ devlink region new netdevsim/netdevsim1/dummy
  netdevsim/netdevsim1/dummy: snapshot 0
  $ devlink -jp region new netdevsim/netdevsim1/dummy
  {
      "regions": {
          "netdevsim/netdevsim1/dummy": {
              "snapshot": [ 1 ]
          }
      }
  }
  $ devlink region show netdevsim/netdevsim1/dummy
  netdevsim/netdevsim1/dummy: size 32768 snapshot [0 1]

In case the operation fails only the error is printed:

  $ devlink region new netdevsim/netdevsim1/dummy
  Error: devlink: failure injected after ID notification was sent.
  devlink answers: Input/output error
  $

v2:
 - make sure we don't print anything on failure.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 devlink/devlink.c | 66 ++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 63 insertions(+), 3 deletions(-)

diff --git a/devlink/devlink.c b/devlink/devlink.c
index bd48a73bc0e4..73a1f02a5339 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -6476,20 +6476,80 @@ static int cmd_region_read(struct dl *dl)
 	return err;
 }
 
+static int cmd_region_snapshot_new_response_print(struct dl *dl,
+						  const struct nlmsghdr *nlh)
+{
+	struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
+
+	if (dl->opts.present & DL_OPT_REGION_SNAPSHOT_ID)
+		return 0;
+
+	if (!nlh->nlmsg_len) {
+		pr_err("no response from the kernel\n");
+		return -EINVAL;
+	}
+
+	mnl_attr_parse(nlh, sizeof(struct genlmsghdr), attr_cb, tb);
+	if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
+	    !tb[DEVLINK_ATTR_REGION_NAME] ||
+	    !tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
+		pr_err("kernel response does not contain expected attributes\n");
+		return -EINVAL;
+	}
+
+	pr_out_section_start(dl, "regions");
+	pr_out_region(dl, tb);
+	pr_out_section_end(dl);
+
+	return 0;
+}
+
+static int cmd_region_snapshot_new_cb(const struct nlmsghdr *nlh, void *data)
+{
+	struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
+	struct nlmsghdr *nlh_copy = data;
+
+	if (genl->cmd != DEVLINK_CMD_REGION_NEW)
+		return MNL_CB_OK;
+
+	/* Make sure we only receive one message from the kernel */
+	if (nlh_copy->nlmsg_len)
+		return MNL_CB_ERROR;
+
+	if (nlh->nlmsg_len > MNL_SOCKET_BUFFER_SIZE)
+		return MNL_CB_ERROR;
+
+	memcpy(data, nlh, nlh->nlmsg_len);
+
+	return MNL_CB_OK;
+}
+
 static int cmd_region_snapshot_new(struct dl *dl)
 {
+	unsigned char *msg_copy;
 	struct nlmsghdr *nlh;
 	int err;
 
 	nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_NEW,
 			       NLM_F_REQUEST | NLM_F_ACK);
 
-	err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION |
-				DL_OPT_REGION_SNAPSHOT_ID, 0);
+	err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION,
+				DL_OPT_REGION_SNAPSHOT_ID);
 	if (err)
 		return err;
 
-	return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
+	msg_copy = calloc(MNL_SOCKET_BUFFER_SIZE, 1);
+
+	err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_region_snapshot_new_cb,
+				  msg_copy);
+	if (err)
+		goto out;
+
+	err = cmd_region_snapshot_new_response_print(dl, (void *)msg_copy);
+out:
+	free(msg_copy);
+
+	return err;
 }
 
 static void cmd_region_help(void)
-- 
2.25.4


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-04-29 23:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-29 23:38 [PATCH iproute2-next v2] devlink: support kernel-side snapshot id allocation Jakub Kicinski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.