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>
26 #include <tcpdump-stdinc.h>
32 #include "interface.h"
34 #include "addrtoname.h"
40 * 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
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | Sflow version (2,4,5) |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | IP version (1 for IPv4 | 2 for IPv6) |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | IP Address AGENT (4 or 16 bytes) |
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | Datagram sequence number |
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * | Switch uptime in ms |
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * | num samples in datagram |
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 struct sflow_datagram_t
{
61 u_int8_t ip_version
[4];
69 struct sflow_sample_header
{
74 #define SFLOW_FLOW_SAMPLE 1
75 #define SFLOW_COUNTER_SAMPLE 2
76 #define SFLOW_EXPANDED_FLOW_SAMPLE 3
77 #define SFLOW_EXPANDED_COUNTER_SAMPLE 4
79 static const struct tok sflow_format_values
[] = {
80 { SFLOW_FLOW_SAMPLE
, "flow sample" },
81 { SFLOW_COUNTER_SAMPLE
, "counter sample" },
82 { SFLOW_EXPANDED_FLOW_SAMPLE
, "expanded flow sample" },
83 { SFLOW_EXPANDED_COUNTER_SAMPLE
, "expanded counter sample" },
87 struct sflow_flow_sample_t
{
89 u_int8_t typesource
[4];
93 u_int8_t in_interface
[4];
94 u_int8_t out_interface
[4];
99 struct sflow_expanded_flow_sample_t
{
106 u_int8_t in_interface_format
[4];
107 u_int8_t in_interface_value
[4];
108 u_int8_t out_interface_format
[4];
109 u_int8_t out_interface_value
[4];
113 #define SFLOW_FLOW_RAW_PACKET 1
114 #define SFLOW_FLOW_ETHERNET_FRAME 2
115 #define SFLOW_FLOW_IPV4_DATA 3
116 #define SFLOW_FLOW_IPV6_DATA 4
117 #define SFLOW_FLOW_EXTENDED_SWITCH_DATA 1001
118 #define SFLOW_FLOW_EXTENDED_ROUTER_DATA 1002
119 #define SFLOW_FLOW_EXTENDED_GATEWAY_DATA 1003
120 #define SFLOW_FLOW_EXTENDED_USER_DATA 1004
121 #define SFLOW_FLOW_EXTENDED_URL_DATA 1005
122 #define SFLOW_FLOW_EXTENDED_MPLS_DATA 1006
123 #define SFLOW_FLOW_EXTENDED_NAT_DATA 1007
124 #define SFLOW_FLOW_EXTENDED_MPLS_TUNNEL 1008
125 #define SFLOW_FLOW_EXTENDED_MPLS_VC 1009
126 #define SFLOW_FLOW_EXTENDED_MPLS_FEC 1010
127 #define SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC 1011
128 #define SFLOW_FLOW_EXTENDED_VLAN_TUNNEL 1012
130 static const struct tok sflow_flow_type_values
[] = {
131 { SFLOW_FLOW_RAW_PACKET
, "Raw packet"},
132 { SFLOW_FLOW_ETHERNET_FRAME
, "Ethernet frame"},
133 { SFLOW_FLOW_IPV4_DATA
, "IPv4 Data"},
134 { SFLOW_FLOW_IPV6_DATA
, "IPv6 Data"},
135 { SFLOW_FLOW_EXTENDED_SWITCH_DATA
, "Extended Switch data"},
136 { SFLOW_FLOW_EXTENDED_ROUTER_DATA
, "Extended Router data"},
137 { SFLOW_FLOW_EXTENDED_GATEWAY_DATA
, "Extended Gateway data"},
138 { SFLOW_FLOW_EXTENDED_USER_DATA
, "Extended User data"},
139 { SFLOW_FLOW_EXTENDED_URL_DATA
, "Extended URL data"},
140 { SFLOW_FLOW_EXTENDED_MPLS_DATA
, "Extended MPLS data"},
141 { SFLOW_FLOW_EXTENDED_NAT_DATA
, "Extended NAT data"},
142 { SFLOW_FLOW_EXTENDED_MPLS_TUNNEL
, "Extended MPLS tunnel"},
143 { SFLOW_FLOW_EXTENDED_MPLS_VC
, "Extended MPLS VC"},
144 { SFLOW_FLOW_EXTENDED_MPLS_FEC
, "Extended MPLS FEC"},
145 { SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC
, "Extended MPLS LVP FEC"},
146 { SFLOW_FLOW_EXTENDED_VLAN_TUNNEL
, "Extended VLAN Tunnel"},
150 #define SFLOW_HEADER_PROTOCOL_ETHERNET 1
151 #define SFLOW_HEADER_PROTOCOL_IPV4 11
152 #define SFLOW_HEADER_PROTOCOL_IPV6 12
154 static const struct tok sflow_flow_raw_protocol_values
[] = {
155 { SFLOW_HEADER_PROTOCOL_ETHERNET
, "Ethernet"},
156 { SFLOW_HEADER_PROTOCOL_IPV4
, "IPv4"},
157 { SFLOW_HEADER_PROTOCOL_IPV6
, "IPv6"},
161 struct sflow_expanded_flow_raw_t
{
162 u_int8_t protocol
[4];
164 u_int8_t stripped_bytes
[4];
165 u_int8_t header_size
[4];
168 struct sflow_ethernet_frame_t
{
175 struct sflow_extended_switch_data_t
{
176 u_int8_t src_vlan
[4];
178 u_int8_t dst_vlan
[4];
182 struct sflow_counter_record_t
{
187 struct sflow_flow_record_t
{
192 struct sflow_counter_sample_t
{
194 u_int8_t typesource
[4];
198 struct sflow_expanded_counter_sample_t
{
205 #define SFLOW_COUNTER_GENERIC 1
206 #define SFLOW_COUNTER_ETHERNET 2
207 #define SFLOW_COUNTER_TOKEN_RING 3
208 #define SFLOW_COUNTER_BASEVG 4
209 #define SFLOW_COUNTER_VLAN 5
210 #define SFLOW_COUNTER_PROCESSOR 1001
212 static const struct tok sflow_counter_type_values
[] = {
213 { SFLOW_COUNTER_GENERIC
, "Generic counter"},
214 { SFLOW_COUNTER_ETHERNET
, "Ethernet counter"},
215 { SFLOW_COUNTER_TOKEN_RING
, "Token ring counter"},
216 { SFLOW_COUNTER_BASEVG
, "100 BaseVG counter"},
217 { SFLOW_COUNTER_VLAN
, "Vlan counter"},
218 { SFLOW_COUNTER_PROCESSOR
, "Processor counter"},
222 #define SFLOW_IFACE_DIRECTION_UNKNOWN 0
223 #define SFLOW_IFACE_DIRECTION_FULLDUPLEX 1
224 #define SFLOW_IFACE_DIRECTION_HALFDUPLEX 2
225 #define SFLOW_IFACE_DIRECTION_IN 3
226 #define SFLOW_IFACE_DIRECTION_OUT 4
228 static const struct tok sflow_iface_direction_values
[] = {
229 { SFLOW_IFACE_DIRECTION_UNKNOWN
, "unknown"},
230 { SFLOW_IFACE_DIRECTION_FULLDUPLEX
, "full-duplex"},
231 { SFLOW_IFACE_DIRECTION_HALFDUPLEX
, "half-duplex"},
232 { SFLOW_IFACE_DIRECTION_IN
, "in"},
233 { SFLOW_IFACE_DIRECTION_OUT
, "out"},
237 struct sflow_generic_counter_t
{
241 u_int8_t ifdirection
[4];
242 u_int8_t ifstatus
[4];
243 u_int8_t ifinoctets
[8];
244 u_int8_t ifinunicastpkts
[4];
245 u_int8_t ifinmulticastpkts
[4];
246 u_int8_t ifinbroadcastpkts
[4];
247 u_int8_t ifindiscards
[4];
248 u_int8_t ifinerrors
[4];
249 u_int8_t ifinunkownprotos
[4];
250 u_int8_t ifoutoctets
[8];
251 u_int8_t ifoutunicastpkts
[4];
252 u_int8_t ifoutmulticastpkts
[4];
253 u_int8_t ifoutbroadcastpkts
[4];
254 u_int8_t ifoutdiscards
[4];
255 u_int8_t ifouterrors
[4];
256 u_int8_t ifpromiscmode
[4];
259 struct sflow_ethernet_counter_t
{
260 u_int8_t alignerrors
[4];
261 u_int8_t fcserrors
[4];
262 u_int8_t single_collision_frames
[4];
263 u_int8_t multiple_collision_frames
[4];
264 u_int8_t test_errors
[4];
265 u_int8_t deferred_transmissions
[4];
266 u_int8_t late_collisions
[4];
267 u_int8_t excessive_collisions
[4];
268 u_int8_t mac_transmit_errors
[4];
269 u_int8_t carrier_sense_errors
[4];
270 u_int8_t frame_too_longs
[4];
271 u_int8_t mac_receive_errors
[4];
272 u_int8_t symbol_errors
[4];
275 struct sflow_100basevg_counter_t
{
276 u_int8_t in_highpriority_frames
[4];
277 u_int8_t in_highpriority_octets
[8];
278 u_int8_t in_normpriority_frames
[4];
279 u_int8_t in_normpriority_octets
[8];
280 u_int8_t in_ipmerrors
[4];
281 u_int8_t in_oversized
[4];
282 u_int8_t in_data_errors
[4];
283 u_int8_t in_null_addressed_frames
[4];
284 u_int8_t out_highpriority_frames
[4];
285 u_int8_t out_highpriority_octets
[8];
286 u_int8_t transitioninto_frames
[4];
287 u_int8_t hc_in_highpriority_octets
[8];
288 u_int8_t hc_in_normpriority_octets
[8];
289 u_int8_t hc_out_highpriority_octets
[8];
292 struct sflow_vlan_counter_t
{
295 u_int8_t unicast_pkt
[4];
296 u_int8_t multicast_pkt
[4];
297 u_int8_t broadcast_pkt
[4];
298 u_int8_t discards
[4];
302 print_sflow_counter_generic(const u_char
*pointer
, u_int len
) {
304 const struct sflow_generic_counter_t
*sflow_gen_counter
;
306 if (len
< sizeof(struct sflow_generic_counter_t
))
310 sflow_gen_counter
= (const struct sflow_generic_counter_t
*)pointer
;
311 printf("\n\t ifindex %u, iftype %u, ifspeed %" PRIu64
", ifdirection %u (%s)",
312 EXTRACT_32BITS(sflow_gen_counter
->ifindex
),
313 EXTRACT_32BITS(sflow_gen_counter
->iftype
),
314 EXTRACT_64BITS(sflow_gen_counter
->ifspeed
),
315 EXTRACT_32BITS(sflow_gen_counter
->ifdirection
),
316 tok2str(sflow_iface_direction_values
, "Unknown",
317 EXTRACT_32BITS(sflow_gen_counter
->ifdirection
)));
318 printf("\n\t ifstatus %u, adminstatus: %s, operstatus: %s",
319 EXTRACT_32BITS(sflow_gen_counter
->ifstatus
),
320 EXTRACT_32BITS(sflow_gen_counter
->ifstatus
)&1 ? "up" : "down",
321 (EXTRACT_32BITS(sflow_gen_counter
->ifstatus
)>>1)&1 ? "up" : "down");
322 printf("\n\t In octets %" PRIu64
323 ", unicast pkts %u, multicast pkts %u, broadcast pkts %u, discards %u",
324 EXTRACT_64BITS(sflow_gen_counter
->ifinoctets
),
325 EXTRACT_32BITS(sflow_gen_counter
->ifinunicastpkts
),
326 EXTRACT_32BITS(sflow_gen_counter
->ifinmulticastpkts
),
327 EXTRACT_32BITS(sflow_gen_counter
->ifinbroadcastpkts
),
328 EXTRACT_32BITS(sflow_gen_counter
->ifindiscards
));
329 printf("\n\t In errors %u, unknown protos %u",
330 EXTRACT_32BITS(sflow_gen_counter
->ifinerrors
),
331 EXTRACT_32BITS(sflow_gen_counter
->ifinunkownprotos
));
332 printf("\n\t Out octets %" PRIu64
333 ", unicast pkts %u, multicast pkts %u, broadcast pkts %u, discards %u",
334 EXTRACT_64BITS(sflow_gen_counter
->ifoutoctets
),
335 EXTRACT_32BITS(sflow_gen_counter
->ifoutunicastpkts
),
336 EXTRACT_32BITS(sflow_gen_counter
->ifoutmulticastpkts
),
337 EXTRACT_32BITS(sflow_gen_counter
->ifoutbroadcastpkts
),
338 EXTRACT_32BITS(sflow_gen_counter
->ifoutdiscards
));
339 printf("\n\t Out errors %u, promisc mode %u",
340 EXTRACT_32BITS(sflow_gen_counter
->ifouterrors
),
341 EXTRACT_32BITS(sflow_gen_counter
->ifpromiscmode
));
347 print_sflow_counter_ethernet(const u_char
*pointer
, u_int len
){
349 const struct sflow_ethernet_counter_t
*sflow_eth_counter
;
351 if (len
< sizeof(struct sflow_ethernet_counter_t
))
354 sflow_eth_counter
= (const struct sflow_ethernet_counter_t
*)pointer
;
355 printf("\n\t align errors %u, fcs errors %u, single collision %u, multiple collision %u, test error %u",
356 EXTRACT_32BITS(sflow_eth_counter
->alignerrors
),
357 EXTRACT_32BITS(sflow_eth_counter
->fcserrors
),
358 EXTRACT_32BITS(sflow_eth_counter
->single_collision_frames
),
359 EXTRACT_32BITS(sflow_eth_counter
->multiple_collision_frames
),
360 EXTRACT_32BITS(sflow_eth_counter
->test_errors
));
361 printf("\n\t deferred %u, late collision %u, excessive collision %u, mac trans error %u",
362 EXTRACT_32BITS(sflow_eth_counter
->deferred_transmissions
),
363 EXTRACT_32BITS(sflow_eth_counter
->late_collisions
),
364 EXTRACT_32BITS(sflow_eth_counter
->excessive_collisions
),
365 EXTRACT_32BITS(sflow_eth_counter
->mac_transmit_errors
));
366 printf("\n\t carrier error %u, frames too long %u, mac receive errors %u, symbol errors %u",
367 EXTRACT_32BITS(sflow_eth_counter
->carrier_sense_errors
),
368 EXTRACT_32BITS(sflow_eth_counter
->frame_too_longs
),
369 EXTRACT_32BITS(sflow_eth_counter
->mac_receive_errors
),
370 EXTRACT_32BITS(sflow_eth_counter
->symbol_errors
));
376 print_sflow_counter_token_ring(const u_char
*pointer _U_
, u_int len _U_
) {
382 print_sflow_counter_basevg(const u_char
*pointer
, u_int len
) {
384 const struct sflow_100basevg_counter_t
*sflow_100basevg_counter
;
386 if (len
< sizeof(struct sflow_100basevg_counter_t
))
389 sflow_100basevg_counter
= (const struct sflow_100basevg_counter_t
*)pointer
;
390 printf("\n\t in high prio frames %u, in high prio octets %" PRIu64
,
391 EXTRACT_32BITS(sflow_100basevg_counter
->in_highpriority_frames
),
392 EXTRACT_64BITS(sflow_100basevg_counter
->in_highpriority_octets
));
393 printf("\n\t in norm prio frames %u, in norm prio octets %" PRIu64
,
394 EXTRACT_32BITS(sflow_100basevg_counter
->in_normpriority_frames
),
395 EXTRACT_64BITS(sflow_100basevg_counter
->in_normpriority_octets
));
396 printf("\n\t in ipm errors %u, oversized %u, in data errors %u, null addressed frames %u",
397 EXTRACT_32BITS(sflow_100basevg_counter
->in_ipmerrors
),
398 EXTRACT_32BITS(sflow_100basevg_counter
->in_oversized
),
399 EXTRACT_32BITS(sflow_100basevg_counter
->in_data_errors
),
400 EXTRACT_32BITS(sflow_100basevg_counter
->in_null_addressed_frames
));
401 printf("\n\t out high prio frames %u, out high prio octets %" PRIu64
402 ", trans into frames %u",
403 EXTRACT_32BITS(sflow_100basevg_counter
->out_highpriority_frames
),
404 EXTRACT_64BITS(sflow_100basevg_counter
->out_highpriority_octets
),
405 EXTRACT_32BITS(sflow_100basevg_counter
->transitioninto_frames
));
406 printf("\n\t in hc high prio octets %" PRIu64
407 ", in hc norm prio octets %" PRIu64
408 ", out hc high prio octets %" PRIu64
,
409 EXTRACT_64BITS(sflow_100basevg_counter
->hc_in_highpriority_octets
),
410 EXTRACT_64BITS(sflow_100basevg_counter
->hc_in_normpriority_octets
),
411 EXTRACT_64BITS(sflow_100basevg_counter
->hc_out_highpriority_octets
));
417 print_sflow_counter_vlan(const u_char
*pointer
, u_int len
) {
419 const struct sflow_vlan_counter_t
*sflow_vlan_counter
;
421 if (len
< sizeof(struct sflow_vlan_counter_t
))
424 sflow_vlan_counter
= (const struct sflow_vlan_counter_t
*)pointer
;
425 printf("\n\t vlan_id %u, octets %" PRIu64
426 ", unicast_pkt %u, multicast_pkt %u, broadcast_pkt %u, discards %u",
427 EXTRACT_32BITS(sflow_vlan_counter
->vlan_id
),
428 EXTRACT_64BITS(sflow_vlan_counter
->octets
),
429 EXTRACT_32BITS(sflow_vlan_counter
->unicast_pkt
),
430 EXTRACT_32BITS(sflow_vlan_counter
->multicast_pkt
),
431 EXTRACT_32BITS(sflow_vlan_counter
->broadcast_pkt
),
432 EXTRACT_32BITS(sflow_vlan_counter
->discards
));
437 struct sflow_processor_counter_t
{
438 u_int8_t five_sec_util
[4];
439 u_int8_t one_min_util
[4];
440 u_int8_t five_min_util
[4];
441 u_int8_t total_memory
[8];
442 u_int8_t free_memory
[8];
446 print_sflow_counter_processor(const u_char
*pointer
, u_int len
) {
448 const struct sflow_processor_counter_t
*sflow_processor_counter
;
450 if (len
< sizeof(struct sflow_processor_counter_t
))
453 sflow_processor_counter
= (const struct sflow_processor_counter_t
*)pointer
;
454 printf("\n\t 5sec %u, 1min %u, 5min %u, total_mem %" PRIu64
455 ", total_mem %" PRIu64
,
456 EXTRACT_32BITS(sflow_processor_counter
->five_sec_util
),
457 EXTRACT_32BITS(sflow_processor_counter
->one_min_util
),
458 EXTRACT_32BITS(sflow_processor_counter
->five_min_util
),
459 EXTRACT_64BITS(sflow_processor_counter
->total_memory
),
460 EXTRACT_64BITS(sflow_processor_counter
->free_memory
));
466 sflow_print_counter_records(const u_char
*pointer
, u_int len
, u_int records
) {
474 const struct sflow_counter_record_t
*sflow_counter_record
;
480 while (nrecords
> 0) {
481 /* do we have the "header?" */
482 if (tlen
< sizeof(struct sflow_counter_record_t
))
484 sflow_counter_record
= (const struct sflow_counter_record_t
*)tptr
;
486 enterprise
= EXTRACT_32BITS(sflow_counter_record
->format
);
487 counter_type
= enterprise
& 0x0FFF;
488 enterprise
= enterprise
>> 20;
489 counter_len
= EXTRACT_32BITS(sflow_counter_record
->length
);
490 printf("\n\t enterprise %u, %s (%u) length %u",
492 (enterprise
== 0) ? tok2str(sflow_counter_type_values
,"Unknown",counter_type
) : "Unknown",
496 tptr
+= sizeof(struct sflow_counter_record_t
);
497 tlen
-= sizeof(struct sflow_counter_record_t
);
499 if (tlen
< counter_len
)
501 if (enterprise
== 0) {
502 switch (counter_type
) {
503 case SFLOW_COUNTER_GENERIC
:
504 if (print_sflow_counter_generic(tptr
,tlen
))
507 case SFLOW_COUNTER_ETHERNET
:
508 if (print_sflow_counter_ethernet(tptr
,tlen
))
511 case SFLOW_COUNTER_TOKEN_RING
:
512 if (print_sflow_counter_token_ring(tptr
,tlen
))
515 case SFLOW_COUNTER_BASEVG
:
516 if (print_sflow_counter_basevg(tptr
,tlen
))
519 case SFLOW_COUNTER_VLAN
:
520 if (print_sflow_counter_vlan(tptr
,tlen
))
523 case SFLOW_COUNTER_PROCESSOR
:
524 if (print_sflow_counter_processor(tptr
,tlen
))
529 print_unknown_data(gndo
,tptr
, "\n\t\t", counter_len
);
544 sflow_print_counter_sample(const u_char
*pointer
, u_int len
) {
546 const struct sflow_counter_sample_t
*sflow_counter_sample
;
553 if (len
< sizeof(struct sflow_counter_sample_t
))
556 sflow_counter_sample
= (const struct sflow_counter_sample_t
*)pointer
;
558 typesource
= EXTRACT_32BITS(sflow_counter_sample
->typesource
);
559 nrecords
= EXTRACT_32BITS(sflow_counter_sample
->records
);
560 type
= typesource
>> 24;
561 index
= typesource
& 0x0FFF;
563 printf(" seqnum %u, type %u, idx %u, records %u",
564 EXTRACT_32BITS(sflow_counter_sample
->seqnum
),
569 return sflow_print_counter_records(pointer
+ sizeof(struct sflow_counter_sample_t
),
570 len
- sizeof(struct sflow_counter_sample_t
),
576 sflow_print_expanded_counter_sample(const u_char
*pointer
, u_int len
) {
578 const struct sflow_expanded_counter_sample_t
*sflow_expanded_counter_sample
;
582 if (len
< sizeof(struct sflow_expanded_counter_sample_t
))
585 sflow_expanded_counter_sample
= (const struct sflow_expanded_counter_sample_t
*)pointer
;
587 nrecords
= EXTRACT_32BITS(sflow_expanded_counter_sample
->records
);
589 printf(" seqnum %u, type %u, idx %u, records %u",
590 EXTRACT_32BITS(sflow_expanded_counter_sample
->seqnum
),
591 EXTRACT_32BITS(sflow_expanded_counter_sample
->type
),
592 EXTRACT_32BITS(sflow_expanded_counter_sample
->index
),
595 return sflow_print_counter_records(pointer
+ sizeof(struct sflow_expanded_counter_sample_t
),
596 len
- sizeof(struct sflow_expanded_counter_sample_t
),
602 print_sflow_raw_packet(const u_char
*pointer
, u_int len
) {
604 const struct sflow_expanded_flow_raw_t
*sflow_flow_raw
;
606 if (len
< sizeof(struct sflow_expanded_flow_raw_t
))
609 sflow_flow_raw
= (const struct sflow_expanded_flow_raw_t
*)pointer
;
610 printf("\n\t protocol %s (%u), length %u, stripped bytes %u, header_size %u",
611 tok2str(sflow_flow_raw_protocol_values
,"Unknown",EXTRACT_32BITS(sflow_flow_raw
->protocol
)),
612 EXTRACT_32BITS(sflow_flow_raw
->protocol
),
613 EXTRACT_32BITS(sflow_flow_raw
->length
),
614 EXTRACT_32BITS(sflow_flow_raw
->stripped_bytes
),
615 EXTRACT_32BITS(sflow_flow_raw
->header_size
));
617 /* QUESTION - should we attempt to print the raw header itself?
618 assuming of course there is wnough data present to do so... */
624 print_sflow_ethernet_frame(const u_char
*pointer
, u_int len
) {
626 const struct sflow_ethernet_frame_t
*sflow_ethernet_frame
;
628 if (len
< sizeof(struct sflow_ethernet_frame_t
))
631 sflow_ethernet_frame
= (const struct sflow_ethernet_frame_t
*)pointer
;
633 printf("\n\t frame len %u, type %u",
634 EXTRACT_32BITS(sflow_ethernet_frame
->length
),
635 EXTRACT_32BITS(sflow_ethernet_frame
->type
));
641 print_sflow_extended_switch_data(const u_char
*pointer
, u_int len
) {
643 const struct sflow_extended_switch_data_t
*sflow_extended_sw_data
;
645 if (len
< sizeof(struct sflow_extended_switch_data_t
))
648 sflow_extended_sw_data
= (const struct sflow_extended_switch_data_t
*)pointer
;
649 printf("\n\t src vlan %u, src pri %u, dst vlan %u, dst pri %u",
650 EXTRACT_32BITS(sflow_extended_sw_data
->src_vlan
),
651 EXTRACT_32BITS(sflow_extended_sw_data
->src_pri
),
652 EXTRACT_32BITS(sflow_extended_sw_data
->dst_vlan
),
653 EXTRACT_32BITS(sflow_extended_sw_data
->dst_pri
));
659 sflow_print_flow_records(const u_char
*pointer
, u_int len
, u_int records
) {
667 const struct sflow_flow_record_t
*sflow_flow_record
;
673 while (nrecords
> 0) {
674 /* do we have the "header?" */
675 if (tlen
< sizeof(struct sflow_flow_record_t
))
678 sflow_flow_record
= (const struct sflow_flow_record_t
*)tptr
;
680 /* so, the funky encoding means we cannot blythly mask-off
681 bits, we must also check the enterprise. */
683 enterprise
= EXTRACT_32BITS(sflow_flow_record
->format
);
684 flow_type
= enterprise
& 0x0FFF;
685 enterprise
= enterprise
>> 12;
686 flow_len
= EXTRACT_32BITS(sflow_flow_record
->length
);
687 printf("\n\t enterprise %u %s (%u) length %u",
689 (enterprise
== 0) ? tok2str(sflow_flow_type_values
,"Unknown",flow_type
) : "Unknown",
693 tptr
+= sizeof(struct sflow_flow_record_t
);
694 tlen
-= sizeof(struct sflow_flow_record_t
);
699 if (enterprise
== 0) {
701 case SFLOW_FLOW_RAW_PACKET
:
702 if (print_sflow_raw_packet(tptr
,tlen
))
705 case SFLOW_FLOW_EXTENDED_SWITCH_DATA
:
706 if (print_sflow_extended_switch_data(tptr
,tlen
))
709 case SFLOW_FLOW_ETHERNET_FRAME
:
710 if (print_sflow_ethernet_frame(tptr
,tlen
))
713 /* FIXME these need a decoder */
714 case SFLOW_FLOW_IPV4_DATA
:
715 case SFLOW_FLOW_IPV6_DATA
:
716 case SFLOW_FLOW_EXTENDED_ROUTER_DATA
:
717 case SFLOW_FLOW_EXTENDED_GATEWAY_DATA
:
718 case SFLOW_FLOW_EXTENDED_USER_DATA
:
719 case SFLOW_FLOW_EXTENDED_URL_DATA
:
720 case SFLOW_FLOW_EXTENDED_MPLS_DATA
:
721 case SFLOW_FLOW_EXTENDED_NAT_DATA
:
722 case SFLOW_FLOW_EXTENDED_MPLS_TUNNEL
:
723 case SFLOW_FLOW_EXTENDED_MPLS_VC
:
724 case SFLOW_FLOW_EXTENDED_MPLS_FEC
:
725 case SFLOW_FLOW_EXTENDED_MPLS_LVP_FEC
:
726 case SFLOW_FLOW_EXTENDED_VLAN_TUNNEL
:
730 print_unknown_data(gndo
,tptr
, "\n\t\t", flow_len
);
744 sflow_print_flow_sample(const u_char
*pointer
, u_int len
) {
746 const struct sflow_flow_sample_t
*sflow_flow_sample
;
752 if (len
< sizeof(struct sflow_flow_sample_t
))
755 sflow_flow_sample
= (struct sflow_flow_sample_t
*)pointer
;
757 typesource
= EXTRACT_32BITS(sflow_flow_sample
->typesource
);
758 nrecords
= EXTRACT_32BITS(sflow_flow_sample
->records
);
759 type
= typesource
>> 24;
760 index
= typesource
& 0x0FFF;
762 printf(" seqnum %u, type %u, idx %u, rate %u, pool %u, drops %u, input %u output %u records %u",
763 EXTRACT_32BITS(sflow_flow_sample
->seqnum
),
766 EXTRACT_32BITS(sflow_flow_sample
->rate
),
767 EXTRACT_32BITS(sflow_flow_sample
->pool
),
768 EXTRACT_32BITS(sflow_flow_sample
->drops
),
769 EXTRACT_32BITS(sflow_flow_sample
->in_interface
),
770 EXTRACT_32BITS(sflow_flow_sample
->out_interface
),
773 return sflow_print_flow_records(pointer
+ sizeof(struct sflow_flow_sample_t
),
774 len
- sizeof(struct sflow_flow_sample_t
),
780 sflow_print_expanded_flow_sample(const u_char
*pointer
, u_int len
) {
782 const struct sflow_expanded_flow_sample_t
*sflow_expanded_flow_sample
;
785 if (len
< sizeof(struct sflow_expanded_flow_sample_t
))
788 sflow_expanded_flow_sample
= (const struct sflow_expanded_flow_sample_t
*)pointer
;
790 nrecords
= EXTRACT_32BITS(sflow_expanded_flow_sample
->records
);
792 printf(" seqnum %u, type %u, idx %u, rate %u, pool %u, drops %u, records %u",
793 EXTRACT_32BITS(sflow_expanded_flow_sample
->seqnum
),
794 EXTRACT_32BITS(sflow_expanded_flow_sample
->type
),
795 EXTRACT_32BITS(sflow_expanded_flow_sample
->index
),
796 EXTRACT_32BITS(sflow_expanded_flow_sample
->rate
),
797 EXTRACT_32BITS(sflow_expanded_flow_sample
->pool
),
798 EXTRACT_32BITS(sflow_expanded_flow_sample
->drops
),
799 EXTRACT_32BITS(sflow_expanded_flow_sample
->records
));
801 return sflow_print_flow_records(pointer
+ sizeof(struct sflow_expanded_flow_sample_t
),
802 len
- sizeof(struct sflow_expanded_flow_sample_t
),
808 sflow_print(const u_char
*pptr
, u_int len
) {
810 const struct sflow_datagram_t
*sflow_datagram
;
811 const struct sflow_sample_header
*sflow_sample
;
815 u_int32_t sflow_sample_type
, sflow_sample_len
;
821 sflow_datagram
= (const struct sflow_datagram_t
*)pptr
;
822 TCHECK(*sflow_datagram
);
825 * Sanity checking of the header.
827 if (EXTRACT_32BITS(sflow_datagram
->version
) != 5) {
828 printf("sFlow version %u packet not supported",
829 EXTRACT_32BITS(sflow_datagram
->version
));
834 printf("sFlowv%u, %s agent %s, agent-id %u, length %u",
835 EXTRACT_32BITS(sflow_datagram
->version
),
836 EXTRACT_32BITS(sflow_datagram
->ip_version
) == 1 ? "IPv4" : "IPv6",
837 ipaddr_string(sflow_datagram
->agent
),
838 EXTRACT_32BITS(sflow_datagram
->agent_id
),
843 /* ok they seem to want to know everything - lets fully decode it */
844 nsamples
=EXTRACT_32BITS(sflow_datagram
->samples
);
845 printf("sFlowv%u, %s agent %s, agent-id %u, seqnum %u, uptime %u, samples %u, length %u",
846 EXTRACT_32BITS(sflow_datagram
->version
),
847 EXTRACT_32BITS(sflow_datagram
->ip_version
) == 1 ? "IPv4" : "IPv6",
848 ipaddr_string(sflow_datagram
->agent
),
849 EXTRACT_32BITS(sflow_datagram
->agent_id
),
850 EXTRACT_32BITS(sflow_datagram
->seqnum
),
851 EXTRACT_32BITS(sflow_datagram
->uptime
),
855 /* skip Common header */
856 tptr
+= sizeof(const struct sflow_datagram_t
);
857 tlen
-= sizeof(const struct sflow_datagram_t
);
859 while (nsamples
> 0 && tlen
> 0) {
860 sflow_sample
= (const struct sflow_sample_header
*)tptr
;
861 TCHECK(*sflow_sample
);
863 sflow_sample_type
= (EXTRACT_32BITS(sflow_sample
->format
)&0x0FFF);
864 sflow_sample_len
= EXTRACT_32BITS(sflow_sample
->len
);
866 if (tlen
< sizeof(struct sflow_sample_header
))
869 tptr
+= sizeof(struct sflow_sample_header
);
870 tlen
-= sizeof(struct sflow_sample_header
);
872 printf("\n\t%s (%u), length %u,",
873 tok2str(sflow_format_values
, "Unknown", sflow_sample_type
),
877 /* basic sanity check */
878 if (sflow_sample_type
== 0 || sflow_sample_len
==0) {
882 if (tlen
< sflow_sample_len
)
885 /* did we capture enough for fully decoding the sample ? */
886 TCHECK2(*tptr
, sflow_sample_len
);
888 switch(sflow_sample_type
) {
889 case SFLOW_FLOW_SAMPLE
:
890 if (sflow_print_flow_sample(tptr
,tlen
))
894 case SFLOW_COUNTER_SAMPLE
:
895 if (sflow_print_counter_sample(tptr
,tlen
))
899 case SFLOW_EXPANDED_FLOW_SAMPLE
:
900 if (sflow_print_expanded_flow_sample(tptr
,tlen
))
904 case SFLOW_EXPANDED_COUNTER_SAMPLE
:
905 if (sflow_print_expanded_counter_sample(tptr
,tlen
))
911 print_unknown_data(gndo
,tptr
, "\n\t ", sflow_sample_len
);
914 tptr
+= sflow_sample_len
;
915 tlen
-= sflow_sample_len
;
926 * c-style: whitesmith