Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / ofproto / ofproto-dpif-ipfix.c
1 /*
2  * Copyright (c) 2012, 2013, 2014 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "ofproto-dpif-ipfix.h"
19 #include <sys/time.h>
20 #include "byte-order.h"
21 #include "collectors.h"
22 #include "flow.h"
23 #include "hash.h"
24 #include "hmap.h"
25 #include "list.h"
26 #include "ofpbuf.h"
27 #include "ofproto.h"
28 #include "packets.h"
29 #include "poll-loop.h"
30 #include "sset.h"
31 #include "util.h"
32 #include "timeval.h"
33 #include "util.h"
34 #include "vlog.h"
35
36 VLOG_DEFINE_THIS_MODULE(ipfix);
37
38 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
39 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
40
41 /* Cf. IETF RFC 5101 Section 10.3.4. */
42 #define IPFIX_DEFAULT_COLLECTOR_PORT 4739
43
44 struct dpif_ipfix_exporter {
45     struct collectors *collectors;
46     uint32_t seq_number;
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;
52 };
53
54 struct dpif_ipfix_bridge_exporter {
55     struct dpif_ipfix_exporter exporter;
56     struct ofproto_ipfix_bridge_exporter_options *options;
57     uint32_t probability;
58 };
59
60 struct dpif_ipfix_flow_exporter {
61     struct dpif_ipfix_exporter exporter;
62     struct ofproto_ipfix_flow_exporter_options *options;
63 };
64
65 struct dpif_ipfix_flow_exporter_map_node {
66     struct hmap_node node;
67     struct dpif_ipfix_flow_exporter exporter;
68 };
69
70 struct dpif_ipfix {
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;
74 };
75
76 #define IPFIX_VERSION 0x000a
77
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
82
83 /* Cf. IETF RFC 5101 Section 3.1. */
84 OVS_PACKED(
85 struct ipfix_header {
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. */
91 });
92 BUILD_ASSERT_DECL(sizeof(struct ipfix_header) == 16);
93
94 #define IPFIX_SET_ID_TEMPLATE 2
95 #define IPFIX_SET_ID_OPTION_TEMPLATE 3
96
97 /* Cf. IETF RFC 5101 Section 3.3.2. */
98 OVS_PACKED(
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. */
102 });
103 BUILD_ASSERT_DECL(sizeof(struct ipfix_set_header) == 4);
104
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. */
109     IPFIX_PROTO_L2_VLAN,
110     NUM_IPFIX_PROTO_L2
111 };
112 enum ipfix_proto_l3 {
113     IPFIX_PROTO_L3_UNKNOWN = 0,
114     IPFIX_PROTO_L3_IPV4,
115     IPFIX_PROTO_L3_IPV6,
116     NUM_IPFIX_PROTO_L3
117 };
118 enum ipfix_proto_l4 {
119     IPFIX_PROTO_L4_UNKNOWN = 0,
120     IPFIX_PROTO_L4_TCP_UDP_SCTP,
121     IPFIX_PROTO_L4_ICMP,
122     NUM_IPFIX_PROTO_L4
123 };
124
125 /* Any Template ID > 255 is usable for Template Records. */
126 #define IPFIX_TEMPLATE_ID_MIN 256
127
128 /* Cf. IETF RFC 5101 Section 3.4.1. */
129 OVS_PACKED(
130 struct ipfix_template_record_header {
131     ovs_be16 template_id;
132     ovs_be16 field_count;
133 });
134 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_record_header) == 4);
135
136 enum ipfix_entity_id {
137 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME)  IPFIX_ENTITY_ID_##ENUM = ID,
138 #include "ofproto/ipfix-entities.def"
139 };
140
141 enum ipfix_entity_size {
142 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME)  IPFIX_ENTITY_SIZE_##ENUM = SIZE,
143 #include "ofproto/ipfix-entities.def"
144 };
145
146 OVS_PACKED(
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. */
151 });
152 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_field_specifier) == 4);
153
154 /* Part of data record flow key for common metadata and Ethernet entities. */
155 OVS_PACKED(
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 */
162 });
163 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_common) == 19);
164
165 /* Part of data record flow key for VLAN entities. */
166 OVS_PACKED(
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 */
171 });
172 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_vlan) == 5);
173
174 /* Part of data record flow key for IP entities. */
175 /* XXX: Replace IP_TTL with MINIMUM_TTL and MAXIMUM_TTL? */
176 OVS_PACKED(
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 */
184 });
185 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ip) == 6);
186
187 /* Part of data record flow key for IPv4 entities. */
188 OVS_PACKED(
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 */
192 });
193 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv4) == 8);
194
195 /* Part of data record flow key for IPv6 entities. */
196 OVS_PACKED(
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 */
201 });
202 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv6) == 36);
203
204 /* Part of data record flow key for TCP/UDP/SCTP entities. */
205 OVS_PACKED(
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 */
209 });
210 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_transport) == 4);
211
212 /* Part of data record flow key for ICMP entities. */
213 OVS_PACKED(
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 */
217 });
218 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_icmp) == 2);
219
220 /* Cf. IETF RFC 5102 Section 5.11.3. */
221 enum ipfix_flow_end_reason {
222     IDLE_TIMEOUT = 0x01,
223     ACTIVE_TIMEOUT = 0x02,
224     END_OF_FLOW_DETECTED = 0x03,
225     FORCED_END = 0x04,
226     LACK_OF_RESOURCES = 0x05
227 };
228
229 /* Part of data record for common aggregated elements. */
230 OVS_PACKED(
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 */
237 });
238 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_common) == 25);
239
240 /* Part of data record for IP aggregated elements. */
241 OVS_PACKED(
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 */
247 });
248 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_ip) == 32);
249
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)))
258
259 #define MAX_DATA_RECORD_LEN                                 \
260     (MAX_FLOW_KEY_LEN                                       \
261      + sizeof(struct ipfix_data_record_aggregated_common)   \
262      + sizeof(struct ipfix_data_record_aggregated_ip))
263
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)
270
271 /* Max length of an IPFIX message. Arbitrarily set to accomodate low
272  * MTU. */
273 #define MAX_MESSAGE_LEN 1024
274
275 /* Cache structures. */
276
277 /* Flow key. */
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)];
283 };
284
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. */
299 };
300
301 static void dpif_ipfix_cache_expire(struct dpif_ipfix_exporter *, bool,
302                                     const uint64_t, const uint32_t);
303
304 static void get_export_time_now(uint64_t *, uint32_t *);
305
306 static void dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter *, bool);
307
308 static 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)
312 {
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));
319 }
320
321 static struct ofproto_ipfix_bridge_exporter_options *
322 ofproto_ipfix_bridge_exporter_options_clone(
323     const struct ofproto_ipfix_bridge_exporter_options *old)
324 {
325     struct ofproto_ipfix_bridge_exporter_options *new =
326         xmemdup(old, sizeof *old);
327     sset_clone(&new->targets, &old->targets);
328     return new;
329 }
330
331 static void
332 ofproto_ipfix_bridge_exporter_options_destroy(
333     struct ofproto_ipfix_bridge_exporter_options *options)
334 {
335     if (options) {
336         sset_destroy(&options->targets);
337         free(options);
338     }
339 }
340
341 static bool
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)
345 {
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));
350 }
351
352 static struct ofproto_ipfix_flow_exporter_options *
353 ofproto_ipfix_flow_exporter_options_clone(
354     const struct ofproto_ipfix_flow_exporter_options *old)
355 {
356     struct ofproto_ipfix_flow_exporter_options *new =
357         xmemdup(old, sizeof *old);
358     sset_clone(&new->targets, &old->targets);
359     return new;
360 }
361
362 static void
363 ofproto_ipfix_flow_exporter_options_destroy(
364     struct ofproto_ipfix_flow_exporter_options *options)
365 {
366     if (options) {
367         sset_destroy(&options->targets);
368         free(options);
369     }
370 }
371
372 static void
373 dpif_ipfix_exporter_init(struct dpif_ipfix_exporter *exporter)
374 {
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;
382 }
383
384 static void
385 dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
386 {
387     /* Flush the cache with flow end reason "forced end." */
388     dpif_ipfix_cache_expire_now(exporter, true);
389
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;
396 }
397
398 static void
399 dpif_ipfix_exporter_destroy(struct dpif_ipfix_exporter *exporter)
400 {
401     dpif_ipfix_exporter_clear(exporter);
402     hmap_destroy(&exporter->cache_flow_key_map);
403 }
404
405 static bool
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)
410 {
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);
418         return false;
419     }
420     exporter->cache_active_timeout = cache_active_timeout;
421     exporter->cache_max_flows = cache_max_flows;
422     return true;
423 }
424
425 static void
426 dpif_ipfix_bridge_exporter_init(struct dpif_ipfix_bridge_exporter *exporter)
427 {
428     dpif_ipfix_exporter_init(&exporter->exporter);
429     exporter->options = NULL;
430     exporter->probability = 0;
431 }
432
433 static void
434 dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
435 {
436     dpif_ipfix_exporter_clear(&exporter->exporter);
437     ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
438     exporter->options = NULL;
439     exporter->probability = 0;
440 }
441
442 static void
443 dpif_ipfix_bridge_exporter_destroy(struct dpif_ipfix_bridge_exporter *exporter)
444 {
445     dpif_ipfix_bridge_exporter_clear(exporter);
446     dpif_ipfix_exporter_destroy(&exporter->exporter);
447 }
448
449 static void
450 dpif_ipfix_bridge_exporter_set_options(
451     struct dpif_ipfix_bridge_exporter *exporter,
452     const struct ofproto_ipfix_bridge_exporter_options *options)
453 {
454     bool options_changed;
455
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);
459         return;
460     }
461
462     options_changed = (
463         !exporter->options
464         || !ofproto_ipfix_bridge_exporter_options_equal(
465             options, exporter->options));
466
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
470      * retry). */
471     if (options_changed
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)) {
477             return;
478         }
479     }
480
481     /* Avoid reconfiguring if options didn't change. */
482     if (!options_changed) {
483         return;
484     }
485
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);
490
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);
494 }
495
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)
499     OVS_REQUIRES(mutex)
500 {
501     struct dpif_ipfix_flow_exporter_map_node *exporter_node;
502
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;
509         }
510     }
511
512     return NULL;
513 }
514
515 static void
516 dpif_ipfix_flow_exporter_init(struct dpif_ipfix_flow_exporter *exporter)
517 {
518     dpif_ipfix_exporter_init(&exporter->exporter);
519     exporter->options = NULL;
520 }
521
522 static void
523 dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
524 {
525     dpif_ipfix_exporter_clear(&exporter->exporter);
526     ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
527     exporter->options = NULL;
528 }
529
530 static void
531 dpif_ipfix_flow_exporter_destroy(struct dpif_ipfix_flow_exporter *exporter)
532 {
533     dpif_ipfix_flow_exporter_clear(exporter);
534     dpif_ipfix_exporter_destroy(&exporter->exporter);
535 }
536
537 static bool
538 dpif_ipfix_flow_exporter_set_options(
539     struct dpif_ipfix_flow_exporter *exporter,
540     const struct ofproto_ipfix_flow_exporter_options *options)
541 {
542     bool options_changed;
543
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);
547         return true;
548     }
549
550     options_changed = (
551         !exporter->options
552         || !ofproto_ipfix_flow_exporter_options_equal(
553             options, exporter->options));
554
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
558      * retry). */
559     if (options_changed
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)) {
565             return false;
566         }
567     }
568
569     /* Avoid reconfiguring if options didn't change. */
570     if (!options_changed) {
571         return true;
572     }
573
574     ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
575     exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
576
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);
580
581     return true;
582 }
583
584 void
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)
590 {
591     int i;
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;
595
596     ovs_mutex_lock(&mutex);
597     dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
598                                            bridge_exporter_options);
599
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);
606         if (!node) {
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));
611         }
612         if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
613             n_broken_flow_exporters_options++;
614         }
615         options++;
616     }
617
618     ovs_assert(hmap_count(&di->flow_exporter_map) >=
619                (n_flow_exporters_options - n_broken_flow_exporters_options));
620
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) {
631                   break;
632               }
633               options++;
634             }
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);
638                 free(node);
639             }
640         }
641     }
642
643     ovs_assert(hmap_count(&di->flow_exporter_map) ==
644                (n_flow_exporters_options - n_broken_flow_exporters_options));
645     ovs_mutex_unlock(&mutex);
646 }
647
648 struct dpif_ipfix *
649 dpif_ipfix_create(void)
650 {
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);
656     return di;
657 }
658
659 struct dpif_ipfix *
660 dpif_ipfix_ref(const struct dpif_ipfix *di_)
661 {
662     struct dpif_ipfix *di = CONST_CAST(struct dpif_ipfix *, di_);
663     if (di) {
664         ovs_refcount_ref(&di->ref_cnt);
665     }
666     return di;
667 }
668
669 uint32_t
670 dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
671     OVS_EXCLUDED(mutex)
672 {
673     uint32_t ret;
674     ovs_mutex_lock(&mutex);
675     ret = di->bridge_exporter.probability;
676     ovs_mutex_unlock(&mutex);
677     return ret;
678 }
679
680 static void
681 dpif_ipfix_clear(struct dpif_ipfix *di) OVS_REQUIRES(mutex)
682 {
683     struct dpif_ipfix_flow_exporter_map_node *exp_node, *exp_next;
684
685     dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
686
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);
690         free(exp_node);
691     }
692 }
693
694 void
695 dpif_ipfix_unref(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
696 {
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);
702         free(di);
703         ovs_mutex_unlock(&mutex);
704     }
705 }
706
707 static void
708 ipfix_init_header(uint32_t export_time_sec, uint32_t seq_number,
709                   uint32_t obs_domain_id, struct ofpbuf *msg)
710 {
711     struct ipfix_header *hdr;
712
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);
719 }
720
721 static void
722 ipfix_send_msg(const struct collectors *collectors, struct ofpbuf *msg)
723 {
724     struct ipfix_header *hdr;
725
726     /* Adjust the length in the header. */
727     hdr = ofpbuf_data(msg);
728     hdr->length = htons(ofpbuf_size(msg));
729
730     collectors_send(collectors, ofpbuf_data(msg), ofpbuf_size(msg));
731     ofpbuf_set_size(msg, 0);
732 }
733
734 static uint16_t
735 ipfix_get_template_id(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
736                       enum ipfix_proto_l4 l4)
737 {
738     uint16_t template_id;
739     template_id = l2;
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;
743 }
744
745 static void
746 ipfix_define_template_entity(enum ipfix_entity_id id,
747                              enum ipfix_entity_size size, struct ofpbuf *msg)
748 {
749     struct ipfix_template_field_specifier *field;
750
751     field = ofpbuf_put_zeros(msg, sizeof *field);
752     field->element_id = htons(id);
753     field->field_length = htons(size);
754 }
755
756 static uint16_t
757 ipfix_define_template_fields(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
758                              enum ipfix_proto_l4 l4, struct ofpbuf *msg)
759 {
760     uint16_t count = 0;
761
762 #define DEF(ID) \
763     { \
764         ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
765                                      IPFIX_ENTITY_SIZE_##ID, msg); \
766         count++; \
767     }
768
769     /* 1. Flow key. */
770
771     DEF(OBSERVATION_POINT_ID);
772
773     /* Common Ethernet entities. */
774     DEF(SOURCE_MAC_ADDRESS);
775     DEF(DESTINATION_MAC_ADDRESS);
776     DEF(ETHERNET_TYPE);
777     DEF(ETHERNET_HEADER_LENGTH);
778
779     if (l2 == IPFIX_PROTO_L2_VLAN) {
780         DEF(VLAN_ID);
781         DEF(DOT1Q_VLAN_ID);
782         DEF(DOT1Q_PRIORITY);
783     }
784
785     if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
786         DEF(IP_VERSION);
787         DEF(IP_TTL);
788         DEF(PROTOCOL_IDENTIFIER);
789         DEF(IP_DIFF_SERV_CODE_POINT);
790         DEF(IP_PRECEDENCE);
791         DEF(IP_CLASS_OF_SERVICE);
792
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) {
800                 DEF(ICMP_TYPE_IPV4);
801                 DEF(ICMP_CODE_IPV4);
802             }
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) {
811                 DEF(ICMP_TYPE_IPV6);
812                 DEF(ICMP_CODE_IPV6);
813             }
814         }
815     }
816
817     /* 2. Flow aggregated data. */
818
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);
824
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);
830     }
831
832 #undef DEF
833
834     return count;
835 }
836
837 static void
838 ipfix_send_template_msg(struct dpif_ipfix_exporter *exporter,
839                         uint32_t export_time_sec, uint32_t obs_domain_id)
840 {
841     uint64_t msg_stub[DIV_ROUND_UP(MAX_MESSAGE_LEN, 8)];
842     struct ofpbuf msg;
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;
850
851     ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
852
853     ipfix_init_header(export_time_sec, exporter->seq_number, obs_domain_id,
854                       &msg);
855     set_hdr_offset = ofpbuf_size(&msg);
856
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);
860
861     /* Define one template for each possible combination of
862      * protocols. */
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) {
868                     continue;
869                 }
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);
878             }
879         }
880     }
881
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);
884
885     /* XXX: Add Options Template Sets, at least to define a Flow Keys
886      * Option Template. */
887
888     ipfix_send_msg(exporter->collectors, &msg);
889
890     ofpbuf_uninit(&msg);
891 }
892
893 static inline uint32_t
894 ipfix_hash_flow_key(const struct ipfix_flow_key *flow_key, uint32_t basis)
895 {
896     uint32_t hash;
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);
901     return hash;
902 }
903
904 static bool
905 ipfix_flow_key_equal(const struct ipfix_flow_key *a,
906                      const struct ipfix_flow_key *b)
907 {
908     /* The template ID determines the flow key size, so not need to
909      * compare it. */
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);
914 }
915
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)
919 {
920     struct ipfix_flow_cache_entry *entry;
921
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)) {
926             return entry;
927         }
928     }
929
930     return NULL;
931 }
932
933 static bool
934 ipfix_cache_next_timeout_msec(const struct dpif_ipfix_exporter *exporter,
935                               long long int *next_timeout_msec)
936 {
937     struct ipfix_flow_cache_entry *entry;
938
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;
943         return true;
944     }
945
946     return false;
947 }
948
949 static void
950 ipfix_cache_aggregate_entries(struct ipfix_flow_cache_entry *from_entry,
951                               struct ipfix_flow_cache_entry *to_entry)
952 {
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;
955
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;
960
961     if (*to_start > *from_start) {
962         *to_start = *from_start;
963     }
964     if (*to_end < *from_end) {
965         *to_end = *from_end;
966     }
967
968     to_entry->packet_delta_count += from_entry->packet_delta_count;
969     to_entry->layer2_octet_delta_count += from_entry->layer2_octet_delta_count;
970
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;
974
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;
979
980     if (!*to_min_len || (*from_min_len && *to_min_len > *from_min_len)) {
981         *to_min_len = *from_min_len;
982     }
983     if (*to_max_len < *from_max_len) {
984         *to_max_len = *from_max_len;
985     }
986 }
987
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. */
991 static void
992 ipfix_cache_update(struct dpif_ipfix_exporter *exporter,
993                    struct ipfix_flow_cache_entry *entry)
994 {
995     struct ipfix_flow_cache_entry *old_entry;
996
997     old_entry = ipfix_cache_find_entry(exporter, &entry->flow_key);
998
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));
1002
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);
1008
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);
1013         }
1014     } else {
1015         ipfix_cache_aggregate_entries(entry, old_entry);
1016         free(entry);
1017     }
1018 }
1019
1020 static void
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)
1025 {
1026     struct ipfix_flow_key *flow_key;
1027     struct ofpbuf msg;
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;
1033
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);
1037
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;
1041
1042     switch(ntohs(flow->dl_type)) {
1043     case ETH_TYPE_IP:
1044         l3 = IPFIX_PROTO_L3_IPV4;
1045         switch(flow->nw_proto) {
1046         case IPPROTO_TCP:
1047         case IPPROTO_UDP:
1048         case IPPROTO_SCTP:
1049             l4 = IPFIX_PROTO_L4_TCP_UDP_SCTP;
1050             break;
1051         case IPPROTO_ICMP:
1052             l4 = IPFIX_PROTO_L4_ICMP;
1053             break;
1054         default:
1055             l4 = IPFIX_PROTO_L4_UNKNOWN;
1056         }
1057         break;
1058     case ETH_TYPE_IPV6:
1059         l3 = IPFIX_PROTO_L3_IPV6;
1060         switch(flow->nw_proto) {
1061         case IPPROTO_TCP:
1062         case IPPROTO_UDP:
1063         case IPPROTO_SCTP:
1064             l4 = IPFIX_PROTO_L4_TCP_UDP_SCTP;
1065             break;
1066         case IPPROTO_ICMPV6:
1067             l4 = IPFIX_PROTO_L4_ICMP;
1068             break;
1069         default:
1070             l4 = IPFIX_PROTO_L4_UNKNOWN;
1071         }
1072         break;
1073     default:
1074         l3 = IPFIX_PROTO_L3_UNKNOWN;
1075         l4 = IPFIX_PROTO_L4_UNKNOWN;
1076     }
1077
1078     flow_key->obs_domain_id = obs_domain_id;
1079     flow_key->template_id = ipfix_get_template_id(l2, l3, l4);
1080
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. */
1084
1085     ethernet_header_length = (l2 == IPFIX_PROTO_L2_VLAN)
1086         ? VLAN_ETH_HEADER_LEN : ETH_HEADER_LEN;
1087     ethernet_total_length = ofpbuf_size(packet);
1088
1089     /* Common Ethernet entities. */
1090     {
1091         struct ipfix_data_record_flow_key_common *data_common;
1092
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;
1101     }
1102
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);
1107
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;
1112     }
1113
1114     if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1115         struct ipfix_data_record_flow_key_ip *data_ip;
1116
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;
1124
1125         if (l3 == IPFIX_PROTO_L3_IPV4) {
1126             struct ipfix_data_record_flow_key_ipv4 *data_ipv4;
1127
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;
1133
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;
1140         }
1141     }
1142
1143     if (l4 == IPFIX_PROTO_L4_TCP_UDP_SCTP) {
1144         struct ipfix_data_record_flow_key_transport *data_transport;
1145
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;
1151
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;
1155     }
1156
1157     flow_key->flow_key_msg_part_size = ofpbuf_size(&msg);
1158
1159     {
1160         struct timeval now;
1161         uint64_t layer2_octet_delta_count;
1162
1163         /* Calculate the total matched octet count by considering as
1164          * an approximation that all matched packets have the same
1165          * length. */
1166         layer2_octet_delta_count = packet_delta_count * ethernet_total_length;
1167
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;
1173     }
1174
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;
1179
1180         /* Calculate the total matched octet count by considering as
1181          * an approximation that all matched packets have the same
1182          * length. */
1183         octet_delta_count = packet_delta_count * ip_total_length;
1184
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;
1189     } else {
1190         entry->octet_delta_sum_of_squares = 0;
1191         entry->minimum_ip_total_length = 0;
1192         entry->maximum_ip_total_length = 0;
1193     }
1194 }
1195
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. */
1199 static void
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,
1203                    struct ofpbuf *msg)
1204 {
1205     size_t set_hdr_offset;
1206     struct ipfix_set_header *set_hdr;
1207
1208     set_hdr_offset = ofpbuf_size(msg);
1209
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);
1213
1214     /* Copy the flow key part of the data record. */
1215
1216     ofpbuf_put(msg, entry->flow_key.flow_key_msg_part,
1217                entry->flow_key.flow_key_msg_part_size);
1218
1219     /* Put the non-key part of the data record. */
1220
1221     {
1222         struct ipfix_data_record_aggregated_common *data_aggregated_common;
1223         uint64_t export_time_usec, flow_start_delta_usec, flow_end_delta_usec;
1224
1225         /* Calculate the negative deltas relative to the export time
1226          * in seconds sent in the header, not the exact export
1227          * time. */
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;
1233
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;
1245     }
1246
1247     if (entry->octet_delta_sum_of_squares) {  /* IP packet. */
1248         struct ipfix_data_record_aggregated_ip *data_aggregated_ip;
1249
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);
1260     }
1261
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);
1264 }
1265
1266 /* Send an IPFIX message with a single data record. */
1267 static void
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)
1272 {
1273     uint64_t msg_stub[DIV_ROUND_UP(MAX_MESSAGE_LEN, 8)];
1274     struct ofpbuf msg;
1275     ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
1276
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);
1281
1282     ofpbuf_uninit(&msg);
1283 }
1284
1285 static void
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)
1290 {
1291     struct ipfix_flow_cache_entry *entry;
1292
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);
1298 }
1299
1300 void
1301 dpif_ipfix_bridge_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
1302                          const struct flow *flow) OVS_EXCLUDED(mutex)
1303 {
1304     uint64_t packet_delta_count;
1305
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,
1311                       packet_delta_count,
1312                       di->bridge_exporter.options->obs_domain_id,
1313                       di->bridge_exporter.options->obs_point_id);
1314     ovs_mutex_unlock(&mutex);
1315 }
1316
1317 void
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)
1322 {
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;
1327
1328     ovs_mutex_lock(&mutex);
1329     node = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
1330     if (node) {
1331         dpif_ipfix_sample(&node->exporter.exporter, packet, flow,
1332                           packet_delta_count, obs_domain_id, obs_point_id);
1333     }
1334     ovs_mutex_unlock(&mutex);
1335 }
1336
1337 static void
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)
1341 {
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;
1346
1347     if (list_is_empty(&exporter->cache_flow_start_timestamp_list)) {
1348         return;
1349     }
1350
1351     max_flow_start_timestamp_usec = export_time_usec -
1352         1000000LL * exporter->cache_active_timeout;
1353
1354     LIST_FOR_EACH_SAFE (entry, next_entry, cache_flow_start_timestamp_list_node,
1355                         &exporter->cache_flow_start_timestamp_list) {
1356         if (forced_end) {
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;
1365         } else {
1366             /* Remaining flows haven't expired yet. */
1367             break;
1368         }
1369
1370         list_remove(&entry->cache_flow_start_timestamp_list_node);
1371         hmap_remove(&exporter->cache_flow_key_map,
1372                     &entry->flow_key_map_node);
1373
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;
1381         }
1382
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);
1386         free(entry);
1387     }
1388 }
1389
1390 static void
1391 get_export_time_now(uint64_t *export_time_usec, uint32_t *export_time_sec)
1392 {
1393     struct timeval export_time;
1394     xgettimeofday(&export_time);
1395
1396     *export_time_usec = export_time.tv_usec + 1000000LL * export_time.tv_sec;
1397
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;
1403     } else {
1404         *export_time_sec = export_time.tv_sec + 1;
1405     }
1406 }
1407
1408 static void
1409 dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter *exporter,
1410                             bool forced_end)
1411 {
1412     uint64_t export_time_usec;
1413     uint32_t export_time_sec;
1414
1415     get_export_time_now(&export_time_usec, &export_time_sec);
1416     dpif_ipfix_cache_expire(exporter, forced_end, export_time_usec,
1417                             export_time_sec);
1418 }
1419
1420 void
1421 dpif_ipfix_run(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
1422 {
1423     uint64_t export_time_usec;
1424     uint32_t export_time_sec;
1425     struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
1426
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,
1432           export_time_sec);
1433     }
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,
1437             export_time_sec);
1438     }
1439     ovs_mutex_unlock(&mutex);
1440 }
1441
1442 void
1443 dpif_ipfix_wait(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
1444 {
1445     long long int next_timeout_msec = LLONG_MAX;
1446     struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
1447
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);
1453         }
1454     }
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);
1459         }
1460     }
1461     ovs_mutex_unlock(&mutex);
1462 }