The ONL Tutorial

Tutorial >> Writing Your First Plugin TOC

Compiling a Plugin

An SPC runs a streamlined NetBSD kernel. Plugins are just modules loaded into the kernel. We compile our plugins on the host onlusr (a Linux host) for a NetBSD host (an SPC) by using a cross-compiler. The end result will be a loadable kernel module for a NetBSD operating system called combined.o.

If you are using a Makefile derived from the standard plugin directory ~onl/stdplugins/template/, compiling is trivial. For example, if you used the newplugins.pl perl script to create your initial code skeletons, the Makefile will allow you to enter the following to compile our new plugin using a cross-compiler:

    cd myplugins/mycounter-1100 # go to our plugin directory
    make                        # compile
    ... fix syntax errors and run make again until no errors ...
    exit			# return to previous host

The make command executes commands in the file Makefile. That's all there is to it! You should not have to modify the Makefile created by newplugin.pl. The first time you try compiling, you will probably get a few syntax errors which should be straightforward to find and correct.

The file Makefile describes how to make the loadable kernel module combined.o. The make command uses this description. After successfully making combined.o, you will also see that the object file mycounter.o was also created. While compiling, several header files are used in addition to your mycounter.h. Enter the following commands and note the lines displayed on your terminal:

    touch mycounter.c	# simulate a change to the .c file
    make -n             # don't really make combined.o
You should see the following output:
    /usr/local/xcomp/bin/i386--netbsdelf-gcc
            -DMSR -D_KERNEL -DMSR_PLUGIN -D_LKM
            -I/users/onl/wu_arl/msr/usr/src/sys -I.
            -c -o mycounter.o mycounter.c
    /usr/local/xcomp/bin/i386--netbsdelf-ld -r -o combined.o mycounter.o
    nm -u combined.o
That is, if you modify mycounter.c, the new combined.o can be made by first compiling mycounter.c to get the object module mycounter.o and then linking to produce the relocatable (-r flag) object module combined.o. Finally, check for undefined symbols by running the nm command.

The first -I flag includes the directory containing subdirectories of system headers; e.g., the directory /users/onl/wu_arl/msr/usr/src/sys/ contains subdirectories such as msr, sys and rp that contain headers used by plugins. For example, your stdinc.h file contains the following lines:

    #include <sys/param.h>
    ...
    
    #include <netinet/in_systm.h>
    #include <netinet/ip.h>
    
    #include <dev/ic/apicvar.h>
    #include <msr/msr.h>
    #include <msr/msr_util.h>
    ...
    
    #include <rp/rp.h>
    #include <rp/rp_plugin.h>
    #include <rp/rp_pcu.h>
The <msr/msr_util.h> include refers to the header file /users/onl/wu_arl/msr/usr/src/sys/msr/msr_util.h; i.e., the msr/msr_util.h file in the directory /users/onl/wu_arl/msr/usr/src/sys.

Now, we are ready to see if the plugin works; i.e., receives and counts packets.


 Revised:  Tue, Aug 15, 2006 

  
  

Tutorial >> Writing Your First Plugin TOC