1 /* Copyright (c) 2002-2009 InMon Corp. Licensed under the terms of either the
2 * Sun Industry Standards Source License 1.1, that is available at:
3 * http://host-sflow.sourceforge.net/sissl.html
4 * or the InMon sFlow License, that is available at:
5 * http://www.inmon.com/technology/sflowlicense.txt
8 #ifndef __CHECKER__ /* Don't run sparse on anything in this file. */
11 #include "sflow_api.h"
13 static void resetSampleCollector(SFLReceiver
*receiver
);
14 static void sendSample(SFLReceiver
*receiver
);
15 static void sflError(SFLReceiver
*receiver
, char *errm
);
16 inline static void putNet32(SFLReceiver
*receiver
, u_int32_t val
);
17 inline static void putAddress(SFLReceiver
*receiver
, SFLAddress
*addr
);
18 #ifdef SFLOW_DO_SOCKET
19 static void initSocket(SFLReceiver
*receiver
);
22 /*_________________--------------------------__________________
23 _________________ sfl_receiver_init __________________
24 -----------------__________________________------------------
27 void sfl_receiver_init(SFLReceiver
*receiver
, SFLAgent
*agent
)
29 /* first clear everything */
30 memset(receiver
, 0, sizeof(*receiver
));
32 /* now copy in the parameters */
33 receiver
->agent
= agent
;
36 receiver
->sFlowRcvrMaximumDatagramSize
= SFL_DEFAULT_DATAGRAM_SIZE
;
37 receiver
->sFlowRcvrPort
= SFL_DEFAULT_COLLECTOR_PORT
;
39 #ifdef SFLOW_DO_SOCKET
40 /* initialize the socket address */
44 /* preset some of the header fields */
45 receiver
->sampleCollector
.datap
= receiver
->sampleCollector
.data
;
46 putNet32(receiver
, SFLDATAGRAM_VERSION5
);
47 putAddress(receiver
, &agent
->myIP
);
48 putNet32(receiver
, agent
->subId
);
50 /* prepare to receive the first sample */
51 resetSampleCollector(receiver
);
54 /*_________________---------------------------__________________
55 _________________ reset __________________
56 -----------------___________________________------------------
58 called on timeout, or when owner string is cleared
61 static void reset(SFLReceiver
*receiver
) {
62 // ask agent to tell samplers and pollers to stop sending samples
63 sfl_agent_resetReceiver(receiver
->agent
, receiver
);
65 sfl_receiver_init(receiver
, receiver
->agent
);
68 #ifdef SFLOW_DO_SOCKET
69 /*_________________---------------------------__________________
70 _________________ initSocket __________________
71 -----------------___________________________------------------
74 static void initSocket(SFLReceiver
*receiver
) {
75 if(receiver
->sFlowRcvrAddress
.type
== SFLADDRESSTYPE_IP_V6
) {
76 struct sockaddr_in6
*sa6
= &receiver
->receiver6
;
77 sa6
->sin6_port
= htons((u_int16_t
)receiver
->sFlowRcvrPort
);
78 sa6
->sin6_family
= AF_INET6
;
79 sa6
->sin6_addr
= receiver
->sFlowRcvrAddress
.address
.ip_v6
;
82 struct sockaddr_in
*sa4
= &receiver
->receiver4
;
83 sa4
->sin_port
= htons((u_int16_t
)receiver
->sFlowRcvrPort
);
84 sa4
->sin_family
= AF_INET
;
85 sa4
->sin_addr
= receiver
->sFlowRcvrAddress
.address
.ip_v4
;
90 /*_________________----------------------------------------_____________
91 _________________ MIB Vars _____________
92 -----------------________________________________________-------------
95 char * sfl_receiver_get_sFlowRcvrOwner(SFLReceiver
*receiver
) {
96 return receiver
->sFlowRcvrOwner
;
98 void sfl_receiver_set_sFlowRcvrOwner(SFLReceiver
*receiver
, char *sFlowRcvrOwner
) {
99 receiver
->sFlowRcvrOwner
= sFlowRcvrOwner
;
100 if(sFlowRcvrOwner
== NULL
|| sFlowRcvrOwner
[0] == '\0') {
101 // reset condition! owner string was cleared
105 time_t sfl_receiver_get_sFlowRcvrTimeout(SFLReceiver
*receiver
) {
106 return receiver
->sFlowRcvrTimeout
;
108 void sfl_receiver_set_sFlowRcvrTimeout(SFLReceiver
*receiver
, time_t sFlowRcvrTimeout
) {
109 receiver
->sFlowRcvrTimeout
=sFlowRcvrTimeout
;
111 u_int32_t
sfl_receiver_get_sFlowRcvrMaximumDatagramSize(SFLReceiver
*receiver
) {
112 return receiver
->sFlowRcvrMaximumDatagramSize
;
114 void sfl_receiver_set_sFlowRcvrMaximumDatagramSize(SFLReceiver
*receiver
, u_int32_t sFlowRcvrMaximumDatagramSize
) {
115 u_int32_t mdz
= sFlowRcvrMaximumDatagramSize
;
116 if(mdz
< SFL_MIN_DATAGRAM_SIZE
) mdz
= SFL_MIN_DATAGRAM_SIZE
;
117 receiver
->sFlowRcvrMaximumDatagramSize
= mdz
;
119 SFLAddress
*sfl_receiver_get_sFlowRcvrAddress(SFLReceiver
*receiver
) {
120 return &receiver
->sFlowRcvrAddress
;
122 void sfl_receiver_set_sFlowRcvrAddress(SFLReceiver
*receiver
, SFLAddress
*sFlowRcvrAddress
) {
123 if(sFlowRcvrAddress
) receiver
->sFlowRcvrAddress
= *sFlowRcvrAddress
; // structure copy
124 #ifdef SFLOW_DO_SOCKET
125 initSocket(receiver
);
128 u_int32_t
sfl_receiver_get_sFlowRcvrPort(SFLReceiver
*receiver
) {
129 return receiver
->sFlowRcvrPort
;
131 void sfl_receiver_set_sFlowRcvrPort(SFLReceiver
*receiver
, u_int32_t sFlowRcvrPort
) {
132 receiver
->sFlowRcvrPort
= sFlowRcvrPort
;
133 // update the socket structure
134 #ifdef SFLOW_DO_SOCKET
135 initSocket(receiver
);
139 /*_________________---------------------------__________________
140 _________________ sfl_receiver_tick __________________
141 -----------------___________________________------------------
144 void sfl_receiver_tick(SFLReceiver
*receiver
, time_t now
)
146 // if there are any samples to send, flush them now
147 if(receiver
->sampleCollector
.numSamples
> 0) sendSample(receiver
);
149 if(receiver
->sFlowRcvrTimeout
&& (u_int32_t
)receiver
->sFlowRcvrTimeout
!= 0xFFFFFFFF) {
150 // count down one tick and reset if we reach 0
151 if(--receiver
->sFlowRcvrTimeout
== 0) reset(receiver
);
155 /*_________________-----------------------------__________________
156 _________________ receiver write utilities __________________
157 -----------------_____________________________------------------
160 inline static void put32(SFLReceiver
*receiver
, u_int32_t val
)
162 *receiver
->sampleCollector
.datap
++ = val
;
165 inline static void putNet32(SFLReceiver
*receiver
, u_int32_t val
)
167 *receiver
->sampleCollector
.datap
++ = htonl(val
);
170 inline static void putNet32_run(SFLReceiver
*receiver
, void *obj
, size_t quads
)
172 u_int32_t
*from
= (u_int32_t
*)obj
;
173 while(quads
--) putNet32(receiver
, *from
++);
176 inline static void putNet64(SFLReceiver
*receiver
, u_int64_t val64
)
178 u_int32_t
*firstQuadPtr
= receiver
->sampleCollector
.datap
;
179 // first copy the bytes in
180 memcpy((u_char
*)firstQuadPtr
, &val64
, 8);
182 // swap the bytes, and reverse the quads too
183 u_int32_t tmp
= *receiver
->sampleCollector
.datap
++;
184 *firstQuadPtr
= htonl(*receiver
->sampleCollector
.datap
);
185 *receiver
->sampleCollector
.datap
++ = htonl(tmp
);
187 else receiver
->sampleCollector
.datap
+= 2;
190 inline static void put128(SFLReceiver
*receiver
, u_char
*val
)
192 memcpy(receiver
->sampleCollector
.datap
, val
, 16);
193 receiver
->sampleCollector
.datap
+= 4;
196 inline static void putString(SFLReceiver
*receiver
, SFLString
*s
)
198 putNet32(receiver
, s
->len
);
199 memcpy(receiver
->sampleCollector
.datap
, s
->str
, s
->len
);
200 receiver
->sampleCollector
.datap
+= (s
->len
+ 3) / 4; /* pad to 4-byte boundary */
201 if ((s
->len
% 4) != 0){
202 u_int8_t padding
= 4 - (s
->len
% 4);
203 memset(((u_int8_t
*)receiver
->sampleCollector
.datap
)-padding
, 0, padding
);
207 inline static u_int32_t
stringEncodingLength(SFLString
*s
) {
208 // answer in bytes, so remember to mulitply by 4 after rounding up to nearest 4-byte boundary
209 return 4 + (((s
->len
+ 3) / 4) * 4);
212 inline static void putAddress(SFLReceiver
*receiver
, SFLAddress
*addr
)
214 // encode unspecified addresses as IPV4:0.0.0.0 - or should we flag this as an error?
215 if(addr
->type
== 0) {
216 putNet32(receiver
, SFLADDRESSTYPE_IP_V4
);
220 putNet32(receiver
, addr
->type
);
221 if(addr
->type
== SFLADDRESSTYPE_IP_V4
) put32(receiver
, addr
->address
.ip_v4
.addr
);
222 else put128(receiver
, addr
->address
.ip_v6
.addr
);
226 inline static u_int32_t
addressEncodingLength(SFLAddress
*addr
) {
227 return (addr
->type
== SFLADDRESSTYPE_IP_V6
) ? 20 : 8; // type + address (unspecified == IPV4)
230 inline static void putMACAddress(SFLReceiver
*receiver
, u_int8_t
*mac
)
232 memcpy(receiver
->sampleCollector
.datap
, mac
, 6);
233 receiver
->sampleCollector
.datap
+= 2;
236 inline static void putSwitch(SFLReceiver
*receiver
, SFLExtended_switch
*sw
)
238 putNet32(receiver
, sw
->src_vlan
);
239 putNet32(receiver
, sw
->src_priority
);
240 putNet32(receiver
, sw
->dst_vlan
);
241 putNet32(receiver
, sw
->dst_priority
);
244 inline static void putRouter(SFLReceiver
*receiver
, SFLExtended_router
*router
)
246 putAddress(receiver
, &router
->nexthop
);
247 putNet32(receiver
, router
->src_mask
);
248 putNet32(receiver
, router
->dst_mask
);
251 inline static u_int32_t
routerEncodingLength(SFLExtended_router
*router
) {
252 return addressEncodingLength(&router
->nexthop
) + 8;
255 inline static void putGateway(SFLReceiver
*receiver
, SFLExtended_gateway
*gw
)
257 putAddress(receiver
, &gw
->nexthop
);
258 putNet32(receiver
, gw
->as
);
259 putNet32(receiver
, gw
->src_as
);
260 putNet32(receiver
, gw
->src_peer_as
);
261 putNet32(receiver
, gw
->dst_as_path_segments
);
264 for(; seg
< gw
->dst_as_path_segments
; seg
++) {
265 putNet32(receiver
, gw
->dst_as_path
[seg
].type
);
266 putNet32(receiver
, gw
->dst_as_path
[seg
].length
);
267 putNet32_run(receiver
, gw
->dst_as_path
[seg
].as
.seq
, gw
->dst_as_path
[seg
].length
);
270 putNet32(receiver
, gw
->communities_length
);
271 putNet32_run(receiver
, gw
->communities
, gw
->communities_length
);
272 putNet32(receiver
, gw
->localpref
);
275 inline static u_int32_t
gatewayEncodingLength(SFLExtended_gateway
*gw
) {
276 u_int32_t elemSiz
= addressEncodingLength(&gw
->nexthop
);
278 elemSiz
+= 16; // as, src_as, src_peer_as, dst_as_path_segments
279 for(; seg
< gw
->dst_as_path_segments
; seg
++) {
280 elemSiz
+= 8; // type, length
281 elemSiz
+= 4 * gw
->dst_as_path
[seg
].length
; // set/seq bytes
283 elemSiz
+= 4; // communities_length
284 elemSiz
+= 4 * gw
->communities_length
; // communities
285 elemSiz
+= 4; // localpref
289 inline static void putUser(SFLReceiver
*receiver
, SFLExtended_user
*user
)
291 putNet32(receiver
, user
->src_charset
);
292 putString(receiver
, &user
->src_user
);
293 putNet32(receiver
, user
->dst_charset
);
294 putString(receiver
, &user
->dst_user
);
297 inline static u_int32_t
userEncodingLength(SFLExtended_user
*user
) {
299 + stringEncodingLength(&user
->src_user
)
301 + stringEncodingLength(&user
->dst_user
);
304 inline static void putUrl(SFLReceiver
*receiver
, SFLExtended_url
*url
)
306 putNet32(receiver
, url
->direction
);
307 putString(receiver
, &url
->url
);
308 putString(receiver
, &url
->host
);
311 inline static u_int32_t
urlEncodingLength(SFLExtended_url
*url
) {
313 + stringEncodingLength(&url
->url
)
314 + stringEncodingLength(&url
->host
);
317 inline static void putLabelStack(SFLReceiver
*receiver
, SFLLabelStack
*labelStack
)
319 putNet32(receiver
, labelStack
->depth
);
320 putNet32_run(receiver
, labelStack
->stack
, labelStack
->depth
);
323 inline static u_int32_t
labelStackEncodingLength(SFLLabelStack
*labelStack
) {
324 return 4 + (4 * labelStack
->depth
);
327 inline static void putMpls(SFLReceiver
*receiver
, SFLExtended_mpls
*mpls
)
329 putAddress(receiver
, &mpls
->nextHop
);
330 putLabelStack(receiver
, &mpls
->in_stack
);
331 putLabelStack(receiver
, &mpls
->out_stack
);
334 inline static u_int32_t
mplsEncodingLength(SFLExtended_mpls
*mpls
) {
335 return addressEncodingLength(&mpls
->nextHop
)
336 + labelStackEncodingLength(&mpls
->in_stack
)
337 + labelStackEncodingLength(&mpls
->out_stack
);
340 inline static void putNat(SFLReceiver
*receiver
, SFLExtended_nat
*nat
)
342 putAddress(receiver
, &nat
->src
);
343 putAddress(receiver
, &nat
->dst
);
346 inline static u_int32_t
natEncodingLength(SFLExtended_nat
*nat
) {
347 return addressEncodingLength(&nat
->src
)
348 + addressEncodingLength(&nat
->dst
);
351 inline static void putMplsTunnel(SFLReceiver
*receiver
, SFLExtended_mpls_tunnel
*tunnel
)
353 putString(receiver
, &tunnel
->tunnel_lsp_name
);
354 putNet32(receiver
, tunnel
->tunnel_id
);
355 putNet32(receiver
, tunnel
->tunnel_cos
);
358 inline static u_int32_t
mplsTunnelEncodingLength(SFLExtended_mpls_tunnel
*tunnel
) {
359 return stringEncodingLength(&tunnel
->tunnel_lsp_name
) + 8;
362 inline static void putMplsVc(SFLReceiver
*receiver
, SFLExtended_mpls_vc
*vc
)
364 putString(receiver
, &vc
->vc_instance_name
);
365 putNet32(receiver
, vc
->vll_vc_id
);
366 putNet32(receiver
, vc
->vc_label_cos
);
369 inline static u_int32_t
mplsVcEncodingLength(SFLExtended_mpls_vc
*vc
) {
370 return stringEncodingLength( &vc
->vc_instance_name
) + 8;
373 inline static void putMplsFtn(SFLReceiver
*receiver
, SFLExtended_mpls_FTN
*ftn
)
375 putString(receiver
, &ftn
->mplsFTNDescr
);
376 putNet32(receiver
, ftn
->mplsFTNMask
);
379 inline static u_int32_t
mplsFtnEncodingLength(SFLExtended_mpls_FTN
*ftn
) {
380 return stringEncodingLength( &ftn
->mplsFTNDescr
) + 4;
383 inline static void putMplsLdpFec(SFLReceiver
*receiver
, SFLExtended_mpls_LDP_FEC
*ldpfec
)
385 putNet32(receiver
, ldpfec
->mplsFecAddrPrefixLength
);
388 inline static u_int32_t
mplsLdpFecEncodingLength(SFLExtended_mpls_LDP_FEC
*ldpfec
) {
392 inline static void putVlanTunnel(SFLReceiver
*receiver
, SFLExtended_vlan_tunnel
*vlanTunnel
)
394 putLabelStack(receiver
, &vlanTunnel
->stack
);
397 inline static u_int32_t
vlanTunnelEncodingLength(SFLExtended_vlan_tunnel
*vlanTunnel
) {
398 return labelStackEncodingLength(&vlanTunnel
->stack
);
402 inline static void putGenericCounters(SFLReceiver
*receiver
, SFLIf_counters
*counters
)
404 putNet32(receiver
, counters
->ifIndex
);
405 putNet32(receiver
, counters
->ifType
);
406 putNet64(receiver
, counters
->ifSpeed
);
407 putNet32(receiver
, counters
->ifDirection
);
408 putNet32(receiver
, counters
->ifStatus
);
409 putNet64(receiver
, counters
->ifInOctets
);
410 putNet32(receiver
, counters
->ifInUcastPkts
);
411 putNet32(receiver
, counters
->ifInMulticastPkts
);
412 putNet32(receiver
, counters
->ifInBroadcastPkts
);
413 putNet32(receiver
, counters
->ifInDiscards
);
414 putNet32(receiver
, counters
->ifInErrors
);
415 putNet32(receiver
, counters
->ifInUnknownProtos
);
416 putNet64(receiver
, counters
->ifOutOctets
);
417 putNet32(receiver
, counters
->ifOutUcastPkts
);
418 putNet32(receiver
, counters
->ifOutMulticastPkts
);
419 putNet32(receiver
, counters
->ifOutBroadcastPkts
);
420 putNet32(receiver
, counters
->ifOutDiscards
);
421 putNet32(receiver
, counters
->ifOutErrors
);
422 putNet32(receiver
, counters
->ifPromiscuousMode
);
426 /*_________________-----------------------------__________________
427 _________________ computeFlowSampleSize __________________
428 -----------------_____________________________------------------
431 static int computeFlowSampleSize(SFLReceiver
*receiver
, SFL_FLOW_SAMPLE_TYPE
*fs
)
433 SFLFlow_sample_element
*elem
= fs
->elements
;
434 #ifdef SFL_USE_32BIT_INDEX
435 u_int siz
= 52; /* tag, length, sequence_number, ds_class, ds_index, sampling_rate,
436 sample_pool, drops, inputFormat, input, outputFormat, output, number of elements */
438 u_int siz
= 40; /* tag, length, sequence_number, source_id, sampling_rate,
439 sample_pool, drops, input, output, number of elements */
442 fs
->num_elements
= 0; /* we're going to count them again even if this was set by the client */
443 for(; elem
!= NULL
; elem
= elem
->nxt
) {
446 siz
+= 8; /* tag, length */
449 elemSiz
= 16; /* header_protocol, frame_length, stripped, header_length */
450 elemSiz
+= ((elem
->flowType
.header
.header_length
+ 3) / 4) * 4; /* header, rounded up to nearest 4 bytes */
452 case SFLFLOW_ETHERNET
: elemSiz
= sizeof(SFLSampled_ethernet
); break;
453 case SFLFLOW_IPV4
: elemSiz
= sizeof(SFLSampled_ipv4
); break;
454 case SFLFLOW_IPV6
: elemSiz
= sizeof(SFLSampled_ipv6
); break;
455 case SFLFLOW_EX_SWITCH
: elemSiz
= sizeof(SFLExtended_switch
); break;
456 case SFLFLOW_EX_ROUTER
: elemSiz
= routerEncodingLength(&elem
->flowType
.router
); break;
457 case SFLFLOW_EX_GATEWAY
: elemSiz
= gatewayEncodingLength(&elem
->flowType
.gateway
); break;
458 case SFLFLOW_EX_USER
: elemSiz
= userEncodingLength(&elem
->flowType
.user
); break;
459 case SFLFLOW_EX_URL
: elemSiz
= urlEncodingLength(&elem
->flowType
.url
); break;
460 case SFLFLOW_EX_MPLS
: elemSiz
= mplsEncodingLength(&elem
->flowType
.mpls
); break;
461 case SFLFLOW_EX_NAT
: elemSiz
= natEncodingLength(&elem
->flowType
.nat
); break;
462 case SFLFLOW_EX_MPLS_TUNNEL
: elemSiz
= mplsTunnelEncodingLength(&elem
->flowType
.mpls_tunnel
); break;
463 case SFLFLOW_EX_MPLS_VC
: elemSiz
= mplsVcEncodingLength(&elem
->flowType
.mpls_vc
); break;
464 case SFLFLOW_EX_MPLS_FTN
: elemSiz
= mplsFtnEncodingLength(&elem
->flowType
.mpls_ftn
); break;
465 case SFLFLOW_EX_MPLS_LDP_FEC
: elemSiz
= mplsLdpFecEncodingLength(&elem
->flowType
.mpls_ldp_fec
); break;
466 case SFLFLOW_EX_VLAN_TUNNEL
: elemSiz
= vlanTunnelEncodingLength(&elem
->flowType
.vlan_tunnel
); break;
467 case SFLFLOW_EX_IPV4_TUNNEL_EGRESS
:
468 case SFLFLOW_EX_IPV4_TUNNEL_INGRESS
:
469 elemSiz
= sizeof(SFLSampled_ipv4
);
471 case SFLFLOW_EX_VNI_EGRESS
:
472 case SFLFLOW_EX_VNI_INGRESS
:
473 elemSiz
= sizeof(SFLExtended_vni
);
476 sflError(receiver
, "unexpected packet_data_tag");
480 // cache the element size, and accumulate it into the overall FlowSample size
481 elem
->length
= elemSiz
;
488 /*_________________-------------------------------__________________
489 _________________ sfl_receiver_writeFlowSample __________________
490 -----------------_______________________________------------------
493 int sfl_receiver_writeFlowSample(SFLReceiver
*receiver
, SFL_FLOW_SAMPLE_TYPE
*fs
)
496 if(fs
== NULL
) return -1;
497 if((packedSize
= computeFlowSampleSize(receiver
, fs
)) == -1) return -1;
499 // check in case this one sample alone is too big for the datagram
500 // in fact - if it is even half as big then we should ditch it. Very
501 // important to avoid overruning the packet buffer.
502 if(packedSize
> (int)(receiver
->sFlowRcvrMaximumDatagramSize
/ 2)) {
503 sflError(receiver
, "flow sample too big for datagram");
507 // if the sample pkt is full enough so that this sample might put
508 // it over the limit, then we should send it now before going on.
509 if((receiver
->sampleCollector
.pktlen
+ packedSize
) >= receiver
->sFlowRcvrMaximumDatagramSize
)
510 sendSample(receiver
);
512 receiver
->sampleCollector
.numSamples
++;
514 #ifdef SFL_USE_32BIT_INDEX
515 putNet32(receiver
, SFLFLOW_SAMPLE_EXPANDED
);
517 putNet32(receiver
, SFLFLOW_SAMPLE
);
520 putNet32(receiver
, packedSize
- 8); // don't include tag and len
521 putNet32(receiver
, fs
->sequence_number
);
523 #ifdef SFL_USE_32BIT_INDEX
524 putNet32(receiver
, fs
->ds_class
);
525 putNet32(receiver
, fs
->ds_index
);
527 putNet32(receiver
, fs
->source_id
);
530 putNet32(receiver
, fs
->sampling_rate
);
531 putNet32(receiver
, fs
->sample_pool
);
532 putNet32(receiver
, fs
->drops
);
534 #ifdef SFL_USE_32BIT_INDEX
535 putNet32(receiver
, fs
->inputFormat
);
536 putNet32(receiver
, fs
->input
);
537 putNet32(receiver
, fs
->outputFormat
);
538 putNet32(receiver
, fs
->output
);
540 putNet32(receiver
, fs
->input
);
541 putNet32(receiver
, fs
->output
);
544 putNet32(receiver
, fs
->num_elements
);
547 SFLFlow_sample_element
*elem
= fs
->elements
;
548 for(; elem
!= NULL
; elem
= elem
->nxt
) {
550 putNet32(receiver
, elem
->tag
);
551 putNet32(receiver
, elem
->length
); // length cached in computeFlowSampleSize()
555 putNet32(receiver
, elem
->flowType
.header
.header_protocol
);
556 putNet32(receiver
, elem
->flowType
.header
.frame_length
);
557 putNet32(receiver
, elem
->flowType
.header
.stripped
);
558 putNet32(receiver
, elem
->flowType
.header
.header_length
);
560 memcpy(receiver
->sampleCollector
.datap
, elem
->flowType
.header
.header_bytes
, elem
->flowType
.header
.header_length
);
561 /* round up to multiple of 4 to preserve alignment */
562 receiver
->sampleCollector
.datap
+= ((elem
->flowType
.header
.header_length
+ 3) / 4);
564 case SFLFLOW_ETHERNET
:
565 putNet32(receiver
, elem
->flowType
.ethernet
.eth_len
);
566 putMACAddress(receiver
, elem
->flowType
.ethernet
.src_mac
);
567 putMACAddress(receiver
, elem
->flowType
.ethernet
.dst_mac
);
568 putNet32(receiver
, elem
->flowType
.ethernet
.eth_type
);
571 case SFLFLOW_EX_IPV4_TUNNEL_EGRESS
:
572 case SFLFLOW_EX_IPV4_TUNNEL_INGRESS
:
573 putNet32(receiver
, elem
->flowType
.ipv4
.length
);
574 putNet32(receiver
, elem
->flowType
.ipv4
.protocol
);
575 put32(receiver
, elem
->flowType
.ipv4
.src_ip
.addr
);
576 put32(receiver
, elem
->flowType
.ipv4
.dst_ip
.addr
);
577 putNet32(receiver
, elem
->flowType
.ipv4
.src_port
);
578 putNet32(receiver
, elem
->flowType
.ipv4
.dst_port
);
579 putNet32(receiver
, elem
->flowType
.ipv4
.tcp_flags
);
580 putNet32(receiver
, elem
->flowType
.ipv4
.tos
);
583 putNet32(receiver
, elem
->flowType
.ipv6
.length
);
584 putNet32(receiver
, elem
->flowType
.ipv6
.protocol
);
585 put128(receiver
, elem
->flowType
.ipv6
.src_ip
.addr
);
586 put128(receiver
, elem
->flowType
.ipv6
.dst_ip
.addr
);
587 putNet32(receiver
, elem
->flowType
.ipv6
.src_port
);
588 putNet32(receiver
, elem
->flowType
.ipv6
.dst_port
);
589 putNet32(receiver
, elem
->flowType
.ipv6
.tcp_flags
);
590 putNet32(receiver
, elem
->flowType
.ipv6
.priority
);
592 case SFLFLOW_EX_SWITCH
: putSwitch(receiver
, &elem
->flowType
.sw
); break;
593 case SFLFLOW_EX_ROUTER
: putRouter(receiver
, &elem
->flowType
.router
); break;
594 case SFLFLOW_EX_GATEWAY
: putGateway(receiver
, &elem
->flowType
.gateway
); break;
595 case SFLFLOW_EX_USER
: putUser(receiver
, &elem
->flowType
.user
); break;
596 case SFLFLOW_EX_URL
: putUrl(receiver
, &elem
->flowType
.url
); break;
597 case SFLFLOW_EX_MPLS
: putMpls(receiver
, &elem
->flowType
.mpls
); break;
598 case SFLFLOW_EX_NAT
: putNat(receiver
, &elem
->flowType
.nat
); break;
599 case SFLFLOW_EX_MPLS_TUNNEL
: putMplsTunnel(receiver
, &elem
->flowType
.mpls_tunnel
); break;
600 case SFLFLOW_EX_MPLS_VC
: putMplsVc(receiver
, &elem
->flowType
.mpls_vc
); break;
601 case SFLFLOW_EX_MPLS_FTN
: putMplsFtn(receiver
, &elem
->flowType
.mpls_ftn
); break;
602 case SFLFLOW_EX_MPLS_LDP_FEC
: putMplsLdpFec(receiver
, &elem
->flowType
.mpls_ldp_fec
); break;
603 case SFLFLOW_EX_VLAN_TUNNEL
: putVlanTunnel(receiver
, &elem
->flowType
.vlan_tunnel
); break;
604 case SFLFLOW_EX_VNI_EGRESS
:
605 case SFLFLOW_EX_VNI_INGRESS
:
606 putNet32(receiver
, elem
->flowType
.tunnel_vni
.vni
);
610 sflError(receiver
, "unexpected packet_data_tag");
618 assert(((u_char
*)receiver
->sampleCollector
.datap
619 - (u_char
*)receiver
->sampleCollector
.data
620 - receiver
->sampleCollector
.pktlen
) == (u_int32_t
)packedSize
);
623 receiver
->sampleCollector
.pktlen
= (u_char
*)receiver
->sampleCollector
.datap
- (u_char
*)receiver
->sampleCollector
.data
;
627 /*_________________-----------------------------__________________
628 _________________ computeCountersSampleSize __________________
629 -----------------_____________________________------------------
632 static int computeCountersSampleSize(SFLReceiver
*receiver
, SFL_COUNTERS_SAMPLE_TYPE
*cs
)
634 SFLCounters_sample_element
*elem
= cs
->elements
;
635 #ifdef SFL_USE_32BIT_INDEX
636 u_int siz
= 24; /* tag, length, sequence_number, ds_class, ds_index, number of elements */
638 u_int siz
= 20; /* tag, length, sequence_number, source_id, number of elements */
641 cs
->num_elements
= 0; /* we're going to count them again even if this was set by the client */
642 for(; elem
!= NULL
; elem
= elem
->nxt
) {
645 siz
+= 8; /* tag, length */
647 case SFLCOUNTERS_GENERIC
: elemSiz
= SFL_CTR_GENERIC_XDR_SIZE
; break;
648 case SFLCOUNTERS_ETHERNET
: elemSiz
= SFL_CTR_ETHERNET_XDR_SIZE
; break;
649 case SFLCOUNTERS_TOKENRING
: elemSiz
= sizeof(elem
->counterBlock
.tokenring
); break;
650 case SFLCOUNTERS_VG
: elemSiz
= sizeof(elem
->counterBlock
.vg
); break;
651 case SFLCOUNTERS_VLAN
: elemSiz
= sizeof(elem
->counterBlock
.vlan
); break;
652 case SFLCOUNTERS_LACP
: elemSiz
= SFL_CTR_LACP_XDR_SIZE
; break;
653 case SFLCOUNTERS_OPENFLOWPORT
: elemSiz
= SFL_CTR_OPENFLOWPORT_XDR_SIZE
; break;
654 case SFLCOUNTERS_PORTNAME
: elemSiz
= stringEncodingLength(&elem
->counterBlock
.portName
.portName
); break;
655 case SFLCOUNTERS_APP_RESOURCES
: elemSiz
= SFL_CTR_APP_RESOURCES_XDR_SIZE
; break;
656 case SFLCOUNTERS_OVSDP
: elemSiz
= SFL_CTR_OVSDP_XDR_SIZE
; break;
658 sflError(receiver
, "unexpected counters_tag");
662 // cache the element size, and accumulate it into the overall FlowSample size
663 elem
->length
= elemSiz
;
669 /*_________________----------------------------------__________________
670 _________________ sfl_receiver_writeCountersSample __________________
671 -----------------__________________________________------------------
674 int sfl_receiver_writeCountersSample(SFLReceiver
*receiver
, SFL_COUNTERS_SAMPLE_TYPE
*cs
)
677 if(cs
== NULL
) return -1;
678 // if the sample pkt is full enough so that this sample might put
679 // it over the limit, then we should send it now.
680 if((packedSize
= computeCountersSampleSize(receiver
, cs
)) == -1) return -1;
682 // check in case this one sample alone is too big for the datagram
683 // in fact - if it is even half as big then we should ditch it. Very
684 // important to avoid overruning the packet buffer.
685 if(packedSize
> (int)(receiver
->sFlowRcvrMaximumDatagramSize
/ 2)) {
686 sflError(receiver
, "counters sample too big for datagram");
690 if((receiver
->sampleCollector
.pktlen
+ packedSize
) >= receiver
->sFlowRcvrMaximumDatagramSize
)
691 sendSample(receiver
);
693 receiver
->sampleCollector
.numSamples
++;
695 #ifdef SFL_USE_32BIT_INDEX
696 putNet32(receiver
, SFLCOUNTERS_SAMPLE_EXPANDED
);
698 putNet32(receiver
, SFLCOUNTERS_SAMPLE
);
701 putNet32(receiver
, packedSize
- 8); // tag and length not included
702 putNet32(receiver
, cs
->sequence_number
);
704 #ifdef SFL_USE_32BIT_INDEX
705 putNet32(receiver
, cs
->ds_class
);
706 putNet32(receiver
, cs
->ds_index
);
708 putNet32(receiver
, cs
->source_id
);
711 putNet32(receiver
, cs
->num_elements
);
714 SFLCounters_sample_element
*elem
= cs
->elements
;
715 for(; elem
!= NULL
; elem
= elem
->nxt
) {
717 putNet32(receiver
, elem
->tag
);
718 putNet32(receiver
, elem
->length
); // length cached in computeCountersSampleSize()
721 case SFLCOUNTERS_GENERIC
:
722 putGenericCounters(receiver
, &(elem
->counterBlock
.generic
));
724 case SFLCOUNTERS_ETHERNET
:
725 // all these counters are 32-bit
726 putNet32_run(receiver
, &elem
->counterBlock
.ethernet
, sizeof(elem
->counterBlock
.ethernet
) / 4);
728 case SFLCOUNTERS_TOKENRING
:
729 // all these counters are 32-bit
730 putNet32_run(receiver
, &elem
->counterBlock
.tokenring
, sizeof(elem
->counterBlock
.tokenring
) / 4);
734 putNet32(receiver
, elem
->counterBlock
.vg
.dot12InHighPriorityFrames
);
735 putNet64(receiver
, elem
->counterBlock
.vg
.dot12InHighPriorityOctets
);
736 putNet32(receiver
, elem
->counterBlock
.vg
.dot12InNormPriorityFrames
);
737 putNet64(receiver
, elem
->counterBlock
.vg
.dot12InNormPriorityOctets
);
738 putNet32(receiver
, elem
->counterBlock
.vg
.dot12InIPMErrors
);
739 putNet32(receiver
, elem
->counterBlock
.vg
.dot12InOversizeFrameErrors
);
740 putNet32(receiver
, elem
->counterBlock
.vg
.dot12InDataErrors
);
741 putNet32(receiver
, elem
->counterBlock
.vg
.dot12InNullAddressedFrames
);
742 putNet32(receiver
, elem
->counterBlock
.vg
.dot12OutHighPriorityFrames
);
743 putNet64(receiver
, elem
->counterBlock
.vg
.dot12OutHighPriorityOctets
);
744 putNet32(receiver
, elem
->counterBlock
.vg
.dot12TransitionIntoTrainings
);
745 putNet64(receiver
, elem
->counterBlock
.vg
.dot12HCInHighPriorityOctets
);
746 putNet64(receiver
, elem
->counterBlock
.vg
.dot12HCInNormPriorityOctets
);
747 putNet64(receiver
, elem
->counterBlock
.vg
.dot12HCOutHighPriorityOctets
);
749 case SFLCOUNTERS_VLAN
:
751 putNet32(receiver
, elem
->counterBlock
.vlan
.vlan_id
);
752 putNet64(receiver
, elem
->counterBlock
.vlan
.octets
);
753 putNet32(receiver
, elem
->counterBlock
.vlan
.ucastPkts
);
754 putNet32(receiver
, elem
->counterBlock
.vlan
.multicastPkts
);
755 putNet32(receiver
, elem
->counterBlock
.vlan
.broadcastPkts
);
756 putNet32(receiver
, elem
->counterBlock
.vlan
.discards
);
758 case SFLCOUNTERS_LACP
:
759 putMACAddress(receiver
, elem
->counterBlock
.lacp
.actorSystemID
);
760 putMACAddress(receiver
, elem
->counterBlock
.lacp
.partnerSystemID
);
761 putNet32(receiver
, elem
->counterBlock
.lacp
.attachedAggID
);
762 put32(receiver
, elem
->counterBlock
.lacp
.portState
.all
);
763 putNet32(receiver
, elem
->counterBlock
.lacp
.LACPDUsRx
);
764 putNet32(receiver
, elem
->counterBlock
.lacp
.markerPDUsRx
);
765 putNet32(receiver
, elem
->counterBlock
.lacp
.markerResponsePDUsRx
);
766 putNet32(receiver
, elem
->counterBlock
.lacp
.unknownRx
);
767 putNet32(receiver
, elem
->counterBlock
.lacp
.illegalRx
);
768 putNet32(receiver
, elem
->counterBlock
.lacp
.LACPDUsTx
);
769 putNet32(receiver
, elem
->counterBlock
.lacp
.markerPDUsTx
);
770 putNet32(receiver
, elem
->counterBlock
.lacp
.markerResponsePDUsTx
);
772 case SFLCOUNTERS_OPENFLOWPORT
:
773 putNet64(receiver
, elem
->counterBlock
.ofPort
.datapath_id
);
774 putNet32(receiver
, elem
->counterBlock
.ofPort
.port_no
);
776 case SFLCOUNTERS_PORTNAME
:
777 putString(receiver
, &elem
->counterBlock
.portName
.portName
);
779 case SFLCOUNTERS_APP_RESOURCES
:
780 putNet32(receiver
, elem
->counterBlock
.appResources
.user_time
);
781 putNet32(receiver
, elem
->counterBlock
.appResources
.system_time
);
782 putNet64(receiver
, elem
->counterBlock
.appResources
.mem_used
);
783 putNet64(receiver
, elem
->counterBlock
.appResources
.mem_max
);
784 putNet32(receiver
, elem
->counterBlock
.appResources
.fd_open
);
785 putNet32(receiver
, elem
->counterBlock
.appResources
.fd_max
);
786 putNet32(receiver
, elem
->counterBlock
.appResources
.conn_open
);
787 putNet32(receiver
, elem
->counterBlock
.appResources
.conn_max
);
789 case SFLCOUNTERS_OVSDP
:
790 putNet32(receiver
, elem
->counterBlock
.ovsdp
.n_hit
);
791 putNet32(receiver
, elem
->counterBlock
.ovsdp
.n_missed
);
792 putNet32(receiver
, elem
->counterBlock
.ovsdp
.n_lost
);
793 putNet32(receiver
, elem
->counterBlock
.ovsdp
.n_mask_hit
);
794 putNet32(receiver
, elem
->counterBlock
.ovsdp
.n_flows
);
795 putNet32(receiver
, elem
->counterBlock
.ovsdp
.n_masks
);
798 sflError(receiver
, "unexpected counters_tag");
805 assert(((u_char
*)receiver
->sampleCollector
.datap
806 - (u_char
*)receiver
->sampleCollector
.data
807 - receiver
->sampleCollector
.pktlen
) == (u_int32_t
)packedSize
);
810 receiver
->sampleCollector
.pktlen
= (u_char
*)receiver
->sampleCollector
.datap
- (u_char
*)receiver
->sampleCollector
.data
;
814 /*_________________---------------------------------__________________
815 _________________ sfl_receiver_samplePacketsSent __________________
816 -----------------_________________________________------------------
819 u_int32_t
sfl_receiver_samplePacketsSent(SFLReceiver
*receiver
)
821 return receiver
->sampleCollector
.packetSeqNo
;
824 /*_________________---------------------------__________________
825 _________________ sendSample __________________
826 -----------------___________________________------------------
829 static void sendSample(SFLReceiver
*receiver
)
831 /* construct and send out the sample, then reset for the next one... */
832 /* first fill in the header with the latest values */
833 /* version, agent_address and sub_agent_id were pre-set. */
834 u_int32_t hdrIdx
= (receiver
->agent
->myIP
.type
== SFLADDRESSTYPE_IP_V6
) ? 7 : 4;
835 receiver
->sampleCollector
.data
[hdrIdx
++] = htonl(++receiver
->sampleCollector
.packetSeqNo
); /* seq no */
836 receiver
->sampleCollector
.data
[hdrIdx
++] = htonl((receiver
->agent
->now
- receiver
->agent
->bootTime
) * 1000); /* uptime */
837 receiver
->sampleCollector
.data
[hdrIdx
++] = htonl(receiver
->sampleCollector
.numSamples
); /* num samples */
839 if(receiver
->agent
->sendFn
) (*receiver
->agent
->sendFn
)(receiver
->agent
->magic
,
842 (u_char
*)receiver
->sampleCollector
.data
,
843 receiver
->sampleCollector
.pktlen
);
845 #ifdef SFLOW_DO_SOCKET
847 if (receiver
->sFlowRcvrAddress
.type
== SFLADDRESSTYPE_IP_V6
) {
848 u_int32_t soclen
= sizeof(struct sockaddr_in6
);
849 int result
= sendto(receiver
->agent
->receiverSocket6
,
850 receiver
->sampleCollector
.data
,
851 receiver
->sampleCollector
.pktlen
,
853 (struct sockaddr
*)&receiver
->receiver6
,
855 if(result
== -1 && errno
!= EINTR
) sfl_agent_sysError(receiver
->agent
, "receiver", "IPv6 socket sendto error");
856 if(result
== 0) sfl_agent_error(receiver
->agent
, "receiver", "IPv6 socket sendto returned 0");
859 u_int32_t soclen
= sizeof(struct sockaddr_in
);
860 int result
= sendto(receiver
->agent
->receiverSocket4
,
861 receiver
->sampleCollector
.data
,
862 receiver
->sampleCollector
.pktlen
,
864 (struct sockaddr
*)&receiver
->receiver4
,
866 if(result
== -1 && errno
!= EINTR
) sfl_agent_sysError(receiver
->agent
, "receiver", "socket sendto error");
867 if(result
== 0) sfl_agent_error(receiver
->agent
, "receiver", "socket sendto returned 0");
872 /* reset for the next time */
873 resetSampleCollector(receiver
);
876 /*_________________---------------------------__________________
877 _________________ resetSampleCollector __________________
878 -----------------___________________________------------------
881 static void resetSampleCollector(SFLReceiver
*receiver
)
883 receiver
->sampleCollector
.pktlen
= 0;
884 receiver
->sampleCollector
.numSamples
= 0;
885 /* point the datap to just after the header */
886 receiver
->sampleCollector
.datap
= (receiver
->agent
->myIP
.type
== SFLADDRESSTYPE_IP_V6
) ?
887 (receiver
->sampleCollector
.data
+ 10) : (receiver
->sampleCollector
.data
+ 7);
889 receiver
->sampleCollector
.pktlen
= (u_char
*)receiver
->sampleCollector
.datap
- (u_char
*)receiver
->sampleCollector
.data
;
892 /*_________________---------------------------__________________
893 _________________ sflError __________________
894 -----------------___________________________------------------
897 static void sflError(SFLReceiver
*receiver
, char *msg
)
899 sfl_agent_error(receiver
->agent
, "receiver", msg
);
900 resetSampleCollector(receiver
);
903 #endif /* !__CHECKER__ */