Exporter Usage

How-To Export IPFIX

Each fixbuf application must have a single fbInfoModel_t instance that represents the Information Elements that the application understands. The fbInfoModelAlloc() call allocates a new Information Model with the IANA-managed information elements (current as of the fixbuf release date) preloaded. Additional vendor-specific information elements may be added with fbInfoModelAddElement(), fbInfoModelAddElementArray(), fbInfoModelReadXMLFile(), and fbInfoModelReadXMLData().

To create an Exporter, first create an fbSession_t attached to the application's fbInfoModel_t to hold the Exporter's Transport Session state using fbSessionAlloc().

Then create an fbExporter_t to encapsulate the connection to the Collecting Process or the file on disk, using the fbExporterAllocFP(), fbExporterAllocFile(), fbExporterAllocNet(), or fbExporterAllocBuffer() calls.

With an fbSession_t and an fbExporter_t available, create a Buffer (fBuf_t) for writing via fBufAllocForExport().

Create and populate templates for addition to this session using fbTemplateAlloc() and fbTemplateAppendSpecArray(), fbTemplateAppendSpec(), fbTemplateAppend(), fbTemplateAppendArraySpecId(), or fbTemlateAppendSpecId(). The layout of the template usually matches the C struct that holds the record. For details see Template Definition.

Add the templates to the session via fbSessionAddTemplate(). Typically each template is added to the session twice, once as an internal template---which describes how the record appears in memory---and again as an external template---which describes how the record appears in the IPFIX message. A convenience function, fbSessionAddTemplatesForExport(), exists to do this for you.

In this tutorial, the output Buffer (the fBuf_t) is created before adding external Templates to the Session, and calling fbSessionAddTemplate() automatically adds the Templates to the Buffer. It is also possible to add Templates to a Session before creating the Buffer. In that case, one calls fbSessionExportTemplates() to add the Templates to the output Buffer. (fbSessionExportTemplates() writes all Templates to the output Buffer; calling it needlessly results in the Template definitions appearing multiple times in the output.)

To write data, set the internal and external template IDs with fBufSetInternalTemplate() and fBufSetExportTemplate() or with the convenience function fBufSetTemplatesForExport(). You can then use fBufAppend() to write records into IPFIX Messages and Messages to the output stream.

When exporting multiple record formats, you must call fBufSetTemplatesForExport() or the fBufSetInternalTemplate() and fBufSetExportTemplate() pair whenever the record format changes.

By default, fBufAppend() will emit an IPFIX Message to the output stream when the end of the message buffer is reached on write. The fBufSetAutomaticNextMessage() call can be used to modify this behavior, causing fBufAppend() to return FB_ERROR_EOM when at end of message. Use this if your application requires manual control of message export. In this case, fBufEmit() will emit a Message to the output stream. If your exporter was created via fbExporterAllocBuffer(), you may use fbExporterGetMsgLen() to get the message length.

If not in automatic mode and a session's templates do not fit into a single message, use fbSessionExportTemplate() to export each template individually instead of relying on fbSessionExportTemplates().

Manual control of message export is incompatible with template and information element metadata (fbSessionSetMetadataExportTemplates() and fbSessionSetMetadataExportElements()). There are several functions that may cause the metadata options records to be exported, and the Session must be free to create a new record set or template set as needed.

Complete sample_exporter.c program:

#include <fixbuf/public.h>
#define FATAL(e) \
{ fprintf(stderr, "Failed at %s:%d: %s\n", \
__FILE__, __LINE__, e->message); \
exit(1); }
int main()
{
fbInfoElementSpec_t exportTemplate[] = {
{"flowStartMilliseconds", 8, 0 },
{"flowEndMilliseconds", 8, 0 },
{"octetTotalCount", 8, 0 },
{"packetTotalCount", 8, 0 },
{"sourceIPv4Address", 4, 0 },
{"destinationIPv4Address", 4, 0 },
{"sourceTransportPort", 2, 0 },
{"destinationTransportPort", 2, 0 },
{"protocolIdentifier", 1, 0 },
{"paddingOctets", 3, 0 },
{"ipPayloadPacketSection", 0, 0 },
};
struct exportRecord_st {
uint64_t flowStartMilliseconds;
uint64_t flowEndMilliseconds;
uint64_t octetTotalCount;
uint64_t packetTotalCount;
uint32_t sourceIPv4Address;
uint32_t destinationIPv4Address;
uint16_t sourceTransportPort;
uint16_t destinationTransportPort;
uint8_t protocolIdentifier;
uint8_t padding[3];
fbVarfield_t payload;
} exportRecord;
fbInfoModel_t *model;
fbSession_t *session;
fbExporter_t *exporter;
fbTemplate_t *tmpl;
fBuf_t *fbuf;
uint16_t tid;
int i;
GError *err = NULL;
memset(&exportRecord, 0, sizeof(exportRecord));
model = fbInfoModelAlloc();
// Use if needed to define elements used by YAF.
//if (!fbInfoModelReadXMLFile(model, "cert_ipfix.xml", &err))
// FATAL(err);
session = fbSessionAlloc(model);
exporter = fbExporterAllocFP(stdout);
fbuf = fBufAllocForExport(session, exporter);
tmpl = fbTemplateAlloc(model);
if (!fbTemplateAppendSpecArray(tmpl, exportTemplate, ~0, &err))
FATAL(err);
session, FB_TID_AUTO, tmpl, NULL, &err)))
FATAL(err);
// No need to call this since templates were added after
// the Session and Exporter were linked to the fBuf_t.
//if (!fbSessionExportTemplates(session, &err))
// FATAL(err);
if (!fBufSetTemplatesForExport(fbuf, tid, &err))
FATAL(err);
// This sample exports 5 records
for (i = 0; i < 5; ++i) {
// Fill the exportRecord with data
An IPFIX template or options template structure.
Definition: private.h:108
Fixbuf IPFIX protocol library public interface.
struct fbExporter_st fbExporter_t
IPFIX Exporting Process endpoint.
Definition: public.h:2077
fbSession_t * fbSessionAlloc(fbInfoModel_t *model)
Allocates an empty transport session state container.
fbInfoModel_t * fbInfoModelAlloc(void)
Allocates a new information model.
#define FB_TID_AUTO
Template ID argument used when adding an fbTemplate_t to an fbSession_t that automatically assigns a ...
Definition: public.h:1574
uint16_t fbSessionAddTemplatesForExport(fbSession_t *session, uint16_t tid, fbTemplate_t *tmpl, fbTemplateInfo_t *tmplInfo, GError **err)
Adds a Template to a Session being used for export as both an internal template and an external templ...
struct fbInfoModel_st fbInfoModel_t
An IPFIX information model.
Definition: public.h:164
struct fbSession_st fbSession_t
An IPFIX Transport Session state container.
Definition: public.h:1711
gboolean fBufSetTemplatesForExport(fBuf_t *fbuf, uint16_t tid, GError **err)
Sets the internal and external templates on a Buffer to the given template ID.
fBuf_t * fBufAllocForExport(fbSession_t *session, fbExporter_t *exporter)
Allocates a new buffer for export.
struct fBuf_st fBuf_t
An IPFIX message buffer.
Definition: public.h:139
fbExporter_t * fbExporterAllocFP(FILE *fp)
Allocates an exporting process endpoint for an opened ANSI C file pointer.
fbTemplate_t * fbTemplateAlloc(fbInfoModel_t *model)
Allocates a new empty template.
#define FB_IESPEC_NULL
Convenience macro defining a null information element specification initializer (fbInfoElementSpec_t)...
Definition: public.h:1596
gboolean fbTemplateAppendSpecArray(fbTemplate_t *tmpl, const fbInfoElementSpec_t *spec, uint32_t wantedFlags, GError **err)
Appends information elements described by a specifier array to a template.
A single IPFIX Information Element specification.
Definition: public.h:1606
A variable-length field value.
Definition: public.h:147
if (!fBufAppend(fbuf, (uint8_t *)&exportRecord,
sizeof(exportRecord), &err))
FATAL(err);
}
if (!fBufEmit(fbuf, &err))
FATAL(err);;
// This frees the Buffer, Session, Templates, and Exporter.
fBufFree(fbuf);
return 0;
}
// EndOfExample
void fBufFree(fBuf_t *fbuf)
Frees a buffer.
gboolean fBufEmit(fBuf_t *fbuf, GError **err)
Emits the message currently in a buffer using the associated exporting process endpoint.
void fbInfoModelFree(fbInfoModel_t *model)
Frees an information model.
gboolean fBufAppend(fBuf_t *fbuf, uint8_t *recbase, size_t recsize, GError **err)
Appends a record to a buffer.

Previous: Using libfixbuf in Your Program | Next: IPFIX File Collectors