Skip to contentSkip to conversation

genetlink: introduce pre/post methods

PATCH In review Johannes Berg 2 participants 2 messages Feb 13, 2009 – Feb 19, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 genetlink: introduce pre/post methods

Sometimes doit/dumpit callbacks for a given family consist of quite a bit of boilerplate code, for example locking or generic sanity checks on messages.

These methods allow writing the boilerplate code just once instead of copying it into all handlers.

---
I'd like to have this go in via the wireless tree because I have a bugfix for that tree which depends on this infrastructure.

Note: struct genl_info just moves.

 include/net/genetlink.h |   47 ++++++++++++++++++++++++++++-------------------
 net/netlink/genetlink.c |   19 ++++++++++++++++---
 2 files changed, 44 insertions(+), 22 deletions(-)
include/net/genetlink.h +28 -19
@@ -21,12 +21,35 @@ struct genl_multicast_group
 };
 
 /**
+ * struct genl_info - receiving information
+ * @snd_seq: sending sequence number
+ * @snd_pid: netlink pid of sender
+ * @nlhdr: netlink message header
+ * @genlhdr: generic netlink message header
+ * @userhdr: user specific header
+ * @attrs: netlink attributes
+ */
+struct genl_info
+{
+	u32			snd_seq;
+	u32			snd_pid;
+	struct nlmsghdr *	nlhdr;
+	struct genlmsghdr *	genlhdr;
+	void *			userhdr;
+	struct nlattr **	attrs;
+};
+
+/**
  * struct genl_family - generic netlink family
  * @id: protocol family idenfitier
  * @hdrsize: length of user specific header in bytes
  * @name: name of family
  * @version: protocol version
  * @maxattr: maximum number of attributes supported
+ * @pre_doit: called before any doit op
+ * @post_doit: called after any doit op
+ * @pre_dumpit: called before any dumpit op
+ * @post_dumpit: called after any dumpit op
  * @attrbuf: buffer to store parsed attributes
  * @ops_list: list of all assigned operations
  * @family_list: family list
@@ -39,6 +62,11 @@ struct genl_family
 	char			name[GENL_NAMSIZ];
 	unsigned int		version;
 	unsigned int		maxattr;
+	int			(*pre_doit)(struct sk_buff *skb,
+					    struct genl_info *info);
+	void			(*post_doit)(void);
+	int			(*pre_dumpit)(void);
+	void			(*post_dumpit)(void);
 	struct nlattr **	attrbuf;	/* private */
 	struct list_head	ops_list;	/* private */
 	struct list_head	family_list;	/* private */
@@ -46,25 +74,6 @@ struct genl_family
 };
 
 /**
- * struct genl_info - receiving information
- * @snd_seq: sending sequence number
- * @snd_pid: netlink pid of sender
- * @nlhdr: netlink message header
- * @genlhdr: generic netlink message header
- * @userhdr: user specific header
- * @attrs: netlink attributes
- */
-struct genl_info
-{
-	u32			snd_seq;
-	u32			snd_pid;
-	struct nlmsghdr *	nlhdr;
-	struct genlmsghdr *	genlhdr;
-	void *			userhdr;
-	struct nlattr **	attrs;
-};
-
-/**
  * struct genl_ops - generic netlink operations
  * @cmd: command identifier
  * @flags: flags
net/netlink/genetlink.c +16 -3
@@ -446,8 +446,14 @@ static int genl_rcv_msg(struct sk_buff *
 			return -EOPNOTSUPP;
 
 		genl_unlock();
-		err = netlink_dump_start(genl_sock, skb, nlh,
-					 ops->dumpit, ops->done);
+		err = 0;
+		if (family->pre_dumpit)
+			err = family->pre_dumpit();
+		if (!err)
+			err = netlink_dump_start(genl_sock, skb, nlh,
+						 ops->dumpit, ops->done);
+		if (family->post_dumpit)
+			family->post_dumpit();
 		genl_lock();
 		return err;
 	}
@@ -469,7 +475,14 @@ static int genl_rcv_msg(struct sk_buff *
 	info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
 	info.attrs = family->attrbuf;
 
-	return ops->doit(skb, &info);
+	err = 0;
+	if (family->pre_doit)
+		err = family->pre_doit(skb, &info);
+	if (!err)
+		err = ops->doit(skb, &info);
+	if (family->post_doit)
+		family->post_doit();
+	return err;
 }
 
 static void genl_rcv(struct sk_buff *skb)

--
To unsubscribe from this list: send the line "unsubscribe linux-wireless" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer Acked-by on v1 1/1
#1 lore raw

From: Johannes Berg <[email protected]>
Date: Fri, 13 Feb 2009 10:04:25 +0100

Quoted 8 lines

Sometimes doit/dumpit callbacks for a given family consist of quite a bit of boilerplate code, for example locking or generic sanity checks on messages.

These methods allow writing the boilerplate code just once instead of copying it into all handlers.

I'm fine with this:

Feel free to toss it into the wireless tree.