Re: [PATCH 08/16] [XFRM] netlink: Use nlmsg_new() and type-safe size calculation helpers

2007-08-22 Thread David Miller
From: Thomas Graf <[EMAIL PROTECTED]>
Date: Wed, 22 Aug 2007 16:55:46 +0200

> Moves all complex message size calculation into own inlined helper
> functions and makes use of the type-safe netlink interface.
> 
> Using nlmsg_new() simplifies the calculation itself as it takes care
> of the netlink header length by itself.
> 
> Signed-off-by: Thomas Graf <[EMAIL PROTECTED]>

Applied.
-
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 08/16] [XFRM] netlink: Use nlmsg_new() and type-safe size calculation helpers

2007-08-22 Thread Thomas Graf
Moves all complex message size calculation into own inlined helper
functions and makes use of the type-safe netlink interface.

Using nlmsg_new() simplifies the calculation itself as it takes care
of the netlink header length by itself.

Signed-off-by: Thomas Graf <[EMAIL PROTECTED]>

Index: net-2.6.24/net/xfrm/xfrm_user.c
===
--- net-2.6.24.orig/net/xfrm/xfrm_user.c2007-08-21 17:04:46.0 
+0200
+++ net-2.6.24/net/xfrm/xfrm_user.c 2007-08-21 17:07:38.0 +0200
@@ -670,7 +670,7 @@ static struct sk_buff *xfrm_state_netlin
struct xfrm_dump_info info;
struct sk_buff *skb;
 
-   skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
+   skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
if (!skb)
return ERR_PTR(-ENOMEM);
 
@@ -688,6 +688,13 @@ static struct sk_buff *xfrm_state_netlin
return skb;
 }
 
+static inline size_t xfrm_spdinfo_msgsize(void)
+{
+   return NLMSG_ALIGN(4)
+  + nla_total_size(sizeof(struct xfrmu_spdinfo))
+  + nla_total_size(sizeof(struct xfrmu_spdhinfo));
+}
+
 static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
 {
struct xfrmk_spdinfo si;
@@ -729,12 +736,8 @@ static int xfrm_get_spdinfo(struct sk_bu
u32 *flags = nlmsg_data(nlh);
u32 spid = NETLINK_CB(skb).pid;
u32 seq = nlh->nlmsg_seq;
-   int len = NLMSG_LENGTH(sizeof(u32));
 
-   len += RTA_SPACE(sizeof(struct xfrmu_spdinfo));
-   len += RTA_SPACE(sizeof(struct xfrmu_spdhinfo));
-
-   r_skb = alloc_skb(len, GFP_ATOMIC);
+   r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
if (r_skb == NULL)
return -ENOMEM;
 
@@ -744,6 +747,13 @@ static int xfrm_get_spdinfo(struct sk_bu
return nlmsg_unicast(xfrm_nl, r_skb, spid);
 }
 
+static inline size_t xfrm_sadinfo_msgsize(void)
+{
+   return NLMSG_ALIGN(4)
+  + nla_total_size(sizeof(struct xfrmu_sadhinfo))
+  + nla_total_size(4); /* XFRMA_SAD_CNT */
+}
+
 static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
 {
struct xfrmk_sadinfo si;
@@ -779,13 +789,8 @@ static int xfrm_get_sadinfo(struct sk_bu
u32 *flags = nlmsg_data(nlh);
u32 spid = NETLINK_CB(skb).pid;
u32 seq = nlh->nlmsg_seq;
-   int len = NLMSG_LENGTH(sizeof(u32));
-
-   len += RTA_SPACE(sizeof(struct xfrmu_sadhinfo));
-   len += RTA_SPACE(sizeof(u32));
-
-   r_skb = alloc_skb(len, GFP_ATOMIC);
 
+   r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
if (r_skb == NULL)
return -ENOMEM;
 
@@ -1311,7 +1316,7 @@ static struct sk_buff *xfrm_policy_netli
struct xfrm_dump_info info;
struct sk_buff *skb;
 
-   skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
+   skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (!skb)
return ERR_PTR(-ENOMEM);
 
@@ -1425,6 +1430,14 @@ static int xfrm_flush_sa(struct sk_buff 
return 0;
 }
 
+static inline size_t xfrm_aevent_msgsize(void)
+{
+   return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
+  + nla_total_size(sizeof(struct xfrm_replay_state))
+  + nla_total_size(sizeof(struct xfrm_lifetime_cur))
+  + nla_total_size(4) /* XFRM_AE_RTHR */
+  + nla_total_size(4); /* XFRM_AE_ETHR */
+}
 
 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct 
km_event *c)
 {
@@ -1469,19 +1482,9 @@ static int xfrm_get_ae(struct sk_buff *s
int err;
struct km_event c;
struct xfrm_aevent_id *p = nlmsg_data(nlh);
-   int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
struct xfrm_usersa_id *id = &p->sa_id;
 
-   len += RTA_SPACE(sizeof(struct xfrm_replay_state));
-   len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
-
-   if (p->flags&XFRM_AE_RTHR)
-   len+=RTA_SPACE(sizeof(u32));
-
-   if (p->flags&XFRM_AE_ETHR)
-   len+=RTA_SPACE(sizeof(u32));
-
-   r_skb = alloc_skb(len, GFP_ATOMIC);
+   r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
if (r_skb == NULL)
return -ENOMEM;
 
@@ -1824,6 +1827,13 @@ static int copy_to_user_migrate(struct x
return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
 }
 
+static inline size_t xfrm_migrate_msgsize(int num_migrate)
+{
+   return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
+  + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
+  + userpolicy_type_attrsize();
+}
+
 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
 int num_migrate, struct xfrm_selector *sel,
 u8 dir, u8 type)
@@ -1861,12 +1871,8 @@ static int xfrm_send_migrate(struct xfrm
 struct xfrm_migrate *m, int num_migrate)
 {
struct sk_buff *skb;
-   siz