2 * Copyright (c) 1998-2007 The TCPDUMP project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code
6 * distributions retain the above copyright notice and this paragraph
7 * in its entirety, and (2) distributions including binary code include
8 * the above copyright notice and this paragraph in its entirety in
9 * the documentation or other materials provided with the distribution.
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
15 * The SFLOW protocol as per http://www.sflow.org/developers/specifications.php
17 * Original code by Carles Kishimoto <carles.kishimoto@gmail.com>
19 * Expansion and refactoring by Rick Jones <rick.jones2@hp.com>
23 static const char rcsid
[] _U_
=
24 "@(#) $Header: /tcpdump/master/tcpdump/print-sflow.c,v 1.1 2007-08-08 17:20:58 hannes Exp $";
31 #include <tcpdump-stdinc.h>
37 #include "interface.h"
39 #include "addrtoname.h"
45 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | Sflow version (2,4,5) |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * | IP version (1 for IPv4 | 2 for IPv6) |
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * | IP Address AGENT (4 or 16 bytes) |
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * | Datagram sequence number |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 * | Switch uptime in ms |
58 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 * | num samples in datagram |
60 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 struct sflow_datagram_t
{
66 u_int8_t ip_version
[4];
74 struct sflow_sample_header
{
79 #define SFLOW_FLOW_SAMPLE 1
80 #define SFLOW_COUNTER_SAMPLE 2
81 #define SFLOW_EXPANDED_FLOW_SAMPLE 3
82 #define SFLOW_EXPANDED_COUNTER_SAMPLE 4
84 static const struct tok sflow_format_values
[] = {
85 { SFLOW_FLOW_SAMPLE
, "flow sample" },
86 { SFLOW_COUNTER_SAMPLE
, "counter sample" },
87 { SFLOW_EXPANDED_FLOW_SAMPLE
, "expanded flow sample" },
88 { SFLOW_EXPANDED_COUNTER_SAMPLE
, "expanded counter sample" },
92 struct sflow_flow_sample_t
{
94 u_int8_t typesource
[4];
98 u_int8_t in_interface
[4];
99 u_int8_t out_interface
[4];
104 struct sflow_expanded_flow_sample_t
{
111 u_int8_t in_interface_format
[4];
112 u_int8_t in_interface_value
[4];
113 u_int8_t out_interface_format
[4];
114 u_int8_t out_interface_value
[4];
118 #define SFLOW_FLOW_RAW_PACKET 1
119 #define SFLOW_FLOW_ETHERNET_FRAME 2
120 #define SFLOW_FLOW_IPV4_DATA 3
121 #define SFLOW_FLOW_IPV6_DATA 4
122 #define SFLOW_FLOW_EXTENDED_SWITCH_DATA 1001
123 #define SFLOW_FLOW_EXTENDED_ROUTER_DATA 1002
124 #define SFLOW_FLOW_EXTENDED_GATEWAY_DATA 1003
125 #define SFLOW_FLOW_EXTENDED_USER_DATA 1004
126 #define SFLOW_FLOW_EXTENDED_URL_DATA 1005
127 #define SFLOW_FLOW_EXTENDED_MPLS_DATA 1006
128 #define SFLOW_FLOW_EXTENDED_NAT_DATA 1007
129 #define SFLOW_FLOW_EXTENDED_MPLS_TUNNEL 1008
130 #define SFLOW_FLOW_EXTENDED_MPLS_VC 1009
131 #define SFLOW_FLOW_EXTENDED_MPLS_FEC 1010
132 #define SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC 1011
133 #define SFLOW_FLOW_EXTENDED_VLAN_TUNNEL 1012
135 static const struct tok sflow_flow_type_values
[] = {
136 { SFLOW_FLOW_RAW_PACKET
, "Raw packet"},
137 { SFLOW_FLOW_ETHERNET_FRAME
, "Ethernet frame"},
138 { SFLOW_FLOW_IPV4_DATA
, "IPv4 Data"},
139 { SFLOW_FLOW_IPV6_DATA
, "IPv6 Data"},
140 { SFLOW_FLOW_EXTENDED_SWITCH_DATA
, "Extended Switch data"},
141 { SFLOW_FLOW_EXTENDED_ROUTER_DATA
, "Extended Router data"},
142 { SFLOW_FLOW_EXTENDED_GATEWAY_DATA
, "Extended Gateway data"},
143 { SFLOW_FLOW_EXTENDED_USER_DATA
, "Extended User data"},
144 { SFLOW_FLOW_EXTENDED_URL_DATA
, "Extended URL data"},
145 { SFLOW_FLOW_EXTENDED_MPLS_DATA
, "Extended MPLS data"},
146 { SFLOW_FLOW_EXTENDED_NAT_DATA
, "Extended NAT data"},
147 { SFLOW_FLOW_EXTENDED_MPLS_TUNNEL
, "Extended MPLS tunnel"},
148 { SFLOW_FLOW_EXTENDED_MPLS_VC
, "Extended MPLS VC"},
149 { SFLOW_FLOW_EXTENDED_MPLS_FEC
, "Extended MPLS FEC"},
150 { SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC
, "Extended MPLS LVP FEC"},
151 { SFLOW_FLOW_EXTENDED_VLAN_TUNNEL
, "Extended VLAN Tunnel"},
155 #define SFLOW_HEADER_PROTOCOL_ETHERNET 1
156 #define SFLOW_HEADER_PROTOCOL_IPV4 11
157 #define SFLOW_HEADER_PROTOCOL_IPV6 12
159 static const struct tok sflow_flow_raw_protocol_values
[] = {
160 { SFLOW_HEADER_PROTOCOL_ETHERNET
, "Ethernet"},
161 { SFLOW_HEADER_PROTOCOL_IPV4
, "IPv4"},
162 { SFLOW_HEADER_PROTOCOL_IPV6
, "IPv6"},
166 struct sflow_expanded_flow_raw_t
{
167 u_int8_t protocol
[4];
169 u_int8_t stripped_bytes
[4];
170 u_int8_t header_size
[4];
173 struct sflow_ethernet_frame_t
{
180 struct sflow_extended_switch_data_t
{
181 u_int8_t src_vlan
[4];
183 u_int8_t dst_vlan
[4];
187 struct sflow_counter_record_t
{
192 struct sflow_flow_record_t
{
197 struct sflow_counter_sample_t
{
199 u_int8_t typesource
[4];
203 struct sflow_expanded_counter_sample_t
{
210 #define SFLOW_COUNTER_GENERIC 1
211 #define SFLOW_COUNTER_ETHERNET 2
212 #define SFLOW_COUNTER_TOKEN_RING 3
213 #define SFLOW_COUNTER_BASEVG 4
214 #define SFLOW_COUNTER_VLAN 5
215 #define SFLOW_COUNTER_PROCESSOR 1001
217 static const struct tok sflow_counter_type_values
[] = {
218 { SFLOW_COUNTER_GENERIC
, "Generic counter"},
219 { SFLOW_COUNTER_ETHERNET
, "Ethernet counter"},
220 { SFLOW_COUNTER_TOKEN_RING
, "Token ring counter"},
221 { SFLOW_COUNTER_BASEVG
, "100 BaseVG counter"},
222 { SFLOW_COUNTER_VLAN
, "Vlan counter"},
223 { SFLOW_COUNTER_PROCESSOR
, "Processor counter"},
227 #define SFLOW_IFACE_DIRECTION_UNKNOWN 0
228 #define SFLOW_IFACE_DIRECTION_FULLDUPLEX 1
229 #define SFLOW_IFACE_DIRECTION_HALFDUPLEX 2
230 #define SFLOW_IFACE_DIRECTION_IN 3
231 #define SFLOW_IFACE_DIRECTION_OUT 4
233 static const struct tok sflow_iface_direction_values
[] = {
234 { SFLOW_IFACE_DIRECTION_UNKNOWN
, "unknown"},
235 { SFLOW_IFACE_DIRECTION_FULLDUPLEX
, "full-duplex"},
236 { SFLOW_IFACE_DIRECTION_HALFDUPLEX
, "half-duplex"},
237 { SFLOW_IFACE_DIRECTION_IN
, "in"},
238 { SFLOW_IFACE_DIRECTION_OUT
, "out"},
242 struct sflow_generic_counter_t
{
246 u_int8_t ifdirection
[4];
247 u_int8_t ifstatus
[4];
248 u_int8_t ifinoctets
[8];
249 u_int8_t ifinunicastpkts
[4];
250 u_int8_t ifinmulticastpkts
[4];
251 u_int8_t ifinbroadcastpkts
[4];
252 u_int8_t ifindiscards
[4];
253 u_int8_t ifinerrors
[4];
254 u_int8_t ifinunkownprotos
[4];
255 u_int8_t ifoutoctets
[8];
256 u_int8_t ifoutunicastpkts
[4];
257 u_int8_t ifoutmulticastpkts
[4];
258 u_int8_t ifoutbroadcastpkts
[4];
259 u_int8_t ifoutdiscards
[4];
260 u_int8_t ifouterrors
[4];
261 u_int8_t ifpromiscmode
[4];
264 struct sflow_ethernet_counter_t
{
265 u_int8_t alignerrors
[4];
266 u_int8_t fcserrors
[4];
267 u_int8_t single_collision_frames
[4];
268 u_int8_t multiple_collision_frames
[4];
269 u_int8_t test_errors
[4];
270 u_int8_t deferred_transmissions
[4];
271 u_int8_t late_collisions
[4];
272 u_int8_t excessive_collisions
[4];
273 u_int8_t mac_transmit_errors
[4];
274 u_int8_t carrier_sense_errors
[4];
275 u_int8_t frame_too_longs
[4];
276 u_int8_t mac_receive_errors
[4];
277 u_int8_t symbol_errors
[4];
280 struct sflow_100basevg_counter_t
{
281 u_int8_t in_highpriority_frames
[4];
282 u_int8_t in_highpriority_octets
[8];
283 u_int8_t in_normpriority_frames
[4];
284 u_int8_t in_normpriority_octets
[8];
285 u_int8_t in_ipmerrors
[4];
286 u_int8_t in_oversized
[4];
287 u_int8_t in_data_errors
[4];
288 u_int8_t in_null_addressed_frames
[4];
289 u_int8_t out_highpriority_frames
[4];
290 u_int8_t out_highpriority_octets
[8];
291 u_int8_t transitioninto_frames
[4];
292 u_int8_t hc_in_highpriority_octets
[8];
293 u_int8_t hc_in_normpriority_octets
[8];
294 u_int8_t hc_out_highpriority_octets
[8];
297 struct sflow_vlan_counter_t
{
300 u_int8_t unicast_pkt
[4];
301 u_int8_t multicast_pkt
[4];
302 u_int8_t broadcast_pkt
[4];
303 u_int8_t discards
[4];
307 print_sflow_counter_generic(const u_char
*pointer
, u_int len
) {
309 const struct sflow_generic_counter_t
*sflow_gen_counter
;
311 if (len
< sizeof(struct sflow_generic_counter_t
))
314 sflow_gen_counter
= (const struct sflow_generic_counter_t
*)pointer
;
315 TCHECK(*sflow_gen_counter
);
316 printf("\n\t ifindex %u, iftype %u, ifspeed %" PRIu64
", ifdirection %u (%s)",
317 EXTRACT_32BITS(sflow_gen_counter
->ifindex
),
318 EXTRACT_32BITS(sflow_gen_counter
->iftype
),
319 EXTRACT_64BITS(sflow_gen_counter
->ifspeed
),
320 EXTRACT_32BITS(sflow_gen_counter
->ifdirection
),
321 tok2str(sflow_iface_direction_values
, "Unknown",
322 EXTRACT_32BITS(sflow_gen_counter
->ifdirection
)));
323 printf("\n\t ifstatus %u, adminstatus: %s, operstatus: %s",
324 EXTRACT_32BITS(sflow_gen_counter
->ifstatus
),
325 EXTRACT_32BITS(sflow_gen_counter
->ifstatus
)&1 ? "up" : "down",
326 (EXTRACT_32BITS(sflow_gen_counter
->ifstatus
)>>1)&1 ? "up" : "down");
327 printf("\n\t In octets %" PRIu64
328 ", unicast pkts %u, multicast pkts %u, broadcast pkts %u, discards %u",
329 EXTRACT_64BITS(sflow_gen_counter
->ifinoctets
),
330 EXTRACT_32BITS(sflow_gen_counter
->ifinunicastpkts
),
331 EXTRACT_32BITS(sflow_gen_counter
->ifinmulticastpkts
),
332 EXTRACT_32BITS(sflow_gen_counter
->ifinbroadcastpkts
),
333 EXTRACT_32BITS(sflow_gen_counter
->ifindiscards
));
334 printf("\n\t In errors %u, unknown protos %u",
335 EXTRACT_32BITS(sflow_gen_counter
->ifinerrors
),
336 EXTRACT_32BITS(sflow_gen_counter
->ifinunkownprotos
));
337 printf("\n\t Out octets %" PRIu64
338 ", unicast pkts %u, multicast pkts %u, broadcast pkts %u, discards %u",
339 EXTRACT_64BITS(sflow_gen_counter
->ifoutoctets
),
340 EXTRACT_32BITS(sflow_gen_counter
->ifoutunicastpkts
),
341 EXTRACT_32BITS(sflow_gen_counter
->ifoutmulticastpkts
),
342 EXTRACT_32BITS(sflow_gen_counter
->ifoutbroadcastpkts
),
343 EXTRACT_32BITS(sflow_gen_counter
->ifoutdiscards
));
344 printf("\n\t Out errors %u, promisc mode %u",
345 EXTRACT_32BITS(sflow_gen_counter
->ifouterrors
),
346 EXTRACT_32BITS(sflow_gen_counter
->ifpromiscmode
));
355 print_sflow_counter_ethernet(const u_char
*pointer
, u_int len
){
357 const struct sflow_ethernet_counter_t
*sflow_eth_counter
;
359 if (len
< sizeof(struct sflow_ethernet_counter_t
))
362 sflow_eth_counter
= (const struct sflow_ethernet_counter_t
*)pointer
;
363 TCHECK(*sflow_eth_counter
);
364 printf("\n\t align errors %u, fcs errors %u, single collision %u, multiple collision %u, test error %u",
365 EXTRACT_32BITS(sflow_eth_counter
->alignerrors
),
366 EXTRACT_32BITS(sflow_eth_counter
->fcserrors
),
367 EXTRACT_32BITS(sflow_eth_counter
->single_collision_frames
),
368 EXTRACT_32BITS(sflow_eth_counter
->multiple_collision_frames
),
369 EXTRACT_32BITS(sflow_eth_counter
->test_errors
));
370 printf("\n\t deferred %u, late collision %u, excessive collision %u, mac trans error %u",
371 EXTRACT_32BITS(sflow_eth_counter
->deferred_transmissions
),
372 EXTRACT_32BITS(sflow_eth_counter
->late_collisions
),
373 EXTRACT_32BITS(sflow_eth_counter
->excessive_collisions
),
374 EXTRACT_32BITS(sflow_eth_counter
->mac_transmit_errors
));
375 printf("\n\t carrier error %u, frames too long %u, mac receive errors %u, symbol errors %u",
376 EXTRACT_32BITS(sflow_eth_counter
->carrier_sense_errors
),
377 EXTRACT_32BITS(sflow_eth_counter
->frame_too_longs
),
378 EXTRACT_32BITS(sflow_eth_counter
->mac_receive_errors
),
379 EXTRACT_32BITS(sflow_eth_counter
->symbol_errors
));
388 print_sflow_counter_token_ring(const u_char
*pointer _U_
, u_int len _U_
) {
394 print_sflow_counter_basevg(const u_char
*pointer
, u_int len
) {
396 const struct sflow_100basevg_counter_t
*sflow_100basevg_counter
;
398 if (len
< sizeof(struct sflow_100basevg_counter_t
))
401 sflow_100basevg_counter
= (const struct sflow_100basevg_counter_t
*)pointer
;
402 TCHECK(*sflow_100basevg_counter
);
403 printf("\n\t in high prio frames %u, in high prio octets %" PRIu64
,
404 EXTRACT_32BITS(sflow_100basevg_counter
->in_highpriority_frames
),
405 EXTRACT_64BITS(sflow_100basevg_counter
->in_highpriority_octets
));
406 printf("\n\t in norm prio frames %u, in norm prio octets %" PRIu64
,
407 EXTRACT_32BITS(sflow_100basevg_counter
->in_normpriority_frames
),
408 EXTRACT_64BITS(sflow_100basevg_counter
->in_normpriority_octets
));
409 printf("\n\t in ipm errors %u, oversized %u, in data errors %u, null addressed frames %u",
410 EXTRACT_32BITS(sflow_100basevg_counter
->in_ipmerrors
),
411 EXTRACT_32BITS(sflow_100basevg_counter
->in_oversized
),
412 EXTRACT_32BITS(sflow_100basevg_counter
->in_data_errors
),
413 EXTRACT_32BITS(sflow_100basevg_counter
->in_null_addressed_frames
));
414 printf("\n\t out high prio frames %u, out high prio octets %" PRIu64
415 ", trans into frames %u",
416 EXTRACT_32BITS(sflow_100basevg_counter
->out_highpriority_frames
),
417 EXTRACT_64BITS(sflow_100basevg_counter
->out_highpriority_octets
),
418 EXTRACT_32BITS(sflow_100basevg_counter
->transitioninto_frames
));
419 printf("\n\t in hc high prio octets %" PRIu64
420 ", in hc norm prio octets %" PRIu64
421 ", out hc high prio octets %" PRIu64
,
422 EXTRACT_64BITS(sflow_100basevg_counter
->hc_in_highpriority_octets
),
423 EXTRACT_64BITS(sflow_100basevg_counter
->hc_in_normpriority_octets
),
424 EXTRACT_64BITS(sflow_100basevg_counter
->hc_out_highpriority_octets
));
433 print_sflow_counter_vlan(const u_char
*pointer
, u_int len
) {
435 const struct sflow_vlan_counter_t
*sflow_vlan_counter
;
437 if (len
< sizeof(struct sflow_vlan_counter_t
))
440 sflow_vlan_counter
= (const struct sflow_vlan_counter_t
*)pointer
;
441 TCHECK(*sflow_vlan_counter
);
442 printf("\n\t vlan_id %u, octets %" PRIu64
443 ", unicast_pkt %u, multicast_pkt %u, broadcast_pkt %u, discards %u",
444 EXTRACT_32BITS(sflow_vlan_counter
->vlan_id
),
445 EXTRACT_64BITS(sflow_vlan_counter
->octets
),
446 EXTRACT_32BITS(sflow_vlan_counter
->unicast_pkt
),
447 EXTRACT_32BITS(sflow_vlan_counter
->multicast_pkt
),
448 EXTRACT_32BITS(sflow_vlan_counter
->broadcast_pkt
),
449 EXTRACT_32BITS(sflow_vlan_counter
->discards
));
457 struct sflow_processor_counter_t
{
458 u_int8_t five_sec_util
[4];
459 u_int8_t one_min_util
[4];
460 u_int8_t five_min_util
[4];
461 u_int8_t total_memory
[8];
462 u_int8_t free_memory
[8];
466 print_sflow_counter_processor(const u_char
*pointer
, u_int len
) {
468 const struct sflow_processor_counter_t
*sflow_processor_counter
;
470 if (len
< sizeof(struct sflow_processor_counter_t
))
473 sflow_processor_counter
= (const struct sflow_processor_counter_t
*)pointer
;
474 TCHECK(*sflow_processor_counter
);
475 printf("\n\t 5sec %u, 1min %u, 5min %u, total_mem %" PRIu64
476 ", total_mem %" PRIu64
,
477 EXTRACT_32BITS(sflow_processor_counter
->five_sec_util
),
478 EXTRACT_32BITS(sflow_processor_counter
->one_min_util
),
479 EXTRACT_32BITS(sflow_processor_counter
->five_min_util
),
480 EXTRACT_64BITS(sflow_processor_counter
->total_memory
),
481 EXTRACT_64BITS(sflow_processor_counter
->free_memory
));
490 sflow_print_counter_records(const u_char
*pointer
, u_int len
, u_int records
) {
498 const struct sflow_counter_record_t
*sflow_counter_record
;
504 while (nrecords
> 0) {
505 /* do we have the "header?" */
506 if (tlen
< sizeof(struct sflow_counter_record_t
))
508 sflow_counter_record
= (const struct sflow_counter_record_t
*)tptr
;
509 TCHECK(*sflow_counter_record
);
511 enterprise
= EXTRACT_32BITS(sflow_counter_record
->format
);
512 counter_type
= enterprise
& 0x0FFF;
513 enterprise
= enterprise
>> 20;
514 counter_len
= EXTRACT_32BITS(sflow_counter_record
->length
);
515 printf("\n\t enterprise %u, %s (%u) length %u",
517 (enterprise
== 0) ? tok2str(sflow_counter_type_values
,"Unknown",counter_type
) : "Unknown",
521 tptr
+= sizeof(struct sflow_counter_record_t
);
522 tlen
-= sizeof(struct sflow_counter_record_t
);
524 if (tlen
< counter_len
)
526 if (enterprise
== 0) {
527 switch (counter_type
) {
528 case SFLOW_COUNTER_GENERIC
:
529 if (print_sflow_counter_generic(tptr
,tlen
))
532 case SFLOW_COUNTER_ETHERNET
:
533 if (print_sflow_counter_ethernet(tptr
,tlen
))
536 case SFLOW_COUNTER_TOKEN_RING
:
537 if (print_sflow_counter_token_ring(tptr
,tlen
))
540 case SFLOW_COUNTER_BASEVG
:
541 if (print_sflow_counter_basevg(tptr
,tlen
))
544 case SFLOW_COUNTER_VLAN
:
545 if (print_sflow_counter_vlan(tptr
,tlen
))
548 case SFLOW_COUNTER_PROCESSOR
:
549 if (print_sflow_counter_processor(tptr
,tlen
))
554 print_unknown_data(tptr
, "\n\t\t", counter_len
);
572 sflow_print_counter_sample(const u_char
*pointer
, u_int len
) {
574 const struct sflow_counter_sample_t
*sflow_counter_sample
;
580 if (len
< sizeof(struct sflow_counter_sample_t
))
583 sflow_counter_sample
= (const struct sflow_counter_sample_t
*)pointer
;
584 TCHECK(*sflow_counter_sample
);
586 typesource
= EXTRACT_32BITS(sflow_counter_sample
->typesource
);
587 nrecords
= EXTRACT_32BITS(sflow_counter_sample
->records
);
588 type
= typesource
>> 24;
589 index
= typesource
& 0x0FFF;
591 printf(" seqnum %u, type %u, idx %u, records %u",
592 EXTRACT_32BITS(sflow_counter_sample
->seqnum
),
597 return sflow_print_counter_records(pointer
+ sizeof(struct sflow_counter_sample_t
),
598 len
- sizeof(struct sflow_counter_sample_t
),
606 sflow_print_expanded_counter_sample(const u_char
*pointer
, u_int len
) {
608 const struct sflow_expanded_counter_sample_t
*sflow_expanded_counter_sample
;
612 if (len
< sizeof(struct sflow_expanded_counter_sample_t
))
615 sflow_expanded_counter_sample
= (const struct sflow_expanded_counter_sample_t
*)pointer
;
616 TCHECK(*sflow_expanded_counter_sample
);
618 nrecords
= EXTRACT_32BITS(sflow_expanded_counter_sample
->records
);
620 printf(" seqnum %u, type %u, idx %u, records %u",
621 EXTRACT_32BITS(sflow_expanded_counter_sample
->seqnum
),
622 EXTRACT_32BITS(sflow_expanded_counter_sample
->type
),
623 EXTRACT_32BITS(sflow_expanded_counter_sample
->index
),
626 return sflow_print_counter_records(pointer
+ sizeof(struct sflow_expanded_counter_sample_t
),
627 len
- sizeof(struct sflow_expanded_counter_sample_t
),
635 print_sflow_raw_packet(const u_char
*pointer
, u_int len
) {
637 const struct sflow_expanded_flow_raw_t
*sflow_flow_raw
;
639 if (len
< sizeof(struct sflow_expanded_flow_raw_t
))
642 sflow_flow_raw
= (const struct sflow_expanded_flow_raw_t
*)pointer
;
643 TCHECK(*sflow_flow_raw
);
644 printf("\n\t protocol %s (%u), length %u, stripped bytes %u, header_size %u",
645 tok2str(sflow_flow_raw_protocol_values
,"Unknown",EXTRACT_32BITS(sflow_flow_raw
->protocol
)),
646 EXTRACT_32BITS(sflow_flow_raw
->protocol
),
647 EXTRACT_32BITS(sflow_flow_raw
->length
),
648 EXTRACT_32BITS(sflow_flow_raw
->stripped_bytes
),
649 EXTRACT_32BITS(sflow_flow_raw
->header_size
));
651 /* QUESTION - should we attempt to print the raw header itself?
652 assuming of course there is wnough data present to do so... */
661 print_sflow_ethernet_frame(const u_char
*pointer
, u_int len
) {
663 const struct sflow_ethernet_frame_t
*sflow_ethernet_frame
;
665 if (len
< sizeof(struct sflow_ethernet_frame_t
))
668 sflow_ethernet_frame
= (const struct sflow_ethernet_frame_t
*)pointer
;
669 TCHECK(*sflow_ethernet_frame
);
671 printf("\n\t frame len %u, type %u",
672 EXTRACT_32BITS(sflow_ethernet_frame
->length
),
673 EXTRACT_32BITS(sflow_ethernet_frame
->type
));
682 print_sflow_extended_switch_data(const u_char
*pointer
, u_int len
) {
684 const struct sflow_extended_switch_data_t
*sflow_extended_sw_data
;
686 if (len
< sizeof(struct sflow_extended_switch_data_t
))
689 sflow_extended_sw_data
= (const struct sflow_extended_switch_data_t
*)pointer
;
690 TCHECK(*sflow_extended_sw_data
);
691 printf("\n\t src vlan %u, src pri %u, dst vlan %u, dst pri %u",
692 EXTRACT_32BITS(sflow_extended_sw_data
->src_vlan
),
693 EXTRACT_32BITS(sflow_extended_sw_data
->src_pri
),
694 EXTRACT_32BITS(sflow_extended_sw_data
->dst_vlan
),
695 EXTRACT_32BITS(sflow_extended_sw_data
->dst_pri
));
704 sflow_print_flow_records(const u_char
*pointer
, u_int len
, u_int records
) {
712 const struct sflow_flow_record_t
*sflow_flow_record
;
718 while (nrecords
> 0) {
719 /* do we have the "header?" */
720 if (tlen
< sizeof(struct sflow_flow_record_t
))
723 sflow_flow_record
= (const struct sflow_flow_record_t
*)tptr
;
724 TCHECK(*sflow_flow_record
);
726 /* so, the funky encoding means we cannot blythly mask-off
727 bits, we must also check the enterprise. */
729 enterprise
= EXTRACT_32BITS(sflow_flow_record
->format
);
730 flow_type
= enterprise
& 0x0FFF;
731 enterprise
= enterprise
>> 12;
732 flow_len
= EXTRACT_32BITS(sflow_flow_record
->length
);
733 printf("\n\t enterprise %u %s (%u) length %u",
735 (enterprise
== 0) ? tok2str(sflow_flow_type_values
,"Unknown",flow_type
) : "Unknown",
739 tptr
+= sizeof(struct sflow_flow_record_t
);
740 tlen
-= sizeof(struct sflow_flow_record_t
);
745 if (enterprise
== 0) {
747 case SFLOW_FLOW_RAW_PACKET
:
748 if (print_sflow_raw_packet(tptr
,tlen
))
751 case SFLOW_FLOW_EXTENDED_SWITCH_DATA
:
752 if (print_sflow_extended_switch_data(tptr
,tlen
))
755 case SFLOW_FLOW_ETHERNET_FRAME
:
756 if (print_sflow_ethernet_frame(tptr
,tlen
))
759 /* FIXME these need a decoder */
760 case SFLOW_FLOW_IPV4_DATA
:
761 case SFLOW_FLOW_IPV6_DATA
:
762 case SFLOW_FLOW_EXTENDED_ROUTER_DATA
:
763 case SFLOW_FLOW_EXTENDED_GATEWAY_DATA
:
764 case SFLOW_FLOW_EXTENDED_USER_DATA
:
765 case SFLOW_FLOW_EXTENDED_URL_DATA
:
766 case SFLOW_FLOW_EXTENDED_MPLS_DATA
:
767 case SFLOW_FLOW_EXTENDED_NAT_DATA
:
768 case SFLOW_FLOW_EXTENDED_MPLS_TUNNEL
:
769 case SFLOW_FLOW_EXTENDED_MPLS_VC
:
770 case SFLOW_FLOW_EXTENDED_MPLS_FEC
:
771 case SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC
:
772 case SFLOW_FLOW_EXTENDED_VLAN_TUNNEL
:
776 print_unknown_data(tptr
, "\n\t\t", flow_len
);
793 sflow_print_flow_sample(const u_char
*pointer
, u_int len
) {
795 const struct sflow_flow_sample_t
*sflow_flow_sample
;
801 if (len
< sizeof(struct sflow_flow_sample_t
))
804 sflow_flow_sample
= (struct sflow_flow_sample_t
*)pointer
;
805 TCHECK(*sflow_flow_sample
);
807 typesource
= EXTRACT_32BITS(sflow_flow_sample
->typesource
);
808 nrecords
= EXTRACT_32BITS(sflow_flow_sample
->records
);
809 type
= typesource
>> 24;
810 index
= typesource
& 0x0FFF;
812 printf(" seqnum %u, type %u, idx %u, rate %u, pool %u, drops %u, input %u output %u records %u",
813 EXTRACT_32BITS(sflow_flow_sample
->seqnum
),
816 EXTRACT_32BITS(sflow_flow_sample
->rate
),
817 EXTRACT_32BITS(sflow_flow_sample
->pool
),
818 EXTRACT_32BITS(sflow_flow_sample
->drops
),
819 EXTRACT_32BITS(sflow_flow_sample
->in_interface
),
820 EXTRACT_32BITS(sflow_flow_sample
->out_interface
),
823 return sflow_print_flow_records(pointer
+ sizeof(struct sflow_flow_sample_t
),
824 len
- sizeof(struct sflow_flow_sample_t
),
832 sflow_print_expanded_flow_sample(const u_char
*pointer
, u_int len
) {
834 const struct sflow_expanded_flow_sample_t
*sflow_expanded_flow_sample
;
837 if (len
< sizeof(struct sflow_expanded_flow_sample_t
))
840 sflow_expanded_flow_sample
= (const struct sflow_expanded_flow_sample_t
*)pointer
;
841 TCHECK(*sflow_expanded_flow_sample
);
843 nrecords
= EXTRACT_32BITS(sflow_expanded_flow_sample
->records
);
845 printf(" seqnum %u, type %u, idx %u, rate %u, pool %u, drops %u, records %u",
846 EXTRACT_32BITS(sflow_expanded_flow_sample
->seqnum
),
847 EXTRACT_32BITS(sflow_expanded_flow_sample
->type
),
848 EXTRACT_32BITS(sflow_expanded_flow_sample
->index
),
849 EXTRACT_32BITS(sflow_expanded_flow_sample
->rate
),
850 EXTRACT_32BITS(sflow_expanded_flow_sample
->pool
),
851 EXTRACT_32BITS(sflow_expanded_flow_sample
->drops
),
852 EXTRACT_32BITS(sflow_expanded_flow_sample
->records
));
854 return sflow_print_flow_records(pointer
+ sizeof(struct sflow_expanded_flow_sample_t
),
855 len
- sizeof(struct sflow_expanded_flow_sample_t
),
863 sflow_print(const u_char
*pptr
, u_int len
) {
865 const struct sflow_datagram_t
*sflow_datagram
;
866 const struct sflow_sample_header
*sflow_sample
;
870 u_int32_t sflow_sample_type
, sflow_sample_len
;
875 sflow_datagram
= (const struct sflow_datagram_t
*)pptr
;
876 TCHECK(*sflow_datagram
);
879 * Sanity checking of the header.
881 if (EXTRACT_32BITS(sflow_datagram
->version
) != 5) {
882 printf("sFlow version %u packet not supported",
883 EXTRACT_32BITS(sflow_datagram
->version
));
888 printf("sFlowv%u, %s agent %s, agent-id %u, length %u",
889 EXTRACT_32BITS(sflow_datagram
->version
),
890 EXTRACT_32BITS(sflow_datagram
->ip_version
) == 1 ? "IPv4" : "IPv6",
891 ipaddr_string(sflow_datagram
->agent
),
892 EXTRACT_32BITS(sflow_datagram
->agent_id
),
897 /* ok they seem to want to know everything - lets fully decode it */
898 nsamples
=EXTRACT_32BITS(sflow_datagram
->samples
);
899 printf("sFlowv%u, %s agent %s, agent-id %u, seqnum %u, uptime %u, samples %u, length %u",
900 EXTRACT_32BITS(sflow_datagram
->version
),
901 EXTRACT_32BITS(sflow_datagram
->ip_version
) == 1 ? "IPv4" : "IPv6",
902 ipaddr_string(sflow_datagram
->agent
),
903 EXTRACT_32BITS(sflow_datagram
->agent_id
),
904 EXTRACT_32BITS(sflow_datagram
->seqnum
),
905 EXTRACT_32BITS(sflow_datagram
->uptime
),
909 /* skip Common header */
910 tptr
+= sizeof(const struct sflow_datagram_t
);
911 tlen
-= sizeof(const struct sflow_datagram_t
);
913 while (nsamples
> 0 && tlen
> 0) {
914 sflow_sample
= (const struct sflow_sample_header
*)tptr
;
915 TCHECK(*sflow_sample
);
917 sflow_sample_type
= (EXTRACT_32BITS(sflow_sample
->format
)&0x0FFF);
918 sflow_sample_len
= EXTRACT_32BITS(sflow_sample
->len
);
920 if (tlen
< sizeof(struct sflow_sample_header
))
923 tptr
+= sizeof(struct sflow_sample_header
);
924 tlen
-= sizeof(struct sflow_sample_header
);
926 printf("\n\t%s (%u), length %u,",
927 tok2str(sflow_format_values
, "Unknown", sflow_sample_type
),
931 /* basic sanity check */
932 if (sflow_sample_type
== 0 || sflow_sample_len
==0) {
936 if (tlen
< sflow_sample_len
)
939 /* did we capture enough for fully decoding the sample ? */
940 TCHECK2(*tptr
, sflow_sample_len
);
942 switch(sflow_sample_type
) {
943 case SFLOW_FLOW_SAMPLE
:
944 if (sflow_print_flow_sample(tptr
,tlen
))
948 case SFLOW_COUNTER_SAMPLE
:
949 if (sflow_print_counter_sample(tptr
,tlen
))
953 case SFLOW_EXPANDED_FLOW_SAMPLE
:
954 if (sflow_print_expanded_flow_sample(tptr
,tlen
))
958 case SFLOW_EXPANDED_COUNTER_SAMPLE
:
959 if (sflow_print_expanded_counter_sample(tptr
,tlen
))
965 print_unknown_data(tptr
, "\n\t ", sflow_sample_len
);
968 tptr
+= sflow_sample_len
;
969 tlen
-= sflow_sample_len
;
980 * c-style: whitesmith