|
NAMEsilk-plugin - Creating a SiLK run-time plug-in using CSYNOPSISsk_cc=`silk_config --compiler` sk_cflags=`silk_config --cflags` $sk_cc $sk_cflags -shared -o FILENAME.so FILENAME.c rwfilter --plugin=FILENAME.so [--plugin=FILENAME.so ...] ... rwcut --plugin=FILENAME.so [--plugin=FILENAME.so ...] --fields=FIELDS ... rwgroup --plugin=FILENAME.so [--plugin=FILENAME.so ...] --id-fields=FIELDS ... rwsort --plugin=FILENAME.so [--plugin=FILENAME.so ...] --fields=FIELDS ... rwstats --plugin=FILENAME.so [--plugin=FILENAME.so ...] --fields=FIELDS --values=VALUES ... rwuniq --plugin=FILENAME.so [--plugin=FILENAME.so ...] --fields=FIELDS --values=VALUES ... DESCRIPTIONSeveral of the SiLK analysis tools allow the user to augment the tools' functionality through the use of plug-ins that get loaded at run-time. These tools are:
In addition, all of the above tools support adding new command line switches that can be used to initialize the plug-in itself (for example, to load an auxiliary file that the plug-in requires). The plug-ins for all tools except rwptoflow can be written in either C or using PySiLK (the SiLK Python extension, see pysilk (3)). Although the execution time for PySiLK plug-ins is slower than for C plug-ins, we encourage you to use PySiLK for your plug-ins since the time-to-result can be faster for PySiLK: The faster development time in Python typically more than compensates for the slower execution time. Once you find that your PySiLK plug-in is seeing a great deal of use, or that PySiLK is just too slow for the amount of data you are processing, then re-write the plug-in using C. Even when you intend to write a plug-in using C, it can be helpful to prototype your plug-in using PySiLK. The remainder of this document explains how to create a plug-in for the SiLK analysis tools (except rwptoflow) using the C programming language. For information on creating a plug-in using PySiLK, see silkpython(3). A template file for plug-ins is included in the SiLK source tree, in the silk-VERSION/src/template/c-plugin.c file. The setup functionWhen you provide "--plugin=my-plugin.so" on the command line to an application, the application loads the my-plugin.so file and calls a setup function in that file to determine the new switches and/or fields that "my-plugin.so" provides.This setup function is called with three arguments: the first two describe the version of the plug-in API, and the third is a pointer that is currently unused. skplugin_err_t SKPLUGIN_SETUP_FN( uint16_t major_version, uint16_t minor_version, void *plug_in_data) { ... } There are several tasks this setup function may do: (1) check the API version, (2) register new command line switches (if any), (3) register new filters (if any), and (4) register new fields (if any). Let's describe these in more detail. (1) Check the API version The setup function should ensure that the plug-in and the application agree on the API to use. This provides protection in case the SiLK API to plug-ins changes in the future. To make this determination, call the skpinSimpleCheckVersion() function. A typical invocation is shown here, where the "major_version" and "minor_version" were passed into the SKPLUGIN_SETUP_FN, and "PLUGIN_API_VERSION_MAJOR" and "PLUGIN_API_VERSION_MINOR" are macros defined in the template file to the current version of the API. #define PLUGIN_API_VERSION_MAJOR 1 #define PLUGIN_API_VERSION_MINOR 0 /* Check the plug-in API version */ rv = skpinSimpleCheckVersion(major_version, minor_version, PLUGIN_API_VERSION_MAJOR, PLUGIN_API_VERSION_MINOR, skAppPrintErr); if (rv != SKPLUGIN_OK) { return rv; } (2) Register command line switches If the plug-in wants to define new command line switches, those switches must be registered in the setup function. A typical use of a command line switch is to allow the user to configure the plug-in; for example, the switch may allow the user to specify the location of an auxiliary input file that the plug-in requires, or to set a parameter used by the plug-in. A second use for a command line switch is more subtle. When creating a plug-in for rwfilter, you may want your plug-in to provide several similar features, and only enable each feature when the user requests it via a command line switch. For this case, you want to delay registering the filter until the command line switch is seen, in which case the filter registration function should be invoked in the switch's callback function. Information on registering a command line switch is available below ("Registering command line switches"). (3) Register filters You only need to register filters when the plug-in will be used by rwfilter(1). You may choose to register the filters in the setup function; if you do, the filter will always be used when the plug-in is loaded by rwfilter. If you the plug-in provides several filtering functions that the user may choose from via command line switches, you should call the filter registration function in the callback function for the command line switch. See "Registering filter functions" for details on registering a function to use with rwfilter. (4) Register fields If you want your plug-in to create a new printable field for rwcut(1), a new sorting field for rwsort(1), a new grouping field for rwgroup (1), rwstats(1), or rwuniq(1), or a new aggregate value field for rwstats or rwuniq, you should register those fields in the setup function. (While you can register the fields in a switch's callback function, there is usually little reason to do so.) There are two interfaces to registering a new field:
Registering command line switchesWhen you register a switch, the two important pieces of information you must provide are a name for the switch and a callback function. When the application encounters the command line switch registered by your plug-in, the application will invoke the callback function with the parameter that the user provided (if any) to the command line switch.To register a command line switch, call the skpinRegOption2() function: skplugin_err_t skpinRegOption2( const char *option_name, skplugin_arg_mode_t mode, const char *option_help_string, skplugin_help_fn_t option_help_fn, skplugin_option_fn_t opt_process_fn, void *opt_callback_data, int num_fn_mask, ...); /* list of skplugin_fn_mask_t */ The parameters are
Registering filter functionsWhen you register a filter function, you are specifying a function that rwfilter will call for every SiLK Flow record that rwfilter reads from its input files. If the function returns "SKPLUGIN_FILTER_PASS", rwfilter writes the record into the stream(s) specified by --pass. The record goes to the --fail streams if the function returns "SKPLUGIN_FILTER_FAIL".(The previous paragraph is true only when the plug-in is the only filtering predicate. When multiple tests are specified on the rwfilter command line, rwfilter will put the record into the fail destination as soon as any test fails. If there are multiple tests, your plug-in function will only see records that have not yet failed a test. If a plug-in filter function follows your function, it may fail a record that your filter function passed.) To register a filter function, call the following function: skplugin_err_t skpinRegFilter( skplugin_filter_t **filter_handle, const skplugin_callbacks_t *regdata, void *cbdata);
The function's return value will be "SKPLUGIN_OK" unless the "filter" member of the "regdata" structure is NULL. If your plug-in registers a filter function and the plug-in is used in an application other that rwfilter, the call to skpinRegFilter() is a no-op. Simple field registration functionsUsing a plug-in, you can augment the keys available in the --fields switch on rwcut(1), rwgroup(1), rwsort(1), rwstats(1), and rwuniq(1), and provide new aggregate value fields for the --values switch on rwstats and rwuniq.The standard field registration function, skpinRegField(), is powerful---for example, you can control exactly how the value you compute will be printed. However, that power comes with complexity. Many times, all your plug-in needs to do is to compute a value, and having to write a function to print a number is work with little reward. The functions in this section handle the registration of common field types. All of these functions require a name for the new field. The name is used as one of the arguments to the --fields or --values switch, and the name will also be used as the title when the field is printed (as in rwcut). Field names are case insensitive, and all field names must be unique within an application. You will get a run-time error if you attempt to create a field whose name already exists. (In rwuniq and rwstats, you may have a --fields key and a --values aggregate value with the same name.) The callback functions dealing with integers use "uint64_t" for convenience, but internally the value will be stored in a smaller integer field if possible. Specifying the "max" parameter to the largest value you actually use may allow SiLK to use a smaller integer field. The functions in this section return "SKPLUGIN_OK" unless the callback function is NULL. Integer key field The following function is used to register a key field whose value is an unsigned 64 bit integer. skplugin_err_t skpinRegIntField( const char *name, uint64_t min, uint64_t max, skplugin_int_field_fn_t rec_to_int, size_t width);
IPv4 key field The following function registers a new key field whose value is an IPv4 address. skplugin_err_t skpinRegIPv4Field( const char *name, skplugin_ipv4_field_fn_t rec_to_ipv4, size_t width);
IP key field The following function is used to register a key field whose value is any IP address (an "skipaddr_t"). skplugin_err_t skpinRegIPAddressField( const char *name, skplugin_ip_field_fn_t rec_to_ipaddr, size_t width);
Text key field (from an integer) The following function is used to register a key field whose value is an unsigned 64 bit integer (similar to skpinRegIntField()), but where the printed representation of the field is determined by a second callback function. This allows the plug-in to create arbitrary text for the field. skplugin_err_t skpinRegTextField( const char *name, uint64_t min, uint64_t max, skplugin_int_field_fn_t value_fn, skplugin_text_field_fn_t text_fn, size_t width);
Text key field (from a list) The following function is used to register a field whose value is one of a list of strings. The plug-in provides the list of strings and a callback that takes a SiLK Flow record and returns an index into the list of strings. skplugin_err_t skpinRegStringListField( const char *name, const char **list, size_t entries, const char *default_value, skplugin_int_field_fn_t rec_to_index, size_t width);
Integer sum aggregate value field The following function registers an aggregate value field that maintains a running unsigned integer sum. That is, the values returned by the callback are summed for every SiLK Flow record that matches a bin's key. The sum is printed when the bin is printed. skplugin_err_t skpinRegIntSumAggregator( const char *name, uint64_t max, skplugin_int_field_fn_t rec_to_int, size_t width);
Integer minimum or maximum aggregate value field The following function registers an aggregate value field that maintains the minimum integer value seen among all values returned by the callback function. skplugin_err_t skpinRegIntMinAggregator( const char *name, uint64_t max, skplugin_int_field_fn_t rec_to_int, size_t width); This function is similar, except it maintains the maximum value. skplugin_err_t skpinRegIntMaxAggregator( const char *name, uint64_t max, skplugin_int_field_fn_t rec_to_int, size_t width);
Unsigned integer aggregate value field The following function registers an aggregate value field that can be represented by a 64 bit integer. The plug-in must register two callback functions. The first takes a SiLK Flow record and returns an integer value; the second takes two integer values (as returned by the first callback function) and combines them to form a new aggregate value. skplugin_err_t skpinRegIntAggregator( const char *name, uint64_t max, skplugin_int_field_fn_t rec_to_int, skplugin_agg_fn_t agg, uint64_t initial, size_t width);
Advanced field registration functionWhen the simple field registration functions do not provide what you need, you can use the skpinRegField() function that gives you complete control over the field.skpinRegField() registers a new derived field for record processing. The plug-in must supply the name of the new field. The name is used as one of the arguments to the --fields switch (for key fields) or --values switch (for aggregate value fields). Field names are case insensitive, and all field names must be unique within an application. You will get a run-time error if you attempt to create a field whose name already exists. (In rwuniq and rwstats, you may have a --fields key and a --values aggregate value with the same name.) The skpinRegField() function requires you initialize and pass in a structure. In this structure you will specify the callback functions that the application will call, as well as additional information required by some applications. Although the structure is complex, not all applications use all members. If the plug-in is loaded by an application that does not support fields (such as rwfilter), the function is a no-op. The advanced field registration function is skplugin_err_t skpinRegField( skplugin_field_t **return_field, const char *name, const char *description, const skplugin_callbacks_t *regdata, void *cbdata);
The structure used by the skpinRegField() (and skpinRegFilter()) functions to specify callback functions is shown here: typedef struct skplugin_callbacks_st { skplugin_callback_fn_t init; skplugin_callback_fn_t cleanup; size_t column_width; size_t bin_bytes; skplugin_text_fn_t rec_to_text; skplugin_bin_fn_t rec_to_bin; skplugin_bin_fn_t add_rec_to_bin; skplugin_bin_to_text_fn_t bin_to_text; skplugin_bin_merge_fn_t bin_merge; skplugin_bin_cmp_fn_t bin_compare; skplugin_filter_fn_t filter; skplugin_transform_fn_t transform; const uint8_t *initial; const char **extra; } skplugin_callbacks_t; All of the callback functions reference in this structure take "cbdata" as a parameter, which is the value that was specified in the call to skpinRegField(). The "extra" parameter to the callback functions is used in complex plug-ins and can be ignored. The members of the structure are:
Once a field is registered, you may make changes to it by calling the additional functions described below. In each of these functions, the "field" parameter is the handle returned when the field was registered. By default, the "name" will also be used as the field's title. To specify a different title, the plug-in may call skplugin_err_t skpinSetFieldTitle( skplugin_field_t field, const char title); To create an alternate name for the field (that is, a name that can be used in the --fields or --values switches) call skplugin_err_t skpinAddFieldAlias( skplugin_field_t field, const char alias); To set or modify the textual and binary widths for a field, use the following function. This function should called in the field's "init" callback function. skplugin_err_t skpinSetFieldWidths( skplugin_field_t field, size_t field_width_text, size_t field_width_bin); The following table shows when a member of the "skplugin_callbacks_t" structure is required or optional. (Where the table shows "column_width" and "bin_bytes" as required, the values can be set in the structure or via the skpinSetFieldWidths() function.) rwfilter rwcut rwgroup rwsort rwstats rwuniq rwptoflow init r f f f f,a f,a r cleanup r f f f f,a f,a r column_width . F . . F,A F,A . bin_bytes . . F F F,A F,A . rec_to_text . F . . . . . rec_to_bin . . F F F F . add_rec_to_bin . . . . A A . bin_to_text . . . . F,A F,A . bin_merge . . . . A A . bin_compare . . . . A . . initial . . . . a a . filter R . . . . . . transform . . . . . . R extra r f f f f,a f,a r The legend is
Miscellaneous functionsThe following registers a cleanup function for the plug-in. This function will be called by the application after any field- or filter-specific cleanup functions are called. Specifically, this is the last callback that the application will invoke on a plug-in.skplugin_err_t skpinRegCleanup( skplugin_cleanup_fn_t cleanup); The signature of the "cleanup" function is: void cleanup(void); The plug-in author should invoke the following function to tell rwfilter that this plug-in is not thread safe. Calling this function causes rwfilter not use multiple threads; as such, this function should only be called when the plug-in has registered an active filter function. void skpinSetThreadNonSafe(void); Compiling the plug-inOnce you have finished writing the C code for the plug-in, save it in a file. The following uses the name my-plugin.c for the name of this file.In the following, the leading dollar sign ("$") followed by a space represents the shell prompt. The text after the dollar sign represents the command line. Lines have been wrapped for improved readability, and the back slash ("\") is used to indicate a wrapped line. When compiling a plug-in, you should use the same compiler and compiler-options as when SiLK was compiled. The silk_config(1) utility can be used to obtain that information. To store the compiler used to compile SiLK into the variable "sk_cc", specify the following at a shell prompt (note that those are backquotes, and this assumes a Bourne-compatible shell): $ sk_cc=`silk_config --compiler` To get the compiler flags used to compile SiLK: $ sk_cflags=`silk_config --cflags` Using those two variables, you can now compile the plug-in. The following will work on Linux and Mac OS X: $ $sk_cc $sk_cflags -shared -o my-plugin.so my-plugin.c For Mac OS X: $ $sk_cc $sk_cflags -bundle -flat_namespace -undefined suppress \ -o my-plugin.so my-plugin.c If there are compilation errors, fix them and compile again. Notes: The preceding assumed you were building the plug-in after having installed SiLK. The paths given by silk_config do not work if SiLK has not been installed. To compile the plug-in, you must have access to the SiLK header files. (If you are using an RPM installation of SiLK, ensure that the "silk-devel" RPM is installed.) Once you have created the my-plugin.so file, you can load it into an application by using the --plugin switch on the application as shown in the "SYNOPSIS". When loading a plug-in from the current directly, it is best to prefix the filename with "./": $ rwcut --plugin=./my-plugin.so ... If there are problems loading the plug-in into the application, you can trace the actions the application is doing by setting the SILK_PLUGIN_DEBUG environment variable: $ SILK_PLUGIN_DEBUG=1 rwcut --plugin=./my-plugin.so ... EXAMPLESrwfilterSuppose you want to find traffic destined to a particular host, 10.0.0.23, that is either ICMP or coming from 1434/udp. If you attempt to use:$ rwfilter --daddr=10.0.0.23 --proto=1,17 --sport=1434 \ --pass=outfile.rw flowrec.rw the --sport option will not match any of the ICMP traffic, and your result will not contain ICMP records. To avoid having to use two invocations of rwfilter, you can create the following plug-in to do the entire check in a single pass: #include <silk/silk.h> #include <silk/rwrec.h> #include <silk/skipaddr.h> #include <silk/skplugin.h> #include <silk/utils.h> /* These variables specify the version of the SiLK plug-in API. */ #define PLUGIN_API_VERSION_MAJOR 1 #define PLUGIN_API_VERSION_MINOR 0 /* ip to search for */ static skipaddr_t ipaddr; /* * status = filter(rwrec, reg_data, extra); * * The function should examine the SiLK flow record and return * SKPLUGIN_FILTER_PASS to write the rwRec to the * pass-destination(s) or SKPLUGIN_FILTER_FAIL to write it to the * fail-destination(s). */ static skplugin_err_t filter( const rwRec *rwrec, void *reg_data, void **extra) { skipaddr_t dip; rwRecMemGetDIP(rwrec, &dip); if (0 == skipaddrCompare(&dip, &ipaddr) && (rwRecGetProto(rwrec) == 1 || (rwRecGetProto(rwrec) == 17 && rwRecGetSPort(rwrec) == 1434))) { return SKPLUGIN_FILTER_PASS; } return SKPLUGIN_FILTER_FAIL; } /* The set-up function that the application will call. */ skplugin_err_t SKPLUGIN_SETUP_FN( uint16_t major_version, uint16_t minor_version, void *plug_in_data) { uint32_t ipv4; skplugin_err_t rv; skplugin_callbacks_t regdata; /* Check the plug-in API version */ rv = skpinSimpleCheckVersion(major_version, minor_version, PLUGIN_API_VERSION_MAJOR, PLUGIN_API_VERSION_MINOR, skAppPrintErr); if (rv != SKPLUGIN_OK) { return rv; } /* set global ipaddr */ ipv4 = ((10 << 24) | 23); skipaddrSetV4(&ipaddr, &ipv4); /* register the filter */ memset(®data, 0, sizeof(regdata)); regdata.filter = filter; return skpinRegFilter(NULL, ®data, NULL); } Once this file is created and compiled, you can use it from rwfilter as shown here: $ rwfilter --plugin=./my-plugin.so --pass=outfile.rw flowrec.rw Additional examplesFor additional examples, see the source files in silk-VERSION/src/plugins.ENVIRONMENT
FILES
SEE ALSOrwfilter(1), rwcut(1), rwgroup(1), rwsort(1), rwstats(1), rwuniq(1), silk_config(1), rwptoflow(1), pysilk(3), silkpython(3), flowrate(3), silk(7), pcap (3)
Visit the GSP FreeBSD Man Page Interface. |