2 * Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofproto-dpif-ipfix.h"
20 #include "byte-order.h"
21 #include "collectors.h"
24 #include "openvswitch/hmap.h"
26 #include "openvswitch/list.h"
27 #include "openvswitch/ofpbuf.h"
29 #include "ofproto-dpif.h"
30 #include "dp-packet.h"
32 #include "openvswitch/poll-loop.h"
36 #include "openvswitch/vlog.h"
38 VLOG_DEFINE_THIS_MODULE(ipfix
);
40 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
41 static struct ovs_mutex mutex
= OVS_MUTEX_INITIALIZER
;
43 /* This variable represents a number of exporters that have been created
44 * throughout OvS lifecycle. It's used to identify Exporting Process. Since
45 * it's NOT decreased when exporter is destroyed, it will eventually overflow.
46 * Considering the maximum value it can hold and the fact that Exporting
47 * Process may be re-started with a different ID, this shouldn't be a problem.
49 static uint32_t exporter_total_count
;
51 /* Cf. IETF RFC 5101 Section 10.3.4. */
52 #define IPFIX_DEFAULT_COLLECTOR_PORT 4739
54 /* Cf. IETF RFC 5881 Setion 8. */
55 #define BFD_CONTROL_DEST_PORT 3784
56 #define BFD_ECHO_DEST_PORT 3785
58 enum ipfix_sampled_packet_type
{
59 IPFIX_SAMPLED_PKT_UNKNOWN
= 0x00,
60 IPFIX_SAMPLED_PKT_IPV4_OK
= 0x01,
61 IPFIX_SAMPLED_PKT_IPV6_OK
= 0x02,
62 IPFIX_SAMPLED_PKT_IPV4_ERROR
= 0x03,
63 IPFIX_SAMPLED_PKT_IPV6_ERROR
= 0x04,
64 IPFIX_SAMPLED_PKT_OTHERS
= 0x05
67 /* The standard layer2SegmentId (ID 351) element is included in vDS to send
68 * the VxLAN tunnel's VNI. It is 64-bit long, the most significant byte is
69 * used to indicate the type of tunnel (0x01 = VxLAN, 0x02 = GRE) and the three
70 * least significant bytes hold the value of the layer 2 overlay network
71 * segment identifier: a 24-bit VxLAN tunnel's VNI or a 24-bit GRE tunnel's
72 * TNI. This is not compatible with STT, as implemented in OVS, as
73 * its tunnel IDs is 64-bit.
75 * Two new enterprise information elements are defined which are similar to
76 * laryerSegmentId but support 64-bit IDs:
77 * tunnelType (ID 891) and tunnelKey (ID 892).
79 * The enum dpif_ipfix_tunnel_type is to declare the types supported in the
81 * The number of ipfix tunnel types includes two reserverd types: 0x04 and 0x06.
83 enum dpif_ipfix_tunnel_type
{
84 DPIF_IPFIX_TUNNEL_UNKNOWN
= 0x00,
85 DPIF_IPFIX_TUNNEL_VXLAN
= 0x01,
86 DPIF_IPFIX_TUNNEL_GRE
= 0x02,
87 DPIF_IPFIX_TUNNEL_LISP
= 0x03,
88 DPIF_IPFIX_TUNNEL_STT
= 0x04,
89 DPIF_IPFIX_TUNNEL_GENEVE
= 0x07,
93 typedef struct ofputil_ipfix_stats ofproto_ipfix_stats
;
95 struct dpif_ipfix_global_stats
{
96 uint64_t dropped_packet_total_count
;
97 uint64_t dropped_octet_total_count
;
98 uint64_t packet_total_count
;
99 uint64_t octet_total_count
;
100 uint64_t octet_total_sum_of_squares
;
101 uint64_t layer2_octet_total_count
;
102 uint64_t tcp_ack_total_count
;
103 uint64_t tcp_fin_total_count
;
104 uint64_t tcp_psh_total_count
;
105 uint64_t tcp_rst_total_count
;
106 uint64_t tcp_syn_total_count
;
107 uint64_t tcp_urg_total_count
;
108 uint64_t post_mcast_packet_total_count
;
109 uint64_t post_mcast_octet_total_count
;
110 uint64_t in_ucast_packet_total_count
;
111 uint64_t in_mcast_packet_total_count
;
112 uint64_t in_bcast_packet_total_count
;
113 uint64_t out_ucast_packet_total_count
;
114 uint64_t out_bcast_packet_total_count
;
117 struct dpif_ipfix_port
{
118 struct hmap_node hmap_node
; /* In struct dpif_ipfix's "ports" hmap. */
119 struct ofport
*ofport
; /* To retrieve port stats. */
121 enum dpif_ipfix_tunnel_type tunnel_type
;
122 uint8_t tunnel_key_length
;
126 struct dpif_ipfix_exporter
{
127 uint32_t exporter_id
; /* Exporting Process identifier */
128 struct collectors
*collectors
;
130 time_t last_template_set_time
;
131 struct hmap cache_flow_key_map
; /* ipfix_flow_cache_entry. */
132 struct ovs_list cache_flow_start_timestamp_list
; /* ipfix_flow_cache_entry. */
133 uint32_t cache_active_timeout
; /* In seconds. */
134 uint32_t cache_max_flows
;
135 char *virtual_obs_id
;
136 uint8_t virtual_obs_len
;
138 ofproto_ipfix_stats ofproto_stats
;
139 struct dpif_ipfix_global_stats ipfix_global_stats
;
142 struct dpif_ipfix_bridge_exporter
{
143 struct dpif_ipfix_exporter exporter
;
144 struct ofproto_ipfix_bridge_exporter_options
*options
;
145 uint32_t probability
;
148 struct dpif_ipfix_flow_exporter
{
149 struct dpif_ipfix_exporter exporter
;
150 struct ofproto_ipfix_flow_exporter_options
*options
;
153 struct dpif_ipfix_flow_exporter_map_node
{
154 struct hmap_node node
;
155 struct dpif_ipfix_flow_exporter exporter
;
159 struct dpif_ipfix_bridge_exporter bridge_exporter
;
160 struct hmap flow_exporter_map
; /* dpif_ipfix_flow_exporter_map_node. */
161 struct hmap ports
; /* Contains "struct dpif_ipfix_port"s.
162 * It makes port lookups faster in sampling
164 struct ovs_refcount ref_cnt
;
167 #define IPFIX_VERSION 0x000a
169 /* When using UDP, IPFIX Template Records must be re-sent regularly.
170 * The standard default interval is 10 minutes (600 seconds).
171 * Cf. IETF RFC 5101 Section 10.3.6. */
172 #define IPFIX_TEMPLATE_INTERVAL 600
174 /* Cf. IETF RFC 5101 Section 3.1. */
176 struct ipfix_header
{
177 ovs_be16 version
; /* IPFIX_VERSION. */
178 ovs_be16 length
; /* Length in bytes including this header. */
179 ovs_be32 export_time
; /* Seconds since the epoch. */
180 ovs_be32 seq_number
; /* Message sequence number. */
181 ovs_be32 obs_domain_id
; /* Observation Domain ID. */
183 BUILD_ASSERT_DECL(sizeof(struct ipfix_header
) == 16);
185 #define IPFIX_SET_ID_TEMPLATE 2
186 #define IPFIX_SET_ID_OPTION_TEMPLATE 3
188 enum ipfix_options_template
{
189 IPFIX_OPTIONS_TEMPLATE_EXPORTER_STATS
= 0,
190 NUM_IPFIX_OPTIONS_TEMPLATE
193 /* Cf. IETF RFC 5101 Section 3.3.2. */
195 struct ipfix_set_header
{
196 ovs_be16 set_id
; /* IPFIX_SET_ID_* or valid template ID for Data Sets. */
197 ovs_be16 length
; /* Length of the set in bytes including header. */
199 BUILD_ASSERT_DECL(sizeof(struct ipfix_set_header
) == 4);
201 /* Alternatives for templates at each layer. A template is defined by
202 * a combination of one value for each layer. */
203 enum ipfix_proto_l2
{
204 IPFIX_PROTO_L2_ETH
= 0, /* No VLAN. */
208 enum ipfix_proto_l3
{
209 IPFIX_PROTO_L3_UNKNOWN
= 0,
214 enum ipfix_proto_l4
{
215 IPFIX_PROTO_L4_UNKNOWN
= 0,
222 enum ipfix_proto_tunnel
{
223 IPFIX_PROTO_NOT_TUNNELED
= 0,
224 IPFIX_PROTO_TUNNELED
, /* Support gre, lisp and vxlan. */
225 NUM_IPFIX_PROTO_TUNNEL
228 /* Any Template ID > 255 is usable for Template Records. */
229 #define IPFIX_TEMPLATE_ID_MIN 256
231 /* Cf. IETF RFC 5101 Section 3.4.1. */
233 struct ipfix_template_record_header
{
234 ovs_be16 template_id
;
235 ovs_be16 field_count
;
237 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_record_header
) == 4);
239 /* Cf. IETF RFC 5101 Section 3.4.2.2. */
241 struct ipfix_options_template_record_header
{
242 ovs_be16 template_id
; /* Template ID of Data Set is within 256-65535
244 ovs_be16 field_count
; /* Number of all fields in this Options
245 * Template Record, including the Scope
247 ovs_be16 scope_field_count
; /* Number of scope fields. The number MUST BE
250 BUILD_ASSERT_DECL(sizeof(struct ipfix_options_template_record_header
) == 6);
253 enum ipfix_entity_id
{
254 /* standard IPFIX elements */
255 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ID_##ENUM = ID,
256 #include "ofproto/ipfix-entities.def"
257 /* non-standard IPFIX elements */
258 #define IPFIX_SET_ENTERPRISE(v) (((v) | 0x8000))
259 #define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
260 IPFIX_ENTITY_ID_##ENUM = IPFIX_SET_ENTERPRISE(ID),
261 #include "ofproto/ipfix-enterprise-entities.def"
264 enum ipfix_entity_size
{
265 /* standard IPFIX elements */
266 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_SIZE_##ENUM = SIZE,
267 #include "ofproto/ipfix-entities.def"
268 /* non-standard IPFIX elements */
269 #define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
270 IPFIX_ENTITY_SIZE_##ENUM = SIZE,
271 #include "ofproto/ipfix-enterprise-entities.def"
274 enum ipfix_entity_enterprise
{
275 /* standard IPFIX elements */
276 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ENTERPRISE_##ENUM = 0,
277 #include "ofproto/ipfix-entities.def"
278 /* non-standard IPFIX elements */
279 #define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
280 IPFIX_ENTITY_ENTERPRISE_##ENUM = ENTERPRISE,
281 #include "ofproto/ipfix-enterprise-entities.def"
285 struct ipfix_template_field_specifier
{
286 ovs_be16 element_id
; /* IPFIX_ENTITY_ID_*. */
287 ovs_be16 field_length
; /* Length of the field's value, in bytes.
288 * For Variable-Length element, it should be 65535.
290 ovs_be32 enterprise
; /* Enterprise number */
292 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_field_specifier
) == 8);
294 /* Cf. IETF RFC 5102 Section 5.11.6. */
295 enum ipfix_flow_direction
{
298 NUM_IPFIX_FLOW_DIRECTION
301 /* Part of data record flow key for common metadata and Ethernet entities. */
303 struct ipfix_data_record_flow_key_common
{
304 ovs_be32 observation_point_id
; /* OBSERVATION_POINT_ID */
305 uint8_t flow_direction
; /* FLOW_DIRECTION */
306 struct eth_addr source_mac_address
; /* SOURCE_MAC_ADDRESS */
307 struct eth_addr destination_mac_address
; /* DESTINATION_MAC_ADDRESS */
308 ovs_be16 ethernet_type
; /* ETHERNET_TYPE */
309 uint8_t ethernet_header_length
; /* ETHERNET_HEADER_LENGTH */
311 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_common
) == 20);
313 /* Part of data record flow key for interface information. Since some of the
314 * elements have variable length, members of this structure should be appended
315 * to the 'struct dp_packet' one by one. */
317 struct ipfix_data_record_flow_key_iface
{
318 ovs_be32 if_index
; /* (INGRESS | EGRESS)_INTERFACE */
319 ovs_be32 if_type
; /* (INGRESS | EGRESS)_INTERFACE_TYPE */
320 uint8_t if_name_len
; /* Variable length element: INTERFACE_NAME */
322 uint8_t if_descr_len
; /* Variable length element: INTERFACE_DESCRIPTION */
325 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_iface
) ==
326 10 + 2 * sizeof(char *));
328 /* Part of data record flow key for VLAN entities. */
330 struct ipfix_data_record_flow_key_vlan
{
331 ovs_be16 vlan_id
; /* VLAN_ID */
332 ovs_be16 dot1q_vlan_id
; /* DOT1Q_VLAN_ID */
333 uint8_t dot1q_priority
; /* DOT1Q_PRIORITY */
335 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_vlan
) == 5);
337 /* Part of data record flow key for IP entities. */
338 /* XXX: Replace IP_TTL with MINIMUM_TTL and MAXIMUM_TTL? */
340 struct ipfix_data_record_flow_key_ip
{
341 uint8_t ip_version
; /* IP_VERSION */
342 uint8_t ip_ttl
; /* IP_TTL */
343 uint8_t protocol_identifier
; /* PROTOCOL_IDENTIFIER */
344 uint8_t ip_diff_serv_code_point
; /* IP_DIFF_SERV_CODE_POINT */
345 uint8_t ip_precedence
; /* IP_PRECEDENCE */
346 uint8_t ip_class_of_service
; /* IP_CLASS_OF_SERVICE */
348 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ip
) == 6);
350 /* Part of data record flow key for IPv4 entities. */
352 struct ipfix_data_record_flow_key_ipv4
{
353 ovs_be32 source_ipv4_address
; /* SOURCE_IPV4_ADDRESS */
354 ovs_be32 destination_ipv4_address
; /* DESTINATION_IPV4_ADDRESS */
356 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv4
) == 8);
358 /* Part of data record flow key for IPv6 entities. */
360 struct ipfix_data_record_flow_key_ipv6
{
361 uint8_t source_ipv6_address
[16]; /* SOURCE_IPV6_ADDRESS */
362 uint8_t destination_ipv6_address
[16]; /* DESTINATION_IPV6_ADDRESS */
363 ovs_be32 flow_label_ipv6
; /* FLOW_LABEL_IPV6 */
365 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv6
) == 36);
367 /* Part of data record flow key for TCP/UDP/SCTP entities. */
369 struct ipfix_data_record_flow_key_transport
{
370 ovs_be16 source_transport_port
; /* SOURCE_TRANSPORT_PORT */
371 ovs_be16 destination_transport_port
; /* DESTINATION_TRANSPORT_PORT */
373 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_transport
) == 4);
375 /* Part of data record flow key for ICMP entities. */
377 struct ipfix_data_record_flow_key_icmp
{
378 uint8_t icmp_type
; /* ICMP_TYPE_IPV4 / ICMP_TYPE_IPV6 */
379 uint8_t icmp_code
; /* ICMP_CODE_IPV4 / ICMP_CODE_IPV6 */
381 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_icmp
) == 2);
383 static uint8_t tunnel_protocol
[NUM_DPIF_IPFIX_TUNNEL
] = {
385 IPPROTO_UDP
, /* DPIF_IPFIX_TUNNEL_VXLAN */
386 IPPROTO_GRE
, /* DPIF_IPFIX_TUNNEL_GRE */
387 IPPROTO_UDP
, /* DPIF_IPFIX_TUNNEL_LISP*/
388 IPPROTO_TCP
, /* DPIF_IPFIX_TUNNEL_STT*/
390 IPPROTO_UDP
, /* DPIF_IPFIX_TUNNEL_GENEVE*/
394 struct ipfix_data_record_flow_key_tunnel
{
395 ovs_be32 tunnel_source_ipv4_address
; /* TUNNEL_SOURCE_IPV4_ADDRESS */
396 ovs_be32 tunnel_destination_ipv4_address
; /* TUNNEL_DESTINATION_IPV4_ADDRESS */
397 uint8_t tunnel_protocol_identifier
; /* TUNNEL_PROTOCOL_IDENTIFIER */
398 ovs_be16 tunnel_source_transport_port
; /* TUNNEL_SOURCE_TRANSPORT_PORT */
399 ovs_be16 tunnel_destination_transport_port
; /* TUNNEL_DESTINATION_TRANSPORT_PORT */
400 uint8_t tunnel_type
; /* TUNNEL_TYPE */
401 uint8_t tunnel_key_length
; /* length of TUNNEL_KEY */
402 uint8_t tunnel_key
[]; /* data of TUNNEL_KEY */
404 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_tunnel
) == 15);
406 /* Cf. IETF RFC 5102 Section 5.11.3. */
407 enum ipfix_flow_end_reason
{
409 ACTIVE_TIMEOUT
= 0x02,
410 END_OF_FLOW_DETECTED
= 0x03,
412 LACK_OF_RESOURCES
= 0x05
415 /* Exporting Process Reliability Statistics data record. */
417 struct ipfix_data_record_exporter_stats
{
419 ovs_be32 exporting_process_id
; /* EXPORTING_PROCESS_ID */
422 ovs_be64 not_sent_packet_total_count
; /* NOT_SENT_PACKET_TOTAL_COUNT */
424 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_exporter_stats
) == 12);
426 /* Part of data record for common aggregated elements. */
428 struct ipfix_data_record_aggregated_common
{
429 ovs_be32 flow_start_delta_microseconds
; /* FLOW_START_DELTA_MICROSECONDS */
430 ovs_be32 flow_end_delta_microseconds
; /* FLOW_END_DELTA_MICROSECONDS */
431 ovs_be64 dropped_packet_delta_count
; /* DROPPED_PACKET_DELTA_COUNT */
432 ovs_be64 dropped_packet_total_count
; /* DROPPED_PACKET_TOTAL_COUNT */
433 ovs_be64 packet_delta_count
; /* PACKET_DELTA_COUNT */
434 ovs_be64 packet_total_count
; /* PACKET_TOTAL_COUNT */
435 /* INGRESS_UNICAST_PACKET_TOTAL_COUNT */
436 ovs_be64 in_ucast_packet_total_count
;
437 /* INGRESS_MULTICAST_PACKET_TOTAL_COUNT */
438 ovs_be64 in_mcast_packet_total_count
;
439 /* INGRESS_BROADCAST_PACKET_TOTAL_COUNT */
440 ovs_be64 in_bcast_packet_total_count
;
441 /* EGRESS_UNICAST_PACKET_TOTAL_COUNT */
442 ovs_be64 out_ucast_packet_total_count
;
443 /* EGRESS_BROADCAST_PACKET_TOTAL_COUNT */
444 ovs_be64 out_bcast_packet_total_count
;
445 ovs_be64 post_mcast_packet_delta_count
; /* POST_MCAST_PACKET_DELTA_COUNT */
446 ovs_be64 post_mcast_packet_total_count
; /* POST_MCAST_PACKET_TOTAL_COUNT */
447 ovs_be64 layer2_octet_delta_count
; /* LAYER2_OCTET_DELTA_COUNT */
448 ovs_be64 layer2_octet_total_count
; /* LAYER2_OCTET_TOTAL_COUNT */
449 uint8_t flow_end_reason
; /* FLOW_END_REASON */
451 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_common
) == 113);
453 /* Part of data record for IP aggregated elements. */
455 struct ipfix_data_record_aggregated_ip
{
456 ovs_be64 dropped_octet_delta_count
; /* DROPPED_OCTET_DELTA_COUNT */
457 ovs_be64 dropped_octet_total_count
; /* DROPPED_OCTET_TOTAL_COUNT */
458 ovs_be64 octet_delta_count
; /* OCTET_DELTA_COUNT */
459 ovs_be64 octet_total_count
; /* OCTET_TOTAL_COUNT */
460 ovs_be64 octet_delta_sum_of_squares
; /* OCTET_DELTA_SUM_OF_SQUARES */
461 ovs_be64 octet_total_sum_of_squares
; /* OCTET_TOTAL_SUM_OF_SQUARES */
462 ovs_be64 minimum_ip_total_length
; /* MINIMUM_IP_TOTAL_LENGTH */
463 ovs_be64 maximum_ip_total_length
; /* MAXIMUM_IP_TOTAL_LENGTH */
464 ovs_be64 post_mcast_octet_delta_count
; /* POST_MCAST_OCTET_DELTA_COUNT */
465 ovs_be64 post_mcast_octet_total_count
; /* POST_MCAST_OCTET_TOTAL_COUNT */
467 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_ip
) == 80);
469 /* Part of data record for TCP aggregated elements. */
471 struct ipfix_data_record_aggregated_tcp
{
472 ovs_be64 tcp_ack_total_count
; /* TCP_ACK_TOTAL_COUNT */
473 ovs_be64 tcp_fin_total_count
; /* TCP_FIN_TOTAL_COUNT */
474 ovs_be64 tcp_psh_total_count
; /* TCP_PSH_TOTAL_COUNT */
475 ovs_be64 tcp_rst_total_count
; /* TCP_RST_TOTAL_COUNT */
476 ovs_be64 tcp_syn_total_count
; /* TCP_SYN_TOTAL_COUNT */
477 ovs_be64 tcp_urg_total_count
; /* TCP_URG_TOTAL_COUNT */
479 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_tcp
) == 48);
482 * Refer to RFC 7011, the length of Variable length element is 0~65535:
483 * In most case, it should be less than 255 octets:
485 * 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
486 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
487 * | Length (< 255)| Information Element |
488 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
489 * | ... continuing as needed |
490 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
492 * When it is greater than or equeal to 255 octets:
494 * 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
495 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
496 * | 255 | Length (0 to 65535) | IE |
497 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
498 * | ... continuing as needed |
499 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
502 * Now, only the virtual_obs_id whose length < 255 is implemented.
505 #define IPFIX_VIRTUAL_OBS_MAX_LEN 254
508 * support tunnel key for:
511 * LISP: 24-bit instance ID
514 #define MAX_TUNNEL_KEY_LEN 8
516 #define MAX_IF_NAME_LEN 64
517 #define MAX_IF_DESCR_LEN 128
520 * Calculate interface information length in flow key.
521 * This is used to calculate max flow key length.
523 #define FLOW_KEY_IFACE_LEN \
524 (sizeof(struct ipfix_data_record_flow_key_iface) \
525 - 2 * sizeof(char *) \
526 + MAX_IF_NAME_LEN + MAX_IF_DESCR_LEN)
528 #define MAX_FLOW_KEY_LEN \
529 (sizeof(struct ipfix_data_record_flow_key_common) \
530 + FLOW_KEY_IFACE_LEN \
531 + sizeof(struct ipfix_data_record_flow_key_vlan) \
532 + sizeof(struct ipfix_data_record_flow_key_ip) \
533 + MAX(sizeof(struct ipfix_data_record_flow_key_ipv4), \
534 sizeof(struct ipfix_data_record_flow_key_ipv6)) \
535 + MAX(sizeof(struct ipfix_data_record_flow_key_icmp), \
536 sizeof(struct ipfix_data_record_flow_key_transport)) \
537 + sizeof(struct ipfix_data_record_flow_key_tunnel) \
538 + MAX_TUNNEL_KEY_LEN)
540 #define MAX_DATA_RECORD_LEN \
542 + sizeof(struct ipfix_data_record_aggregated_common) \
543 + sizeof(struct ipfix_data_record_aggregated_ip) \
544 + sizeof(struct ipfix_data_record_aggregated_tcp))
546 #define MAX_OPTIONS_DATA_RECORD_LEN \
547 (sizeof(struct ipfix_data_record_exporter_stats))
550 /* Max length of a data set. To simplify the implementation, each
551 * data record is sent in a separate data set, so each data set
552 * contains at most one data record. */
553 #define MAX_DATA_SET_LEN \
554 (sizeof(struct ipfix_set_header) \
555 + MAX(MAX_DATA_RECORD_LEN, \
556 MAX_OPTIONS_DATA_RECORD_LEN))
558 /* Max length of an IPFIX message. Arbitrarily set to accommodate low
560 #define MAX_MESSAGE_LEN 1024
562 /* Cache structures. */
565 struct ipfix_flow_key
{
566 uint32_t obs_domain_id
;
567 uint16_t template_id
;
568 size_t flow_key_msg_part_size
;
569 uint64_t flow_key_msg_part
[DIV_ROUND_UP(MAX_FLOW_KEY_LEN
, 8)];
572 /* Flow cache entry. */
573 struct ipfix_flow_cache_entry
{
574 struct hmap_node flow_key_map_node
;
575 struct ovs_list cache_flow_start_timestamp_list_node
;
576 struct ipfix_flow_key flow_key
;
577 /* Common aggregated elements. */
578 uint64_t flow_start_timestamp_usec
;
579 uint64_t flow_end_timestamp_usec
;
580 uint64_t dropped_packet_delta_count
;
581 uint64_t dropped_packet_total_count
;
582 uint64_t packet_delta_count
;
583 uint64_t packet_total_count
;
584 uint64_t in_ucast_packet_total_count
;
585 uint64_t in_mcast_packet_total_count
;
586 uint64_t in_bcast_packet_total_count
;
587 uint64_t out_ucast_packet_total_count
;
588 uint64_t out_bcast_packet_total_count
;
589 uint64_t post_mcast_packet_total_count
;
590 uint64_t post_mcast_packet_delta_count
;
591 uint64_t post_mcast_octet_total_count
;
592 uint64_t post_mcast_octet_delta_count
;
593 uint64_t layer2_octet_delta_count
;
594 uint64_t layer2_octet_total_count
;
595 uint64_t dropped_octet_delta_count
;
596 uint64_t dropped_octet_total_count
;
597 uint64_t octet_delta_count
;
598 uint64_t octet_total_count
;
599 uint64_t octet_delta_sum_of_squares
; /* 0 if not IP. */
600 uint64_t octet_total_sum_of_squares
; /* 0 if not IP. */
601 uint16_t minimum_ip_total_length
; /* 0 if not IP. */
602 uint16_t maximum_ip_total_length
; /* 0 if not IP. */
603 uint64_t tcp_packet_delta_count
;
604 uint64_t tcp_ack_total_count
;
605 uint64_t tcp_fin_total_count
;
606 uint64_t tcp_psh_total_count
;
607 uint64_t tcp_rst_total_count
;
608 uint64_t tcp_syn_total_count
;
609 uint64_t tcp_urg_total_count
;
612 static void dpif_ipfix_cache_expire(struct dpif_ipfix_exporter
*, bool,
613 const uint64_t, const uint32_t);
615 static void get_export_time_now(uint64_t *, uint32_t *);
617 static void dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter
*, bool);
620 ofproto_ipfix_bridge_exporter_options_equal(
621 const struct ofproto_ipfix_bridge_exporter_options
*a
,
622 const struct ofproto_ipfix_bridge_exporter_options
*b
)
624 return (a
->obs_domain_id
== b
->obs_domain_id
625 && a
->obs_point_id
== b
->obs_point_id
626 && a
->sampling_rate
== b
->sampling_rate
627 && a
->cache_active_timeout
== b
->cache_active_timeout
628 && a
->cache_max_flows
== b
->cache_max_flows
629 && a
->enable_tunnel_sampling
== b
->enable_tunnel_sampling
630 && a
->enable_input_sampling
== b
->enable_input_sampling
631 && a
->enable_output_sampling
== b
->enable_output_sampling
632 && sset_equals(&a
->targets
, &b
->targets
)
633 && nullable_string_is_equal(a
->virtual_obs_id
, b
->virtual_obs_id
));
636 static struct ofproto_ipfix_bridge_exporter_options
*
637 ofproto_ipfix_bridge_exporter_options_clone(
638 const struct ofproto_ipfix_bridge_exporter_options
*old
)
640 struct ofproto_ipfix_bridge_exporter_options
*new =
641 xmemdup(old
, sizeof *old
);
642 sset_clone(&new->targets
, &old
->targets
);
643 new->virtual_obs_id
= nullable_xstrdup(old
->virtual_obs_id
);
648 ofproto_ipfix_bridge_exporter_options_destroy(
649 struct ofproto_ipfix_bridge_exporter_options
*options
)
652 sset_destroy(&options
->targets
);
653 free(options
->virtual_obs_id
);
659 ofproto_ipfix_flow_exporter_options_equal(
660 const struct ofproto_ipfix_flow_exporter_options
*a
,
661 const struct ofproto_ipfix_flow_exporter_options
*b
)
663 return (a
->collector_set_id
== b
->collector_set_id
664 && a
->cache_active_timeout
== b
->cache_active_timeout
665 && a
->cache_max_flows
== b
->cache_max_flows
666 && a
->enable_tunnel_sampling
== b
->enable_tunnel_sampling
667 && sset_equals(&a
->targets
, &b
->targets
)
668 && nullable_string_is_equal(a
->virtual_obs_id
, b
->virtual_obs_id
));
671 static struct ofproto_ipfix_flow_exporter_options
*
672 ofproto_ipfix_flow_exporter_options_clone(
673 const struct ofproto_ipfix_flow_exporter_options
*old
)
675 struct ofproto_ipfix_flow_exporter_options
*new =
676 xmemdup(old
, sizeof *old
);
677 sset_clone(&new->targets
, &old
->targets
);
678 new->virtual_obs_id
= nullable_xstrdup(old
->virtual_obs_id
);
683 ofproto_ipfix_flow_exporter_options_destroy(
684 struct ofproto_ipfix_flow_exporter_options
*options
)
687 sset_destroy(&options
->targets
);
688 free(options
->virtual_obs_id
);
694 dpif_ipfix_exporter_init(struct dpif_ipfix_exporter
*exporter
)
696 exporter
->exporter_id
= ++exporter_total_count
;
697 exporter
->collectors
= NULL
;
698 exporter
->seq_number
= 1;
699 exporter
->last_template_set_time
= 0;
700 hmap_init(&exporter
->cache_flow_key_map
);
701 ovs_list_init(&exporter
->cache_flow_start_timestamp_list
);
702 exporter
->cache_active_timeout
= 0;
703 exporter
->cache_max_flows
= 0;
704 exporter
->virtual_obs_id
= NULL
;
705 exporter
->virtual_obs_len
= 0;
707 memset(&exporter
->ipfix_global_stats
, 0,
708 sizeof(struct dpif_ipfix_global_stats
));
712 dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter
*exporter
)
714 /* Flush the cache with flow end reason "forced end." */
715 dpif_ipfix_cache_expire_now(exporter
, true);
717 collectors_destroy(exporter
->collectors
);
718 exporter
->exporter_id
= 0;
719 exporter
->collectors
= NULL
;
720 exporter
->seq_number
= 1;
721 exporter
->last_template_set_time
= 0;
722 exporter
->cache_active_timeout
= 0;
723 exporter
->cache_max_flows
= 0;
724 free(exporter
->virtual_obs_id
);
725 exporter
->virtual_obs_id
= NULL
;
726 exporter
->virtual_obs_len
= 0;
728 memset(&exporter
->ipfix_global_stats
, 0,
729 sizeof(struct dpif_ipfix_global_stats
));
733 dpif_ipfix_exporter_destroy(struct dpif_ipfix_exporter
*exporter
)
735 dpif_ipfix_exporter_clear(exporter
);
736 hmap_destroy(&exporter
->cache_flow_key_map
);
740 dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter
*exporter
,
741 const struct sset
*targets
,
742 const uint32_t cache_active_timeout
,
743 const uint32_t cache_max_flows
,
744 const char *virtual_obs_id
)
746 size_t virtual_obs_len
;
747 collectors_destroy(exporter
->collectors
);
748 collectors_create(targets
, IPFIX_DEFAULT_COLLECTOR_PORT
,
749 &exporter
->collectors
);
750 if (exporter
->collectors
== NULL
) {
751 VLOG_WARN_RL(&rl
, "no collectors could be initialized, "
752 "IPFIX exporter disabled");
753 dpif_ipfix_exporter_clear(exporter
);
756 exporter
->cache_active_timeout
= cache_active_timeout
;
757 exporter
->cache_max_flows
= cache_max_flows
;
758 virtual_obs_len
= virtual_obs_id
? strlen(virtual_obs_id
) : 0;
759 if (virtual_obs_len
> IPFIX_VIRTUAL_OBS_MAX_LEN
) {
760 VLOG_WARN_RL(&rl
, "Virtual obsevation ID too long (%d bytes), "
761 "should not be longer than %d bytes.",
762 exporter
->virtual_obs_len
, IPFIX_VIRTUAL_OBS_MAX_LEN
);
763 dpif_ipfix_exporter_clear(exporter
);
766 exporter
->virtual_obs_len
= virtual_obs_len
;
767 exporter
->virtual_obs_id
= nullable_xstrdup(virtual_obs_id
);
771 static struct dpif_ipfix_port
*
772 dpif_ipfix_find_port(const struct dpif_ipfix
*di
,
773 odp_port_t odp_port
) OVS_REQUIRES(mutex
)
775 struct dpif_ipfix_port
*dip
;
777 HMAP_FOR_EACH_IN_BUCKET (dip
, hmap_node
, hash_odp_port(odp_port
),
779 if (dip
->odp_port
== odp_port
) {
787 dpif_ipfix_del_port__(struct dpif_ipfix
*di
,
788 struct dpif_ipfix_port
*dip
)
791 hmap_remove(&di
->ports
, &dip
->hmap_node
);
795 static enum dpif_ipfix_tunnel_type
796 dpif_ipfix_tunnel_type(const struct ofport
*ofport
)
798 const char *type
= netdev_get_type(ofport
->netdev
);
801 return DPIF_IPFIX_TUNNEL_UNKNOWN
;
803 if (strcmp(type
, "gre") == 0) {
804 return DPIF_IPFIX_TUNNEL_GRE
;
805 } else if (strcmp(type
, "vxlan") == 0) {
806 return DPIF_IPFIX_TUNNEL_VXLAN
;
807 } else if (strcmp(type
, "lisp") == 0) {
808 return DPIF_IPFIX_TUNNEL_LISP
;
809 } else if (strcmp(type
, "geneve") == 0) {
810 return DPIF_IPFIX_TUNNEL_GENEVE
;
811 } else if (strcmp(type
, "stt") == 0) {
812 return DPIF_IPFIX_TUNNEL_STT
;
815 return DPIF_IPFIX_TUNNEL_UNKNOWN
;
819 dpif_ipfix_tunnel_key_length(enum dpif_ipfix_tunnel_type tunnel_type
)
822 switch (tunnel_type
) {
823 case DPIF_IPFIX_TUNNEL_GRE
:
826 case DPIF_IPFIX_TUNNEL_VXLAN
:
827 case DPIF_IPFIX_TUNNEL_LISP
:
828 case DPIF_IPFIX_TUNNEL_GENEVE
:
830 case DPIF_IPFIX_TUNNEL_STT
:
832 case DPIF_IPFIX_TUNNEL_UNKNOWN
:
833 case NUM_DPIF_IPFIX_TUNNEL
:
840 dpif_ipfix_add_port(struct dpif_ipfix
*di
, struct ofport
*ofport
,
841 odp_port_t odp_port
) OVS_EXCLUDED(mutex
)
843 struct dpif_ipfix_port
*dip
;
846 ovs_mutex_lock(&mutex
);
847 dip
= dpif_ipfix_find_port(di
, odp_port
);
849 dpif_ipfix_del_port__(di
, dip
);
852 ifindex
= netdev_get_ifindex(ofport
->netdev
);
857 /* Add to table of ports. */
858 dip
= xmalloc(sizeof *dip
);
859 dip
->ofport
= ofport
;
860 dip
->odp_port
= odp_port
;
861 dip
->tunnel_type
= dpif_ipfix_tunnel_type(ofport
);
862 dip
->tunnel_key_length
= dpif_ipfix_tunnel_key_length(dip
->tunnel_type
);
863 dip
->ifindex
= ifindex
;
864 hmap_insert(&di
->ports
, &dip
->hmap_node
, hash_odp_port(odp_port
));
866 ovs_mutex_unlock(&mutex
);
870 dpif_ipfix_del_port(struct dpif_ipfix
*di
, odp_port_t odp_port
)
873 struct dpif_ipfix_port
*dip
;
874 ovs_mutex_lock(&mutex
);
875 dip
= dpif_ipfix_find_port(di
, odp_port
);
877 dpif_ipfix_del_port__(di
, dip
);
879 ovs_mutex_unlock(&mutex
);
882 static struct dpif_ipfix_port
*
883 dpif_ipfix_find_tunnel_port(const struct dpif_ipfix
*di
, odp_port_t odp_port
)
886 struct dpif_ipfix_port
*dip
= dpif_ipfix_find_port(di
, odp_port
);
887 return (dip
&& dip
->tunnel_type
!= DPIF_IPFIX_TUNNEL_UNKNOWN
) ? dip
: NULL
;
891 dpif_ipfix_is_tunnel_port(const struct dpif_ipfix
*di
, odp_port_t odp_port
)
894 struct dpif_ipfix_port
*dip
;
895 ovs_mutex_lock(&mutex
);
896 dip
= dpif_ipfix_find_tunnel_port(di
, odp_port
);
897 ovs_mutex_unlock(&mutex
);
902 dpif_ipfix_bridge_exporter_init(struct dpif_ipfix_bridge_exporter
*exporter
)
904 dpif_ipfix_exporter_init(&exporter
->exporter
);
905 exporter
->options
= NULL
;
906 exporter
->probability
= 0;
910 dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter
*exporter
)
912 dpif_ipfix_exporter_clear(&exporter
->exporter
);
913 ofproto_ipfix_bridge_exporter_options_destroy(exporter
->options
);
914 exporter
->options
= NULL
;
915 exporter
->probability
= 0;
919 dpif_ipfix_bridge_exporter_destroy(struct dpif_ipfix_bridge_exporter
*exporter
)
921 dpif_ipfix_bridge_exporter_clear(exporter
);
922 dpif_ipfix_exporter_destroy(&exporter
->exporter
);
926 dpif_ipfix_bridge_exporter_set_options(
927 struct dpif_ipfix_bridge_exporter
*exporter
,
928 const struct ofproto_ipfix_bridge_exporter_options
*options
)
930 bool options_changed
;
932 if (!options
|| sset_is_empty(&options
->targets
)) {
933 /* No point in doing any work if there are no targets. */
934 dpif_ipfix_bridge_exporter_clear(exporter
);
940 || !ofproto_ipfix_bridge_exporter_options_equal(
941 options
, exporter
->options
));
943 /* Configure collectors if options have changed or if we're
944 * shortchanged in collectors (which indicates that opening one or
945 * more of the configured collectors failed, so that we should
948 || collectors_count(exporter
->exporter
.collectors
)
949 < sset_count(&options
->targets
)) {
950 if (!dpif_ipfix_exporter_set_options(
951 &exporter
->exporter
, &options
->targets
,
952 options
->cache_active_timeout
, options
->cache_max_flows
,
953 options
->virtual_obs_id
)) {
958 /* Avoid reconfiguring if options didn't change. */
959 if (!options_changed
) {
963 ofproto_ipfix_bridge_exporter_options_destroy(exporter
->options
);
964 exporter
->options
= ofproto_ipfix_bridge_exporter_options_clone(options
);
965 exporter
->probability
=
966 MAX(1, UINT32_MAX
/ exporter
->options
->sampling_rate
);
968 /* Run over the cache as some entries might have expired after
969 * changing the timeouts. */
970 dpif_ipfix_cache_expire_now(&exporter
->exporter
, false);
973 static struct dpif_ipfix_flow_exporter_map_node
*
974 dpif_ipfix_find_flow_exporter_map_node(
975 const struct dpif_ipfix
*di
, const uint32_t collector_set_id
)
978 struct dpif_ipfix_flow_exporter_map_node
*exporter_node
;
980 HMAP_FOR_EACH_WITH_HASH (exporter_node
, node
,
981 hash_int(collector_set_id
, 0),
982 &di
->flow_exporter_map
) {
983 if (exporter_node
->exporter
.options
->collector_set_id
984 == collector_set_id
) {
985 return exporter_node
;
993 dpif_ipfix_flow_exporter_init(struct dpif_ipfix_flow_exporter
*exporter
)
995 dpif_ipfix_exporter_init(&exporter
->exporter
);
996 exporter
->options
= NULL
;
1000 dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter
*exporter
)
1002 dpif_ipfix_exporter_clear(&exporter
->exporter
);
1003 ofproto_ipfix_flow_exporter_options_destroy(exporter
->options
);
1004 exporter
->options
= NULL
;
1008 dpif_ipfix_flow_exporter_destroy(struct dpif_ipfix_flow_exporter
*exporter
)
1010 dpif_ipfix_flow_exporter_clear(exporter
);
1011 dpif_ipfix_exporter_destroy(&exporter
->exporter
);
1015 dpif_ipfix_flow_exporter_set_options(
1016 struct dpif_ipfix_flow_exporter
*exporter
,
1017 const struct ofproto_ipfix_flow_exporter_options
*options
)
1019 bool options_changed
;
1021 if (sset_is_empty(&options
->targets
)) {
1022 /* No point in doing any work if there are no targets. */
1023 dpif_ipfix_flow_exporter_clear(exporter
);
1029 || !ofproto_ipfix_flow_exporter_options_equal(
1030 options
, exporter
->options
));
1032 /* Configure collectors if options have changed or if we're
1033 * shortchanged in collectors (which indicates that opening one or
1034 * more of the configured collectors failed, so that we should
1037 || collectors_count(exporter
->exporter
.collectors
)
1038 < sset_count(&options
->targets
)) {
1039 if (!dpif_ipfix_exporter_set_options(
1040 &exporter
->exporter
, &options
->targets
,
1041 options
->cache_active_timeout
, options
->cache_max_flows
,
1042 options
->virtual_obs_id
)) {
1047 /* Avoid reconfiguring if options didn't change. */
1048 if (!options_changed
) {
1052 ofproto_ipfix_flow_exporter_options_destroy(exporter
->options
);
1053 exporter
->options
= ofproto_ipfix_flow_exporter_options_clone(options
);
1055 /* Run over the cache as some entries might have expired after
1056 * changing the timeouts. */
1057 dpif_ipfix_cache_expire_now(&exporter
->exporter
, false);
1063 remove_flow_exporter(struct dpif_ipfix
*di
,
1064 struct dpif_ipfix_flow_exporter_map_node
*node
)
1066 hmap_remove(&di
->flow_exporter_map
, &node
->node
);
1067 dpif_ipfix_flow_exporter_destroy(&node
->exporter
);
1072 dpif_ipfix_set_options(
1073 struct dpif_ipfix
*di
,
1074 const struct ofproto_ipfix_bridge_exporter_options
*bridge_exporter_options
,
1075 const struct ofproto_ipfix_flow_exporter_options
*flow_exporters_options
,
1076 size_t n_flow_exporters_options
) OVS_EXCLUDED(mutex
)
1079 struct ofproto_ipfix_flow_exporter_options
*options
;
1080 struct dpif_ipfix_flow_exporter_map_node
*node
, *next
;
1082 ovs_mutex_lock(&mutex
);
1083 dpif_ipfix_bridge_exporter_set_options(&di
->bridge_exporter
,
1084 bridge_exporter_options
);
1086 /* Add new flow exporters and update current flow exporters. */
1087 options
= (struct ofproto_ipfix_flow_exporter_options
*)
1088 flow_exporters_options
;
1089 for (i
= 0; i
< n_flow_exporters_options
; i
++) {
1090 node
= dpif_ipfix_find_flow_exporter_map_node(
1091 di
, options
->collector_set_id
);
1093 node
= xzalloc(sizeof *node
);
1094 dpif_ipfix_flow_exporter_init(&node
->exporter
);
1095 hmap_insert(&di
->flow_exporter_map
, &node
->node
,
1096 hash_int(options
->collector_set_id
, 0));
1098 if (!dpif_ipfix_flow_exporter_set_options(&node
->exporter
, options
)) {
1099 remove_flow_exporter(di
, node
);
1104 /* Remove dropped flow exporters, if any needs to be removed. */
1105 HMAP_FOR_EACH_SAFE (node
, next
, node
, &di
->flow_exporter_map
) {
1106 /* This is slow but doesn't take any extra memory, and
1107 * this table is not supposed to contain many rows anyway. */
1108 options
= (struct ofproto_ipfix_flow_exporter_options
*)
1109 flow_exporters_options
;
1110 for (i
= 0; i
< n_flow_exporters_options
; i
++) {
1111 if (node
->exporter
.options
->collector_set_id
1112 == options
->collector_set_id
) {
1117 if (i
== n_flow_exporters_options
) { // Not found.
1118 remove_flow_exporter(di
, node
);
1122 ovs_mutex_unlock(&mutex
);
1126 dpif_ipfix_create(void)
1128 struct dpif_ipfix
*di
;
1129 di
= xzalloc(sizeof *di
);
1130 dpif_ipfix_bridge_exporter_init(&di
->bridge_exporter
);
1131 hmap_init(&di
->flow_exporter_map
);
1132 hmap_init(&di
->ports
);
1133 ovs_refcount_init(&di
->ref_cnt
);
1138 dpif_ipfix_ref(const struct dpif_ipfix
*di_
)
1140 struct dpif_ipfix
*di
= CONST_CAST(struct dpif_ipfix
*, di_
);
1142 ovs_refcount_ref(&di
->ref_cnt
);
1148 dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix
*di
)
1152 ovs_mutex_lock(&mutex
);
1153 ret
= di
->bridge_exporter
.probability
;
1154 ovs_mutex_unlock(&mutex
);
1159 dpif_ipfix_get_bridge_exporter_input_sampling(const struct dpif_ipfix
*di
)
1163 ovs_mutex_lock(&mutex
);
1164 if (di
->bridge_exporter
.options
) {
1165 ret
= di
->bridge_exporter
.options
->enable_input_sampling
;
1167 ovs_mutex_unlock(&mutex
);
1172 dpif_ipfix_get_bridge_exporter_output_sampling(const struct dpif_ipfix
*di
)
1176 ovs_mutex_lock(&mutex
);
1177 if (di
->bridge_exporter
.options
) {
1178 ret
= di
->bridge_exporter
.options
->enable_output_sampling
;
1180 ovs_mutex_unlock(&mutex
);
1185 dpif_ipfix_get_bridge_exporter_tunnel_sampling(const struct dpif_ipfix
*di
)
1189 ovs_mutex_lock(&mutex
);
1190 if (di
->bridge_exporter
.options
) {
1191 ret
= di
->bridge_exporter
.options
->enable_tunnel_sampling
;
1193 ovs_mutex_unlock(&mutex
);
1198 dpif_ipfix_get_flow_exporter_tunnel_sampling(const struct dpif_ipfix
*di
,
1199 const uint32_t collector_set_id
)
1202 ovs_mutex_lock(&mutex
);
1203 struct dpif_ipfix_flow_exporter_map_node
*node
1204 = dpif_ipfix_find_flow_exporter_map_node(di
, collector_set_id
);
1206 && node
->exporter
.options
1207 && node
->exporter
.options
->enable_tunnel_sampling
);
1208 ovs_mutex_unlock(&mutex
);
1214 dpif_ipfix_clear(struct dpif_ipfix
*di
) OVS_REQUIRES(mutex
)
1216 struct dpif_ipfix_flow_exporter_map_node
*exp_node
;
1217 struct dpif_ipfix_port
*dip
, *next
;
1219 dpif_ipfix_bridge_exporter_clear(&di
->bridge_exporter
);
1221 HMAP_FOR_EACH_POP (exp_node
, node
, &di
->flow_exporter_map
) {
1222 dpif_ipfix_flow_exporter_destroy(&exp_node
->exporter
);
1226 HMAP_FOR_EACH_SAFE (dip
, next
, hmap_node
, &di
->ports
) {
1227 dpif_ipfix_del_port__(di
, dip
);
1232 dpif_ipfix_unref(struct dpif_ipfix
*di
) OVS_EXCLUDED(mutex
)
1234 if (di
&& ovs_refcount_unref_relaxed(&di
->ref_cnt
) == 1) {
1235 ovs_mutex_lock(&mutex
);
1236 dpif_ipfix_clear(di
);
1237 dpif_ipfix_bridge_exporter_destroy(&di
->bridge_exporter
);
1238 hmap_destroy(&di
->flow_exporter_map
);
1239 hmap_destroy(&di
->ports
);
1241 ovs_mutex_unlock(&mutex
);
1246 ipfix_init_header(uint32_t export_time_sec
, uint32_t seq_number
,
1247 uint32_t obs_domain_id
, struct dp_packet
*msg
)
1249 struct ipfix_header
*hdr
;
1251 hdr
= dp_packet_put_zeros(msg
, sizeof *hdr
);
1252 hdr
->version
= htons(IPFIX_VERSION
);
1253 hdr
->length
= htons(sizeof *hdr
); /* Updated in ipfix_send_msg. */
1254 hdr
->export_time
= htonl(export_time_sec
);
1255 hdr
->seq_number
= htonl(seq_number
);
1256 hdr
->obs_domain_id
= htonl(obs_domain_id
);
1260 ipfix_send_msg(const struct collectors
*collectors
, struct dp_packet
*msg
)
1262 struct ipfix_header
*hdr
;
1265 /* Adjust the length in the header. */
1266 hdr
= dp_packet_data(msg
);
1267 hdr
->length
= htons(dp_packet_size(msg
));
1269 tx_errors
= collectors_send(collectors
,
1270 dp_packet_data(msg
), dp_packet_size(msg
));
1271 dp_packet_set_size(msg
, 0);
1277 ipfix_get_template_id(enum ipfix_proto_l2 l2
, enum ipfix_proto_l3 l3
,
1278 enum ipfix_proto_l4 l4
, enum ipfix_proto_tunnel tunnel
,
1279 enum ipfix_flow_direction flow_direction
)
1281 uint16_t template_id
;
1283 template_id
= template_id
* NUM_IPFIX_PROTO_L3
+ l3
;
1284 template_id
= template_id
* NUM_IPFIX_PROTO_L4
+ l4
;
1285 template_id
= template_id
* NUM_IPFIX_PROTO_TUNNEL
+ tunnel
;
1286 template_id
= template_id
* NUM_IPFIX_FLOW_DIRECTION
+ flow_direction
;
1287 return IPFIX_TEMPLATE_ID_MIN
+ template_id
;
1291 ipfix_get_options_template_id(enum ipfix_options_template opt_tmpl_type
)
1293 /* Check what is the maximum possible Template ID for Template Record and
1294 * use it as a base number for Template ID in Options Template Record. */
1295 uint16_t max_tmpl_id
= ipfix_get_template_id(NUM_IPFIX_PROTO_L2
,
1298 NUM_IPFIX_PROTO_TUNNEL
,
1299 NUM_IPFIX_FLOW_DIRECTION
);
1301 return max_tmpl_id
+ opt_tmpl_type
;
1306 ipfix_define_template_entity(enum ipfix_entity_id id
,
1307 enum ipfix_entity_size size
,
1308 enum ipfix_entity_enterprise enterprise
,
1309 struct dp_packet
*msg
)
1311 struct ipfix_template_field_specifier
*field
;
1315 field_size
= sizeof *field
;
1317 /* No enterprise number */
1318 field_size
= sizeof *field
- sizeof(ovs_be32
);
1320 field
= dp_packet_put_zeros(msg
, field_size
);
1321 field
->element_id
= htons(id
);
1323 field
->field_length
= htons(size
);
1325 /* RFC 5101, Section 7. Variable-Length Information Element */
1326 field
->field_length
= OVS_BE16_MAX
;
1329 field
->enterprise
= htonl(enterprise
);
1336 ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
1337 IPFIX_ENTITY_SIZE_##ID, \
1338 IPFIX_ENTITY_ENTERPRISE_##ID, msg); \
1342 /* Defines The Exporting Process Reliability Statistics Options Template
1343 * fields, including scope fields. Updates 'scope_field_count' and
1344 * 'field_count' in Options Template Record Header. */
1346 ipfix_def_exporter_options_template_fields(size_t opt_tmpl_hdr_offset
,
1347 struct dp_packet
*msg
)
1350 struct ipfix_options_template_record_header
*opt_tmpl_hdr
;
1352 /* 1. Scope Fields Specifiers */
1353 DEF(EXPORTING_PROCESS_ID
);
1355 /* Update 'scope_field_count' in options template header. */
1356 opt_tmpl_hdr
= (struct ipfix_options_template_record_header
*)
1357 ((uint8_t *)dp_packet_data(msg
) + opt_tmpl_hdr_offset
);
1358 opt_tmpl_hdr
->scope_field_count
= htons(count
);
1360 /* 2. Fields Specifiers */
1361 DEF(NOT_SENT_PACKET_TOTAL_COUNT
);
1363 /* Update 'field_count' in options template header. */
1364 opt_tmpl_hdr
= (struct ipfix_options_template_record_header
*)
1365 ((uint8_t *)dp_packet_data(msg
) + opt_tmpl_hdr_offset
);
1366 opt_tmpl_hdr
->field_count
= htons(count
);
1372 ipfix_def_options_template_fields(enum ipfix_options_template opt_tmpl_type
,
1373 size_t opt_tmpl_hdr_offset
,
1374 struct dp_packet
*msg
)
1376 switch (opt_tmpl_type
) {
1377 case IPFIX_OPTIONS_TEMPLATE_EXPORTER_STATS
:
1378 return ipfix_def_exporter_options_template_fields(opt_tmpl_hdr_offset
,
1381 case NUM_IPFIX_OPTIONS_TEMPLATE
:
1389 /* Defines fields in Template Record. Updates 'field_count' in Template Record
1392 ipfix_define_template_fields(enum ipfix_proto_l2 l2
, enum ipfix_proto_l3 l3
,
1393 enum ipfix_proto_l4 l4
, enum ipfix_proto_tunnel tunnel
,
1394 enum ipfix_flow_direction flow_direction
,
1395 bool virtual_obs_id_set
,
1396 size_t tmpl_hdr_offset
,
1397 struct dp_packet
*msg
)
1400 struct ipfix_template_record_header
*tmpl_hdr
;
1405 DEF(OBSERVATION_POINT_ID
);
1406 DEF(FLOW_DIRECTION
);
1408 /* Common Ethernet entities. */
1409 DEF(SOURCE_MAC_ADDRESS
);
1410 DEF(DESTINATION_MAC_ADDRESS
);
1412 DEF(ETHERNET_HEADER_LENGTH
);
1414 /* Interface Information Elements */
1415 DEF(INGRESS_INTERFACE
);
1416 DEF(INGRESS_INTERFACE_TYPE
);
1417 DEF(INTERFACE_NAME
);
1418 DEF(INTERFACE_DESCRIPTION
);
1420 if (flow_direction
== EGRESS_FLOW
) {
1421 DEF(EGRESS_INTERFACE
);
1422 DEF(EGRESS_INTERFACE_TYPE
);
1423 DEF(INTERFACE_NAME
);
1424 DEF(INTERFACE_DESCRIPTION
);
1427 if (l2
== IPFIX_PROTO_L2_VLAN
) {
1430 DEF(DOT1Q_PRIORITY
);
1433 if (l3
!= IPFIX_PROTO_L3_UNKNOWN
) {
1436 DEF(PROTOCOL_IDENTIFIER
);
1437 DEF(IP_DIFF_SERV_CODE_POINT
);
1439 DEF(IP_CLASS_OF_SERVICE
);
1441 if (l3
== IPFIX_PROTO_L3_IPV4
) {
1442 DEF(SOURCE_IPV4_ADDRESS
);
1443 DEF(DESTINATION_IPV4_ADDRESS
);
1444 if (l4
== IPFIX_PROTO_L4_TCP
1445 || l4
== IPFIX_PROTO_L4_UDP
1446 || l4
== IPFIX_PROTO_L4_SCTP
) {
1447 DEF(SOURCE_TRANSPORT_PORT
);
1448 DEF(DESTINATION_TRANSPORT_PORT
);
1449 } else if (l4
== IPFIX_PROTO_L4_ICMP
) {
1450 DEF(ICMP_TYPE_IPV4
);
1451 DEF(ICMP_CODE_IPV4
);
1453 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
1454 DEF(SOURCE_IPV6_ADDRESS
);
1455 DEF(DESTINATION_IPV6_ADDRESS
);
1456 DEF(FLOW_LABEL_IPV6
);
1457 if (l4
== IPFIX_PROTO_L4_TCP
1458 || l4
== IPFIX_PROTO_L4_UDP
1459 || l4
== IPFIX_PROTO_L4_SCTP
) {
1460 DEF(SOURCE_TRANSPORT_PORT
);
1461 DEF(DESTINATION_TRANSPORT_PORT
);
1462 } else if (l4
== IPFIX_PROTO_L4_ICMP
) {
1463 DEF(ICMP_TYPE_IPV6
);
1464 DEF(ICMP_CODE_IPV6
);
1469 if (tunnel
!= IPFIX_PROTO_NOT_TUNNELED
) {
1470 DEF(TUNNEL_SOURCE_IPV4_ADDRESS
);
1471 DEF(TUNNEL_DESTINATION_IPV4_ADDRESS
);
1472 DEF(TUNNEL_PROTOCOL_IDENTIFIER
);
1473 DEF(TUNNEL_SOURCE_TRANSPORT_PORT
);
1474 DEF(TUNNEL_DESTINATION_TRANSPORT_PORT
);
1479 /* 2. Virtual observation ID, which is not a part of flow key. */
1480 if (virtual_obs_id_set
) {
1481 DEF(VIRTUAL_OBS_ID
);
1484 /* 3. Flow aggregated data. */
1486 DEF(FLOW_START_DELTA_MICROSECONDS
);
1487 DEF(FLOW_END_DELTA_MICROSECONDS
);
1488 DEF(DROPPED_PACKET_DELTA_COUNT
);
1489 DEF(DROPPED_PACKET_TOTAL_COUNT
);
1490 DEF(PACKET_DELTA_COUNT
);
1491 DEF(PACKET_TOTAL_COUNT
);
1492 DEF(INGRESS_UNICAST_PACKET_TOTAL_COUNT
);
1493 DEF(INGRESS_MULTICAST_PACKET_TOTAL_COUNT
);
1494 DEF(INGRESS_BROADCAST_PACKET_TOTAL_COUNT
);
1495 DEF(EGRESS_UNICAST_PACKET_TOTAL_COUNT
);
1496 DEF(EGRESS_BROADCAST_PACKET_TOTAL_COUNT
);
1497 DEF(POST_MCAST_PACKET_DELTA_COUNT
);
1498 DEF(POST_MCAST_PACKET_TOTAL_COUNT
);
1499 DEF(LAYER2_OCTET_DELTA_COUNT
);
1500 DEF(LAYER2_OCTET_TOTAL_COUNT
);
1501 DEF(FLOW_END_REASON
);
1503 if (l3
!= IPFIX_PROTO_L3_UNKNOWN
) {
1504 DEF(DROPPED_OCTET_DELTA_COUNT
);
1505 DEF(DROPPED_OCTET_TOTAL_COUNT
);
1506 DEF(OCTET_DELTA_COUNT
);
1507 DEF(OCTET_TOTAL_COUNT
);
1508 DEF(OCTET_DELTA_SUM_OF_SQUARES
);
1509 DEF(OCTET_TOTAL_SUM_OF_SQUARES
);
1510 DEF(MINIMUM_IP_TOTAL_LENGTH
);
1511 DEF(MAXIMUM_IP_TOTAL_LENGTH
);
1512 DEF(POST_MCAST_OCTET_DELTA_COUNT
);
1513 DEF(POST_MCAST_OCTET_TOTAL_COUNT
);
1516 if (l4
== IPFIX_PROTO_L4_TCP
) {
1517 DEF(TCP_ACK_TOTAL_COUNT
);
1518 DEF(TCP_FIN_TOTAL_COUNT
);
1519 DEF(TCP_PSH_TOTAL_COUNT
);
1520 DEF(TCP_RST_TOTAL_COUNT
);
1521 DEF(TCP_SYN_TOTAL_COUNT
);
1522 DEF(TCP_URG_TOTAL_COUNT
);
1525 /* Update 'field_count' in template header. */
1526 tmpl_hdr
= (struct ipfix_template_record_header
*)
1527 ((uint8_t *)dp_packet_data(msg
) + tmpl_hdr_offset
);
1528 tmpl_hdr
->field_count
= htons(count
);
1536 ipfix_init_template_msg(uint32_t export_time_sec
,
1537 uint32_t seq_number
, uint32_t obs_domain_id
,
1538 uint16_t set_id
, struct dp_packet
*msg
,
1539 size_t *set_hdr_offset
)
1541 struct ipfix_set_header
*set_hdr
;
1543 dp_packet_clear(msg
);
1545 ipfix_init_header(export_time_sec
, seq_number
, obs_domain_id
, msg
);
1546 *set_hdr_offset
= dp_packet_size(msg
);
1548 /* Add a Set Header. */
1549 set_hdr
= dp_packet_put_zeros(msg
, sizeof *set_hdr
);
1550 set_hdr
->set_id
= htons(set_id
);
1554 ipfix_send_template_msg(const struct collectors
*collectors
,
1555 struct dp_packet
*msg
, size_t set_hdr_offset
)
1557 struct ipfix_set_header
*set_hdr
;
1560 /* Send template message. */
1561 set_hdr
= (struct ipfix_set_header
*)
1562 ((uint8_t*)dp_packet_data(msg
) + set_hdr_offset
);
1563 set_hdr
->length
= htons(dp_packet_size(msg
) - set_hdr_offset
);
1565 tx_errors
= ipfix_send_msg(collectors
, msg
);
1571 ipfix_add_options_template_record(enum ipfix_options_template opt_tmpl_type
,
1572 struct dp_packet
*msg
)
1574 struct ipfix_options_template_record_header
*opt_tmpl_hdr
;
1575 size_t opt_tmpl_hdr_offset
;
1577 opt_tmpl_hdr_offset
= dp_packet_size(msg
);
1578 opt_tmpl_hdr
= dp_packet_put_zeros(msg
, sizeof *opt_tmpl_hdr
);
1579 opt_tmpl_hdr
->template_id
=
1580 htons(ipfix_get_options_template_id(opt_tmpl_type
));
1581 ipfix_def_options_template_fields(opt_tmpl_type
, opt_tmpl_hdr_offset
, msg
);
1585 ipfix_send_options_template_msgs(struct dpif_ipfix_exporter
*exporter
,
1586 uint32_t export_time_sec
,
1587 uint32_t obs_domain_id
,
1588 struct dp_packet
*msg
)
1590 size_t set_hdr_offset
;
1591 size_t tx_packets
= 0;
1592 size_t tx_errors
= 0, error_pkts
;
1593 enum ipfix_options_template opt_tmpl_type
;
1595 ipfix_init_template_msg(export_time_sec
, exporter
->seq_number
,
1596 obs_domain_id
, IPFIX_SET_ID_OPTION_TEMPLATE
, msg
,
1599 for (opt_tmpl_type
= 0; opt_tmpl_type
< NUM_IPFIX_OPTIONS_TEMPLATE
;
1601 if (dp_packet_size(msg
) >= MAX_MESSAGE_LEN
) {
1602 /* Send template message. */
1603 error_pkts
= ipfix_send_template_msg(exporter
->collectors
, msg
,
1605 tx_errors
+= error_pkts
;
1606 tx_packets
+= collectors_count(exporter
->collectors
) - error_pkts
;
1608 /* Reinitialize the template msg. */
1609 ipfix_init_template_msg(export_time_sec
, exporter
->seq_number
,
1611 IPFIX_SET_ID_OPTION_TEMPLATE
,
1616 ipfix_add_options_template_record(opt_tmpl_type
, msg
);
1619 error_pkts
= ipfix_send_template_msg(exporter
->collectors
, msg
,
1621 tx_errors
+= error_pkts
;
1622 tx_packets
+= collectors_count(exporter
->collectors
) - error_pkts
;
1623 exporter
->ofproto_stats
.tx_pkts
+= tx_packets
;
1624 exporter
->ofproto_stats
.tx_errors
+= tx_errors
;
1628 ipfix_add_template_record(enum ipfix_proto_l2 l2
, enum ipfix_proto_l3 l3
,
1629 enum ipfix_proto_l4 l4
,
1630 enum ipfix_proto_tunnel tunnel
,
1631 enum ipfix_flow_direction flow_direction
,
1632 bool virtual_obs_id_set
,
1633 struct dp_packet
*msg
)
1635 struct ipfix_template_record_header
*tmpl_hdr
;
1636 size_t tmpl_hdr_offset
= dp_packet_size(msg
);
1638 tmpl_hdr
= dp_packet_put_zeros(msg
, sizeof *tmpl_hdr
);
1639 tmpl_hdr
->template_id
=
1640 htons(ipfix_get_template_id(l2
, l3
, l4
, tunnel
, flow_direction
));
1641 ipfix_define_template_fields(l2
, l3
, l4
, tunnel
, flow_direction
,
1642 virtual_obs_id_set
, tmpl_hdr_offset
, msg
);
1646 ipfix_send_template_msgs(struct dpif_ipfix_exporter
*exporter
,
1647 uint32_t export_time_sec
, uint32_t obs_domain_id
)
1649 uint64_t msg_stub
[DIV_ROUND_UP(MAX_MESSAGE_LEN
, 8)];
1650 struct dp_packet msg
;
1651 dp_packet_use_stub(&msg
, msg_stub
, sizeof msg_stub
);
1653 size_t set_hdr_offset
, error_pkts
;
1654 size_t tx_packets
= 0;
1655 size_t tx_errors
= 0;
1656 enum ipfix_proto_l2 l2
;
1657 enum ipfix_proto_l3 l3
;
1658 enum ipfix_proto_l4 l4
;
1659 enum ipfix_proto_tunnel tunnel
;
1660 enum ipfix_flow_direction flow_direction
;
1662 ipfix_init_template_msg(export_time_sec
, exporter
->seq_number
,
1663 obs_domain_id
, IPFIX_SET_ID_TEMPLATE
, &msg
,
1665 /* Define one template for each possible combination of
1667 for (l2
= 0; l2
< NUM_IPFIX_PROTO_L2
; l2
++) {
1668 for (l3
= 0; l3
< NUM_IPFIX_PROTO_L3
; l3
++) {
1669 for (l4
= 0; l4
< NUM_IPFIX_PROTO_L4
; l4
++) {
1670 if (l3
== IPFIX_PROTO_L3_UNKNOWN
&&
1671 l4
!= IPFIX_PROTO_L4_UNKNOWN
) {
1674 for (tunnel
= 0; tunnel
< NUM_IPFIX_PROTO_TUNNEL
; tunnel
++) {
1675 for (flow_direction
= 0;
1676 flow_direction
< NUM_IPFIX_FLOW_DIRECTION
;
1678 /* When the size of the template packet reaches
1679 * MAX_MESSAGE_LEN(1024), send it out.
1680 * And then reinitialize the msg to construct a new
1681 * packet for the following templates.
1683 if (dp_packet_size(&msg
) >= MAX_MESSAGE_LEN
) {
1684 /* Send template message. */
1686 ipfix_send_template_msg(exporter
->collectors
,
1687 &msg
, set_hdr_offset
);
1688 tx_errors
+= error_pkts
;
1690 collectors_count(exporter
->collectors
)
1693 /* Reinitialize the template msg. */
1694 ipfix_init_template_msg(export_time_sec
,
1695 exporter
->seq_number
,
1697 IPFIX_SET_ID_TEMPLATE
,
1698 &msg
, &set_hdr_offset
);
1701 ipfix_add_template_record(l2
, l3
, l4
, tunnel
,
1703 exporter
->virtual_obs_id
!= NULL
, &msg
);
1710 /* Send template message. */
1711 error_pkts
= ipfix_send_template_msg(exporter
->collectors
, &msg
,
1713 tx_errors
+= error_pkts
;
1714 tx_packets
+= collectors_count(exporter
->collectors
) - error_pkts
;
1716 exporter
->ofproto_stats
.tx_pkts
+= tx_packets
;
1717 exporter
->ofproto_stats
.tx_errors
+= tx_errors
;
1719 /* XXX: Add Options Template Sets, at least to define a Flow Keys
1720 * Option Template. */
1722 /* At the moment only a single Options Template Set is used, which contains
1723 * Exporting Process Statistics. It means that there is no specific
1724 * Observation Domain ID relevant for the entire IPFIX message and it
1725 * should be set to 0. */
1726 ipfix_send_options_template_msgs(exporter
, export_time_sec
, 0U, &msg
);
1728 dp_packet_uninit(&msg
);
1731 static inline uint32_t
1732 ipfix_hash_flow_key(const struct ipfix_flow_key
*flow_key
, uint32_t basis
)
1735 hash
= hash_int(flow_key
->obs_domain_id
, basis
);
1736 hash
= hash_int(flow_key
->template_id
, hash
);
1737 hash
= hash_bytes(flow_key
->flow_key_msg_part
,
1738 flow_key
->flow_key_msg_part_size
, hash
);
1743 ipfix_flow_key_equal(const struct ipfix_flow_key
*a
,
1744 const struct ipfix_flow_key
*b
)
1746 /* The template ID determines the flow key size, so not need to
1748 return (a
->obs_domain_id
== b
->obs_domain_id
1749 && a
->template_id
== b
->template_id
1750 && memcmp(a
->flow_key_msg_part
, b
->flow_key_msg_part
,
1751 a
->flow_key_msg_part_size
) == 0);
1754 static struct ipfix_flow_cache_entry
*
1755 ipfix_cache_find_entry(const struct dpif_ipfix_exporter
*exporter
,
1756 const struct ipfix_flow_key
*flow_key
)
1758 struct ipfix_flow_cache_entry
*entry
;
1760 HMAP_FOR_EACH_WITH_HASH (entry
, flow_key_map_node
,
1761 ipfix_hash_flow_key(flow_key
, 0),
1762 &exporter
->cache_flow_key_map
) {
1763 if (ipfix_flow_key_equal(&entry
->flow_key
, flow_key
)) {
1772 ipfix_cache_next_timeout_msec(const struct dpif_ipfix_exporter
*exporter
,
1773 long long int *next_timeout_msec
)
1775 struct ipfix_flow_cache_entry
*entry
;
1777 LIST_FOR_EACH (entry
, cache_flow_start_timestamp_list_node
,
1778 &exporter
->cache_flow_start_timestamp_list
) {
1779 *next_timeout_msec
= entry
->flow_start_timestamp_usec
/ 1000LL
1780 + 1000LL * exporter
->cache_active_timeout
;
1788 ipfix_cache_aggregate_entries(struct ipfix_flow_cache_entry
*from_entry
,
1789 struct ipfix_flow_cache_entry
*to_entry
)
1791 uint64_t *to_start
, *to_end
, *from_start
, *from_end
;
1792 uint16_t *to_min_len
, *to_max_len
, *from_min_len
, *from_max_len
;
1794 to_start
= &to_entry
->flow_start_timestamp_usec
;
1795 to_end
= &to_entry
->flow_end_timestamp_usec
;
1796 from_start
= &from_entry
->flow_start_timestamp_usec
;
1797 from_end
= &from_entry
->flow_end_timestamp_usec
;
1799 if (*to_start
> *from_start
) {
1800 *to_start
= *from_start
;
1802 if (*to_end
< *from_end
) {
1803 *to_end
= *from_end
;
1807 to_entry
->dropped_packet_delta_count
+=
1808 from_entry
->dropped_packet_delta_count
;
1809 to_entry
->packet_delta_count
+= from_entry
->packet_delta_count
;
1810 to_entry
->layer2_octet_delta_count
+= from_entry
->layer2_octet_delta_count
;
1812 to_entry
->dropped_packet_total_count
=
1813 from_entry
->dropped_packet_total_count
;
1814 to_entry
->packet_total_count
= from_entry
->packet_total_count
;
1815 to_entry
->in_ucast_packet_total_count
=
1816 from_entry
->in_ucast_packet_total_count
;
1817 to_entry
->in_mcast_packet_total_count
=
1818 from_entry
->in_mcast_packet_total_count
;
1819 to_entry
->in_bcast_packet_total_count
=
1820 from_entry
->in_bcast_packet_total_count
;
1821 to_entry
->out_ucast_packet_total_count
=
1822 from_entry
->out_ucast_packet_total_count
;
1823 to_entry
->out_bcast_packet_total_count
=
1824 from_entry
->out_bcast_packet_total_count
;
1825 to_entry
->layer2_octet_total_count
= from_entry
->layer2_octet_total_count
;
1826 to_entry
->post_mcast_packet_delta_count
+=
1827 from_entry
->post_mcast_packet_delta_count
;
1828 to_entry
->post_mcast_octet_delta_count
+=
1829 from_entry
->post_mcast_octet_delta_count
;
1831 to_entry
->dropped_octet_delta_count
+=
1832 from_entry
->dropped_octet_delta_count
;
1833 to_entry
->octet_delta_count
+= from_entry
->octet_delta_count
;
1834 to_entry
->octet_delta_sum_of_squares
+=
1835 from_entry
->octet_delta_sum_of_squares
;
1837 to_entry
->dropped_octet_total_count
=
1838 from_entry
->dropped_octet_total_count
;
1839 to_entry
->octet_total_count
= from_entry
->octet_total_count
;
1840 to_entry
->octet_total_sum_of_squares
=
1841 from_entry
->octet_total_sum_of_squares
;
1843 to_entry
->post_mcast_packet_total_count
=
1844 from_entry
->post_mcast_packet_total_count
;
1845 to_entry
->post_mcast_octet_total_count
=
1846 from_entry
->post_mcast_octet_total_count
;
1848 to_min_len
= &to_entry
->minimum_ip_total_length
;
1849 to_max_len
= &to_entry
->maximum_ip_total_length
;
1850 from_min_len
= &from_entry
->minimum_ip_total_length
;
1851 from_max_len
= &from_entry
->maximum_ip_total_length
;
1853 if (!*to_min_len
|| (*from_min_len
&& *to_min_len
> *from_min_len
)) {
1854 *to_min_len
= *from_min_len
;
1856 if (*to_max_len
< *from_max_len
) {
1857 *to_max_len
= *from_max_len
;
1860 to_entry
->tcp_packet_delta_count
+= from_entry
->tcp_packet_delta_count
;
1861 to_entry
->tcp_ack_total_count
= from_entry
->tcp_ack_total_count
;
1862 to_entry
->tcp_fin_total_count
= from_entry
->tcp_fin_total_count
;
1863 to_entry
->tcp_psh_total_count
= from_entry
->tcp_psh_total_count
;
1864 to_entry
->tcp_rst_total_count
= from_entry
->tcp_rst_total_count
;
1865 to_entry
->tcp_syn_total_count
= from_entry
->tcp_syn_total_count
;
1866 to_entry
->tcp_urg_total_count
= from_entry
->tcp_urg_total_count
;
1869 /* Get statistics */
1871 ipfix_get_stats__(const struct dpif_ipfix_exporter
*exporter
,
1872 ofproto_ipfix_stats
*stats
)
1874 memset(stats
, 0xff, sizeof *stats
);
1880 *stats
= exporter
->ofproto_stats
;
1884 ipfix_get_bridge_stats(const struct dpif_ipfix_bridge_exporter
*exporter
,
1885 ofproto_ipfix_stats
*stats
)
1887 ipfix_get_stats__(&exporter
->exporter
, stats
);
1891 ipfix_get_flow_stats(const struct dpif_ipfix_flow_exporter
*exporter
,
1892 ofproto_ipfix_stats
*stats
)
1894 ipfix_get_stats__(&exporter
->exporter
, stats
);
1895 stats
->collector_set_id
= exporter
->options
->collector_set_id
;
1899 dpif_ipfix_get_stats(const struct dpif_ipfix
*di
,
1901 struct ovs_list
*replies
)
1904 struct dpif_ipfix_flow_exporter_map_node
*flow_exporter_node
;
1905 struct ofputil_ipfix_stats ois
;
1907 ovs_mutex_lock(&mutex
);
1909 if (!di
->bridge_exporter
.options
) {
1910 ovs_mutex_unlock(&mutex
);
1911 return OFPERR_NXST_NOT_CONFIGURED
;
1914 ipfix_get_bridge_stats(&di
->bridge_exporter
, &ois
);
1915 ofputil_append_ipfix_stat(replies
, &ois
);
1917 if (hmap_count(&di
->flow_exporter_map
) == 0) {
1918 ovs_mutex_unlock(&mutex
);
1919 return OFPERR_NXST_NOT_CONFIGURED
;
1922 HMAP_FOR_EACH (flow_exporter_node
, node
,
1923 &di
->flow_exporter_map
) {
1924 ipfix_get_flow_stats(&flow_exporter_node
->exporter
, &ois
);
1925 ofputil_append_ipfix_stat(replies
, &ois
);
1928 ovs_mutex_unlock(&mutex
);
1933 /* Update partial ipfix stats */
1935 ipfix_update_stats(struct dpif_ipfix_exporter
*exporter
,
1937 size_t current_flows
,
1938 enum ipfix_sampled_packet_type sampled_pkt_type
)
1941 exporter
->ofproto_stats
.total_flows
++;
1942 exporter
->ofproto_stats
.current_flows
= current_flows
;
1944 exporter
->ofproto_stats
.pkts
++;
1946 switch (sampled_pkt_type
) {
1947 case IPFIX_SAMPLED_PKT_IPV4_OK
:
1948 exporter
->ofproto_stats
.ipv4_pkts
++;
1950 case IPFIX_SAMPLED_PKT_IPV6_OK
:
1951 exporter
->ofproto_stats
.ipv6_pkts
++;
1953 case IPFIX_SAMPLED_PKT_IPV4_ERROR
:
1954 exporter
->ofproto_stats
.ipv4_error_pkts
++;
1955 exporter
->ofproto_stats
.error_pkts
++;
1957 case IPFIX_SAMPLED_PKT_IPV6_ERROR
:
1958 exporter
->ofproto_stats
.ipv6_error_pkts
++;
1959 exporter
->ofproto_stats
.error_pkts
++;
1961 case IPFIX_SAMPLED_PKT_UNKNOWN
:
1962 exporter
->ofproto_stats
.error_pkts
++;
1964 case IPFIX_SAMPLED_PKT_OTHERS
:
1970 /* Returns the current time in the form used by IPFIX (microseconds since the
1975 return time_wall_msec() * 1000ULL;
1978 /* Add an entry into a flow cache. The entry is either aggregated into
1979 * an existing entry with the same flow key and free()d, or it is
1980 * inserted into the cache. And IPFIX stats will be updated */
1982 ipfix_cache_update(struct dpif_ipfix_exporter
*exporter
,
1983 struct ipfix_flow_cache_entry
*entry
,
1984 enum ipfix_sampled_packet_type sampled_pkt_type
)
1986 struct ipfix_flow_cache_entry
*old_entry
;
1987 size_t current_flows
= 0;
1989 old_entry
= ipfix_cache_find_entry(exporter
, &entry
->flow_key
);
1991 if (old_entry
== NULL
) {
1992 hmap_insert(&exporter
->cache_flow_key_map
, &entry
->flow_key_map_node
,
1993 ipfix_hash_flow_key(&entry
->flow_key
, 0));
1995 /* As the latest entry added into the cache, it should
1996 * logically have the highest flow_start_timestamp_usec, so
1997 * append it at the tail. */
1998 ovs_list_push_back(&exporter
->cache_flow_start_timestamp_list
,
1999 &entry
->cache_flow_start_timestamp_list_node
);
2001 /* Enforce exporter->cache_max_flows limit. */
2002 current_flows
= hmap_count(&exporter
->cache_flow_key_map
);
2003 ipfix_update_stats(exporter
, true, current_flows
, sampled_pkt_type
);
2004 if (current_flows
> exporter
->cache_max_flows
) {
2005 dpif_ipfix_cache_expire_now(exporter
, false);
2008 ipfix_cache_aggregate_entries(entry
, old_entry
);
2010 ipfix_update_stats(exporter
, false, current_flows
, sampled_pkt_type
);
2015 ipfix_destroy_iface_data_record(struct ipfix_data_record_flow_key_iface
*data
)
2017 free(data
->if_descr
);
2018 free(data
->if_name
);
2021 /* Fills '*data' structure based on port number 'port_no'. Caller must destroy
2022 * 'data' with ipfix_destroy_iface_data_record(). */
2024 ipfix_get_iface_data_record(const struct dpif_ipfix
*di
, odp_port_t port_no
,
2025 struct ipfix_data_record_flow_key_iface
*data
)
2028 struct dpif_ipfix_port
*port
;
2029 struct smap netdev_status
;
2031 port
= dpif_ipfix_find_port(di
, port_no
);
2036 smap_init(&netdev_status
);
2037 if (!netdev_get_status(port
->ofport
->netdev
, &netdev_status
)) {
2038 data
->if_type
= htonl(smap_get_ullong(&netdev_status
, "if_type", 0));
2039 data
->if_descr
= nullable_xstrdup(smap_get(&netdev_status
,
2043 data
->if_descr
= NULL
;
2046 smap_destroy(&netdev_status
);
2047 data
->if_index
= htonl(port
->ifindex
);
2048 data
->if_descr_len
= data
->if_descr
? strnlen(data
->if_descr
,
2049 MAX_IF_DESCR_LEN
) : 0;
2050 data
->if_name
= nullable_xstrdup(netdev_get_name(port
->ofport
->netdev
));
2051 data
->if_name_len
= data
->if_name
? strnlen(data
->if_name
,
2052 MAX_IF_NAME_LEN
) : 0;
2058 ipfix_put_iface_data_record(const struct dpif_ipfix
*di
, odp_port_t port_no
,
2059 struct dp_packet
*msg
)
2062 struct ipfix_data_record_flow_key_iface data
;
2065 memset(&data
, 0, sizeof(struct ipfix_data_record_flow_key_iface
));
2066 err
= ipfix_get_iface_data_record(di
, port_no
, &data
);
2068 dp_packet_put(msg
, &data
.if_index
, sizeof data
.if_index
);
2069 dp_packet_put(msg
, &data
.if_type
, sizeof data
.if_type
);
2070 dp_packet_put(msg
, &data
.if_name_len
, sizeof data
.if_name_len
);
2071 if (data
.if_name_len
) {
2072 dp_packet_put(msg
, data
.if_name
, data
.if_name_len
);
2074 dp_packet_put(msg
, &data
.if_descr_len
, sizeof data
.if_descr_len
);
2075 if (data
.if_descr_len
) {
2076 dp_packet_put(msg
, data
.if_descr
, data
.if_descr_len
);
2078 ipfix_destroy_iface_data_record(&data
);
2080 dp_packet_put_zeros(msg
, sizeof data
.if_index
);
2081 dp_packet_put_zeros(msg
, sizeof data
.if_type
);
2082 dp_packet_put_zeros(msg
, sizeof data
.if_name_len
);
2083 dp_packet_put_zeros(msg
, sizeof data
.if_descr_len
);
2087 static enum ipfix_sampled_packet_type
2088 ipfix_cache_entry_init(const struct dpif_ipfix
*di
,
2089 struct ipfix_flow_cache_entry
*entry
,
2090 const struct dp_packet
*packet
, const struct flow
*flow
,
2091 uint64_t packet_delta_count
, uint32_t obs_domain_id
,
2092 uint32_t obs_point_id
, odp_port_t output_odp_port
,
2093 enum nx_action_sample_direction direction
,
2094 const struct dpif_ipfix_port
*tunnel_port
,
2095 const struct flow_tnl
*tunnel_key
,
2096 struct dpif_ipfix_global_stats
*stats
,
2097 const struct dpif_ipfix_actions
*ipfix_actions
)
2100 struct ipfix_flow_key
*flow_key
;
2101 struct dp_packet msg
;
2102 enum ipfix_proto_l2 l2
;
2103 enum ipfix_proto_l3 l3
;
2104 enum ipfix_proto_l4 l4
;
2105 enum ipfix_proto_tunnel tunnel
= IPFIX_PROTO_NOT_TUNNELED
;
2106 enum ipfix_sampled_packet_type sampled_pkt_type
= IPFIX_SAMPLED_PKT_UNKNOWN
;
2107 uint8_t ethernet_header_length
;
2108 uint16_t ethernet_total_length
;
2109 bool is_multicast
= false;
2110 bool is_broadcast
= false;
2112 flow_key
= &entry
->flow_key
;
2113 dp_packet_use_stub(&msg
, flow_key
->flow_key_msg_part
,
2114 sizeof flow_key
->flow_key_msg_part
);
2116 /* Choose the right template ID matching the protocols in the
2117 * sampled packet. */
2118 l2
= (flow
->vlans
[0].tci
== 0) ? IPFIX_PROTO_L2_ETH
: IPFIX_PROTO_L2_VLAN
;
2120 switch(ntohs(flow
->dl_type
)) {
2122 l3
= IPFIX_PROTO_L3_IPV4
;
2123 sampled_pkt_type
= IPFIX_SAMPLED_PKT_IPV4_OK
;
2124 switch(flow
->nw_proto
) {
2126 l4
= IPFIX_PROTO_L4_TCP
;
2129 l4
= IPFIX_PROTO_L4_UDP
;
2132 l4
= IPFIX_PROTO_L4_SCTP
;
2135 l4
= IPFIX_PROTO_L4_ICMP
;
2138 l4
= IPFIX_PROTO_L4_UNKNOWN
;
2139 sampled_pkt_type
= IPFIX_SAMPLED_PKT_IPV4_ERROR
;
2143 l3
= IPFIX_PROTO_L3_IPV6
;
2144 sampled_pkt_type
= IPFIX_SAMPLED_PKT_IPV6_OK
;
2145 switch(flow
->nw_proto
) {
2147 l4
= IPFIX_PROTO_L4_TCP
;
2150 l4
= IPFIX_PROTO_L4_UDP
;
2153 l4
= IPFIX_PROTO_L4_SCTP
;
2155 case IPPROTO_ICMPV6
:
2156 l4
= IPFIX_PROTO_L4_ICMP
;
2159 l4
= IPFIX_PROTO_L4_UNKNOWN
;
2160 sampled_pkt_type
= IPFIX_SAMPLED_PKT_IPV6_ERROR
;
2164 l3
= IPFIX_PROTO_L3_UNKNOWN
;
2165 l4
= IPFIX_PROTO_L4_UNKNOWN
;
2166 sampled_pkt_type
= IPFIX_SAMPLED_PKT_OTHERS
;
2169 if (tunnel_port
&& tunnel_key
) {
2170 tunnel
= IPFIX_PROTO_TUNNELED
;
2173 uint8_t flow_direction
=
2174 (direction
== NX_ACTION_SAMPLE_INGRESS
? INGRESS_FLOW
2175 : direction
== NX_ACTION_SAMPLE_EGRESS
? EGRESS_FLOW
2176 : output_odp_port
== ODPP_NONE
? INGRESS_FLOW
: EGRESS_FLOW
);
2178 flow_key
->obs_domain_id
= obs_domain_id
;
2179 flow_key
->template_id
= ipfix_get_template_id(l2
, l3
, l4
, tunnel
,
2182 /* The fields defined in the ipfix_data_record_* structs and sent
2183 * below must match exactly the templates defined in
2184 * ipfix_define_template_fields. */
2186 ethernet_header_length
= (l2
== IPFIX_PROTO_L2_VLAN
)
2187 ? VLAN_ETH_HEADER_LEN
: ETH_HEADER_LEN
;
2188 ethernet_total_length
= dp_packet_size(packet
);
2190 /* Common Ethernet entities. */
2192 struct ipfix_data_record_flow_key_common
*data_common
;
2194 data_common
= dp_packet_put_zeros(&msg
, sizeof *data_common
);
2195 data_common
->observation_point_id
= htonl(obs_point_id
);
2196 data_common
->flow_direction
= flow_direction
;
2197 data_common
->source_mac_address
= flow
->dl_src
;
2198 data_common
->destination_mac_address
= flow
->dl_dst
;
2199 data_common
->ethernet_type
= flow
->dl_type
;
2200 data_common
->ethernet_header_length
= ethernet_header_length
;
2203 /* Interface Information Elements */
2204 ipfix_put_iface_data_record(di
, flow
->in_port
.odp_port
, &msg
);
2206 if (flow_direction
== EGRESS_FLOW
) {
2207 ipfix_put_iface_data_record(di
, output_odp_port
, &msg
);
2210 if (l2
== IPFIX_PROTO_L2_VLAN
) {
2211 struct ipfix_data_record_flow_key_vlan
*data_vlan
;
2212 uint16_t vlan_id
= vlan_tci_to_vid(flow
->vlans
[0].tci
);
2213 uint8_t priority
= vlan_tci_to_pcp(flow
->vlans
[0].tci
);
2215 data_vlan
= dp_packet_put_zeros(&msg
, sizeof *data_vlan
);
2216 data_vlan
->vlan_id
= htons(vlan_id
);
2217 data_vlan
->dot1q_vlan_id
= htons(vlan_id
);
2218 data_vlan
->dot1q_priority
= priority
;
2221 if (l3
!= IPFIX_PROTO_L3_UNKNOWN
) {
2222 struct ipfix_data_record_flow_key_ip
*data_ip
;
2224 data_ip
= dp_packet_put_zeros(&msg
, sizeof *data_ip
);
2225 data_ip
->ip_version
= (l3
== IPFIX_PROTO_L3_IPV4
) ? 4 : 6;
2226 data_ip
->ip_ttl
= flow
->nw_ttl
;
2227 data_ip
->protocol_identifier
= flow
->nw_proto
;
2228 data_ip
->ip_diff_serv_code_point
= flow
->nw_tos
>> 2;
2229 data_ip
->ip_precedence
= flow
->nw_tos
>> 5;
2230 data_ip
->ip_class_of_service
= flow
->nw_tos
;
2232 if (l3
== IPFIX_PROTO_L3_IPV4
) {
2233 struct ipfix_data_record_flow_key_ipv4
*data_ipv4
;
2235 data_ipv4
= dp_packet_put_zeros(&msg
, sizeof *data_ipv4
);
2236 data_ipv4
->source_ipv4_address
= flow
->nw_src
;
2237 data_ipv4
->destination_ipv4_address
= flow
->nw_dst
;
2238 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
2239 struct ipfix_data_record_flow_key_ipv6
*data_ipv6
;
2241 data_ipv6
= dp_packet_put_zeros(&msg
, sizeof *data_ipv6
);
2242 memcpy(data_ipv6
->source_ipv6_address
, &flow
->ipv6_src
,
2243 sizeof flow
->ipv6_src
);
2244 memcpy(data_ipv6
->destination_ipv6_address
, &flow
->ipv6_dst
,
2245 sizeof flow
->ipv6_dst
);
2246 data_ipv6
->flow_label_ipv6
= flow
->ipv6_label
;
2250 if (l4
== IPFIX_PROTO_L4_TCP
2251 || l4
== IPFIX_PROTO_L4_UDP
2252 || l4
== IPFIX_PROTO_L4_SCTP
) {
2253 struct ipfix_data_record_flow_key_transport
*data_transport
;
2255 data_transport
= dp_packet_put_zeros(&msg
, sizeof *data_transport
);
2256 data_transport
->source_transport_port
= flow
->tp_src
;
2257 data_transport
->destination_transport_port
= flow
->tp_dst
;
2258 } else if (l4
== IPFIX_PROTO_L4_ICMP
) {
2259 struct ipfix_data_record_flow_key_icmp
*data_icmp
;
2261 data_icmp
= dp_packet_put_zeros(&msg
, sizeof *data_icmp
);
2262 data_icmp
->icmp_type
= ntohs(flow
->tp_src
) & 0xff;
2263 data_icmp
->icmp_code
= ntohs(flow
->tp_dst
) & 0xff;
2266 if (tunnel
== IPFIX_PROTO_TUNNELED
) {
2267 struct ipfix_data_record_flow_key_tunnel
*data_tunnel
;
2268 const uint8_t *tun_id
;
2270 data_tunnel
= dp_packet_put_zeros(&msg
, sizeof *data_tunnel
+
2271 tunnel_port
->tunnel_key_length
);
2272 data_tunnel
->tunnel_source_ipv4_address
= tunnel_key
->ip_src
;
2273 data_tunnel
->tunnel_destination_ipv4_address
= tunnel_key
->ip_dst
;
2274 /* The tunnel_protocol_identifier is from tunnel_proto array, which
2275 * contains protocol_identifiers of each tunnel type.
2277 data_tunnel
->tunnel_protocol_identifier
=
2278 tunnel_protocol
[tunnel_port
->tunnel_type
];
2279 data_tunnel
->tunnel_source_transport_port
= tunnel_key
->tp_src
;
2280 data_tunnel
->tunnel_destination_transport_port
= tunnel_key
->tp_dst
;
2281 data_tunnel
->tunnel_type
= tunnel_port
->tunnel_type
;
2282 data_tunnel
->tunnel_key_length
= tunnel_port
->tunnel_key_length
;
2283 /* tun_id is in network order, and tunnel key is in low bits. */
2284 tun_id
= (const uint8_t *) &tunnel_key
->tun_id
;
2285 memcpy(data_tunnel
->tunnel_key
,
2286 &tun_id
[8 - tunnel_port
->tunnel_key_length
],
2287 tunnel_port
->tunnel_key_length
);
2290 flow_key
->flow_key_msg_part_size
= dp_packet_size(&msg
);
2292 if (eth_addr_is_broadcast(flow
->dl_dst
)) {
2293 is_broadcast
= true;
2294 } else if (eth_addr_is_multicast(flow
->dl_dst
)) {
2295 is_multicast
= true;
2299 uint64_t layer2_octet_delta_count
;
2301 /* Calculate the total matched octet count by considering as
2302 * an approximation that all matched packets have the same
2304 layer2_octet_delta_count
= packet_delta_count
* ethernet_total_length
;
2306 entry
->flow_end_timestamp_usec
= ipfix_now();
2307 entry
->flow_start_timestamp_usec
= entry
->flow_end_timestamp_usec
;
2309 if (ipfix_actions
&& ipfix_actions
->output_action
) {
2310 entry
->dropped_packet_delta_count
= 0;
2312 entry
->dropped_packet_delta_count
= packet_delta_count
;
2315 entry
->packet_delta_count
= packet_delta_count
;
2316 entry
->layer2_octet_delta_count
= layer2_octet_delta_count
;
2318 stats
->dropped_packet_total_count
+= entry
->dropped_packet_delta_count
;
2319 stats
->packet_total_count
+= packet_delta_count
;
2320 stats
->layer2_octet_total_count
+= layer2_octet_delta_count
;
2322 entry
->post_mcast_packet_delta_count
= 0;
2324 if (flow_direction
== INGRESS_FLOW
) {
2325 stats
->in_bcast_packet_total_count
+= packet_delta_count
;
2326 } else if (flow_direction
== EGRESS_FLOW
) {
2327 stats
->out_bcast_packet_total_count
+= packet_delta_count
;
2329 } else if (is_multicast
) {
2330 if (flow_direction
== INGRESS_FLOW
) {
2331 stats
->in_mcast_packet_total_count
+= packet_delta_count
;
2332 } else if (flow_direction
== EGRESS_FLOW
) {
2333 entry
->post_mcast_packet_delta_count
= packet_delta_count
;
2334 stats
->post_mcast_packet_total_count
+= packet_delta_count
;
2337 if (flow_direction
== INGRESS_FLOW
) {
2338 stats
->in_ucast_packet_total_count
+= packet_delta_count
;
2339 } else if (flow_direction
== EGRESS_FLOW
) {
2340 stats
->out_ucast_packet_total_count
+= packet_delta_count
;
2344 entry
->dropped_packet_total_count
= stats
->dropped_packet_total_count
;
2345 entry
->packet_total_count
= stats
->packet_total_count
;
2346 entry
->in_ucast_packet_total_count
=
2347 stats
->in_ucast_packet_total_count
;
2348 entry
->in_mcast_packet_total_count
=
2349 stats
->in_mcast_packet_total_count
;
2350 entry
->in_bcast_packet_total_count
=
2351 stats
->in_bcast_packet_total_count
;
2352 entry
->out_ucast_packet_total_count
=
2353 stats
->out_ucast_packet_total_count
;
2354 entry
->out_bcast_packet_total_count
=
2355 stats
->out_bcast_packet_total_count
;
2356 entry
->post_mcast_packet_total_count
=
2357 stats
->post_mcast_packet_total_count
;
2358 entry
->layer2_octet_total_count
= stats
->layer2_octet_total_count
;
2361 if (l3
!= IPFIX_PROTO_L3_UNKNOWN
) {
2362 uint16_t ip_total_length
=
2363 ethernet_total_length
- ethernet_header_length
;
2364 uint64_t octet_delta_count
;
2366 /* Calculate the total matched octet count by considering as
2367 * an approximation that all matched packets have the same
2369 octet_delta_count
= packet_delta_count
* ip_total_length
;
2371 if (ipfix_actions
&& ipfix_actions
->output_action
) {
2372 entry
->dropped_octet_delta_count
= 0;
2374 entry
->dropped_octet_delta_count
= octet_delta_count
;
2377 entry
->octet_delta_count
= octet_delta_count
;
2378 entry
->octet_delta_sum_of_squares
= octet_delta_count
* ip_total_length
;
2379 entry
->minimum_ip_total_length
= ip_total_length
;
2380 entry
->maximum_ip_total_length
= ip_total_length
;
2382 stats
->dropped_octet_total_count
+= entry
->dropped_octet_delta_count
;
2383 stats
->octet_total_count
+= octet_delta_count
;
2384 stats
->octet_total_sum_of_squares
+= entry
->octet_delta_sum_of_squares
;
2386 if (is_multicast
&& flow_direction
== EGRESS_FLOW
) {
2387 entry
->post_mcast_octet_delta_count
= octet_delta_count
;
2388 stats
->post_mcast_octet_total_count
+= octet_delta_count
;
2390 entry
->post_mcast_octet_delta_count
= 0;
2393 entry
->octet_delta_sum_of_squares
= 0;
2394 entry
->minimum_ip_total_length
= 0;
2395 entry
->maximum_ip_total_length
= 0;
2398 entry
->dropped_octet_total_count
= stats
->dropped_octet_total_count
;
2399 entry
->octet_total_sum_of_squares
= stats
->octet_total_sum_of_squares
;
2400 entry
->octet_total_count
= stats
->octet_total_count
;
2401 entry
->post_mcast_octet_total_count
=
2402 stats
->post_mcast_octet_total_count
;
2404 if (l4
== IPFIX_PROTO_L4_TCP
) {
2405 uint16_t tcp_flags
= ntohs(flow
->tcp_flags
);
2406 entry
->tcp_packet_delta_count
= packet_delta_count
;
2408 if (tcp_flags
& TCP_ACK
) {
2409 stats
->tcp_ack_total_count
+= packet_delta_count
;
2411 if (tcp_flags
& TCP_FIN
) {
2412 stats
->tcp_fin_total_count
+= packet_delta_count
;
2414 if (tcp_flags
& TCP_PSH
) {
2415 stats
->tcp_psh_total_count
+= packet_delta_count
;
2417 if (tcp_flags
& TCP_RST
) {
2418 stats
->tcp_rst_total_count
+= packet_delta_count
;
2420 if (tcp_flags
& TCP_SYN
) {
2421 stats
->tcp_syn_total_count
+= packet_delta_count
;
2423 if (tcp_flags
& TCP_URG
) {
2424 stats
->tcp_urg_total_count
+= packet_delta_count
;
2427 entry
->tcp_packet_delta_count
= 0;
2430 entry
->tcp_ack_total_count
= stats
->tcp_ack_total_count
;
2431 entry
->tcp_fin_total_count
= stats
->tcp_fin_total_count
;
2432 entry
->tcp_psh_total_count
= stats
->tcp_psh_total_count
;
2433 entry
->tcp_rst_total_count
= stats
->tcp_rst_total_count
;
2434 entry
->tcp_syn_total_count
= stats
->tcp_syn_total_count
;
2435 entry
->tcp_urg_total_count
= stats
->tcp_urg_total_count
;
2437 return sampled_pkt_type
;
2440 /* Send each single data record in its own data set, to simplify the
2441 * implementation by avoiding having to group record by template ID
2442 * before sending. */
2444 ipfix_put_data_set(uint32_t export_time_sec
,
2445 struct ipfix_flow_cache_entry
*entry
,
2446 enum ipfix_flow_end_reason flow_end_reason
,
2447 const char *virtual_obs_id
,
2448 uint8_t virtual_obs_len
,
2449 struct dp_packet
*msg
)
2451 size_t set_hdr_offset
;
2452 struct ipfix_set_header
*set_hdr
;
2454 set_hdr_offset
= dp_packet_size(msg
);
2456 /* Put a Data Set. */
2457 set_hdr
= dp_packet_put_zeros(msg
, sizeof *set_hdr
);
2458 set_hdr
->set_id
= htons(entry
->flow_key
.template_id
);
2460 /* Copy the flow key part of the data record. */
2461 dp_packet_put(msg
, entry
->flow_key
.flow_key_msg_part
,
2462 entry
->flow_key
.flow_key_msg_part_size
);
2464 /* Export virtual observation ID. */
2465 if (virtual_obs_id
) {
2466 dp_packet_put(msg
, &virtual_obs_len
, sizeof(virtual_obs_len
));
2467 dp_packet_put(msg
, virtual_obs_id
, virtual_obs_len
);
2470 /* Put the non-key part of the data record. */
2473 struct ipfix_data_record_aggregated_common
*data_aggregated_common
;
2474 uint64_t export_time_usec
, flow_start_delta_usec
, flow_end_delta_usec
;
2476 /* Calculate the negative deltas relative to the export time
2477 * in seconds sent in the header, not the exact export
2479 export_time_usec
= 1000000LL * export_time_sec
;
2480 flow_start_delta_usec
= export_time_usec
2481 - entry
->flow_start_timestamp_usec
;
2482 flow_end_delta_usec
= export_time_usec
2483 - entry
->flow_end_timestamp_usec
;
2485 data_aggregated_common
= dp_packet_put_zeros(
2486 msg
, sizeof *data_aggregated_common
);
2487 data_aggregated_common
->flow_start_delta_microseconds
= htonl(
2488 flow_start_delta_usec
);
2489 data_aggregated_common
->flow_end_delta_microseconds
= htonl(
2490 flow_end_delta_usec
);
2491 data_aggregated_common
->dropped_packet_delta_count
= htonll(
2492 entry
->dropped_packet_delta_count
);
2493 data_aggregated_common
->dropped_packet_total_count
= htonll(
2494 entry
->dropped_packet_total_count
);
2495 data_aggregated_common
->packet_delta_count
= htonll(
2496 entry
->packet_delta_count
);
2497 data_aggregated_common
->packet_total_count
= htonll(
2498 entry
->packet_total_count
);
2499 data_aggregated_common
->in_ucast_packet_total_count
= htonll(
2500 entry
->in_ucast_packet_total_count
);
2501 data_aggregated_common
->in_mcast_packet_total_count
= htonll(
2502 entry
->in_mcast_packet_total_count
);
2503 data_aggregated_common
->in_bcast_packet_total_count
= htonll(
2504 entry
->in_bcast_packet_total_count
);
2505 data_aggregated_common
->out_ucast_packet_total_count
= htonll(
2506 entry
->out_ucast_packet_total_count
);
2507 data_aggregated_common
->out_bcast_packet_total_count
= htonll(
2508 entry
->out_bcast_packet_total_count
);
2509 data_aggregated_common
->layer2_octet_delta_count
= htonll(
2510 entry
->layer2_octet_delta_count
);
2511 data_aggregated_common
->layer2_octet_total_count
= htonll(
2512 entry
->layer2_octet_total_count
);
2513 data_aggregated_common
->flow_end_reason
= flow_end_reason
;
2514 data_aggregated_common
->post_mcast_packet_delta_count
= htonll(
2515 entry
->post_mcast_packet_delta_count
);
2516 data_aggregated_common
->post_mcast_packet_total_count
= htonll(
2517 entry
->post_mcast_packet_total_count
);
2520 if (entry
->octet_delta_sum_of_squares
) { /* IP packet. */
2521 struct ipfix_data_record_aggregated_ip
*data_aggregated_ip
;
2523 data_aggregated_ip
= dp_packet_put_zeros(
2524 msg
, sizeof *data_aggregated_ip
);
2525 data_aggregated_ip
->dropped_octet_delta_count
= htonll(
2526 entry
->dropped_octet_delta_count
);
2527 data_aggregated_ip
->dropped_octet_total_count
= htonll(
2528 entry
->dropped_octet_total_count
);
2529 data_aggregated_ip
->octet_delta_count
= htonll(
2530 entry
->octet_delta_count
);
2531 data_aggregated_ip
->octet_total_count
= htonll(
2532 entry
->octet_total_count
);
2533 data_aggregated_ip
->octet_delta_sum_of_squares
= htonll(
2534 entry
->octet_delta_sum_of_squares
);
2535 data_aggregated_ip
->octet_total_sum_of_squares
= htonll(
2536 entry
->octet_total_sum_of_squares
);
2537 data_aggregated_ip
->minimum_ip_total_length
= htonll(
2538 entry
->minimum_ip_total_length
);
2539 data_aggregated_ip
->maximum_ip_total_length
= htonll(
2540 entry
->maximum_ip_total_length
);
2541 data_aggregated_ip
->post_mcast_octet_delta_count
= htonll(
2542 entry
->post_mcast_octet_delta_count
);
2543 data_aggregated_ip
->post_mcast_octet_total_count
= htonll(
2544 entry
->post_mcast_octet_total_count
);
2547 if (entry
->tcp_packet_delta_count
) {
2548 struct ipfix_data_record_aggregated_tcp
*data_aggregated_tcp
;
2550 data_aggregated_tcp
= dp_packet_put_zeros(
2551 msg
, sizeof *data_aggregated_tcp
);
2552 data_aggregated_tcp
->tcp_ack_total_count
= htonll(
2553 entry
->tcp_ack_total_count
);
2554 data_aggregated_tcp
->tcp_fin_total_count
= htonll(
2555 entry
->tcp_fin_total_count
);
2556 data_aggregated_tcp
->tcp_psh_total_count
= htonll(
2557 entry
->tcp_psh_total_count
);
2558 data_aggregated_tcp
->tcp_rst_total_count
= htonll(
2559 entry
->tcp_rst_total_count
);
2560 data_aggregated_tcp
->tcp_syn_total_count
= htonll(
2561 entry
->tcp_syn_total_count
);
2562 data_aggregated_tcp
->tcp_urg_total_count
= htonll(
2563 entry
->tcp_urg_total_count
);
2566 set_hdr
= (struct ipfix_set_header
*)((uint8_t*)dp_packet_data(msg
) + set_hdr_offset
);
2567 set_hdr
->length
= htons(dp_packet_size(msg
) - set_hdr_offset
);
2571 ipfix_put_exporter_data_set(uint32_t exporting_process_id
,
2572 const ofproto_ipfix_stats
*ofproto_stats
,
2573 struct dp_packet
*msg
)
2575 size_t set_hdr_offset
;
2576 struct ipfix_set_header
*set_hdr
;
2578 set_hdr_offset
= dp_packet_size(msg
);
2580 /* Put a Data Set. */
2581 set_hdr
= dp_packet_put_zeros(msg
, sizeof *set_hdr
);
2582 set_hdr
->set_id
= htons(
2583 ipfix_get_options_template_id(IPFIX_OPTIONS_TEMPLATE_EXPORTER_STATS
));
2586 struct ipfix_data_record_exporter_stats
*data_exporter_stats
;
2588 data_exporter_stats
= dp_packet_put_zeros(
2589 msg
, sizeof *data_exporter_stats
);
2591 data_exporter_stats
->exporting_process_id
=
2592 htonl(exporting_process_id
);
2593 data_exporter_stats
->not_sent_packet_total_count
= htonll(
2594 ofproto_stats
->tx_errors
);
2597 set_hdr
= (struct ipfix_set_header
*)
2598 ((uint8_t *)dp_packet_data(msg
) + set_hdr_offset
);
2599 set_hdr
->length
= htons(dp_packet_size(msg
) - set_hdr_offset
);
2602 /* Send an IPFIX message with a single data set containing Exporting Process
2603 * Reliability Statistics. */
2605 ipfix_send_exporter_data_msg(struct dpif_ipfix_exporter
*exporter
,
2606 uint32_t export_time_sec
)
2608 uint64_t msg_stub
[DIV_ROUND_UP(MAX_MESSAGE_LEN
, 8)];
2609 struct dp_packet msg
;
2612 dp_packet_use_stub(&msg
, msg_stub
, sizeof msg_stub
);
2614 /* In case of Exporting Process Statistics, Observation Domain ID should
2616 ipfix_init_header(export_time_sec
, exporter
->seq_number
++, 0U, &msg
);
2617 ipfix_put_exporter_data_set(exporter
->exporter_id
,
2618 &exporter
->ofproto_stats
, &msg
);
2619 tx_errors
= ipfix_send_msg(exporter
->collectors
, &msg
);
2621 dp_packet_uninit(&msg
);
2623 exporter
->ofproto_stats
.tx_pkts
+=
2624 collectors_count(exporter
->collectors
) - tx_errors
;
2625 exporter
->ofproto_stats
.tx_errors
+= tx_errors
;
2628 /* Send an IPFIX message with a single data record. */
2630 ipfix_send_data_msg(struct dpif_ipfix_exporter
*exporter
,
2631 uint32_t export_time_sec
,
2632 struct ipfix_flow_cache_entry
*entry
,
2633 enum ipfix_flow_end_reason flow_end_reason
)
2635 uint64_t msg_stub
[DIV_ROUND_UP(MAX_MESSAGE_LEN
, 8)];
2636 struct dp_packet msg
;
2639 dp_packet_use_stub(&msg
, msg_stub
, sizeof msg_stub
);
2641 ipfix_init_header(export_time_sec
, exporter
->seq_number
++,
2642 entry
->flow_key
.obs_domain_id
, &msg
);
2643 ipfix_put_data_set(export_time_sec
, entry
, flow_end_reason
,
2644 exporter
->virtual_obs_id
, exporter
->virtual_obs_len
,
2646 tx_errors
= ipfix_send_msg(exporter
->collectors
, &msg
);
2648 dp_packet_uninit(&msg
);
2650 exporter
->ofproto_stats
.current_flows
--;
2651 exporter
->ofproto_stats
.tx_pkts
+=
2652 collectors_count(exporter
->collectors
) - tx_errors
;
2653 exporter
->ofproto_stats
.tx_errors
+= tx_errors
;
2657 dpif_ipfix_sample(const struct dpif_ipfix
*di
,
2658 struct dpif_ipfix_exporter
*exporter
,
2659 const struct dp_packet
*packet
, const struct flow
*flow
,
2660 uint64_t packet_delta_count
, uint32_t obs_domain_id
,
2661 uint32_t obs_point_id
, odp_port_t output_odp_port
,
2662 enum nx_action_sample_direction direction
,
2663 const struct dpif_ipfix_port
*tunnel_port
,
2664 const struct flow_tnl
*tunnel_key
,
2665 const struct dpif_ipfix_actions
*ipfix_actions
)
2668 struct ipfix_flow_cache_entry
*entry
;
2669 enum ipfix_sampled_packet_type sampled_packet_type
;
2671 /* Create a flow cache entry from the sample. */
2672 entry
= xmalloc(sizeof *entry
);
2673 sampled_packet_type
=
2674 ipfix_cache_entry_init(di
, entry
, packet
,
2675 flow
, packet_delta_count
,
2676 obs_domain_id
, obs_point_id
,
2677 output_odp_port
, direction
,
2678 tunnel_port
, tunnel_key
,
2679 &exporter
->ipfix_global_stats
,
2682 ipfix_cache_update(exporter
, entry
, sampled_packet_type
);
2686 bridge_exporter_enabled(struct dpif_ipfix
*di
)
2688 return di
->bridge_exporter
.probability
> 0;
2692 dpif_ipfix_bridge_sample(struct dpif_ipfix
*di
, const struct dp_packet
*packet
,
2693 const struct flow
*flow
,
2694 odp_port_t input_odp_port
, odp_port_t output_odp_port
,
2695 const struct flow_tnl
*output_tunnel_key
,
2696 const struct dpif_ipfix_actions
*ipfix_actions
)
2699 uint64_t packet_delta_count
;
2700 const struct flow_tnl
*tunnel_key
= NULL
;
2701 struct dpif_ipfix_port
* tunnel_port
= NULL
;
2703 ovs_mutex_lock(&mutex
);
2704 if (!bridge_exporter_enabled(di
)) {
2705 ovs_mutex_unlock(&mutex
);
2709 /* Skip BFD packets:
2710 * Bidirectional Forwarding Detection(BFD) packets are for monitoring
2711 * the tunnel link status and consumed by ovs itself. No need to
2713 * CF IETF RFC 5881, BFD control packet is the UDP packet with
2714 * destination port 3784, and BFD echo packet is the UDP packet with
2715 * destination port 3785.
2717 if (is_ip_any(flow
) &&
2718 flow
->nw_proto
== IPPROTO_UDP
&&
2719 (flow
->tp_dst
== htons(BFD_CONTROL_DEST_PORT
) ||
2720 flow
->tp_dst
== htons(BFD_ECHO_DEST_PORT
))) {
2721 ovs_mutex_unlock(&mutex
);
2725 /* Use the sampling probability as an approximation of the number
2726 * of matched packets. */
2727 packet_delta_count
= UINT32_MAX
/ di
->bridge_exporter
.probability
;
2728 if (di
->bridge_exporter
.options
->enable_tunnel_sampling
) {
2729 if (output_odp_port
== ODPP_NONE
&& flow
->tunnel
.ip_dst
) {
2731 tunnel_key
= &flow
->tunnel
;
2732 tunnel_port
= dpif_ipfix_find_tunnel_port(di
, input_odp_port
);
2734 if (output_odp_port
!= ODPP_NONE
&& output_tunnel_key
) {
2735 /* Output tunnel, output_tunnel_key must be valid. */
2736 tunnel_key
= output_tunnel_key
;
2737 tunnel_port
= dpif_ipfix_find_tunnel_port(di
, output_odp_port
);
2741 dpif_ipfix_sample(di
, &di
->bridge_exporter
.exporter
, packet
, flow
,
2743 di
->bridge_exporter
.options
->obs_domain_id
,
2744 di
->bridge_exporter
.options
->obs_point_id
,
2745 output_odp_port
, NX_ACTION_SAMPLE_DEFAULT
,
2746 tunnel_port
, tunnel_key
, ipfix_actions
);
2747 ovs_mutex_unlock(&mutex
);
2751 dpif_ipfix_flow_sample(struct dpif_ipfix
*di
, const struct dp_packet
*packet
,
2752 const struct flow
*flow
,
2753 const struct user_action_cookie
*cookie
,
2754 odp_port_t input_odp_port
,
2755 const struct flow_tnl
*output_tunnel_key
,
2756 const struct dpif_ipfix_actions
*ipfix_actions
)
2759 struct dpif_ipfix_flow_exporter_map_node
*node
;
2760 const struct flow_tnl
*tunnel_key
= NULL
;
2761 struct dpif_ipfix_port
* tunnel_port
= NULL
;
2762 odp_port_t output_odp_port
= cookie
->flow_sample
.output_odp_port
;
2763 uint32_t collector_set_id
= cookie
->flow_sample
.collector_set_id
;
2764 uint16_t probability
= cookie
->flow_sample
.probability
;
2766 /* Use the sampling probability as an approximation of the number
2767 * of matched packets. */
2768 uint64_t packet_delta_count
= USHRT_MAX
/ probability
;
2770 ovs_mutex_lock(&mutex
);
2771 node
= dpif_ipfix_find_flow_exporter_map_node(di
, collector_set_id
);
2773 if (node
->exporter
.options
->enable_tunnel_sampling
) {
2774 if (output_odp_port
== ODPP_NONE
&& flow
->tunnel
.ip_dst
) {
2776 tunnel_key
= &flow
->tunnel
;
2777 tunnel_port
= dpif_ipfix_find_tunnel_port(di
, input_odp_port
);
2779 if (output_odp_port
!= ODPP_NONE
&& output_tunnel_key
) {
2780 /* Output tunnel, output_tunnel_key must be valid. */
2781 tunnel_key
= output_tunnel_key
;
2782 tunnel_port
= dpif_ipfix_find_tunnel_port(di
, output_odp_port
);
2786 dpif_ipfix_sample(di
, &node
->exporter
.exporter
, packet
, flow
,
2788 cookie
->flow_sample
.obs_domain_id
,
2789 cookie
->flow_sample
.obs_point_id
,
2790 output_odp_port
, cookie
->flow_sample
.direction
,
2791 tunnel_port
, tunnel_key
, ipfix_actions
);
2793 ovs_mutex_unlock(&mutex
);
2797 dpif_ipfix_cache_expire(struct dpif_ipfix_exporter
*exporter
,
2798 bool forced_end
, const uint64_t export_time_usec
,
2799 const uint32_t export_time_sec
)
2801 struct ipfix_flow_cache_entry
*entry
, *next_entry
;
2802 uint64_t max_flow_start_timestamp_usec
;
2803 bool template_msg_sent
= false;
2804 enum ipfix_flow_end_reason flow_end_reason
;
2806 if (ovs_list_is_empty(&exporter
->cache_flow_start_timestamp_list
)) {
2810 max_flow_start_timestamp_usec
= export_time_usec
-
2811 1000000LL * exporter
->cache_active_timeout
;
2813 LIST_FOR_EACH_SAFE (entry
, next_entry
, cache_flow_start_timestamp_list_node
,
2814 &exporter
->cache_flow_start_timestamp_list
) {
2816 flow_end_reason
= FORCED_END
;
2817 } else if (entry
->flow_start_timestamp_usec
2818 <= max_flow_start_timestamp_usec
) {
2819 flow_end_reason
= ACTIVE_TIMEOUT
;
2820 } else if (hmap_count(&exporter
->cache_flow_key_map
)
2821 > exporter
->cache_max_flows
) {
2822 /* Enforce exporter->cache_max_flows. */
2823 flow_end_reason
= LACK_OF_RESOURCES
;
2825 /* Remaining flows haven't expired yet. */
2829 ovs_list_remove(&entry
->cache_flow_start_timestamp_list_node
);
2830 hmap_remove(&exporter
->cache_flow_key_map
,
2831 &entry
->flow_key_map_node
);
2833 /* XXX: Make frequency of the (Options) Template and Exporter Process
2834 * Statistics transmission configurable.
2835 * Cf. IETF RFC 5101 Section 4.3. and 10.3.6. */
2836 if (!template_msg_sent
2837 && (exporter
->last_template_set_time
+ IPFIX_TEMPLATE_INTERVAL
)
2838 <= export_time_sec
) {
2839 ipfix_send_template_msgs(exporter
, export_time_sec
,
2840 entry
->flow_key
.obs_domain_id
);
2841 exporter
->last_template_set_time
= export_time_sec
;
2842 template_msg_sent
= true;
2844 /* Send Exporter Process Statistics. */
2845 ipfix_send_exporter_data_msg(exporter
, export_time_sec
);
2848 /* XXX: Group multiple data records for the same obs domain id
2849 * into the same message. */
2850 ipfix_send_data_msg(exporter
, export_time_sec
, entry
, flow_end_reason
);
2856 get_export_time_now(uint64_t *export_time_usec
, uint32_t *export_time_sec
)
2858 *export_time_usec
= ipfix_now();
2860 /* The IPFIX start and end deltas are negative deltas relative to
2861 * the export time, so set the export time 1 second off to
2862 * calculate those deltas. */
2863 *export_time_sec
= DIV_ROUND_UP(*export_time_usec
, 1000000);
2867 dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter
*exporter
,
2870 uint64_t export_time_usec
;
2871 uint32_t export_time_sec
;
2873 get_export_time_now(&export_time_usec
, &export_time_sec
);
2874 dpif_ipfix_cache_expire(exporter
, forced_end
, export_time_usec
,
2879 dpif_ipfix_run(struct dpif_ipfix
*di
) OVS_EXCLUDED(mutex
)
2881 uint64_t export_time_usec
;
2882 uint32_t export_time_sec
;
2883 struct dpif_ipfix_flow_exporter_map_node
*flow_exporter_node
;
2885 ovs_mutex_lock(&mutex
);
2886 get_export_time_now(&export_time_usec
, &export_time_sec
);
2887 if (bridge_exporter_enabled(di
)) {
2888 dpif_ipfix_cache_expire(
2889 &di
->bridge_exporter
.exporter
, false, export_time_usec
,
2892 HMAP_FOR_EACH (flow_exporter_node
, node
, &di
->flow_exporter_map
) {
2893 dpif_ipfix_cache_expire(
2894 &flow_exporter_node
->exporter
.exporter
, false, export_time_usec
,
2897 ovs_mutex_unlock(&mutex
);
2901 dpif_ipfix_wait(struct dpif_ipfix
*di
) OVS_EXCLUDED(mutex
)
2903 long long int next_timeout_msec
= LLONG_MAX
;
2904 struct dpif_ipfix_flow_exporter_map_node
*flow_exporter_node
;
2906 ovs_mutex_lock(&mutex
);
2907 if (bridge_exporter_enabled(di
)) {
2908 if (ipfix_cache_next_timeout_msec(
2909 &di
->bridge_exporter
.exporter
, &next_timeout_msec
)) {
2910 poll_timer_wait_until(next_timeout_msec
);
2913 HMAP_FOR_EACH (flow_exporter_node
, node
, &di
->flow_exporter_map
) {
2914 if (ipfix_cache_next_timeout_msec(
2915 &flow_exporter_node
->exporter
.exporter
, &next_timeout_msec
)) {
2916 poll_timer_wait_until(next_timeout_msec
);
2919 ovs_mutex_unlock(&mutex
);
2923 dpif_ipfix_read_sample_actions(const struct flow
*flow
,
2924 const struct nlattr
*actions
,
2926 struct dpif_ipfix_actions
*ipfix_actions
)
2928 const struct nlattr
*a
;
2930 uint32_t probability
= 0;
2931 struct dpif_ipfix_actions sample_actions
= {0};
2933 if (actions_len
== 0) {
2937 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
2939 enum ovs_sample_attr type
= nl_attr_type(a
);
2941 case OVS_SAMPLE_ATTR_PROBABILITY
:
2942 probability
= nl_attr_get_u32(a
);
2945 case OVS_SAMPLE_ATTR_ACTIONS
:
2946 dpif_ipfix_read_actions(flow
, nl_attr_get(a
), nl_attr_get_size(a
),
2950 case OVS_SAMPLE_ATTR_UNSPEC
:
2951 case __OVS_SAMPLE_ATTR_MAX
:
2957 /* An output action inside sample action is truly an output if the sampling
2958 * probability is set to 100% */
2959 if (probability
== UINT32_MAX
&& sample_actions
.output_action
== true) {
2960 ipfix_actions
->output_action
= true;
2965 dpif_ipfix_read_actions(const struct flow
*flow
,
2966 const struct nlattr
*actions
,
2968 struct dpif_ipfix_actions
*ipfix_actions
)
2970 const struct nlattr
*a
;
2973 if (actions_len
== 0) {
2977 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
2978 enum ovs_action_attr type
= nl_attr_type(a
);
2980 case OVS_ACTION_ATTR_OUTPUT
:
2981 ipfix_actions
->output_action
= true;
2983 case OVS_ACTION_ATTR_SAMPLE
:
2984 dpif_ipfix_read_sample_actions(flow
, nl_attr_get(a
),
2985 nl_attr_get_size(a
), ipfix_actions
);
2987 case OVS_ACTION_ATTR_CLONE
:
2988 dpif_ipfix_read_actions(flow
, nl_attr_get(a
), nl_attr_get_size(a
),
2992 /* OVS_ACTION_ATTR_USERSPACE and OVS_ACTION_ATTR_RECIRC actions can
2993 * yield absolutely any kind of behavior. Let's assume that flow drops
2994 * the packet if there isn't another clear OVS_ACTION_ATTR_OUTPUT
2995 * action associated with packet */
2996 case OVS_ACTION_ATTR_USERSPACE
:
2997 case OVS_ACTION_ATTR_RECIRC
:
2999 case OVS_ACTION_ATTR_TUNNEL_POP
:
3000 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
3001 case OVS_ACTION_ATTR_TRUNC
:
3002 case OVS_ACTION_ATTR_HASH
:
3003 case OVS_ACTION_ATTR_CT
:
3004 case OVS_ACTION_ATTR_CT_CLEAR
:
3005 case OVS_ACTION_ATTR_METER
:
3006 case OVS_ACTION_ATTR_SET_MASKED
:
3007 case OVS_ACTION_ATTR_SET
:
3008 case OVS_ACTION_ATTR_PUSH_VLAN
:
3009 case OVS_ACTION_ATTR_POP_VLAN
:
3010 case OVS_ACTION_ATTR_PUSH_MPLS
:
3011 case OVS_ACTION_ATTR_POP_MPLS
:
3012 case OVS_ACTION_ATTR_PUSH_ETH
:
3013 case OVS_ACTION_ATTR_POP_ETH
:
3014 case OVS_ACTION_ATTR_PUSH_NSH
:
3015 case OVS_ACTION_ATTR_POP_NSH
:
3016 case OVS_ACTION_ATTR_UNSPEC
:
3017 case __OVS_ACTION_ATTR_MAX
: