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>
21 static ipfix_datarecord_t g_data
= { NULL
, NULL
, 0 };
22 static int do_close
= 0;
24 struct wprobe_mapping
{
27 const char *wprobe_id
;
28 struct wprobe_value
*val
;
32 #define ARRAY_SIZE(_array) (sizeof(_array) / sizeof((_array)[0]))
35 #define WMAP(_id, _name, ...) \
38 .id = IPFIX_FT_WPROBE_##_id##_AVG, \
43 #define WPROBE_OFFSET 2
45 static struct wprobe_mapping map_globals
[] = {
47 WMAP(PHY_BUSY
, "phy_busy"),
48 WMAP(PHY_RX
, "phy_rx"),
49 WMAP(PHY_TX
, "phy_tx"),
52 static struct wprobe_mapping map_perlink
[] = {
53 WMAP(IEEE_TX_RATE
, "tx_rate", .scale
= 10.0f
),
54 WMAP(IEEE_RX_RATE
, "rx_rate", .scale
= 10.0f
),
56 WMAP(SIGNAL
, "signal"),
57 WMAP(RETRANSMIT_200
, "retransmit_200"),
58 WMAP(RETRANSMIT_400
, "retransmit_400"),
59 WMAP(RETRANSMIT_800
, "retransmit_800"),
60 WMAP(RETRANSMIT_1600
, "retransmit_1600"),
63 static unsigned char link_local
[6];
64 static char link_default
[6];
65 static LIST_HEAD(global_attr
);
66 static LIST_HEAD(link_attr
);
67 static LIST_HEAD(links
);
68 static int nfields
= 0;
70 #define FOKUS_USERID 12325
73 match_template(struct wprobe_mapping
*map
, int n
, struct list_head
*list
)
75 struct wprobe_attribute
*attr
;
78 list_for_each_entry(attr
, list
, list
) {
79 for (i
= 0; i
< n
; i
++) {
80 j
= (last
+ 1 + i
) % n
;
81 if (!strcmp(attr
->name
, map
[j
].wprobe_id
))
87 map
[j
].val
= &attr
->val
;
88 memset(&attr
->val
, 0, sizeof(attr
->val
));
93 /* name: export_ipfix_get_template()
95 static ipfix_template_t
*
96 prepare_template(ipfix_t
*handle
)
98 ipfix_template_t
*t
= NULL
;
99 int size
= 3 * nfields
+ WPROBE_OFFSET
;
102 if (ipfix_new_data_template( handle
, &t
, size
) < 0) {
103 mlogf( 0, "ipfix_new_template() failed: %s\n", strerror(errno
) );
107 ipfix_add_field(handle
, t
, 0, IPFIX_FT_SOURCEMACADDRESS
, 6);
108 ipfix_add_field(handle
, t
, 0, IPFIX_FT_DESTINATIONMACADDRESS
, 6);
110 g_data
.lens
= calloc(size
, sizeof(g_data
.lens
[0]));
113 for (i
= WPROBE_OFFSET
; i
< size
; i
++)
116 g_data
.addrs
= calloc(size
, sizeof(g_data
.addrs
[0]));
117 g_data
.addrs
[0] = link_local
;
118 g_data
.maxfields
= WPROBE_OFFSET
;
123 add_template_fields(ipfix_t
*handle
, ipfix_template_t
*t
, struct wprobe_mapping
*map
, int n
)
125 int f
= g_data
.maxfields
;
128 for (i
= 0; i
< n
; i
++) {
132 g_data
.addrs
[f
++] = &map
[i
].val
->avg
;
133 g_data
.addrs
[f
++] = &map
[i
].val
->stdev
;
134 g_data
.addrs
[f
++] = &map
[i
].val
->n
;
136 if (ipfix_add_field( handle
, t
, FOKUS_USERID
, map
[i
].id
+ 0, 4) < 0)
138 if (ipfix_add_field( handle
, t
, FOKUS_USERID
, map
[i
].id
+ 1, 4) < 0)
140 if (ipfix_add_field( handle
, t
, FOKUS_USERID
, map
[i
].id
+ 2, 4) < 0)
143 g_data
.maxfields
= f
;
147 wprobe_dump_data(ipfix_t
*ipfixh
, ipfix_template_t
*ipfixt
, const char *ifname
, struct list_head
*gl
, struct list_head
*ll
, struct list_head
*ls
)
149 struct wprobe_link
*link
;
151 wprobe_update_links(ifname
, ls
);
152 wprobe_request_data(ifname
, gl
, NULL
, 2);
153 if (list_empty(ls
)) {
154 g_data
.addrs
[1] = link_default
;
155 ipfix_export_array(ipfixh
, ipfixt
, g_data
.maxfields
, g_data
.addrs
, g_data
.lens
);
156 ipfix_export_flush(ipfixh
);
158 list_for_each_entry(link
, ls
, list
) {
159 g_data
.addrs
[1] = link
->addr
;
160 wprobe_request_data(ifname
, ll
, link
->addr
, 2);
161 ipfix_export_array(ipfixh
, ipfixt
, g_data
.maxfields
, g_data
.addrs
, g_data
.lens
);
162 ipfix_export_flush(ipfixh
);
166 int main ( int argc
, char **argv
)
168 ipfix_template_t
*ipfixt
= NULL
;
169 ipfix_t
*ipfixh
= NULL
;
170 int protocol
= IPFIX_PROTO_TCP
;
173 int sourceid
= 12345;
174 int port
= IPFIX_PORTNO
;
175 int verbose_level
= 0;
178 while ((opt
= getopt(argc
, argv
, "hi:c:p:vstu")) != EOF
) {
181 if ((port
=atoi(optarg
)) <0) {
182 fprintf( stderr
, "Invalid -p argument!\n" );
194 protocol
= IPFIX_PROTO_SCTP
;
198 protocol
= IPFIX_PROTO_TCP
;
202 protocol
= IPFIX_PROTO_UDP
;
211 fprintf(stderr
, "usage: %s [-hstuv] -i <interface> -c <collector> [-p portno]\n"
213 " -i <interface> wprobe interface\n"
214 " -c <collector> collector address\n"
215 " -p <portno> collector port number (default=%d)\n"
216 " -s send data via SCTP\n"
217 " -t send data via TCP (default)\n"
218 " -u send data via UDP\n"
219 " -v increase verbose level\n\n",
220 argv
[0], IPFIX_PORTNO
);
226 fprintf(stderr
, "No interface specified\n");
231 fprintf(stderr
, "No collector specified\n");
235 if (wprobe_init() != 0) {
236 fprintf(stderr
, "wprobe init failed\n");
240 wprobe_dump_attributes(ifname
, false, &global_attr
, (char *) link_local
);
241 wprobe_dump_attributes(ifname
, true, &link_attr
, NULL
);
242 if (list_empty(&global_attr
) && list_empty(&link_attr
)) {
243 fprintf(stderr
, "Cannot connect to wprobe on interface '%s'\n", ifname
);
247 match_template(map_globals
, ARRAY_SIZE(map_globals
), &global_attr
);
248 match_template(map_perlink
, ARRAY_SIZE(map_perlink
), &link_attr
);
250 fprintf(stderr
, "No usable attributes found\n");
254 mlog_set_vlevel( verbose_level
);
255 if (ipfix_init() < 0) {
256 fprintf( stderr
, "cannot init ipfix module: %s\n", strerror(errno
) );
260 ipfix_add_vendor_information_elements(ipfix_ft_fokus
);
261 if (ipfix_open(&ipfixh
, sourceid
, IPFIX_VERSION
) < 0) {
262 fprintf( stderr
, "ipfix_open() failed: %s\n", strerror(errno
) );
266 if (ipfix_add_collector( ipfixh
, chost
, port
, protocol
) < 0) {
267 fprintf( stderr
, "ipfix_add_collector(%s,%d) failed: %s\n",
268 chost
, port
, strerror(errno
));
272 fprintf(stderr
, "Local link address: %02x:%02x:%02x:%02x:%02x:%02x\n",
273 link_local
[0], link_local
[1], link_local
[2],
274 link_local
[3], link_local
[4], link_local
[5]);
276 ipfixt
= prepare_template(ipfixh
);
277 add_template_fields(ipfixh
, ipfixt
, map_globals
, ARRAY_SIZE(map_globals
));
278 add_template_fields(ipfixh
, ipfixt
, map_perlink
, ARRAY_SIZE(map_perlink
));
282 wprobe_measure(ifname
);
288 wprobe_dump_data(ipfixh
, ipfixt
, ifname
, &global_attr
, &link_attr
, &links
);
291 ipfix_delete_template( ipfixh
, ipfixt
);
292 ipfix_close( ipfixh
);