• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

looping

Hi I am writing a kernel module.

I need a continuous loop that will look for tcp traffic and read from skb to store each source ip address from ip_hdr->saddr into an array

I can get it to read packets one at a time but I need it to loop, but when I try to implement a continuous loop the system crashes/hangs.

Can anyone offer any advice/possible fixes?

Cheers
0
codey-06
Asked:
codey-06
  • 5
  • 3
3 Solutions
 
Anthony2000Commented:
Here is an example of using a thread in the kernel module.

http://www.scs.ch/~frey/linux/kernelthreads.html

I think your problem might be that your loop is running and using all available kernel/process time. Thus, giving the result of maybe locking up your machine. Is that what is happening?
0
 
codey-06Author Commented:
Hi yes you are correct, the loop is using up all the kernel resources.  Is there a quick fix?
0
 
Anthony2000Commented:
Have you tried some kind of sleep (I cannot remember the API call). This may give the OS and other processes time to run.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
codey-06Author Commented:
Thank you. If anyone could offer a bit more help it would be very much appreciated?
0
 
sarabandeCommented:
if you added an infinite or time consuming loop into a kernel module that was needed by others you were blocking all those activities dependent on those calls and the system hangs and some processes would crash or fail because of timeouts. so, the first thing you need is to start a thread where you would add the loop and the normal kernel function could proceed. of course you may not create a new thread for every call but use one thread for all requests. in the thread you would run an infinite loop and at end of loop you should add a call to usleep which would make that the thread waits a little and so other processes and threads could be scheduled by the os. the usleep takes microseconds as argument and i would use at least 1000 (1 millisecond) to not slow down the rest of the system cause that also would depend on your other actions made in the loop.

Sara
0
 
Anthony2000Commented:
Here is another related article.

http://www.linuxjournal.com/article/8144
0
 
Anthony2000Commented:
Also, if you are not familiar with this book, its also a good reference.

http://lwn.net/Kernel/LDD3/
0
 
Anthony2000Commented:
Also, take a look at the functions:
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout (signed long timeout);

See chapter 7 of the book link I provided.
0
 
codey-06Author Commented:
Brilliant thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now