[PATCH v2] genl: modify genl_ctrl_resolve and friends to allow for module auto-loading

Neil Horman nhorman at tuxdriver.com
Fri Jun 1 09:11:58 EDT 2012


Generic netlink has the ability to autoload modules in response to a request for
a family.  Currently libnl uses a GETFAMILY call with the NLM_F_DUMP flag to
list all the available families, but doing so neglects the possibility of an
autoloaded module.  This patch modifies the genl code to probe the kernel for a
specific family rather than dumping a list of all the currenlty available ones,
making autoload work properly.

Signed-off-by: Neil Horman <nhorman at tuxdriver.com>
CC: Thomas Graf <tgraf at redhat.com>

Change notes:
V2)	Modified the nlattr array to use CTRL_ATTR_MAX instead of __CTRL_ATTR_MAX
	removed some unused code that I missed
---
 lib/genl/ctrl.c |  133 +++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 119 insertions(+), 14 deletions(-)

diff --git a/lib/genl/ctrl.c b/lib/genl/ctrl.c
index 107a4fa..9b29000 100644
--- a/lib/genl/ctrl.c
+++ b/lib/genl/ctrl.c
@@ -250,6 +250,120 @@ struct genl_family *genl_ctrl_search_by_name(struct nl_cache *cache,
 /** @} */
 
 /**
+ * process responses from from the query sent by genl_ctrl_probe_by_name 
+ * @arg nl_msg		Returned message.
+ * @arg name		genl_family structure to fill out.
+ *
+ * Process returned messages, filling out the missing informatino in the
+ * genl_family structure
+ *
+ * @return Indicator to keep processing frames or not
+ *
+ */
+static int probe_response(struct nl_msg *msg, void *arg)
+{
+	struct nlattr *tb[__CTRL_ATTR_MAX+1];
+	struct nlmsghdr *nlh = nlmsg_hdr(msg);
+	struct genl_family *ret = (struct genl_family *)arg;
+
+	if (genlmsg_parse(nlh, 0, tb, __CTRL_ATTR_MAX, ctrl_policy))
+		return NL_SKIP;
+
+
+	if (tb[CTRL_ATTR_FAMILY_ID])
+		genl_family_set_id(ret, nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]));
+
+	if (tb[CTRL_ATTR_MCAST_GROUPS]) {
+		struct nlattr *nla, *nla_grps;
+		int remaining;
+		nla_grps = tb[CTRL_ATTR_MCAST_GROUPS];
+		nla_for_each_nested(nla, nla_grps, remaining) {
+			struct nlattr *ntb[CTRL_ATTR_MCAST_GRP_MAX+1];
+			int id;
+			const char * name;
+
+			if (nla_parse_nested(ntb, CTRL_ATTR_MCAST_GRP_MAX, nla,
+					       family_grp_policy) < 0)
+				return NL_SKIP;
+
+			if (ntb[CTRL_ATTR_MCAST_GRP_ID] == NULL)
+				return NL_SKIP;
+			id = nla_get_u32(ntb[CTRL_ATTR_MCAST_GRP_ID]);
+
+			if (ntb[CTRL_ATTR_MCAST_GRP_NAME] == NULL)
+				return NL_SKIP;
+
+			name = nla_get_string(ntb[CTRL_ATTR_MCAST_GRP_NAME]);
+
+			if (genl_family_add_grp(ret, id, name))
+				return NL_SKIP;
+		}
+	}
+
+	return NL_STOP;
+}
+
+/**
+ * Look up generic netlink family by family name querying the kernel directly
+ * @arg sk		Socket.
+ * @arg name		Family name.
+ *
+ * Directly query's the kernel for a given family name.  The caller will own a
+ * reference on the returned object which needsd to be given back after usage
+ * using genl_family_put.
+ *
+ * Note: This API call differs from genl_ctrl_search_by_name in that it querys
+ * the kernel directly, alowing for module autoload to take place to resolve the
+ * family request. Using an nl_cache prevents that operation
+ *
+ * @return Generic netlink family object or NULL if no match was found.
+ */
+static struct genl_family *genl_ctrl_probe_by_name(struct nl_sock *sk, const char *name)
+{
+	struct nl_msg *msg;
+	struct genl_family *ret = NULL;
+	int rc;
+	void *hdr;
+
+	ret = genl_family_alloc();
+	if (!ret)
+		goto out;
+
+	nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, probe_response, (void *)ret);
+	genl_family_set_name(ret, name);
+
+	msg = nlmsg_alloc();
+	if (!msg)
+		goto out_fam_free;
+
+	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, GENL_ID_CTRL,
+		    0, 0, CTRL_CMD_GETFAMILY, 1);
+
+	if (nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, name))
+		goto out_msg_free;
+
+	rc = nl_send_auto_complete(sk, msg);
+	if (rc < 0)
+		goto out_msg_free;
+
+	nl_recvmsgs_default(sk);
+
+	if (genl_family_get_id(ret) != 0) {
+		nlmsg_free(msg);
+		return ret;
+	}
+
+out_msg_free:
+	nlmsg_free(msg);
+out_fam_free:
+	genl_family_put(ret);
+	ret = NULL;
+out:
+	return ret;
+}
+
+
+/**
  * Resolve generic netlink family name to its identifier
  * @arg sk		Netlink socket.
  * @arg name		Name of generic netlink family
@@ -261,14 +375,10 @@ struct genl_family *genl_ctrl_search_by_name(struct nl_cache *cache,
  */
 int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
 {
-	struct nl_cache *cache;
 	struct genl_family *family;
 	int err;
 
-	if ((err = genl_ctrl_alloc_cache(sk, &cache)) < 0)
-		return err;
-
-	family = genl_ctrl_search_by_name(cache, name);
+	family = genl_ctrl_probe_by_name(sk, name);
 	if (family == NULL) {
 		err = -NLE_OBJ_NOTFOUND;
 		goto errout;
@@ -277,8 +387,6 @@ int genl_ctrl_resolve(struct nl_sock *sk, const char *name)
 	err = genl_family_get_id(family);
 	genl_family_put(family);
 errout:
-	nl_cache_free(cache);
-
 	return err;
 }
 
@@ -299,14 +407,11 @@ static int genl_ctrl_grp_by_name(const struct genl_family *family,
 int genl_ctrl_resolve_grp(struct nl_sock *sk, const char *family_name,
 	const char *grp_name)
 {
-	struct nl_cache *cache;
+
 	struct genl_family *family;
 	int err;
 
-	if ((err = genl_ctrl_alloc_cache(sk, &cache)) < 0)
-		return err;
-
-	family = genl_ctrl_search_by_name(cache, family_name);
+	family = genl_ctrl_probe_by_name(sk, family_name);
 	if (family == NULL) {
 		err = -NLE_OBJ_NOTFOUND;
 		goto errout;
@@ -315,8 +420,6 @@ int genl_ctrl_resolve_grp(struct nl_sock *sk, const char *family_name,
 	err = genl_ctrl_grp_by_name(family, grp_name);
 	genl_family_put(family);
 errout:
-	nl_cache_free(cache);
-
 	return err;
 }
 
@@ -337,6 +440,8 @@ static struct genl_cmd genl_cmds[] = {
 	{
 		.c_id		= CTRL_CMD_GETFAMILY,
 		.c_name		= "GETFAMILY" ,
+		.c_maxattr	= CTRL_ATTR_MAX,
+		.c_attr_policy	= ctrl_policy,
 	},
 	{
 		.c_id		= CTRL_CMD_NEWOPS,
-- 
1.7.7.6




More information about the libnl mailing list