The simplest network driver


 
Thread Tools Search this Thread
Top Forums Programming The simplest network driver
# 1  
Old 11-10-2011
The simplest network driver

Hi, I am trying to write the simplest network driver that would send whatever through cable.

My configuration is:
Linux machine with some Intel network adapter
Another machine with WireShark

I connected Intel network adapter to second machine and want anything to pop up at wireshark.

For now I have compiled that driver:
LXR / The Linux Cross Reference
and want to refactor it to allow bind to Intel network adapter and send 1 packet in a simplest way.

Expected result is that after 'insmod dummy.ko' I will see 1 packet leaving Intel network adapter in WireShark.

Could you point me to some necessary steps I should do to achieve that?
I guess handling 'ping' with my refactored dummy driver is harder task...
Thanks
# 2  
Old 11-10-2011
Humm, you are not going to get anywhere by refactoring the dummy network driver. The necessary code is not in that driver for you to connect to any physical hardware.

I suggest you look at the code for the e1000 or e1000e driver.
# 3  
Old 11-17-2011
Ok, as I said before expected result is to send one packet. Another thing is that I would like to learn linux networking details.

e1000e driver is quite complicated.
For example at the begining of driver life in kernel space it does only:
Code:
init_module {
pci_register_driver(&e1000_driver);
}

No more routines are done.
Well... maybe they are done, but I have no idea where to even look for them.

Last edited by fpmurphy; 11-18-2011 at 07:06 AM..
# 4  
Old 11-17-2011
That's just how device drivers work. It gives the kernel a list of functions when it's loaded, the kernel decides which to call when. It's not like a userspace program where it all starts in main()...

You may need to study the organization of the kernel, not just the source code for one particular driver, to understand it.

Studying the source for some other driver won't tell you how the driver you want works, usually.
# 5  
Old 11-18-2011
May I count on you a bit?
I see, that while performing:

Code:
static int __init e1000_init_module(void)
{
       return  pci_register_driver(&e1000_driver);
}

function pointed by .probe is called.

Code:
/* PCI Device API Driver */
static struct pci_driver e1000_driver = {
       .name     = e1000e_driver_name,
       .id_table = e1000e_pci_tbl,
       .probe    = e1000_probe,
       .remove   = __devexit_p(e1000_remove),
       .shutdown = e1000_shutdown,
};

Is that behaviour described somewhere?
As I remember, exactly at this moment (pci_register_driver) driver announce set of device IDs he support, and kernel looking at this announcement and into PCI config space of every device binds proper driver to every device. Am I correct?
# 6  
Old 11-18-2011
You really should take the time to read the book Linux Device Drivers by Corbet, Rubini and Kroah-Hartman. Chapter 12 describes PCI drivers in considerable detail.
# 7  
Old 11-18-2011
Quote:
Originally Posted by Chrisdot
May I count on you a bit?
I see, that while performing:

Code:
static int __init e1000_init_module(void)
{
       return  pci_register_driver(&e1000_driver);
}

function pointed by .probe is called.

Code:
/* PCI Device API Driver */
static struct pci_driver e1000_driver = {
       .name     = e1000e_driver_name,
       .id_table = e1000e_pci_tbl,
       .probe    = e1000_probe,
       .remove   = __devexit_p(e1000_remove),
       .shutdown = e1000_shutdown,
};

Is that behaviour described somewhere?
I suppose you've checked inside Documentation/PCI/ inside the kernel source code?

My understanding of them is far from perfect, mostly limited to editing in extra PCI ID's when manufacturers do shell games me. I don't perfectly understand the internal details, and if I ever did my knowledge would go obsolete in weeks. ;p But I've done a lot of programming making/using/fixing plugin libraries, which makes a lot of what I see in device drivers look familiar.

Imagine a plugin for a media player -- maybe a software visualizer of some sort. The plugin would look like this:

Code:
// these names don't get exported in the library.  They're local-only.
static int visualizer_open(void) { return(0); }
static int visualizer_close(void) { return(0); }
static int visualizer_handledata(void *data, int len)
{
        fprintf(stderr, "%d bytes at %p\n", len, data);
        show_music_on_screen_in_amusing_manner(data, len);
        return(0);
}

struct function
{
        char *formats_supported[4];
        int (*open)(void);
        int (*close)(void);
        int (*handledata)(void *data, int len);
} functions={
       { "wav", "mp3", "ogg", NULL },
       visualizer_open, visualizer_close, visualizer_handledata
};

// This function IS exported.
struct function *get_callbacks(void)  {        return(&functions); }

To use it, the media player opens the library, looks up get_callbacks and calls it.

Code:
struct function
{
        char *formats_supported[4];
        int (*open)(void);
        int (*close)(void);
        int (*handledata)(void *data, int len);
} *functions;

void *lib=dlopen("crazylinesvisualizer.so", RTLD_NOW);
void *(*getcallback)=dlsym(lib, "get_callbacks");
dlclose(lib);

functions=getcallback();

if(strcmp(functions->formats_supported[0], "wav") != 0)
{
        fprintf(stderr, "visualizer doesn't support wav");
        exit(1);
}

functions->open();
whle(music) functions->handledata(data, len);
functions->close();

Bundling them in a structure like that makes it more efficient -- one load operation instead of one for each function -- and more organized.

Kernel device drivers work just like this. Look in a device driver for a character device and you'll find a big structure containing pointers the driver's own personal open(), close(), read(), write(), ioctl(), mmap(), kitchen_sink(), and other such functions which get called whenever a user calls open, read, write, ioctl, mmap, etc. on that device file in userspace. Even when you build drivers in, they're still in a great big table. That's how the kernel finds them, how the kernel decides what order to initialize them, and how it decides which to use for what PCI id.
Quote:
As I remember, exactly at this moment (pci_register_driver) driver announce set of device IDs he support
I don't think the driver "announces" it, it just keeps a list in that big structure. The kernel checks if it matches any present devices and, if not, doesn't bother initializing it.

Non-PNP things might have to do manual probing to see if a device exists.

Last edited by Corona688; 11-18-2011 at 12:00 PM..
These 2 Users Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Compiling virtual network adapter driver problem (net_device struct)

Hi, I found on linuxgazette.net/93/bhaskaran.html page very useful sample of virtual driver (not connected to real hardware). I try to compile it with no effect. So: I got fresh Ubuntu 9.10 (kernel 2.6.31-14) My source is saved in networkAdapter.c file in /usr/src/myModules directory. I... (21 Replies)
Discussion started by: Chrisdot
21 Replies

2. UNIX for Dummies Questions & Answers

Simulated driver for Network Interface Adapter

Hi all, I got sort of a task to do. I have to write in C "simulated network driver". What does it mean? - It has to run on all network adapters - It has to be as simple as it can be - It has to run on all linux distributions (or at least most of them) - It does not have to run a network... (4 Replies)
Discussion started by: Chrisdot
4 Replies

3. Solaris

Can I Install the Gani Network Driver Using the Application that Came on the CD?

When I boot up the Solaris 10 5/09 install CD and select 'Solaris' from the GRUB menu that comes up, a menu loads. Option 5 is 'Apply Driver Updates'. Can I install the Gani driver using that? I tried using the tar file (the way it came) that I wrote to a floppy but when I asked it to look at the... (8 Replies)
Discussion started by: Bradj47
8 Replies

4. Red Hat

How to know whether the network driver alredy installed in my system

I am using NVIDIA nforce: Networking controllers And Redhat enterprice linux ws version 4 32 bit: OS I want to connect my system to Broadband internet using linux. From where can I download the driver for nertwork. How to know whether the network driver alredy installed in my system. ... (0 Replies)
Discussion started by: rijas
0 Replies

5. Solaris

network driver not detecting in solaris 10 X86 on HPDL380G5 Server

I have installed solaris10 x86 on HP DL380 G5 Server, but network card is not getting detected. i have installed the network driver, downloaded from the following link HP ProLiant DL380 G5 Server series- Download drivers and software - HP Business Support Center Can any one suggest me how to... (1 Reply)
Discussion started by: raj.chinnu
1 Replies

6. Solaris

Installing Network on Computer, might be driver

Hi All, Just completing my second Solaris installation, in the previous one which was on a Dell X64 machine, I went through the Network configuration setting, on the current computer which I am installing Solaris on, its a custom built machine and for some reason, I didnt see the screen where I... (1 Reply)
Discussion started by: platforminc
1 Replies

7. Solaris

network driver cpu usage

Hello all, Needed a suggestion from you all, if you know anything about this stuff. We have a high network traffic application. Close to around 700Meg /sec on one NIC. When the traffic is around 200Meg on the NIC, the VCPU(not the CPU, cause we have 24 VCPU) utilization by the NIC driver... (5 Replies)
Discussion started by: Naanu
5 Replies

8. Programming

Network device driver

HI, I am writing a network device driver for RTL8139c card on 2.6.18 kernel ... I am facing few queries listed below 1. Can i able to at all write a driver for RTL8139C or Realtek had designed new chip for 2.6 series kernel? 2. If no then which driver file 2.6.18 uses .. Is it 8139too.c or... (1 Reply)
Discussion started by: niketan
1 Replies
Login or Register to Ask a Question