2 ** exporter.c - example exporter
4 ** Copyright Fraunhofer FOKUS
13 #include <ipfix_def.h>
14 #include <ipfix_def_fokus.h>
15 #include <ipfix_fields_fokus.h>
22 static ipfix_datarecord_t g_data
= { NULL
, NULL
, 0 };
23 static int do_close
= 0;
25 struct wprobe_mapping
{
28 const char *wprobe_id
;
29 struct wprobe_value
*val
;
33 #define ARRAY_SIZE(_array) (sizeof(_array) / sizeof((_array)[0]))
36 #define WMAP(_id, _name, ...) \
39 .id = IPFIX_FT_WPROBE_##_id##_N, \
44 #define WMAP_COUNTER(_id, _name, ...) \
47 .id = IPFIX_FT_WPROBE_##_id, \
53 #define WPROBE_OFFSET 2
55 static struct wprobe_mapping map_globals
[] = {
57 WMAP(PHY_BUSY
, "phy_busy"),
58 WMAP(PHY_RX
, "phy_rx"),
59 WMAP(PHY_TX
, "phy_tx"),
60 WMAP_COUNTER(FRAMES
, "frames"),
61 WMAP_COUNTER(PROBEREQ
, "probereq"),
64 static struct wprobe_mapping map_perlink
[] = {
65 WMAP(IEEE_TX_RATE
, "tx_rate"),
66 WMAP(IEEE_RX_RATE
, "rx_rate"),
68 WMAP(SIGNAL
, "signal"),
69 WMAP(RETRANSMIT_200
, "retransmit_200"),
70 WMAP(RETRANSMIT_400
, "retransmit_400"),
71 WMAP(RETRANSMIT_800
, "retransmit_800"),
72 WMAP(RETRANSMIT_1600
, "retransmit_1600"),
75 static unsigned char link_local
[6];
76 static char link_default
[6];
77 static int nfields
= 0;
79 #define FOKUS_USERID 12325
82 match_template(struct wprobe_mapping
*map
, int n
, struct list_head
*list
)
84 struct wprobe_attribute
*attr
;
87 list_for_each_entry(attr
, list
, list
) {
88 for (i
= 0; i
< n
; i
++) {
89 j
= (last
+ 1 + i
) % n
;
90 if (!strcmp(attr
->name
, map
[j
].wprobe_id
))
96 map
[j
].val
= &attr
->val
;
97 memset(&attr
->val
, 0, sizeof(attr
->val
));
102 /* name: export_ipfix_get_template()
104 static ipfix_template_t
*
105 prepare_template(ipfix_t
*handle
)
107 ipfix_template_t
*t
= NULL
;
108 int size
= 3 * nfields
+ WPROBE_OFFSET
;
111 if (ipfix_new_data_template( handle
, &t
, size
) < 0) {
112 mlogf( 0, "ipfix_new_template() failed: %s\n", strerror(errno
) );
116 ipfix_add_field(handle
, t
, 0, IPFIX_FT_SOURCEMACADDRESS
, 6);
117 ipfix_add_field(handle
, t
, 0, IPFIX_FT_DESTINATIONMACADDRESS
, 6);
119 g_data
.lens
= calloc(size
, sizeof(g_data
.lens
[0]));
122 for (i
= WPROBE_OFFSET
; i
< size
; i
++)
125 g_data
.addrs
= calloc(size
, sizeof(g_data
.addrs
[0]));
126 g_data
.addrs
[0] = link_local
;
127 g_data
.maxfields
= WPROBE_OFFSET
;
132 add_template_fields(ipfix_t
*handle
, ipfix_template_t
*t
, struct wprobe_mapping
*map
, int n
)
134 int f
= g_data
.maxfields
;
137 for (i
= 0; i
< n
; i
++) {
142 g_data
.addrs
[f
++] = &map
[i
].val
->U32
;
144 g_data
.addrs
[f
++] = &map
[i
].val
->n
;
146 if (ipfix_add_field( handle
, t
, FOKUS_USERID
, map
[i
].id
+ 0, 4) < 0)
153 g_data
.addrs
[f
++] = &map
[i
].val
->s
;
156 g_data
.addrs
[f
++] = &map
[i
].val
->ss
;
157 if (ipfix_add_field( handle
, t
, FOKUS_USERID
, map
[i
].id
+ 1, 8) < 0)
159 if (ipfix_add_field( handle
, t
, FOKUS_USERID
, map
[i
].id
+ 2, 8) < 0)
162 g_data
.maxfields
= f
;
166 wprobe_dump_data(ipfix_t
*ipfixh
, ipfix_template_t
*ipfixt
, struct wprobe_iface
*dev
)
168 struct wprobe_link
*link
;
170 wprobe_update_links(dev
);
171 wprobe_request_data(dev
, NULL
);
172 if (list_empty(&dev
->links
)) {
173 g_data
.addrs
[1] = link_default
;
174 ipfix_export_array(ipfixh
, ipfixt
, g_data
.maxfields
, g_data
.addrs
, g_data
.lens
);
175 ipfix_export_flush(ipfixh
);
177 list_for_each_entry(link
, &dev
->links
, list
) {
178 g_data
.addrs
[1] = link
->addr
;
179 wprobe_request_data(dev
, link
->addr
);
180 ipfix_export_array(ipfixh
, ipfixt
, g_data
.maxfields
, g_data
.addrs
, g_data
.lens
);
181 ipfix_export_flush(ipfixh
);
185 int main ( int argc
, char **argv
)
187 struct wprobe_iface
*dev
= NULL
;
188 ipfix_template_t
*ipfixt
= NULL
;
189 ipfix_t
*ipfixh
= NULL
;
190 int protocol
= IPFIX_PROTO_TCP
;
193 int sourceid
= 12345;
194 int port
= IPFIX_PORTNO
;
195 int verbose_level
= 0;
199 while ((opt
= getopt(argc
, argv
, "hi:c:p:vstu")) != EOF
) {
202 if ((port
=atoi(optarg
)) <0) {
203 fprintf( stderr
, "Invalid -p argument!\n" );
215 protocol
= IPFIX_PROTO_SCTP
;
219 protocol
= IPFIX_PROTO_TCP
;
223 protocol
= IPFIX_PROTO_UDP
;
232 fprintf(stderr
, "usage: %s [-hstuv] -i <interface> -c <collector> [-p portno]\n"
234 " -i <interface> wprobe interface\n"
235 " -c <collector> collector address\n"
236 " -p <portno> collector port number (default=%d)\n"
237 " -s send data via SCTP\n"
238 " -t send data via TCP (default)\n"
239 " -u send data via UDP\n"
240 " -v increase verbose level\n\n",
241 argv
[0], IPFIX_PORTNO
);
247 fprintf(stderr
, "No interface specified\n");
252 fprintf(stderr
, "No collector specified\n");
256 dev
= wprobe_get_auto(ifname
, &err
);
257 if (!dev
|| (list_empty(&dev
->global_attr
) && list_empty(&dev
->link_attr
))) {
258 fprintf(stderr
, "Cannot connect to wprobe on interface '%s': %s\n", ifname
, (err
? err
: "Unknown error"));
262 match_template(map_globals
, ARRAY_SIZE(map_globals
), &dev
->global_attr
);
263 match_template(map_perlink
, ARRAY_SIZE(map_perlink
), &dev
->link_attr
);
265 fprintf(stderr
, "No usable attributes found\n");
269 mlog_set_vlevel( verbose_level
);
270 if (ipfix_init() < 0) {
271 fprintf( stderr
, "cannot init ipfix module: %s\n", strerror(errno
) );
275 ipfix_add_vendor_information_elements(ipfix_ft_fokus
);
276 if (ipfix_open(&ipfixh
, sourceid
, IPFIX_VERSION
) < 0) {
277 fprintf( stderr
, "ipfix_open() failed: %s\n", strerror(errno
) );
281 if (ipfix_add_collector( ipfixh
, chost
, port
, protocol
) < 0) {
282 fprintf( stderr
, "ipfix_add_collector(%s,%d) failed: %s\n",
283 chost
, port
, strerror(errno
));
287 fprintf(stderr
, "Local link address: %02x:%02x:%02x:%02x:%02x:%02x\n",
288 link_local
[0], link_local
[1], link_local
[2],
289 link_local
[3], link_local
[4], link_local
[5]);
291 ipfixt
= prepare_template(ipfixh
);
292 add_template_fields(ipfixh
, ipfixt
, map_globals
, ARRAY_SIZE(map_globals
));
293 add_template_fields(ipfixh
, ipfixt
, map_perlink
, ARRAY_SIZE(map_perlink
));
297 wprobe_dump_data(ipfixh
, ipfixt
, dev
);
300 ipfix_delete_template( ipfixh
, ipfixt
);
301 ipfix_close( ipfixh
);