Skip to contentSkip to conversation

size calculation wasted cpu time

DISCUSSION Kuzin Andrey 5 participants 6 messages Mar 16, 2009 – Jul 25, 2009 netfilter

lore
Share & tools
lore
#0 lore raw

In /net/netfilter/nfnetlink_queue.c every call of nfqnl_build_packet_message() make series of call nla_total_size() for 'size' calculations.
I think 'size' is constant value for kernel build and may be define static variable for this ?

Thank you.

static struct sk_buff * nfqnl_build_packet_message(struct nfqnl_instance *queue,
struct nf_queue_entry *entry)

{
        sk_buff_data_t old_tail;
        size_t size;
        size_t data_len = 0;
        struct sk_buff *skb;
        struct nfqnl_msg_packet_hdr pmsg;
        struct nlmsghdr *nlh;
        struct nfgenmsg *nfmsg;
        struct sk_buff *entskb = entry->skb;
        struct net_device *indev;
        struct net_device *outdev;

        size =    NLMSG_SPACE(sizeof(struct nfgenmsg))
                + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
                + nla_total_size(sizeof(u_int32_t))     /* ifindex */
                + nla_total_size(sizeof(u_int32_t))     /* ifindex */

#ifdef CONFIG_BRIDGE_NETFILTER
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
#endif
+ nla_total_size(sizeof(u_int32_t)) /* mark */
+ nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
+ nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));

#1 lore raw
Quoted 4 lines · Kuzin Andrey

In /net/netfilter/nfnetlink_queue.c every call of nfqnl_build_packet_message() make series of call nla_total_size() for 'size' calculations.
I think 'size' is constant value for kernel build and may be define static variable for this ?

We can calculate this during the module initialization, brave to cook a patch for it?

Signature · 1 line
"Los honestos son inadaptados sociales" -- Les Luthiers
#2 lore raw
Quoted 8 lines · Pablo Neira Ayuso
Quoted 4 lines · Kuzin Andrey

In /net/netfilter/nfnetlink_queue.c every call of nfqnl_build_packet_message() make series of call nla_total_size() for 'size' calculations.
I think 'size' is constant value for kernel build and may be define static variable for this ?

We can calculate this during the module initialization, brave to cook a patch for it?

The compiler should realize its constant and perform the calculation at calculation time.

#3 lore raw
In reply to Kuzin Andrey
Quoted 9 lines · Kuzin Andrey

In /net/netfilter/nfnetlink_queue.c every call of nfqnl_build_packet_message() make series of call nla_total_size() for 'size' calculations.
I think 'size' is constant value for kernel build and may be define static variable for this ?

size = NLMSG_SPACE(sizeof(struct nfgenmsg))
+ nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */

The compiler will optimize it out.

#4 lore raw
Quoted 9 lines
Quoted 9 lines

In /net/netfilter/nfnetlink_queue.c every call of nfqnl_build_packet_message() make series of call nla_total_size() for 'size' calculations.
I think 'size' is constant value for kernel build and may be define static variable for this ?

size = NLMSG_SPACE(sizeof(struct nfgenmsg))
+ nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */

JE> The compiler will optimize it out.

Okey. It's easy to test.

=========== 1.c =======================

#include <stdlib.h>
#include <stdio.h>

#define NLA_ALIGNTO             4
#define NLA_ALIGN(len)          (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
#define NLA_HDRLEN              30

static inline int nla_attr_size(int payload)

{
      return NLA_HDRLEN + payload;
}

static inline int nla_total_size(int payload)

{
      return NLA_ALIGN(nla_attr_size(payload));
}

size_t nfqnl_build_packet_message(int len) {
size_t size;

size = nla_total_size(sizeof(u_int32_t)) /* ifindex */
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
+ nla_total_size(sizeof(u_int32_t)); /* mark */

return size;
};

int main () {
      long i;
      size_t ret;

      int r = 0;
      for (i = 0; i < 100000000; i++) {
            r = rand();
            printf("%d",r);
            ret = nfqnl_build_packet_message(r);
      };
}

======================== end of 1.c ========================

And other case

===================== 2.c =============================

#include <stdlib.h>
#include <stdio.h>

#define NLA_ALIGNTO             4
#define NLA_ALIGN(len)          (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
#define NLA_HDRLEN              30

static inline int nla_attr_size(int payload)

{
      return NLA_HDRLEN + payload;
}

static inline int nla_total_size(int payload)

{
      return NLA_ALIGN(nla_attr_size(payload));
}

size_t size_global = 0; ///// GLOBAL PRECALCULATED VALUE

size_t nfqnl_build_packet_message(int len) {
      size_t size;

      size = size_global;

      return size;
};
int main () {
      long i;
      size_t ret;

      //////// calculations here
      size_global = nla_total_size(sizeof(u_int32_t))     /* ifindex */
            + nla_total_size(sizeof(u_int32_t))     /* ifindex */
            + nla_total_size(sizeof(u_int32_t))     /* ifindex */
            + nla_total_size(sizeof(u_int32_t))     /* ifindex */
            + nla_total_size(sizeof(u_int32_t));     /* mark */

      int r = 0;
      for (i = 0; i < 100000000; i++) {
            r = rand();
            printf("%d",r);
            ret = nfqnl_build_packet_message(r);
      };
}

===========================================

And some tests....

root@bill:/src/1# gcc 1.c
root@bill:/src/1# time ./a.out > /dev/null
real    0m20.253s
user    0m20.209s
sys     0m0.041s
root@bill:/src/1# gcc 2.c
root@bill:/src/1# time ./a.out > /dev/null
real    0m17.030s
user    0m16.988s
sys     0m0.040s

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Kuzin Andrey - [email protected]
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=