2 * Copyright (c) 2012, 2013, 2014 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"
29 #include "poll-loop.h"
36 VLOG_DEFINE_THIS_MODULE(ipfix);
38 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
39 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
41 /* Cf. IETF RFC 5101 Section 10.3.4. */
42 #define IPFIX_DEFAULT_COLLECTOR_PORT 4739
44 struct dpif_ipfix_exporter {
45 struct collectors *collectors;
47 time_t last_template_set_time;
48 struct hmap cache_flow_key_map; /* ipfix_flow_cache_entry. */
49 struct list cache_flow_start_timestamp_list; /* ipfix_flow_cache_entry. */
50 uint32_t cache_active_timeout; /* In seconds. */
51 uint32_t cache_max_flows;
54 struct dpif_ipfix_bridge_exporter {
55 struct dpif_ipfix_exporter exporter;
56 struct ofproto_ipfix_bridge_exporter_options *options;
60 struct dpif_ipfix_flow_exporter {
61 struct dpif_ipfix_exporter exporter;
62 struct ofproto_ipfix_flow_exporter_options *options;
65 struct dpif_ipfix_flow_exporter_map_node {
66 struct hmap_node node;
67 struct dpif_ipfix_flow_exporter exporter;
71 struct dpif_ipfix_bridge_exporter bridge_exporter;
72 struct hmap flow_exporter_map; /* dpif_ipfix_flow_exporter_map_node. */
73 struct ovs_refcount ref_cnt;
76 #define IPFIX_VERSION 0x000a
78 /* When using UDP, IPFIX Template Records must be re-sent regularly.
79 * The standard default interval is 10 minutes (600 seconds).
80 * Cf. IETF RFC 5101 Section 10.3.6. */
81 #define IPFIX_TEMPLATE_INTERVAL 600
83 /* Cf. IETF RFC 5101 Section 3.1. */
86 ovs_be16 version; /* IPFIX_VERSION. */
87 ovs_be16 length; /* Length in bytes including this header. */
88 ovs_be32 export_time; /* Seconds since the epoch. */
89 ovs_be32 seq_number; /* Message sequence number. */
90 ovs_be32 obs_domain_id; /* Observation Domain ID. */
92 BUILD_ASSERT_DECL(sizeof(struct ipfix_header) == 16);
94 #define IPFIX_SET_ID_TEMPLATE 2
95 #define IPFIX_SET_ID_OPTION_TEMPLATE 3
97 /* Cf. IETF RFC 5101 Section 3.3.2. */
99 struct ipfix_set_header {
100 ovs_be16 set_id; /* IPFIX_SET_ID_* or valid template ID for Data Sets. */
101 ovs_be16 length; /* Length of the set in bytes including header. */
103 BUILD_ASSERT_DECL(sizeof(struct ipfix_set_header) == 4);
105 /* Alternatives for templates at each layer. A template is defined by
106 * a combination of one value for each layer. */
107 enum ipfix_proto_l2 {
108 IPFIX_PROTO_L2_ETH = 0, /* No VLAN. */
112 enum ipfix_proto_l3 {
113 IPFIX_PROTO_L3_UNKNOWN = 0,
118 enum ipfix_proto_l4 {
119 IPFIX_PROTO_L4_UNKNOWN = 0,
120 IPFIX_PROTO_L4_TCP_UDP_SCTP,
125 /* Any Template ID > 255 is usable for Template Records. */
126 #define IPFIX_TEMPLATE_ID_MIN 256
128 /* Cf. IETF RFC 5101 Section 3.4.1. */
130 struct ipfix_template_record_header {
131 ovs_be16 template_id;
132 ovs_be16 field_count;
134 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_record_header) == 4);
136 enum ipfix_entity_id {
137 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ID_##ENUM = ID,
138 #include "ofproto/ipfix-entities.def"
141 enum ipfix_entity_size {
142 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_SIZE_##ENUM = SIZE,
143 #include "ofproto/ipfix-entities.def"
147 struct ipfix_template_field_specifier {
148 ovs_be16 element_id; /* IPFIX_ENTITY_ID_*. */
149 ovs_be16 field_length; /* Length of the field's value, in bytes. */
150 /* No Enterprise ID, since only standard element IDs are specified. */
152 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_field_specifier) == 4);
154 /* Part of data record flow key for common metadata and Ethernet entities. */
156 struct ipfix_data_record_flow_key_common {
157 ovs_be32 observation_point_id; /* OBSERVATION_POINT_ID */
158 uint8_t source_mac_address[6]; /* SOURCE_MAC_ADDRESS */
159 uint8_t destination_mac_address[6]; /* DESTINATION_MAC_ADDRESS */
160 ovs_be16 ethernet_type; /* ETHERNET_TYPE */
161 uint8_t ethernet_header_length; /* ETHERNET_HEADER_LENGTH */
163 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_common) == 19);
165 /* Part of data record flow key for VLAN entities. */
167 struct ipfix_data_record_flow_key_vlan {
168 ovs_be16 vlan_id; /* VLAN_ID */
169 ovs_be16 dot1q_vlan_id; /* DOT1Q_VLAN_ID */
170 uint8_t dot1q_priority; /* DOT1Q_PRIORITY */
172 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_vlan) == 5);
174 /* Part of data record flow key for IP entities. */
175 /* XXX: Replace IP_TTL with MINIMUM_TTL and MAXIMUM_TTL? */
177 struct ipfix_data_record_flow_key_ip {
178 uint8_t ip_version; /* IP_VERSION */
179 uint8_t ip_ttl; /* IP_TTL */
180 uint8_t protocol_identifier; /* PROTOCOL_IDENTIFIER */
181 uint8_t ip_diff_serv_code_point; /* IP_DIFF_SERV_CODE_POINT */
182 uint8_t ip_precedence; /* IP_PRECEDENCE */
183 uint8_t ip_class_of_service; /* IP_CLASS_OF_SERVICE */
185 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ip) == 6);
187 /* Part of data record flow key for IPv4 entities. */
189 struct ipfix_data_record_flow_key_ipv4 {
190 ovs_be32 source_ipv4_address; /* SOURCE_IPV4_ADDRESS */
191 ovs_be32 destination_ipv4_address; /* DESTINATION_IPV4_ADDRESS */
193 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv4) == 8);
195 /* Part of data record flow key for IPv6 entities. */
197 struct ipfix_data_record_flow_key_ipv6 {
198 uint8_t source_ipv6_address[16]; /* SOURCE_IPV6_ADDRESS */
199 uint8_t destination_ipv6_address[16]; /* DESTINATION_IPV6_ADDRESS */
200 ovs_be32 flow_label_ipv6; /* FLOW_LABEL_IPV6 */
202 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv6) == 36);
204 /* Part of data record flow key for TCP/UDP/SCTP entities. */
206 struct ipfix_data_record_flow_key_transport {
207 ovs_be16 source_transport_port; /* SOURCE_TRANSPORT_PORT */
208 ovs_be16 destination_transport_port; /* DESTINATION_TRANSPORT_PORT */
210 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_transport) == 4);
212 /* Part of data record flow key for ICMP entities. */
214 struct ipfix_data_record_flow_key_icmp {
215 uint8_t icmp_type; /* ICMP_TYPE_IPV4 / ICMP_TYPE_IPV6 */
216 uint8_t icmp_code; /* ICMP_CODE_IPV4 / ICMP_CODE_IPV6 */
218 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_icmp) == 2);
220 /* Cf. IETF RFC 5102 Section 5.11.3. */
221 enum ipfix_flow_end_reason {
223 ACTIVE_TIMEOUT = 0x02,
224 END_OF_FLOW_DETECTED = 0x03,
226 LACK_OF_RESOURCES = 0x05
229 /* Part of data record for common aggregated elements. */
231 struct ipfix_data_record_aggregated_common {
232 ovs_be32 flow_start_delta_microseconds; /* FLOW_START_DELTA_MICROSECONDS */
233 ovs_be32 flow_end_delta_microseconds; /* FLOW_END_DELTA_MICROSECONDS */
234 ovs_be64 packet_delta_count; /* PACKET_DELTA_COUNT */
235 ovs_be64 layer2_octet_delta_count; /* LAYER2_OCTET_DELTA_COUNT */
236 uint8_t flow_end_reason; /* FLOW_END_REASON */
238 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_common) == 25);
240 /* Part of data record for IP aggregated elements. */
242 struct ipfix_data_record_aggregated_ip {
243 ovs_be64 octet_delta_count; /* OCTET_DELTA_COUNT */
244 ovs_be64 octet_delta_sum_of_squares; /* OCTET_DELTA_SUM_OF_SQUARES */
245 ovs_be64 minimum_ip_total_length; /* MINIMUM_IP_TOTAL_LENGTH */
246 ovs_be64 maximum_ip_total_length; /* MAXIMUM_IP_TOTAL_LENGTH */
248 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_ip) == 32);
250 #define MAX_FLOW_KEY_LEN \
251 (sizeof(struct ipfix_data_record_flow_key_common) \
252 + sizeof(struct ipfix_data_record_flow_key_vlan) \
253 + sizeof(struct ipfix_data_record_flow_key_ip) \
254 + MAX(sizeof(struct ipfix_data_record_flow_key_ipv4), \
255 sizeof(struct ipfix_data_record_flow_key_ipv6)) \
256 + MAX(sizeof(struct ipfix_data_record_flow_key_icmp), \
257 sizeof(struct ipfix_data_record_flow_key_transport)))
259 #define MAX_DATA_RECORD_LEN \
261 + sizeof(struct ipfix_data_record_aggregated_common) \
262 + sizeof(struct ipfix_data_record_aggregated_ip))
264 /* Max length of a data set. To simplify the implementation, each
265 * data record is sent in a separate data set, so each data set
266 * contains at most one data record. */
267 #define MAX_DATA_SET_LEN \
268 (sizeof(struct ipfix_set_header) \
269 + MAX_DATA_RECORD_LEN)
271 /* Max length of an IPFIX message. Arbitrarily set to accomodate low
273 #define MAX_MESSAGE_LEN 1024
275 /* Cache structures. */
278 struct ipfix_flow_key {
279 uint32_t obs_domain_id;
280 uint16_t template_id;
281 size_t flow_key_msg_part_size;
282 uint64_t flow_key_msg_part[DIV_ROUND_UP(MAX_FLOW_KEY_LEN, 8)];
285 /* Flow cache entry. */
286 struct ipfix_flow_cache_entry {
287 struct hmap_node flow_key_map_node;
288 struct list cache_flow_start_timestamp_list_node;
289 struct ipfix_flow_key flow_key;
290 /* Common aggregated elements. */
291 uint64_t flow_start_timestamp_usec;
292 uint64_t flow_end_timestamp_usec;
293 uint64_t packet_delta_count;
294 uint64_t layer2_octet_delta_count;
295 uint64_t octet_delta_count;
296 uint64_t octet_delta_sum_of_squares; /* 0 if not IP. */
297 uint16_t minimum_ip_total_length; /* 0 if not IP. */
298 uint16_t maximum_ip_total_length; /* 0 if not IP. */
301 static void dpif_ipfix_cache_expire(struct dpif_ipfix_exporter *, bool,
302 const uint64_t, const uint32_t);
304 static void get_export_time_now(uint64_t *, uint32_t *);
306 static void dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter *, bool);
309 ofproto_ipfix_bridge_exporter_options_equal(
310 const struct ofproto_ipfix_bridge_exporter_options *a,
311 const struct ofproto_ipfix_bridge_exporter_options *b)
313 return (a->obs_domain_id == b->obs_domain_id
314 && a->obs_point_id == b->obs_point_id
315 && a->sampling_rate == b->sampling_rate
316 && a->cache_active_timeout == b->cache_active_timeout
317 && a->cache_max_flows == b->cache_max_flows
318 && sset_equals(&a->targets, &b->targets));
321 static struct ofproto_ipfix_bridge_exporter_options *
322 ofproto_ipfix_bridge_exporter_options_clone(
323 const struct ofproto_ipfix_bridge_exporter_options *old)
325 struct ofproto_ipfix_bridge_exporter_options *new =
326 xmemdup(old, sizeof *old);
327 sset_clone(&new->targets, &old->targets);
332 ofproto_ipfix_bridge_exporter_options_destroy(
333 struct ofproto_ipfix_bridge_exporter_options *options)
336 sset_destroy(&options->targets);
342 ofproto_ipfix_flow_exporter_options_equal(
343 const struct ofproto_ipfix_flow_exporter_options *a,
344 const struct ofproto_ipfix_flow_exporter_options *b)
346 return (a->collector_set_id == b->collector_set_id
347 && a->cache_active_timeout == b->cache_active_timeout
348 && a->cache_max_flows == b->cache_max_flows
349 && sset_equals(&a->targets, &b->targets));
352 static struct ofproto_ipfix_flow_exporter_options *
353 ofproto_ipfix_flow_exporter_options_clone(
354 const struct ofproto_ipfix_flow_exporter_options *old)
356 struct ofproto_ipfix_flow_exporter_options *new =
357 xmemdup(old, sizeof *old);
358 sset_clone(&new->targets, &old->targets);
363 ofproto_ipfix_flow_exporter_options_destroy(
364 struct ofproto_ipfix_flow_exporter_options *options)
367 sset_destroy(&options->targets);
373 dpif_ipfix_exporter_init(struct dpif_ipfix_exporter *exporter)
375 exporter->collectors = NULL;
376 exporter->seq_number = 1;
377 exporter->last_template_set_time = TIME_MIN;
378 hmap_init(&exporter->cache_flow_key_map);
379 list_init(&exporter->cache_flow_start_timestamp_list);
380 exporter->cache_active_timeout = 0;
381 exporter->cache_max_flows = 0;
385 dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
387 /* Flush the cache with flow end reason "forced end." */
388 dpif_ipfix_cache_expire_now(exporter, true);
390 collectors_destroy(exporter->collectors);
391 exporter->collectors = NULL;
392 exporter->seq_number = 1;
393 exporter->last_template_set_time = TIME_MIN;
394 exporter->cache_active_timeout = 0;
395 exporter->cache_max_flows = 0;
399 dpif_ipfix_exporter_destroy(struct dpif_ipfix_exporter *exporter)
401 dpif_ipfix_exporter_clear(exporter);
402 hmap_destroy(&exporter->cache_flow_key_map);
406 dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter *exporter,
407 const struct sset *targets,
408 const uint32_t cache_active_timeout,
409 const uint32_t cache_max_flows)
411 collectors_destroy(exporter->collectors);
412 collectors_create(targets, IPFIX_DEFAULT_COLLECTOR_PORT,
413 &exporter->collectors);
414 if (exporter->collectors == NULL) {
415 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
416 "IPFIX exporter disabled");
417 dpif_ipfix_exporter_clear(exporter);
420 exporter->cache_active_timeout = cache_active_timeout;
421 exporter->cache_max_flows = cache_max_flows;
426 dpif_ipfix_bridge_exporter_init(struct dpif_ipfix_bridge_exporter *exporter)
428 dpif_ipfix_exporter_init(&exporter->exporter);
429 exporter->options = NULL;
430 exporter->probability = 0;
434 dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
436 dpif_ipfix_exporter_clear(&exporter->exporter);
437 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
438 exporter->options = NULL;
439 exporter->probability = 0;
443 dpif_ipfix_bridge_exporter_destroy(struct dpif_ipfix_bridge_exporter *exporter)
445 dpif_ipfix_bridge_exporter_clear(exporter);
446 dpif_ipfix_exporter_destroy(&exporter->exporter);
450 dpif_ipfix_bridge_exporter_set_options(
451 struct dpif_ipfix_bridge_exporter *exporter,
452 const struct ofproto_ipfix_bridge_exporter_options *options)
454 bool options_changed;
456 if (!options || sset_is_empty(&options->targets)) {
457 /* No point in doing any work if there are no targets. */
458 dpif_ipfix_bridge_exporter_clear(exporter);
464 || !ofproto_ipfix_bridge_exporter_options_equal(
465 options, exporter->options));
467 /* Configure collectors if options have changed or if we're
468 * shortchanged in collectors (which indicates that opening one or
469 * more of the configured collectors failed, so that we should
472 || collectors_count(exporter->exporter.collectors)
473 < sset_count(&options->targets)) {
474 if (!dpif_ipfix_exporter_set_options(
475 &exporter->exporter, &options->targets,
476 options->cache_active_timeout, options->cache_max_flows)) {
481 /* Avoid reconfiguring if options didn't change. */
482 if (!options_changed) {
486 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
487 exporter->options = ofproto_ipfix_bridge_exporter_options_clone(options);
488 exporter->probability =
489 MAX(1, UINT32_MAX / exporter->options->sampling_rate);
491 /* Run over the cache as some entries might have expired after
492 * changing the timeouts. */
493 dpif_ipfix_cache_expire_now(&exporter->exporter, false);
496 static struct dpif_ipfix_flow_exporter_map_node*
497 dpif_ipfix_find_flow_exporter_map_node(
498 const struct dpif_ipfix *di, const uint32_t collector_set_id)
501 struct dpif_ipfix_flow_exporter_map_node *exporter_node;
503 HMAP_FOR_EACH_WITH_HASH (exporter_node, node,
504 hash_int(collector_set_id, 0),
505 &di->flow_exporter_map) {
506 if (exporter_node->exporter.options->collector_set_id
507 == collector_set_id) {
508 return exporter_node;
516 dpif_ipfix_flow_exporter_init(struct dpif_ipfix_flow_exporter *exporter)
518 dpif_ipfix_exporter_init(&exporter->exporter);
519 exporter->options = NULL;
523 dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
525 dpif_ipfix_exporter_clear(&exporter->exporter);
526 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
527 exporter->options = NULL;
531 dpif_ipfix_flow_exporter_destroy(struct dpif_ipfix_flow_exporter *exporter)
533 dpif_ipfix_flow_exporter_clear(exporter);
534 dpif_ipfix_exporter_destroy(&exporter->exporter);
538 dpif_ipfix_flow_exporter_set_options(
539 struct dpif_ipfix_flow_exporter *exporter,
540 const struct ofproto_ipfix_flow_exporter_options *options)
542 bool options_changed;
544 if (sset_is_empty(&options->targets)) {
545 /* No point in doing any work if there are no targets. */
546 dpif_ipfix_flow_exporter_clear(exporter);
552 || !ofproto_ipfix_flow_exporter_options_equal(
553 options, exporter->options));
555 /* Configure collectors if options have changed or if we're
556 * shortchanged in collectors (which indicates that opening one or
557 * more of the configured collectors failed, so that we should
560 || collectors_count(exporter->exporter.collectors)
561 < sset_count(&options->targets)) {
562 if (!dpif_ipfix_exporter_set_options(
563 &exporter->exporter, &options->targets,
564 options->cache_active_timeout, options->cache_max_flows)) {
569 /* Avoid reconfiguring if options didn't change. */
570 if (!options_changed) {
574 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
575 exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
577 /* Run over the cache as some entries might have expired after
578 * changing the timeouts. */
579 dpif_ipfix_cache_expire_now(&exporter->exporter, false);
585 dpif_ipfix_set_options(
586 struct dpif_ipfix *di,
587 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
588 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
589 size_t n_flow_exporters_options) OVS_EXCLUDED(mutex)
592 struct ofproto_ipfix_flow_exporter_options *options;
593 struct dpif_ipfix_flow_exporter_map_node *node, *next;
594 size_t n_broken_flow_exporters_options = 0;
596 ovs_mutex_lock(&mutex);
597 dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
598 bridge_exporter_options);
600 /* Add new flow exporters and update current flow exporters. */
601 options = (struct ofproto_ipfix_flow_exporter_options *)
602 flow_exporters_options;
603 for (i = 0; i < n_flow_exporters_options; i++) {
604 node = dpif_ipfix_find_flow_exporter_map_node(
605 di, options->collector_set_id);
607 node = xzalloc(sizeof *node);
608 dpif_ipfix_flow_exporter_init(&node->exporter);
609 hmap_insert(&di->flow_exporter_map, &node->node,
610 hash_int(options->collector_set_id, 0));
612 if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
613 n_broken_flow_exporters_options++;
618 ovs_assert(hmap_count(&di->flow_exporter_map) >=
619 (n_flow_exporters_options - n_broken_flow_exporters_options));
621 /* Remove dropped flow exporters, if any needs to be removed. */
622 if (hmap_count(&di->flow_exporter_map) > n_flow_exporters_options) {
623 HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
624 /* This is slow but doesn't take any extra memory, and
625 * this table is not supposed to contain many rows anyway. */
626 options = (struct ofproto_ipfix_flow_exporter_options *)
627 flow_exporters_options;
628 for (i = 0; i < n_flow_exporters_options; i++) {
629 if (node->exporter.options->collector_set_id
630 == options->collector_set_id) {
635 if (i == n_flow_exporters_options) { // Not found.
636 hmap_remove(&di->flow_exporter_map, &node->node);
637 dpif_ipfix_flow_exporter_destroy(&node->exporter);
643 ovs_assert(hmap_count(&di->flow_exporter_map) ==
644 (n_flow_exporters_options - n_broken_flow_exporters_options));
645 ovs_mutex_unlock(&mutex);
649 dpif_ipfix_create(void)
651 struct dpif_ipfix *di;
652 di = xzalloc(sizeof *di);
653 dpif_ipfix_bridge_exporter_init(&di->bridge_exporter);
654 hmap_init(&di->flow_exporter_map);
655 ovs_refcount_init(&di->ref_cnt);
660 dpif_ipfix_ref(const struct dpif_ipfix *di_)
662 struct dpif_ipfix *di = CONST_CAST(struct dpif_ipfix *, di_);
664 ovs_refcount_ref(&di->ref_cnt);
670 dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
674 ovs_mutex_lock(&mutex);
675 ret = di->bridge_exporter.probability;
676 ovs_mutex_unlock(&mutex);
681 dpif_ipfix_clear(struct dpif_ipfix *di) OVS_REQUIRES(mutex)
683 struct dpif_ipfix_flow_exporter_map_node *exp_node, *exp_next;
685 dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
687 HMAP_FOR_EACH_SAFE (exp_node, exp_next, node, &di->flow_exporter_map) {
688 hmap_remove(&di->flow_exporter_map, &exp_node->node);
689 dpif_ipfix_flow_exporter_destroy(&exp_node->exporter);
695 dpif_ipfix_unref(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
697 if (di && ovs_refcount_unref(&di->ref_cnt) == 1) {
698 ovs_mutex_lock(&mutex);
699 dpif_ipfix_clear(di);
700 dpif_ipfix_bridge_exporter_destroy(&di->bridge_exporter);
701 hmap_destroy(&di->flow_exporter_map);
703 ovs_mutex_unlock(&mutex);
708 ipfix_init_header(uint32_t export_time_sec, uint32_t seq_number,
709 uint32_t obs_domain_id, struct ofpbuf *msg)
711 struct ipfix_header *hdr;
713 hdr = ofpbuf_put_zeros(msg, sizeof *hdr);
714 hdr->version = htons(IPFIX_VERSION);
715 hdr->length = htons(sizeof *hdr); /* Updated in ipfix_send_msg. */
716 hdr->export_time = htonl(export_time_sec);
717 hdr->seq_number = htonl(seq_number);
718 hdr->obs_domain_id = htonl(obs_domain_id);
722 ipfix_send_msg(const struct collectors *collectors, struct ofpbuf *msg)
724 struct ipfix_header *hdr;
726 /* Adjust the length in the header. */
727 hdr = ofpbuf_data(msg);
728 hdr->length = htons(ofpbuf_size(msg));
730 collectors_send(collectors, ofpbuf_data(msg), ofpbuf_size(msg));
731 ofpbuf_set_size(msg, 0);
735 ipfix_get_template_id(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
736 enum ipfix_proto_l4 l4)
738 uint16_t template_id;
740 template_id = template_id * NUM_IPFIX_PROTO_L3 + l3;
741 template_id = template_id * NUM_IPFIX_PROTO_L4 + l4;
742 return IPFIX_TEMPLATE_ID_MIN + template_id;
746 ipfix_define_template_entity(enum ipfix_entity_id id,
747 enum ipfix_entity_size size, struct ofpbuf *msg)
749 struct ipfix_template_field_specifier *field;
751 field = ofpbuf_put_zeros(msg, sizeof *field);
752 field->element_id = htons(id);
753 field->field_length = htons(size);
757 ipfix_define_template_fields(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
758 enum ipfix_proto_l4 l4, struct ofpbuf *msg)
764 ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
765 IPFIX_ENTITY_SIZE_##ID, msg); \
771 DEF(OBSERVATION_POINT_ID);
773 /* Common Ethernet entities. */
774 DEF(SOURCE_MAC_ADDRESS);
775 DEF(DESTINATION_MAC_ADDRESS);
777 DEF(ETHERNET_HEADER_LENGTH);
779 if (l2 == IPFIX_PROTO_L2_VLAN) {
785 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
788 DEF(PROTOCOL_IDENTIFIER);
789 DEF(IP_DIFF_SERV_CODE_POINT);
791 DEF(IP_CLASS_OF_SERVICE);
793 if (l3 == IPFIX_PROTO_L3_IPV4) {
794 DEF(SOURCE_IPV4_ADDRESS);
795 DEF(DESTINATION_IPV4_ADDRESS);
796 if (l4 == IPFIX_PROTO_L4_TCP_UDP_SCTP) {
797 DEF(SOURCE_TRANSPORT_PORT);
798 DEF(DESTINATION_TRANSPORT_PORT);
799 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
803 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
804 DEF(SOURCE_IPV6_ADDRESS);
805 DEF(DESTINATION_IPV6_ADDRESS);
806 DEF(FLOW_LABEL_IPV6);
807 if (l4 == IPFIX_PROTO_L4_TCP_UDP_SCTP) {
808 DEF(SOURCE_TRANSPORT_PORT);
809 DEF(DESTINATION_TRANSPORT_PORT);
810 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
817 /* 2. Flow aggregated data. */
819 DEF(FLOW_START_DELTA_MICROSECONDS);
820 DEF(FLOW_END_DELTA_MICROSECONDS);
821 DEF(PACKET_DELTA_COUNT);
822 DEF(LAYER2_OCTET_DELTA_COUNT);
823 DEF(FLOW_END_REASON);
825 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
826 DEF(OCTET_DELTA_COUNT);
827 DEF(OCTET_DELTA_SUM_OF_SQUARES);
828 DEF(MINIMUM_IP_TOTAL_LENGTH);
829 DEF(MAXIMUM_IP_TOTAL_LENGTH);
838 ipfix_send_template_msg(struct dpif_ipfix_exporter *exporter,
839 uint32_t export_time_sec, uint32_t obs_domain_id)
841 uint64_t msg_stub[DIV_ROUND_UP(MAX_MESSAGE_LEN, 8)];
843 size_t set_hdr_offset, tmpl_hdr_offset;
844 struct ipfix_set_header *set_hdr;
845 struct ipfix_template_record_header *tmpl_hdr;
846 uint16_t field_count;
847 enum ipfix_proto_l2 l2;
848 enum ipfix_proto_l3 l3;
849 enum ipfix_proto_l4 l4;
851 ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
853 ipfix_init_header(export_time_sec, exporter->seq_number, obs_domain_id,
855 set_hdr_offset = ofpbuf_size(&msg);
857 /* Add a Template Set. */
858 set_hdr = ofpbuf_put_zeros(&msg, sizeof *set_hdr);
859 set_hdr->set_id = htons(IPFIX_SET_ID_TEMPLATE);
861 /* Define one template for each possible combination of
863 for (l2 = 0; l2 < NUM_IPFIX_PROTO_L2; l2++) {
864 for (l3 = 0; l3 < NUM_IPFIX_PROTO_L3; l3++) {
865 for (l4 = 0; l4 < NUM_IPFIX_PROTO_L4; l4++) {
866 if (l3 == IPFIX_PROTO_L3_UNKNOWN &&
867 l4 != IPFIX_PROTO_L4_UNKNOWN) {
870 tmpl_hdr_offset = ofpbuf_size(&msg);
871 tmpl_hdr = ofpbuf_put_zeros(&msg, sizeof *tmpl_hdr);
872 tmpl_hdr->template_id = htons(
873 ipfix_get_template_id(l2, l3, l4));
874 field_count = ipfix_define_template_fields(l2, l3, l4, &msg);
875 tmpl_hdr = (struct ipfix_template_record_header*)
876 ((uint8_t*)ofpbuf_data(&msg) + tmpl_hdr_offset);
877 tmpl_hdr->field_count = htons(field_count);
882 set_hdr = (struct ipfix_set_header*)((uint8_t*)ofpbuf_data(&msg) + set_hdr_offset);
883 set_hdr->length = htons(ofpbuf_size(&msg) - set_hdr_offset);
885 /* XXX: Add Options Template Sets, at least to define a Flow Keys
886 * Option Template. */
888 ipfix_send_msg(exporter->collectors, &msg);
893 static inline uint32_t
894 ipfix_hash_flow_key(const struct ipfix_flow_key *flow_key, uint32_t basis)
897 hash = hash_int(flow_key->obs_domain_id, basis);
898 hash = hash_int(flow_key->template_id, hash);
899 hash = hash_bytes(flow_key->flow_key_msg_part,
900 flow_key->flow_key_msg_part_size, hash);
905 ipfix_flow_key_equal(const struct ipfix_flow_key *a,
906 const struct ipfix_flow_key *b)
908 /* The template ID determines the flow key size, so not need to
910 return (a->obs_domain_id == b->obs_domain_id
911 && a->template_id == b->template_id
912 && memcmp(a->flow_key_msg_part, b->flow_key_msg_part,
913 a->flow_key_msg_part_size) == 0);
916 static struct ipfix_flow_cache_entry*
917 ipfix_cache_find_entry(const struct dpif_ipfix_exporter *exporter,
918 const struct ipfix_flow_key *flow_key)
920 struct ipfix_flow_cache_entry *entry;
922 HMAP_FOR_EACH_WITH_HASH (entry, flow_key_map_node,
923 ipfix_hash_flow_key(flow_key, 0),
924 &exporter->cache_flow_key_map) {
925 if (ipfix_flow_key_equal(&entry->flow_key, flow_key)) {
934 ipfix_cache_next_timeout_msec(const struct dpif_ipfix_exporter *exporter,
935 long long int *next_timeout_msec)
937 struct ipfix_flow_cache_entry *entry;
939 LIST_FOR_EACH (entry, cache_flow_start_timestamp_list_node,
940 &exporter->cache_flow_start_timestamp_list) {
941 *next_timeout_msec = entry->flow_start_timestamp_usec / 1000LL
942 + 1000LL * exporter->cache_active_timeout;
950 ipfix_cache_aggregate_entries(struct ipfix_flow_cache_entry *from_entry,
951 struct ipfix_flow_cache_entry *to_entry)
953 uint64_t *to_start, *to_end, *from_start, *from_end;
954 uint16_t *to_min_len, *to_max_len, *from_min_len, *from_max_len;
956 to_start = &to_entry->flow_start_timestamp_usec;
957 to_end = &to_entry->flow_end_timestamp_usec;
958 from_start = &from_entry->flow_start_timestamp_usec;
959 from_end = &from_entry->flow_end_timestamp_usec;
961 if (*to_start > *from_start) {
962 *to_start = *from_start;
964 if (*to_end < *from_end) {
968 to_entry->packet_delta_count += from_entry->packet_delta_count;
969 to_entry->layer2_octet_delta_count += from_entry->layer2_octet_delta_count;
971 to_entry->octet_delta_count += from_entry->octet_delta_count;
972 to_entry->octet_delta_sum_of_squares +=
973 from_entry->octet_delta_sum_of_squares;
975 to_min_len = &to_entry->minimum_ip_total_length;
976 to_max_len = &to_entry->maximum_ip_total_length;
977 from_min_len = &from_entry->minimum_ip_total_length;
978 from_max_len = &from_entry->maximum_ip_total_length;
980 if (!*to_min_len || (*from_min_len && *to_min_len > *from_min_len)) {
981 *to_min_len = *from_min_len;
983 if (*to_max_len < *from_max_len) {
984 *to_max_len = *from_max_len;
988 /* Add an entry into a flow cache. The entry is either aggregated into
989 * an existing entry with the same flow key and free()d, or it is
990 * inserted into the cache. */
992 ipfix_cache_update(struct dpif_ipfix_exporter *exporter,
993 struct ipfix_flow_cache_entry *entry)
995 struct ipfix_flow_cache_entry *old_entry;
997 old_entry = ipfix_cache_find_entry(exporter, &entry->flow_key);
999 if (old_entry == NULL) {
1000 hmap_insert(&exporter->cache_flow_key_map, &entry->flow_key_map_node,
1001 ipfix_hash_flow_key(&entry->flow_key, 0));
1003 /* As the latest entry added into the cache, it should
1004 * logically have the highest flow_start_timestamp_usec, so
1005 * append it at the tail. */
1006 list_push_back(&exporter->cache_flow_start_timestamp_list,
1007 &entry->cache_flow_start_timestamp_list_node);
1009 /* Enforce exporter->cache_max_flows limit. */
1010 if (hmap_count(&exporter->cache_flow_key_map)
1011 > exporter->cache_max_flows) {
1012 dpif_ipfix_cache_expire_now(exporter, false);
1015 ipfix_cache_aggregate_entries(entry, old_entry);
1021 ipfix_cache_entry_init(struct ipfix_flow_cache_entry *entry,
1022 struct ofpbuf *packet, const struct flow *flow,
1023 uint64_t packet_delta_count, uint32_t obs_domain_id,
1024 uint32_t obs_point_id)
1026 struct ipfix_flow_key *flow_key;
1028 enum ipfix_proto_l2 l2;
1029 enum ipfix_proto_l3 l3;
1030 enum ipfix_proto_l4 l4;
1031 uint8_t ethernet_header_length;
1032 uint16_t ethernet_total_length;
1034 flow_key = &entry->flow_key;
1035 ofpbuf_use_stack(&msg, flow_key->flow_key_msg_part,
1036 sizeof flow_key->flow_key_msg_part);
1038 /* Choose the right template ID matching the protocols in the
1039 * sampled packet. */
1040 l2 = (flow->vlan_tci == 0) ? IPFIX_PROTO_L2_ETH : IPFIX_PROTO_L2_VLAN;
1042 switch(ntohs(flow->dl_type)) {
1044 l3 = IPFIX_PROTO_L3_IPV4;
1045 switch(flow->nw_proto) {
1049 l4 = IPFIX_PROTO_L4_TCP_UDP_SCTP;
1052 l4 = IPFIX_PROTO_L4_ICMP;
1055 l4 = IPFIX_PROTO_L4_UNKNOWN;
1059 l3 = IPFIX_PROTO_L3_IPV6;
1060 switch(flow->nw_proto) {
1064 l4 = IPFIX_PROTO_L4_TCP_UDP_SCTP;
1066 case IPPROTO_ICMPV6:
1067 l4 = IPFIX_PROTO_L4_ICMP;
1070 l4 = IPFIX_PROTO_L4_UNKNOWN;
1074 l3 = IPFIX_PROTO_L3_UNKNOWN;
1075 l4 = IPFIX_PROTO_L4_UNKNOWN;
1078 flow_key->obs_domain_id = obs_domain_id;
1079 flow_key->template_id = ipfix_get_template_id(l2, l3, l4);
1081 /* The fields defined in the ipfix_data_record_* structs and sent
1082 * below must match exactly the templates defined in
1083 * ipfix_define_template_fields. */
1085 ethernet_header_length = (l2 == IPFIX_PROTO_L2_VLAN)
1086 ? VLAN_ETH_HEADER_LEN : ETH_HEADER_LEN;
1087 ethernet_total_length = ofpbuf_size(packet);
1089 /* Common Ethernet entities. */
1091 struct ipfix_data_record_flow_key_common *data_common;
1093 data_common = ofpbuf_put_zeros(&msg, sizeof *data_common);
1094 data_common->observation_point_id = htonl(obs_point_id);
1095 memcpy(data_common->source_mac_address, flow->dl_src,
1096 sizeof flow->dl_src);
1097 memcpy(data_common->destination_mac_address, flow->dl_dst,
1098 sizeof flow->dl_dst);
1099 data_common->ethernet_type = flow->dl_type;
1100 data_common->ethernet_header_length = ethernet_header_length;
1103 if (l2 == IPFIX_PROTO_L2_VLAN) {
1104 struct ipfix_data_record_flow_key_vlan *data_vlan;
1105 uint16_t vlan_id = vlan_tci_to_vid(flow->vlan_tci);
1106 uint8_t priority = vlan_tci_to_pcp(flow->vlan_tci);
1108 data_vlan = ofpbuf_put_zeros(&msg, sizeof *data_vlan);
1109 data_vlan->vlan_id = htons(vlan_id);
1110 data_vlan->dot1q_vlan_id = htons(vlan_id);
1111 data_vlan->dot1q_priority = priority;
1114 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1115 struct ipfix_data_record_flow_key_ip *data_ip;
1117 data_ip = ofpbuf_put_zeros(&msg, sizeof *data_ip);
1118 data_ip->ip_version = (l3 == IPFIX_PROTO_L3_IPV4) ? 4 : 6;
1119 data_ip->ip_ttl = flow->nw_ttl;
1120 data_ip->protocol_identifier = flow->nw_proto;
1121 data_ip->ip_diff_serv_code_point = flow->nw_tos >> 2;
1122 data_ip->ip_precedence = flow->nw_tos >> 5;
1123 data_ip->ip_class_of_service = flow->nw_tos;
1125 if (l3 == IPFIX_PROTO_L3_IPV4) {
1126 struct ipfix_data_record_flow_key_ipv4 *data_ipv4;
1128 data_ipv4 = ofpbuf_put_zeros(&msg, sizeof *data_ipv4);
1129 data_ipv4->source_ipv4_address = flow->nw_src;
1130 data_ipv4->destination_ipv4_address = flow->nw_dst;
1131 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
1132 struct ipfix_data_record_flow_key_ipv6 *data_ipv6;
1134 data_ipv6 = ofpbuf_put_zeros(&msg, sizeof *data_ipv6);
1135 memcpy(data_ipv6->source_ipv6_address, &flow->ipv6_src,
1136 sizeof flow->ipv6_src);
1137 memcpy(data_ipv6->destination_ipv6_address, &flow->ipv6_dst,
1138 sizeof flow->ipv6_dst);
1139 data_ipv6->flow_label_ipv6 = flow->ipv6_label;
1143 if (l4 == IPFIX_PROTO_L4_TCP_UDP_SCTP) {
1144 struct ipfix_data_record_flow_key_transport *data_transport;
1146 data_transport = ofpbuf_put_zeros(&msg, sizeof *data_transport);
1147 data_transport->source_transport_port = flow->tp_src;
1148 data_transport->destination_transport_port = flow->tp_dst;
1149 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
1150 struct ipfix_data_record_flow_key_icmp *data_icmp;
1152 data_icmp = ofpbuf_put_zeros(&msg, sizeof *data_icmp);
1153 data_icmp->icmp_type = ntohs(flow->tp_src) & 0xff;
1154 data_icmp->icmp_code = ntohs(flow->tp_dst) & 0xff;
1157 flow_key->flow_key_msg_part_size = ofpbuf_size(&msg);
1161 uint64_t layer2_octet_delta_count;
1163 /* Calculate the total matched octet count by considering as
1164 * an approximation that all matched packets have the same
1166 layer2_octet_delta_count = packet_delta_count * ethernet_total_length;
1168 xgettimeofday(&now);
1169 entry->flow_end_timestamp_usec = now.tv_usec + 1000000LL * now.tv_sec;
1170 entry->flow_start_timestamp_usec = entry->flow_end_timestamp_usec;
1171 entry->packet_delta_count = packet_delta_count;
1172 entry->layer2_octet_delta_count = layer2_octet_delta_count;
1175 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1176 uint16_t ip_total_length =
1177 ethernet_total_length - ethernet_header_length;
1178 uint64_t octet_delta_count;
1180 /* Calculate the total matched octet count by considering as
1181 * an approximation that all matched packets have the same
1183 octet_delta_count = packet_delta_count * ip_total_length;
1185 entry->octet_delta_count = octet_delta_count;
1186 entry->octet_delta_sum_of_squares = octet_delta_count * ip_total_length;
1187 entry->minimum_ip_total_length = ip_total_length;
1188 entry->maximum_ip_total_length = ip_total_length;
1190 entry->octet_delta_sum_of_squares = 0;
1191 entry->minimum_ip_total_length = 0;
1192 entry->maximum_ip_total_length = 0;
1196 /* Send each single data record in its own data set, to simplify the
1197 * implementation by avoiding having to group record by template ID
1198 * before sending. */
1200 ipfix_put_data_set(uint32_t export_time_sec,
1201 struct ipfix_flow_cache_entry *entry,
1202 enum ipfix_flow_end_reason flow_end_reason,
1205 size_t set_hdr_offset;
1206 struct ipfix_set_header *set_hdr;
1208 set_hdr_offset = ofpbuf_size(msg);
1210 /* Put a Data Set. */
1211 set_hdr = ofpbuf_put_zeros(msg, sizeof *set_hdr);
1212 set_hdr->set_id = htons(entry->flow_key.template_id);
1214 /* Copy the flow key part of the data record. */
1216 ofpbuf_put(msg, entry->flow_key.flow_key_msg_part,
1217 entry->flow_key.flow_key_msg_part_size);
1219 /* Put the non-key part of the data record. */
1222 struct ipfix_data_record_aggregated_common *data_aggregated_common;
1223 uint64_t export_time_usec, flow_start_delta_usec, flow_end_delta_usec;
1225 /* Calculate the negative deltas relative to the export time
1226 * in seconds sent in the header, not the exact export
1228 export_time_usec = 1000000LL * export_time_sec;
1229 flow_start_delta_usec = export_time_usec
1230 - entry->flow_start_timestamp_usec;
1231 flow_end_delta_usec = export_time_usec
1232 - entry->flow_end_timestamp_usec;
1234 data_aggregated_common = ofpbuf_put_zeros(
1235 msg, sizeof *data_aggregated_common);
1236 data_aggregated_common->flow_start_delta_microseconds = htonl(
1237 flow_start_delta_usec);
1238 data_aggregated_common->flow_end_delta_microseconds = htonl(
1239 flow_end_delta_usec);
1240 data_aggregated_common->packet_delta_count = htonll(
1241 entry->packet_delta_count);
1242 data_aggregated_common->layer2_octet_delta_count = htonll(
1243 entry->layer2_octet_delta_count);
1244 data_aggregated_common->flow_end_reason = flow_end_reason;
1247 if (entry->octet_delta_sum_of_squares) { /* IP packet. */
1248 struct ipfix_data_record_aggregated_ip *data_aggregated_ip;
1250 data_aggregated_ip = ofpbuf_put_zeros(
1251 msg, sizeof *data_aggregated_ip);
1252 data_aggregated_ip->octet_delta_count = htonll(
1253 entry->octet_delta_count);
1254 data_aggregated_ip->octet_delta_sum_of_squares = htonll(
1255 entry->octet_delta_sum_of_squares);
1256 data_aggregated_ip->minimum_ip_total_length = htonll(
1257 entry->minimum_ip_total_length);
1258 data_aggregated_ip->maximum_ip_total_length = htonll(
1259 entry->maximum_ip_total_length);
1262 set_hdr = (struct ipfix_set_header*)((uint8_t*)ofpbuf_data(msg) + set_hdr_offset);
1263 set_hdr->length = htons(ofpbuf_size(msg) - set_hdr_offset);
1266 /* Send an IPFIX message with a single data record. */
1268 ipfix_send_data_msg(struct dpif_ipfix_exporter *exporter,
1269 uint32_t export_time_sec,
1270 struct ipfix_flow_cache_entry *entry,
1271 enum ipfix_flow_end_reason flow_end_reason)
1273 uint64_t msg_stub[DIV_ROUND_UP(MAX_MESSAGE_LEN, 8)];
1275 ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
1277 ipfix_init_header(export_time_sec, exporter->seq_number++,
1278 entry->flow_key.obs_domain_id, &msg);
1279 ipfix_put_data_set(export_time_sec, entry, flow_end_reason, &msg);
1280 ipfix_send_msg(exporter->collectors, &msg);
1282 ofpbuf_uninit(&msg);
1286 dpif_ipfix_sample(struct dpif_ipfix_exporter *exporter,
1287 struct ofpbuf *packet, const struct flow *flow,
1288 uint64_t packet_delta_count, uint32_t obs_domain_id,
1289 uint32_t obs_point_id)
1291 struct ipfix_flow_cache_entry *entry;
1293 /* Create a flow cache entry from the sample. */
1294 entry = xmalloc(sizeof *entry);
1295 ipfix_cache_entry_init(entry, packet, flow, packet_delta_count,
1296 obs_domain_id, obs_point_id);
1297 ipfix_cache_update(exporter, entry);
1301 dpif_ipfix_bridge_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
1302 const struct flow *flow) OVS_EXCLUDED(mutex)
1304 uint64_t packet_delta_count;
1306 ovs_mutex_lock(&mutex);
1307 /* Use the sampling probability as an approximation of the number
1308 * of matched packets. */
1309 packet_delta_count = UINT32_MAX / di->bridge_exporter.probability;
1310 dpif_ipfix_sample(&di->bridge_exporter.exporter, packet, flow,
1312 di->bridge_exporter.options->obs_domain_id,
1313 di->bridge_exporter.options->obs_point_id);
1314 ovs_mutex_unlock(&mutex);
1318 dpif_ipfix_flow_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
1319 const struct flow *flow, uint32_t collector_set_id,
1320 uint16_t probability, uint32_t obs_domain_id,
1321 uint32_t obs_point_id) OVS_EXCLUDED(mutex)
1323 struct dpif_ipfix_flow_exporter_map_node *node;
1324 /* Use the sampling probability as an approximation of the number
1325 * of matched packets. */
1326 uint64_t packet_delta_count = USHRT_MAX / probability;
1328 ovs_mutex_lock(&mutex);
1329 node = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
1331 dpif_ipfix_sample(&node->exporter.exporter, packet, flow,
1332 packet_delta_count, obs_domain_id, obs_point_id);
1334 ovs_mutex_unlock(&mutex);
1338 dpif_ipfix_cache_expire(struct dpif_ipfix_exporter *exporter,
1339 bool forced_end, const uint64_t export_time_usec,
1340 const uint32_t export_time_sec)
1342 struct ipfix_flow_cache_entry *entry, *next_entry;
1343 uint64_t max_flow_start_timestamp_usec;
1344 bool template_msg_sent = false;
1345 enum ipfix_flow_end_reason flow_end_reason;
1347 if (list_is_empty(&exporter->cache_flow_start_timestamp_list)) {
1351 max_flow_start_timestamp_usec = export_time_usec -
1352 1000000LL * exporter->cache_active_timeout;
1354 LIST_FOR_EACH_SAFE (entry, next_entry, cache_flow_start_timestamp_list_node,
1355 &exporter->cache_flow_start_timestamp_list) {
1357 flow_end_reason = FORCED_END;
1358 } else if (entry->flow_start_timestamp_usec
1359 <= max_flow_start_timestamp_usec) {
1360 flow_end_reason = ACTIVE_TIMEOUT;
1361 } else if (hmap_count(&exporter->cache_flow_key_map)
1362 > exporter->cache_max_flows) {
1363 /* Enforce exporter->cache_max_flows. */
1364 flow_end_reason = LACK_OF_RESOURCES;
1366 /* Remaining flows haven't expired yet. */
1370 list_remove(&entry->cache_flow_start_timestamp_list_node);
1371 hmap_remove(&exporter->cache_flow_key_map,
1372 &entry->flow_key_map_node);
1374 if (!template_msg_sent
1375 && (exporter->last_template_set_time + IPFIX_TEMPLATE_INTERVAL)
1376 <= export_time_sec) {
1377 ipfix_send_template_msg(exporter, export_time_sec,
1378 entry->flow_key.obs_domain_id);
1379 exporter->last_template_set_time = export_time_sec;
1380 template_msg_sent = true;
1383 /* XXX: Group multiple data records for the same obs domain id
1384 * into the same message. */
1385 ipfix_send_data_msg(exporter, export_time_sec, entry, flow_end_reason);
1391 get_export_time_now(uint64_t *export_time_usec, uint32_t *export_time_sec)
1393 struct timeval export_time;
1394 xgettimeofday(&export_time);
1396 *export_time_usec = export_time.tv_usec + 1000000LL * export_time.tv_sec;
1398 /* The IPFIX start and end deltas are negative deltas relative to
1399 * the export time, so set the export time 1 second off to
1400 * calculate those deltas. */
1401 if (export_time.tv_usec == 0) {
1402 *export_time_sec = export_time.tv_sec;
1404 *export_time_sec = export_time.tv_sec + 1;
1409 dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter *exporter,
1412 uint64_t export_time_usec;
1413 uint32_t export_time_sec;
1415 get_export_time_now(&export_time_usec, &export_time_sec);
1416 dpif_ipfix_cache_expire(exporter, forced_end, export_time_usec,
1421 dpif_ipfix_run(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
1423 uint64_t export_time_usec;
1424 uint32_t export_time_sec;
1425 struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
1427 ovs_mutex_lock(&mutex);
1428 get_export_time_now(&export_time_usec, &export_time_sec);
1429 if (di->bridge_exporter.probability > 0) { /* Bridge exporter enabled. */
1430 dpif_ipfix_cache_expire(
1431 &di->bridge_exporter.exporter, false, export_time_usec,
1434 HMAP_FOR_EACH (flow_exporter_node, node, &di->flow_exporter_map) {
1435 dpif_ipfix_cache_expire(
1436 &flow_exporter_node->exporter.exporter, false, export_time_usec,
1439 ovs_mutex_unlock(&mutex);
1443 dpif_ipfix_wait(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
1445 long long int next_timeout_msec = LLONG_MAX;
1446 struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
1448 ovs_mutex_lock(&mutex);
1449 if (di->bridge_exporter.probability > 0) { /* Bridge exporter enabled. */
1450 if (ipfix_cache_next_timeout_msec(
1451 &di->bridge_exporter.exporter, &next_timeout_msec)) {
1452 poll_timer_wait_until(next_timeout_msec);
1455 HMAP_FOR_EACH (flow_exporter_node, node, &di->flow_exporter_map) {
1456 if (ipfix_cache_next_timeout_msec(
1457 &flow_exporter_node->exporter.exporter, &next_timeout_msec)) {
1458 poll_timer_wait_until(next_timeout_msec);
1461 ovs_mutex_unlock(&mutex);