LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 03-22-2011, 10:50 AM   #1
vali103
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Rep: Reputation: 0
How to add threads in ip_forward function


Hello everybody,

I have changed the ip_forward function in order to add a timer : when a packet comes, it will be delayed about 1 or 2 seconds. Then, it will be forwarded.
But if an other packet comes when a previous packet is waiting, there will be a problem. So I want to use threads to avoid blocking states.

But I obtained lots of errors and warnings.
Code:
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <linux/netdevice.h>
#include <linux/slab.h>
#include <net/sock.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <net/udp.h>
#include <net/icmp.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/netfilter_ipv4.h>
#include <net/checksum.h>
#include <linux/route.h>
#include <net/route.h>
#include <net/xfrm.h>
#include <linux/init.h>
#include <linux/linkage.h>
#include <linux/unistd.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/sysctl.h>
#include <linux/compiler.h>
#include <linux/module.h>
#include <linux/jiffies.h>
#include <linux/list.h>
#include <linux/jhash.h>
#include <linux/random.h>
#include <net/dst.h>
#include <net/inetpeer.h>
#include <net/inet_frag.h>
#include <linux/inet.h>
#include <linux/netfilter_ipv4.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/time.h>

int ip_forward(struct sk_buff *skb)
{
        struct iphdr *iph;        /* Our header */
        struct rtable *rt;        /* Route we use */
        struct ip_options * opt        = &(IPCB(skb)->opt);
	struct task_struct *threadIpForward;

        if (skb_warn_if_lro(skb))
                goto drop;

        if (!xfrm4_policy_check(NULL, XFRM_POLICY_FWD, skb))
                goto drop;

        if (IPCB(skb)->opt.router_alert && ip_call_ra_chain(skb))
                return NET_RX_SUCCESS;

        if (skb->pkt_type != PACKET_HOST)
                goto drop;

        skb_forward_csum(skb);

        /*
         *        According to the RFC, we must first decrease the TTL field. If
         *        that reaches zero, we must reply an ICMP control message telling
         *        that the packet's lifetime expired.
         */
        if (ip_hdr(skb)->ttl <= 1)
                goto too_many_hops;

        if (!xfrm4_route_forward(skb))
                goto drop;

        rt = skb_rtable(skb);

        if (opt->is_strictroute && rt->rt_dst != rt->rt_gateway)
                goto sr_failed;

        if (unlikely(skb->len > dst_mtu(&rt->u.dst) && !skb_is_gso(skb) &&
                     (ip_hdr(skb)->frag_off & htons(IP_DF))) && !skb->local_df) {
                IP_INC_STATS(dev_net(rt->u.dst.dev), IPSTATS_MIB_FRAGFAILS);
                icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
                          htonl(dst_mtu(&rt->u.dst)));
                goto drop;
        }

        /* We are about to mangle packet. Copy it! */
        if (skb_cow(skb, LL_RESERVED_SPACE(rt->u.dst.dev)+rt->u.dst.header_len))
                goto drop;
        iph = ip_hdr(skb);

        /* Decrease ttl after skb cow done */
        ip_decrease_ttl(iph);

        /*
         *        We now generate an ICMP HOST REDIRECT giving the route
         *        we calculated.
         */
        if (rt->rt_flags&RTCF_DOREDIRECT && !opt->srr && !skb_sec_path(skb))
                ip_rt_send_redirect(skb);
	skb->priority = rt_tos2priority(iph->tos);
		
 	threadIpForward = kthread_run(ip_forward_thread,(skb,5),"IP_FORWARD_THREAD"); 
	return 0;


sr_failed:
        /*
         *        Strict routing permits no gatewaying
         */
         icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0);
         goto drop;

too_many_hops:
        /* Tell the sender its packet died... */
        IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_INHDRERRORS);
        icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
drop:
        kfree_skb(skb);
        return NET_RX_DROP;
}

and the ip_forward_thread function is :
Code:
int ip_forward_thread(struct sk_buff *skb, int a)
{
        struct rtable *rt;        /* Route we use */
	delay(1000*a);
        return NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev,
                       rt->u.dst.dev, ip_forward_finish);
}

Quote:
net/ipv4/ip_forward.c: In function ‘ip_forward_thread’:
net/ipv4/ip_forward.c:78: error: implicit declaration of function ‘delay’
net/ipv4/ip_forward.c: In function ‘ip_forward’:
net/ipv4/ip_forward.c:144: warning: left-hand operand of comma expression has no effect
net/ipv4/ip_forward.c:144: warning: passing argument 1 of ‘kthread_create’ from incompatible pointer type
include/linux/kthread.h:7: note: expected ‘int (*)(void *)’ but argument is of type ‘int (*)(struct sk_buff *, int)’
net/ipv4/ip_forward.c:144: warning: passing argument 2 of ‘kthread_create’ makes pointer from integer without a cast
include/linux/kthread.h:7: note: expected ‘void *’ but argument is of type ‘int’


Please help me

Thank you for your help
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
Add user-customizable filters for zero reply threads emails bgoodr LQ Suggestions & Feedback 4 12-18-2009 11:14 AM
write() function and threads knobby67 Programming 5 05-03-2009 07:34 AM
How do I add a function to Bash? Gag Halfrunt Linux - Newbie 5 11-25-2004 12:05 PM
Why do we have to add the option -D_REENTRANT when compiling programs with threads?! Baco Programming 2 06-04-2004 11:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration