All of lore.kernel.org
 help / color / mirror / Atom feed
* libnetfilter_queue & multithreading
@ 2017-07-22 13:08 Oleg
  2017-07-22 16:38 ` Florian Westphal
  0 siblings, 1 reply; 5+ messages in thread
From: Oleg @ 2017-07-22 13:08 UTC (permalink / raw)
  To: netfilter-devel

  Hi, all.

My program process multiple NFQUEUEs by creating a separate thread
for every NFQUEUE. An each thread do recv() and nfq_set_verdict2():

main()
{
...
    for(i = 0; i < q_cnt; i++) {
        ret = pthread_create(&(thread_data[i].id), NULL, thread_start,
          &thread_data[i].nfq_num);
        if (ret != 0) {
            fprintf(stderr, "thread creation error: %s", strerror(ret));
            exit(EXIT_FAILURE);
        }
    }
...
}

static void*
thread_start(void *data)
{
        struct nfq_handle *h;
        int fd, n;
        static char *pkt_buf;
        unsigned int nfq_num = *(unsigned int*)data;

        pkt_buf = (char*)malloc(80000);
        if (!pkt_buf) {
                fprintf(stderr, "packet buffer allocating error: no memory");
                exit(EXIT_FAILURE);
        }
        h = init_nfq(nfq_num);      
        fd = nfq_fd(h);
        while ((n = recv(fd, pkt_buf, 80000, 0)) > 0) {
                nfq_handle_packet(h, pkt_buf, n);
        }
...
}

static struct nfq_handle*
init_nfq(unsigned int nfq_num)
{
        struct nfq_handle *h;
        struct nfq_q_handle *qh;
        
        h = nfq_open();
        if (!h) {
                fprintf(stderr, "nfq error: queue %d nfq_open() error", nfq_num);
                exit(EXIT_FAILURE);
        }
        if (nfq_unbind_pf(h, AF_INET) < 0) {
                fprintf(stderr, "nfq error: queue %d nfq_bind_pf() error", nfq_num);
                exit(EXIT_FAILURE);
        }
        if (nfq_bind_pf(h, AF_INET) < 0) {
                fprintf(stderr, "nfq error: queue %d nfq_bind_pf() error", nfq_num);
                exit(EXIT_FAILURE);
        }
        qh = nfq_create_queue(h, nfq_num, &cb, NULL);
        if (!qh) {
                fprintf(stderr, "nfq error: queue %d nfq_create_queue() error", nfq_num);
                exit(EXIT_FAILURE);
        }
        if (nfq_set_mode(qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
                fprintf(stderr, "nfq error: queue %d nfq_set_mode() error", nfq_num);
                exit(EXIT_FAILURE);
        }
        
        return h;
}


Since every thread do nfq_open(), has a separate descriptor and etc, i think
i don't need a lock around recv() and nfq_set_verdict2(). Am i right?

Thanks!

-- 
Олег Неманов (Oleg Nemanov)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: libnetfilter_queue & multithreading
  2017-07-22 13:08 libnetfilter_queue & multithreading Oleg
@ 2017-07-22 16:38 ` Florian Westphal
  2017-07-24  9:40   ` Oleg
  0 siblings, 1 reply; 5+ messages in thread
From: Florian Westphal @ 2017-07-22 16:38 UTC (permalink / raw)
  To: Oleg; +Cc: netfilter-devel

Oleg <lego12239@yandex.ru> wrote:
> My program process multiple NFQUEUEs by creating a separate thread
> for every NFQUEUE. An each thread do recv() and nfq_set_verdict2():
[..]

> main()
> {
> ...
>     for(i = 0; i < q_cnt; i++) {
>         ret = pthread_create(&(thread_data[i].id), NULL, thread_start,
>           &thread_data[i].nfq_num);
>         if (ret != 0) {
>             fprintf(stderr, "thread creation error: %s", strerror(ret));
>             exit(EXIT_FAILURE);
>         }
>     }
> ...
> }
> 
> static void*
> thread_start(void *data)
> {
>         struct nfq_handle *h;
>         int fd, n;
>         static char *pkt_buf;

static? Looks buggy...

> Since every thread do nfq_open(), has a separate descriptor and etc, i think
> i don't need a lock around recv() and nfq_set_verdict2(). Am i right?

Yes, as long as all threads have their own receive buffer this
won't need locks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: libnetfilter_queue & multithreading
  2017-07-22 16:38 ` Florian Westphal
@ 2017-07-24  9:40   ` Oleg
  2017-07-24  9:44     ` Florian Westphal
  0 siblings, 1 reply; 5+ messages in thread
From: Oleg @ 2017-07-24  9:40 UTC (permalink / raw)
  To: Florian Westphal; +Cc: netfilter-devel

On Sat, Jul 22, 2017 at 06:38:55PM +0200, Florian Westphal wrote:
> Oleg <lego12239@yandex.ru> wrote:
> > static void*
> > thread_start(void *data)
> > {
> >         struct nfq_handle *h;
> >         int fd, n;
> >         static char *pkt_buf;
> 
> static? Looks buggy...

  Hm, why :-)? This function must be local and shouldn't be seen in other
source files.


-- 
Олег Неманов (Oleg Nemanov)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: libnetfilter_queue & multithreading
  2017-07-24  9:40   ` Oleg
@ 2017-07-24  9:44     ` Florian Westphal
  2017-07-24 10:11       ` Oleg
  0 siblings, 1 reply; 5+ messages in thread
From: Florian Westphal @ 2017-07-24  9:44 UTC (permalink / raw)
  To: Oleg; +Cc: Florian Westphal, netfilter-devel

Oleg <lego12239@yandex.ru> wrote:
> On Sat, Jul 22, 2017 at 06:38:55PM +0200, Florian Westphal wrote:
> > Oleg <lego12239@yandex.ru> wrote:
> > > static void*
> > > thread_start(void *data)
> > > {
> > >         struct nfq_handle *h;
> > >         int fd, n;
> > >         static char *pkt_buf;
> > 
> > static? Looks buggy...
> 
>   Hm, why :-)? This function must be local and shouldn't be seen in other
> source files.

I was talking about pkt_buf.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: libnetfilter_queue & multithreading
  2017-07-24  9:44     ` Florian Westphal
@ 2017-07-24 10:11       ` Oleg
  0 siblings, 0 replies; 5+ messages in thread
From: Oleg @ 2017-07-24 10:11 UTC (permalink / raw)
  To: Florian Westphal; +Cc: netfilter-devel

On Mon, Jul 24, 2017 at 11:44:51AM +0200, Florian Westphal wrote:
> Oleg <lego12239@yandex.ru> wrote:
> > On Sat, Jul 22, 2017 at 06:38:55PM +0200, Florian Westphal wrote:
> > > Oleg <lego12239@yandex.ru> wrote:
> > > > static void*
> > > > thread_start(void *data)
> > > > {
> > > >         struct nfq_handle *h;
> > > >         int fd, n;
> > > >         static char *pkt_buf;
> > > 
> > > static? Looks buggy...
> > 
> >   Hm, why :-)? This function must be local and shouldn't be seen in other
> > source files.
> 
> I was talking about pkt_buf.

Wow. You are right. This is a copy-paste error. Thank you!

-- 
Олег Неманов (Oleg Nemanov)

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-07-24 10:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-22 13:08 libnetfilter_queue & multithreading Oleg
2017-07-22 16:38 ` Florian Westphal
2017-07-24  9:40   ` Oleg
2017-07-24  9:44     ` Florian Westphal
2017-07-24 10:11       ` Oleg

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.