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"
28 #include "ofproto-dpif.h"
29 #include "dp-packet.h"
31 #include "poll-loop.h"
36 #include "openvswitch/vlog.h"
38 VLOG_DEFINE_THIS_MODULE(ipfix);
40 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
41 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
43 /* Cf. IETF RFC 5101 Section 10.3.4. */
44 #define IPFIX_DEFAULT_COLLECTOR_PORT 4739
46 /* The standard layer2SegmentId (ID 351) element is included in vDS to send
47 * the VxLAN tunnel's VNI. It is 64-bit long, the most significant byte is
48 * used to indicate the type of tunnel (0x01 = VxLAN, 0x02 = GRE) and the three
49 * least significant bytes hold the value of the layer 2 overlay network
50 * segment identifier: a 24-bit VxLAN tunnel's VNI or a 24-bit GRE tunnel's
51 * TNI. This is not compatible with GRE-64 or STT, as implemented in OVS, as
52 * their tunnel IDs are 64-bit.
54 * Two new enterprise information elements are defined which are similar to
55 * laryerSegmentId but support 64-bit IDs:
56 * tunnelType (ID 891) and tunnelKey (ID 892).
58 * The enum dpif_ipfix_tunnel_type is to declare the types supported in the
60 * The number of ipfix tunnel types includes two reserverd types: 0x04 and 0x06.
62 enum dpif_ipfix_tunnel_type {
63 DPIF_IPFIX_TUNNEL_UNKNOWN = 0x00,
64 DPIF_IPFIX_TUNNEL_VXLAN = 0x01,
65 DPIF_IPFIX_TUNNEL_GRE = 0x02,
66 DPIF_IPFIX_TUNNEL_LISP = 0x03,
67 DPIF_IPFIX_TUNNEL_STT = 0x04,
68 DPIF_IPFIX_TUNNEL_IPSEC_GRE = 0x05,
69 DPIF_IPFIX_TUNNEL_GENEVE = 0x07,
73 struct dpif_ipfix_port {
74 struct hmap_node hmap_node; /* In struct dpif_ipfix's "tunnel_ports" hmap. */
75 struct ofport *ofport; /* To retrieve port stats. */
77 enum dpif_ipfix_tunnel_type tunnel_type;
78 uint8_t tunnel_key_length;
81 struct dpif_ipfix_exporter {
82 struct collectors *collectors;
84 time_t last_template_set_time;
85 struct hmap cache_flow_key_map; /* ipfix_flow_cache_entry. */
86 struct ovs_list cache_flow_start_timestamp_list; /* ipfix_flow_cache_entry. */
87 uint32_t cache_active_timeout; /* In seconds. */
88 uint32_t cache_max_flows;
91 struct dpif_ipfix_bridge_exporter {
92 struct dpif_ipfix_exporter exporter;
93 struct ofproto_ipfix_bridge_exporter_options *options;
97 struct dpif_ipfix_flow_exporter {
98 struct dpif_ipfix_exporter exporter;
99 struct ofproto_ipfix_flow_exporter_options *options;
102 struct dpif_ipfix_flow_exporter_map_node {
103 struct hmap_node node;
104 struct dpif_ipfix_flow_exporter exporter;
108 struct dpif_ipfix_bridge_exporter bridge_exporter;
109 struct hmap flow_exporter_map; /* dpif_ipfix_flow_exporter_map_node. */
110 struct hmap tunnel_ports; /* Contains "struct dpif_ipfix_port"s.
111 * It makes tunnel port lookups faster in
112 * sampling upcalls. */
113 struct ovs_refcount ref_cnt;
116 #define IPFIX_VERSION 0x000a
118 /* When using UDP, IPFIX Template Records must be re-sent regularly.
119 * The standard default interval is 10 minutes (600 seconds).
120 * Cf. IETF RFC 5101 Section 10.3.6. */
121 #define IPFIX_TEMPLATE_INTERVAL 600
123 /* Cf. IETF RFC 5101 Section 3.1. */
125 struct ipfix_header {
126 ovs_be16 version; /* IPFIX_VERSION. */
127 ovs_be16 length; /* Length in bytes including this header. */
128 ovs_be32 export_time; /* Seconds since the epoch. */
129 ovs_be32 seq_number; /* Message sequence number. */
130 ovs_be32 obs_domain_id; /* Observation Domain ID. */
132 BUILD_ASSERT_DECL(sizeof(struct ipfix_header) == 16);
134 #define IPFIX_SET_ID_TEMPLATE 2
135 #define IPFIX_SET_ID_OPTION_TEMPLATE 3
137 /* Cf. IETF RFC 5101 Section 3.3.2. */
139 struct ipfix_set_header {
140 ovs_be16 set_id; /* IPFIX_SET_ID_* or valid template ID for Data Sets. */
141 ovs_be16 length; /* Length of the set in bytes including header. */
143 BUILD_ASSERT_DECL(sizeof(struct ipfix_set_header) == 4);
145 /* Alternatives for templates at each layer. A template is defined by
146 * a combination of one value for each layer. */
147 enum ipfix_proto_l2 {
148 IPFIX_PROTO_L2_ETH = 0, /* No VLAN. */
152 enum ipfix_proto_l3 {
153 IPFIX_PROTO_L3_UNKNOWN = 0,
158 enum ipfix_proto_l4 {
159 IPFIX_PROTO_L4_UNKNOWN = 0,
160 IPFIX_PROTO_L4_TCP_UDP_SCTP,
164 enum ipfix_proto_tunnel {
165 IPFIX_PROTO_NOT_TUNNELED = 0,
166 IPFIX_PROTO_TUNNELED, /* Support gre, lisp and vxlan. */
167 NUM_IPFIX_PROTO_TUNNEL
170 /* Any Template ID > 255 is usable for Template Records. */
171 #define IPFIX_TEMPLATE_ID_MIN 256
173 /* Cf. IETF RFC 5101 Section 3.4.1. */
175 struct ipfix_template_record_header {
176 ovs_be16 template_id;
177 ovs_be16 field_count;
179 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_record_header) == 4);
181 enum ipfix_entity_id {
182 /* standard IPFIX elements */
183 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ID_##ENUM = ID,
184 #include "ofproto/ipfix-entities.def"
185 /* non-standard IPFIX elements */
186 #define IPFIX_SET_ENTERPRISE(v) (((v) | 0x8000))
187 #define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
188 IPFIX_ENTITY_ID_##ENUM = IPFIX_SET_ENTERPRISE(ID),
189 #include "ofproto/ipfix-enterprise-entities.def"
192 enum ipfix_entity_size {
193 /* standard IPFIX elements */
194 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_SIZE_##ENUM = SIZE,
195 #include "ofproto/ipfix-entities.def"
196 /* non-standard IPFIX elements */
197 #define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
198 IPFIX_ENTITY_SIZE_##ENUM = SIZE,
199 #include "ofproto/ipfix-enterprise-entities.def"
202 enum ipfix_entity_enterprise {
203 /* standard IPFIX elements */
204 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ENTERPRISE_##ENUM = 0,
205 #include "ofproto/ipfix-entities.def"
206 /* non-standard IPFIX elements */
207 #define IPFIX_ENTERPRISE_ENTITY(ENUM, ID, SIZE, NAME, ENTERPRISE) \
208 IPFIX_ENTITY_ENTERPRISE_##ENUM = ENTERPRISE,
209 #include "ofproto/ipfix-enterprise-entities.def"
213 struct ipfix_template_field_specifier {
214 ovs_be16 element_id; /* IPFIX_ENTITY_ID_*. */
215 ovs_be16 field_length; /* Length of the field's value, in bytes.
216 * For Variable-Length element, it should be 65535.
218 ovs_be32 enterprise; /* Enterprise number */
220 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_field_specifier) == 8);
222 /* Cf. IETF RFC 5102 Section 5.11.6. */
223 enum ipfix_flow_direction {
228 /* Part of data record flow key for common metadata and Ethernet entities. */
230 struct ipfix_data_record_flow_key_common {
231 ovs_be32 observation_point_id; /* OBSERVATION_POINT_ID */
232 uint8_t flow_direction; /* FLOW_DIRECTION */
233 uint8_t source_mac_address[ETH_ADDR_LEN]; /* SOURCE_MAC_ADDRESS */
234 uint8_t destination_mac_address[ETH_ADDR_LEN]; /* DESTINATION_MAC_ADDRESS */
235 ovs_be16 ethernet_type; /* ETHERNET_TYPE */
236 uint8_t ethernet_header_length; /* ETHERNET_HEADER_LENGTH */
238 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_common) == 20);
240 /* Part of data record flow key for VLAN entities. */
242 struct ipfix_data_record_flow_key_vlan {
243 ovs_be16 vlan_id; /* VLAN_ID */
244 ovs_be16 dot1q_vlan_id; /* DOT1Q_VLAN_ID */
245 uint8_t dot1q_priority; /* DOT1Q_PRIORITY */
247 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_vlan) == 5);
249 /* Part of data record flow key for IP entities. */
250 /* XXX: Replace IP_TTL with MINIMUM_TTL and MAXIMUM_TTL? */
252 struct ipfix_data_record_flow_key_ip {
253 uint8_t ip_version; /* IP_VERSION */
254 uint8_t ip_ttl; /* IP_TTL */
255 uint8_t protocol_identifier; /* PROTOCOL_IDENTIFIER */
256 uint8_t ip_diff_serv_code_point; /* IP_DIFF_SERV_CODE_POINT */
257 uint8_t ip_precedence; /* IP_PRECEDENCE */
258 uint8_t ip_class_of_service; /* IP_CLASS_OF_SERVICE */
260 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ip) == 6);
262 /* Part of data record flow key for IPv4 entities. */
264 struct ipfix_data_record_flow_key_ipv4 {
265 ovs_be32 source_ipv4_address; /* SOURCE_IPV4_ADDRESS */
266 ovs_be32 destination_ipv4_address; /* DESTINATION_IPV4_ADDRESS */
268 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv4) == 8);
270 /* Part of data record flow key for IPv6 entities. */
272 struct ipfix_data_record_flow_key_ipv6 {
273 uint8_t source_ipv6_address[16]; /* SOURCE_IPV6_ADDRESS */
274 uint8_t destination_ipv6_address[16]; /* DESTINATION_IPV6_ADDRESS */
275 ovs_be32 flow_label_ipv6; /* FLOW_LABEL_IPV6 */
277 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_ipv6) == 36);
279 /* Part of data record flow key for TCP/UDP/SCTP entities. */
281 struct ipfix_data_record_flow_key_transport {
282 ovs_be16 source_transport_port; /* SOURCE_TRANSPORT_PORT */
283 ovs_be16 destination_transport_port; /* DESTINATION_TRANSPORT_PORT */
285 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_transport) == 4);
287 /* Part of data record flow key for ICMP entities. */
289 struct ipfix_data_record_flow_key_icmp {
290 uint8_t icmp_type; /* ICMP_TYPE_IPV4 / ICMP_TYPE_IPV6 */
291 uint8_t icmp_code; /* ICMP_CODE_IPV4 / ICMP_CODE_IPV6 */
293 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_icmp) == 2);
295 /* For the tunnel type that is on the top of IPSec, the protocol identifier
296 * of the upper tunnel type is used.
298 static uint8_t tunnel_protocol[NUM_DPIF_IPFIX_TUNNEL] = {
300 IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_VXLAN */
301 IPPROTO_GRE, /* DPIF_IPFIX_TUNNEL_GRE */
302 IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_LISP*/
303 IPPROTO_TCP, /* DPIF_IPFIX_TUNNEL_STT*/
304 IPPROTO_GRE, /* DPIF_IPFIX_TUNNEL_IPSEC_GRE */
306 IPPROTO_UDP, /* DPIF_IPFIX_TUNNEL_GENEVE*/
310 struct ipfix_data_record_flow_key_tunnel {
311 ovs_be32 tunnel_source_ipv4_address; /* TUNNEL_SOURCE_IPV4_ADDRESS */
312 ovs_be32 tunnel_destination_ipv4_address; /* TUNNEL_DESTINATION_IPV4_ADDRESS */
313 uint8_t tunnel_protocol_identifier; /* TUNNEL_PROTOCOL_IDENTIFIER */
314 ovs_be16 tunnel_source_transport_port; /* TUNNEL_SOURCE_TRANSPORT_PORT */
315 ovs_be16 tunnel_destination_transport_port; /* TUNNEL_DESTINATION_TRANSPORT_PORT */
316 uint8_t tunnel_type; /* TUNNEL_TYPE */
317 uint8_t tunnel_key_length; /* length of TUNNEL_KEY */
318 uint8_t tunnel_key[]; /* data of TUNNEL_KEY */
320 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_flow_key_tunnel) == 15);
322 /* Cf. IETF RFC 5102 Section 5.11.3. */
323 enum ipfix_flow_end_reason {
325 ACTIVE_TIMEOUT = 0x02,
326 END_OF_FLOW_DETECTED = 0x03,
328 LACK_OF_RESOURCES = 0x05
331 /* Part of data record for common aggregated elements. */
333 struct ipfix_data_record_aggregated_common {
334 ovs_be32 flow_start_delta_microseconds; /* FLOW_START_DELTA_MICROSECONDS */
335 ovs_be32 flow_end_delta_microseconds; /* FLOW_END_DELTA_MICROSECONDS */
336 ovs_be64 packet_delta_count; /* PACKET_DELTA_COUNT */
337 ovs_be64 layer2_octet_delta_count; /* LAYER2_OCTET_DELTA_COUNT */
338 uint8_t flow_end_reason; /* FLOW_END_REASON */
340 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_common) == 25);
342 /* Part of data record for IP aggregated elements. */
344 struct ipfix_data_record_aggregated_ip {
345 ovs_be64 octet_delta_count; /* OCTET_DELTA_COUNT */
346 ovs_be64 octet_delta_sum_of_squares; /* OCTET_DELTA_SUM_OF_SQUARES */
347 ovs_be64 minimum_ip_total_length; /* MINIMUM_IP_TOTAL_LENGTH */
348 ovs_be64 maximum_ip_total_length; /* MAXIMUM_IP_TOTAL_LENGTH */
350 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_aggregated_ip) == 32);
353 * support tunnel key for:
355 * GRE: 32- or 64-bit key,
356 * LISP: 24-bit instance ID
359 #define MAX_TUNNEL_KEY_LEN 8
361 #define MAX_FLOW_KEY_LEN \
362 (sizeof(struct ipfix_data_record_flow_key_common) \
363 + sizeof(struct ipfix_data_record_flow_key_vlan) \
364 + sizeof(struct ipfix_data_record_flow_key_ip) \
365 + MAX(sizeof(struct ipfix_data_record_flow_key_ipv4), \
366 sizeof(struct ipfix_data_record_flow_key_ipv6)) \
367 + MAX(sizeof(struct ipfix_data_record_flow_key_icmp), \
368 sizeof(struct ipfix_data_record_flow_key_transport)) \
369 + sizeof(struct ipfix_data_record_flow_key_tunnel) \
370 + MAX_TUNNEL_KEY_LEN)
372 #define MAX_DATA_RECORD_LEN \
374 + sizeof(struct ipfix_data_record_aggregated_common) \
375 + sizeof(struct ipfix_data_record_aggregated_ip))
377 /* Max length of a data set. To simplify the implementation, each
378 * data record is sent in a separate data set, so each data set
379 * contains at most one data record. */
380 #define MAX_DATA_SET_LEN \
381 (sizeof(struct ipfix_set_header) \
382 + MAX_DATA_RECORD_LEN)
384 /* Max length of an IPFIX message. Arbitrarily set to accommodate low
386 #define MAX_MESSAGE_LEN 1024
388 /* Cache structures. */
391 struct ipfix_flow_key {
392 uint32_t obs_domain_id;
393 uint16_t template_id;
394 size_t flow_key_msg_part_size;
395 uint64_t flow_key_msg_part[DIV_ROUND_UP(MAX_FLOW_KEY_LEN, 8)];
398 /* Flow cache entry. */
399 struct ipfix_flow_cache_entry {
400 struct hmap_node flow_key_map_node;
401 struct ovs_list cache_flow_start_timestamp_list_node;
402 struct ipfix_flow_key flow_key;
403 /* Common aggregated elements. */
404 uint64_t flow_start_timestamp_usec;
405 uint64_t flow_end_timestamp_usec;
406 uint64_t packet_delta_count;
407 uint64_t layer2_octet_delta_count;
408 uint64_t octet_delta_count;
409 uint64_t octet_delta_sum_of_squares; /* 0 if not IP. */
410 uint16_t minimum_ip_total_length; /* 0 if not IP. */
411 uint16_t maximum_ip_total_length; /* 0 if not IP. */
414 static void dpif_ipfix_cache_expire(struct dpif_ipfix_exporter *, bool,
415 const uint64_t, const uint32_t);
417 static void get_export_time_now(uint64_t *, uint32_t *);
419 static void dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter *, bool);
422 ofproto_ipfix_bridge_exporter_options_equal(
423 const struct ofproto_ipfix_bridge_exporter_options *a,
424 const struct ofproto_ipfix_bridge_exporter_options *b)
426 return (a->obs_domain_id == b->obs_domain_id
427 && a->obs_point_id == b->obs_point_id
428 && a->sampling_rate == b->sampling_rate
429 && a->cache_active_timeout == b->cache_active_timeout
430 && a->cache_max_flows == b->cache_max_flows
431 && a->enable_tunnel_sampling == b->enable_tunnel_sampling
432 && a->enable_input_sampling == b->enable_input_sampling
433 && a->enable_output_sampling == b->enable_output_sampling
434 && sset_equals(&a->targets, &b->targets));
437 static struct ofproto_ipfix_bridge_exporter_options *
438 ofproto_ipfix_bridge_exporter_options_clone(
439 const struct ofproto_ipfix_bridge_exporter_options *old)
441 struct ofproto_ipfix_bridge_exporter_options *new =
442 xmemdup(old, sizeof *old);
443 sset_clone(&new->targets, &old->targets);
448 ofproto_ipfix_bridge_exporter_options_destroy(
449 struct ofproto_ipfix_bridge_exporter_options *options)
452 sset_destroy(&options->targets);
458 ofproto_ipfix_flow_exporter_options_equal(
459 const struct ofproto_ipfix_flow_exporter_options *a,
460 const struct ofproto_ipfix_flow_exporter_options *b)
462 return (a->collector_set_id == b->collector_set_id
463 && a->cache_active_timeout == b->cache_active_timeout
464 && a->cache_max_flows == b->cache_max_flows
465 && sset_equals(&a->targets, &b->targets));
468 static struct ofproto_ipfix_flow_exporter_options *
469 ofproto_ipfix_flow_exporter_options_clone(
470 const struct ofproto_ipfix_flow_exporter_options *old)
472 struct ofproto_ipfix_flow_exporter_options *new =
473 xmemdup(old, sizeof *old);
474 sset_clone(&new->targets, &old->targets);
479 ofproto_ipfix_flow_exporter_options_destroy(
480 struct ofproto_ipfix_flow_exporter_options *options)
483 sset_destroy(&options->targets);
489 dpif_ipfix_exporter_init(struct dpif_ipfix_exporter *exporter)
491 exporter->collectors = NULL;
492 exporter->seq_number = 1;
493 exporter->last_template_set_time = TIME_MIN;
494 hmap_init(&exporter->cache_flow_key_map);
495 list_init(&exporter->cache_flow_start_timestamp_list);
496 exporter->cache_active_timeout = 0;
497 exporter->cache_max_flows = 0;
501 dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
503 /* Flush the cache with flow end reason "forced end." */
504 dpif_ipfix_cache_expire_now(exporter, true);
506 collectors_destroy(exporter->collectors);
507 exporter->collectors = NULL;
508 exporter->seq_number = 1;
509 exporter->last_template_set_time = TIME_MIN;
510 exporter->cache_active_timeout = 0;
511 exporter->cache_max_flows = 0;
515 dpif_ipfix_exporter_destroy(struct dpif_ipfix_exporter *exporter)
517 dpif_ipfix_exporter_clear(exporter);
518 hmap_destroy(&exporter->cache_flow_key_map);
522 dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter *exporter,
523 const struct sset *targets,
524 const uint32_t cache_active_timeout,
525 const uint32_t cache_max_flows)
527 collectors_destroy(exporter->collectors);
528 collectors_create(targets, IPFIX_DEFAULT_COLLECTOR_PORT,
529 &exporter->collectors);
530 if (exporter->collectors == NULL) {
531 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
532 "IPFIX exporter disabled");
533 dpif_ipfix_exporter_clear(exporter);
536 exporter->cache_active_timeout = cache_active_timeout;
537 exporter->cache_max_flows = cache_max_flows;
541 static struct dpif_ipfix_port *
542 dpif_ipfix_find_port(const struct dpif_ipfix *di,
543 odp_port_t odp_port) OVS_REQUIRES(mutex)
545 struct dpif_ipfix_port *dip;
547 HMAP_FOR_EACH_IN_BUCKET (dip, hmap_node, hash_odp_port(odp_port),
549 if (dip->odp_port == odp_port) {
557 dpif_ipfix_del_port(struct dpif_ipfix *di,
558 struct dpif_ipfix_port *dip)
561 hmap_remove(&di->tunnel_ports, &dip->hmap_node);
566 dpif_ipfix_add_tunnel_port(struct dpif_ipfix *di, struct ofport *ofport,
567 odp_port_t odp_port) OVS_EXCLUDED(mutex)
569 struct dpif_ipfix_port *dip;
572 ovs_mutex_lock(&mutex);
573 dip = dpif_ipfix_find_port(di, odp_port);
575 dpif_ipfix_del_port(di, dip);
578 type = netdev_get_type(ofport->netdev);
583 /* Add to table of tunnel ports. */
584 dip = xmalloc(sizeof *dip);
585 dip->ofport = ofport;
586 dip->odp_port = odp_port;
587 if (strcmp(type, "gre") == 0) {
589 dip->tunnel_type = DPIF_IPFIX_TUNNEL_GRE;
590 dip->tunnel_key_length = 4;
591 } else if (strcmp(type, "gre64") == 0) {
593 dip->tunnel_type = DPIF_IPFIX_TUNNEL_GRE;
594 dip->tunnel_key_length = 8;
595 } else if (strcmp(type, "ipsec_gre") == 0) {
596 /* 32-bit key ipsec_gre */
597 dip->tunnel_type = DPIF_IPFIX_TUNNEL_IPSEC_GRE;
598 dip->tunnel_key_length = 4;
599 } else if (strcmp(type, "ipsec_gre64") == 0) {
600 /* 64-bit key ipsec_gre */
601 dip->tunnel_type = DPIF_IPFIX_TUNNEL_IPSEC_GRE;
602 dip->tunnel_key_length = 8;
603 } else if (strcmp(type, "vxlan") == 0) {
604 dip->tunnel_type = DPIF_IPFIX_TUNNEL_VXLAN;
605 dip->tunnel_key_length = 3;
606 } else if (strcmp(type, "lisp") == 0) {
607 dip->tunnel_type = DPIF_IPFIX_TUNNEL_LISP;
608 dip->tunnel_key_length = 3;
609 } else if (strcmp(type, "geneve") == 0) {
610 dip->tunnel_type = DPIF_IPFIX_TUNNEL_GENEVE;
611 dip->tunnel_key_length = 3;
612 } else if (strcmp(type, "stt") == 0) {
613 dip->tunnel_type = DPIF_IPFIX_TUNNEL_STT;
614 dip->tunnel_key_length = 8;
619 hmap_insert(&di->tunnel_ports, &dip->hmap_node, hash_odp_port(odp_port));
622 ovs_mutex_unlock(&mutex);
626 dpif_ipfix_del_tunnel_port(struct dpif_ipfix *di, odp_port_t odp_port)
629 struct dpif_ipfix_port *dip;
630 ovs_mutex_lock(&mutex);
631 dip = dpif_ipfix_find_port(di, odp_port);
633 dpif_ipfix_del_port(di, dip);
635 ovs_mutex_unlock(&mutex);
639 dpif_ipfix_get_tunnel_port(const struct dpif_ipfix *di, odp_port_t odp_port)
642 struct dpif_ipfix_port *dip;
643 ovs_mutex_lock(&mutex);
644 dip = dpif_ipfix_find_port(di, odp_port);
645 ovs_mutex_unlock(&mutex);
650 dpif_ipfix_bridge_exporter_init(struct dpif_ipfix_bridge_exporter *exporter)
652 dpif_ipfix_exporter_init(&exporter->exporter);
653 exporter->options = NULL;
654 exporter->probability = 0;
658 dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
660 dpif_ipfix_exporter_clear(&exporter->exporter);
661 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
662 exporter->options = NULL;
663 exporter->probability = 0;
667 dpif_ipfix_bridge_exporter_destroy(struct dpif_ipfix_bridge_exporter *exporter)
669 dpif_ipfix_bridge_exporter_clear(exporter);
670 dpif_ipfix_exporter_destroy(&exporter->exporter);
674 dpif_ipfix_bridge_exporter_set_options(
675 struct dpif_ipfix_bridge_exporter *exporter,
676 const struct ofproto_ipfix_bridge_exporter_options *options)
678 bool options_changed;
680 if (!options || sset_is_empty(&options->targets)) {
681 /* No point in doing any work if there are no targets. */
682 dpif_ipfix_bridge_exporter_clear(exporter);
688 || !ofproto_ipfix_bridge_exporter_options_equal(
689 options, exporter->options));
691 /* Configure collectors if options have changed or if we're
692 * shortchanged in collectors (which indicates that opening one or
693 * more of the configured collectors failed, so that we should
696 || collectors_count(exporter->exporter.collectors)
697 < sset_count(&options->targets)) {
698 if (!dpif_ipfix_exporter_set_options(
699 &exporter->exporter, &options->targets,
700 options->cache_active_timeout, options->cache_max_flows)) {
705 /* Avoid reconfiguring if options didn't change. */
706 if (!options_changed) {
710 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
711 exporter->options = ofproto_ipfix_bridge_exporter_options_clone(options);
712 exporter->probability =
713 MAX(1, UINT32_MAX / exporter->options->sampling_rate);
715 /* Run over the cache as some entries might have expired after
716 * changing the timeouts. */
717 dpif_ipfix_cache_expire_now(&exporter->exporter, false);
720 static struct dpif_ipfix_flow_exporter_map_node*
721 dpif_ipfix_find_flow_exporter_map_node(
722 const struct dpif_ipfix *di, const uint32_t collector_set_id)
725 struct dpif_ipfix_flow_exporter_map_node *exporter_node;
727 HMAP_FOR_EACH_WITH_HASH (exporter_node, node,
728 hash_int(collector_set_id, 0),
729 &di->flow_exporter_map) {
730 if (exporter_node->exporter.options->collector_set_id
731 == collector_set_id) {
732 return exporter_node;
740 dpif_ipfix_flow_exporter_init(struct dpif_ipfix_flow_exporter *exporter)
742 dpif_ipfix_exporter_init(&exporter->exporter);
743 exporter->options = NULL;
747 dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
749 dpif_ipfix_exporter_clear(&exporter->exporter);
750 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
751 exporter->options = NULL;
755 dpif_ipfix_flow_exporter_destroy(struct dpif_ipfix_flow_exporter *exporter)
757 dpif_ipfix_flow_exporter_clear(exporter);
758 dpif_ipfix_exporter_destroy(&exporter->exporter);
762 dpif_ipfix_flow_exporter_set_options(
763 struct dpif_ipfix_flow_exporter *exporter,
764 const struct ofproto_ipfix_flow_exporter_options *options)
766 bool options_changed;
768 if (sset_is_empty(&options->targets)) {
769 /* No point in doing any work if there are no targets. */
770 dpif_ipfix_flow_exporter_clear(exporter);
776 || !ofproto_ipfix_flow_exporter_options_equal(
777 options, exporter->options));
779 /* Configure collectors if options have changed or if we're
780 * shortchanged in collectors (which indicates that opening one or
781 * more of the configured collectors failed, so that we should
784 || collectors_count(exporter->exporter.collectors)
785 < sset_count(&options->targets)) {
786 if (!dpif_ipfix_exporter_set_options(
787 &exporter->exporter, &options->targets,
788 options->cache_active_timeout, options->cache_max_flows)) {
793 /* Avoid reconfiguring if options didn't change. */
794 if (!options_changed) {
798 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
799 exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
801 /* Run over the cache as some entries might have expired after
802 * changing the timeouts. */
803 dpif_ipfix_cache_expire_now(&exporter->exporter, false);
809 dpif_ipfix_set_options(
810 struct dpif_ipfix *di,
811 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
812 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
813 size_t n_flow_exporters_options) OVS_EXCLUDED(mutex)
816 struct ofproto_ipfix_flow_exporter_options *options;
817 struct dpif_ipfix_flow_exporter_map_node *node, *next;
818 size_t n_broken_flow_exporters_options = 0;
820 ovs_mutex_lock(&mutex);
821 dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
822 bridge_exporter_options);
824 /* Add new flow exporters and update current flow exporters. */
825 options = (struct ofproto_ipfix_flow_exporter_options *)
826 flow_exporters_options;
827 for (i = 0; i < n_flow_exporters_options; i++) {
828 node = dpif_ipfix_find_flow_exporter_map_node(
829 di, options->collector_set_id);
831 node = xzalloc(sizeof *node);
832 dpif_ipfix_flow_exporter_init(&node->exporter);
833 hmap_insert(&di->flow_exporter_map, &node->node,
834 hash_int(options->collector_set_id, 0));
836 if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
837 n_broken_flow_exporters_options++;
842 ovs_assert(hmap_count(&di->flow_exporter_map) >=
843 (n_flow_exporters_options - n_broken_flow_exporters_options));
845 /* Remove dropped flow exporters, if any needs to be removed. */
846 if (hmap_count(&di->flow_exporter_map) > n_flow_exporters_options) {
847 HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
848 /* This is slow but doesn't take any extra memory, and
849 * this table is not supposed to contain many rows anyway. */
850 options = (struct ofproto_ipfix_flow_exporter_options *)
851 flow_exporters_options;
852 for (i = 0; i < n_flow_exporters_options; i++) {
853 if (node->exporter.options->collector_set_id
854 == options->collector_set_id) {
859 if (i == n_flow_exporters_options) { // Not found.
860 hmap_remove(&di->flow_exporter_map, &node->node);
861 dpif_ipfix_flow_exporter_destroy(&node->exporter);
867 ovs_assert(hmap_count(&di->flow_exporter_map) ==
868 (n_flow_exporters_options - n_broken_flow_exporters_options));
869 ovs_mutex_unlock(&mutex);
873 dpif_ipfix_create(void)
875 struct dpif_ipfix *di;
876 di = xzalloc(sizeof *di);
877 dpif_ipfix_bridge_exporter_init(&di->bridge_exporter);
878 hmap_init(&di->flow_exporter_map);
879 hmap_init(&di->tunnel_ports);
880 ovs_refcount_init(&di->ref_cnt);
885 dpif_ipfix_ref(const struct dpif_ipfix *di_)
887 struct dpif_ipfix *di = CONST_CAST(struct dpif_ipfix *, di_);
889 ovs_refcount_ref(&di->ref_cnt);
895 dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
899 ovs_mutex_lock(&mutex);
900 ret = di->bridge_exporter.probability;
901 ovs_mutex_unlock(&mutex);
906 dpif_ipfix_get_bridge_exporter_input_sampling(const struct dpif_ipfix *di)
910 ovs_mutex_lock(&mutex);
911 if (di->bridge_exporter.options) {
912 ret = di->bridge_exporter.options->enable_input_sampling;
914 ovs_mutex_unlock(&mutex);
919 dpif_ipfix_get_bridge_exporter_output_sampling(const struct dpif_ipfix *di)
923 ovs_mutex_lock(&mutex);
924 if (di->bridge_exporter.options) {
925 ret = di->bridge_exporter.options->enable_output_sampling;
927 ovs_mutex_unlock(&mutex);
932 dpif_ipfix_get_bridge_exporter_tunnel_sampling(const struct dpif_ipfix *di)
936 ovs_mutex_lock(&mutex);
937 if (di->bridge_exporter.options) {
938 ret = di->bridge_exporter.options->enable_tunnel_sampling;
940 ovs_mutex_unlock(&mutex);
945 dpif_ipfix_clear(struct dpif_ipfix *di) OVS_REQUIRES(mutex)
947 struct dpif_ipfix_flow_exporter_map_node *exp_node, *exp_next;
948 struct dpif_ipfix_port *dip, *next;
950 dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
952 HMAP_FOR_EACH_SAFE (exp_node, exp_next, node, &di->flow_exporter_map) {
953 hmap_remove(&di->flow_exporter_map, &exp_node->node);
954 dpif_ipfix_flow_exporter_destroy(&exp_node->exporter);
958 HMAP_FOR_EACH_SAFE (dip, next, hmap_node, &di->tunnel_ports) {
959 dpif_ipfix_del_port(di, dip);
964 dpif_ipfix_unref(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
966 if (di && ovs_refcount_unref_relaxed(&di->ref_cnt) == 1) {
967 ovs_mutex_lock(&mutex);
968 dpif_ipfix_clear(di);
969 dpif_ipfix_bridge_exporter_destroy(&di->bridge_exporter);
970 hmap_destroy(&di->flow_exporter_map);
971 hmap_destroy(&di->tunnel_ports);
973 ovs_mutex_unlock(&mutex);
978 ipfix_init_header(uint32_t export_time_sec, uint32_t seq_number,
979 uint32_t obs_domain_id, struct dp_packet *msg)
981 struct ipfix_header *hdr;
983 hdr = dp_packet_put_zeros(msg, sizeof *hdr);
984 hdr->version = htons(IPFIX_VERSION);
985 hdr->length = htons(sizeof *hdr); /* Updated in ipfix_send_msg. */
986 hdr->export_time = htonl(export_time_sec);
987 hdr->seq_number = htonl(seq_number);
988 hdr->obs_domain_id = htonl(obs_domain_id);
992 ipfix_send_msg(const struct collectors *collectors, struct dp_packet *msg)
994 struct ipfix_header *hdr;
996 /* Adjust the length in the header. */
997 hdr = dp_packet_data(msg);
998 hdr->length = htons(dp_packet_size(msg));
1000 collectors_send(collectors, dp_packet_data(msg), dp_packet_size(msg));
1001 dp_packet_set_size(msg, 0);
1005 ipfix_get_template_id(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
1006 enum ipfix_proto_l4 l4, enum ipfix_proto_tunnel tunnel)
1008 uint16_t template_id;
1010 template_id = template_id * NUM_IPFIX_PROTO_L3 + l3;
1011 template_id = template_id * NUM_IPFIX_PROTO_L4 + l4;
1012 template_id = template_id * NUM_IPFIX_PROTO_TUNNEL + tunnel;
1013 return IPFIX_TEMPLATE_ID_MIN + template_id;
1017 ipfix_define_template_entity(enum ipfix_entity_id id,
1018 enum ipfix_entity_size size,
1019 enum ipfix_entity_enterprise enterprise,
1020 struct dp_packet *msg)
1022 struct ipfix_template_field_specifier *field;
1026 field_size = sizeof *field;
1028 /* No enterprise number */
1029 field_size = sizeof *field - sizeof(ovs_be32);
1031 field = dp_packet_put_zeros(msg, field_size);
1032 field->element_id = htons(id);
1034 field->field_length = htons(size);
1036 /* RFC 5101, Section 7. Variable-Length Information Element */
1037 field->field_length = OVS_BE16_MAX;
1040 field->enterprise = htonl(enterprise);
1046 ipfix_define_template_fields(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
1047 enum ipfix_proto_l4 l4, enum ipfix_proto_tunnel tunnel,
1048 struct dp_packet *msg)
1054 ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
1055 IPFIX_ENTITY_SIZE_##ID, \
1056 IPFIX_ENTITY_ENTERPRISE_##ID, msg); \
1062 DEF(OBSERVATION_POINT_ID);
1063 DEF(FLOW_DIRECTION);
1065 /* Common Ethernet entities. */
1066 DEF(SOURCE_MAC_ADDRESS);
1067 DEF(DESTINATION_MAC_ADDRESS);
1069 DEF(ETHERNET_HEADER_LENGTH);
1071 if (l2 == IPFIX_PROTO_L2_VLAN) {
1074 DEF(DOT1Q_PRIORITY);
1077 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1080 DEF(PROTOCOL_IDENTIFIER);
1081 DEF(IP_DIFF_SERV_CODE_POINT);
1083 DEF(IP_CLASS_OF_SERVICE);
1085 if (l3 == IPFIX_PROTO_L3_IPV4) {
1086 DEF(SOURCE_IPV4_ADDRESS);
1087 DEF(DESTINATION_IPV4_ADDRESS);
1088 if (l4 == IPFIX_PROTO_L4_TCP_UDP_SCTP) {
1089 DEF(SOURCE_TRANSPORT_PORT);
1090 DEF(DESTINATION_TRANSPORT_PORT);
1091 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
1092 DEF(ICMP_TYPE_IPV4);
1093 DEF(ICMP_CODE_IPV4);
1095 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
1096 DEF(SOURCE_IPV6_ADDRESS);
1097 DEF(DESTINATION_IPV6_ADDRESS);
1098 DEF(FLOW_LABEL_IPV6);
1099 if (l4 == IPFIX_PROTO_L4_TCP_UDP_SCTP) {
1100 DEF(SOURCE_TRANSPORT_PORT);
1101 DEF(DESTINATION_TRANSPORT_PORT);
1102 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
1103 DEF(ICMP_TYPE_IPV6);
1104 DEF(ICMP_CODE_IPV6);
1109 if (tunnel != IPFIX_PROTO_NOT_TUNNELED) {
1110 DEF(TUNNEL_SOURCE_IPV4_ADDRESS);
1111 DEF(TUNNEL_DESTINATION_IPV4_ADDRESS);
1112 DEF(TUNNEL_PROTOCOL_IDENTIFIER);
1113 DEF(TUNNEL_SOURCE_TRANSPORT_PORT);
1114 DEF(TUNNEL_DESTINATION_TRANSPORT_PORT);
1119 /* 2. Flow aggregated data. */
1121 DEF(FLOW_START_DELTA_MICROSECONDS);
1122 DEF(FLOW_END_DELTA_MICROSECONDS);
1123 DEF(PACKET_DELTA_COUNT);
1124 DEF(LAYER2_OCTET_DELTA_COUNT);
1125 DEF(FLOW_END_REASON);
1127 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1128 DEF(OCTET_DELTA_COUNT);
1129 DEF(OCTET_DELTA_SUM_OF_SQUARES);
1130 DEF(MINIMUM_IP_TOTAL_LENGTH);
1131 DEF(MAXIMUM_IP_TOTAL_LENGTH);
1141 ipfix_init_template_msg(void *msg_stub, uint32_t export_time_sec,
1142 uint32_t seq_number, uint32_t obs_domain_id,
1143 struct dp_packet *msg, size_t *set_hdr_offset)
1145 struct ipfix_set_header *set_hdr;
1147 dp_packet_use_stub(msg, msg_stub, sizeof msg_stub);
1149 ipfix_init_header(export_time_sec, seq_number, obs_domain_id, msg);
1150 *set_hdr_offset = dp_packet_size(msg);
1152 /* Add a Template Set. */
1153 set_hdr = dp_packet_put_zeros(msg, sizeof *set_hdr);
1154 set_hdr->set_id = htons(IPFIX_SET_ID_TEMPLATE);
1158 ipfix_send_template_msg(const struct collectors *collectors,
1159 struct dp_packet *msg, size_t set_hdr_offset)
1161 struct ipfix_set_header *set_hdr;
1163 /* Send template message. */
1164 set_hdr = (struct ipfix_set_header*)
1165 ((uint8_t*)dp_packet_data(msg) + set_hdr_offset);
1166 set_hdr->length = htons(dp_packet_size(msg) - set_hdr_offset);
1168 ipfix_send_msg(collectors, msg);
1170 dp_packet_uninit(msg);
1174 ipfix_send_template_msgs(struct dpif_ipfix_exporter *exporter,
1175 uint32_t export_time_sec, uint32_t obs_domain_id)
1177 uint64_t msg_stub[DIV_ROUND_UP(MAX_MESSAGE_LEN, 8)];
1178 struct dp_packet msg;
1179 size_t set_hdr_offset, tmpl_hdr_offset;
1180 struct ipfix_template_record_header *tmpl_hdr;
1181 uint16_t field_count;
1182 enum ipfix_proto_l2 l2;
1183 enum ipfix_proto_l3 l3;
1184 enum ipfix_proto_l4 l4;
1185 enum ipfix_proto_tunnel tunnel;
1187 ipfix_init_template_msg(msg_stub, export_time_sec, exporter->seq_number,
1188 obs_domain_id, &msg, &set_hdr_offset);
1189 /* Define one template for each possible combination of
1191 for (l2 = 0; l2 < NUM_IPFIX_PROTO_L2; l2++) {
1192 for (l3 = 0; l3 < NUM_IPFIX_PROTO_L3; l3++) {
1193 for (l4 = 0; l4 < NUM_IPFIX_PROTO_L4; l4++) {
1194 if (l3 == IPFIX_PROTO_L3_UNKNOWN &&
1195 l4 != IPFIX_PROTO_L4_UNKNOWN) {
1198 for (tunnel = 0; tunnel < NUM_IPFIX_PROTO_TUNNEL; tunnel++) {
1199 /* When the size of the template packet reaches
1200 * MAX_MESSAGE_LEN(1024), send it out.
1201 * And then reinitialize the msg to construct a new
1202 * packet for the following templates.
1204 if (dp_packet_size(&msg) >= MAX_MESSAGE_LEN) {
1205 /* Send template message. */
1206 ipfix_send_template_msg(exporter->collectors,
1207 &msg, set_hdr_offset);
1209 /* Reinitialize the template msg. */
1210 ipfix_init_template_msg(msg_stub, export_time_sec,
1211 exporter->seq_number,
1212 obs_domain_id, &msg,
1216 tmpl_hdr_offset = dp_packet_size(&msg);
1217 tmpl_hdr = dp_packet_put_zeros(&msg, sizeof *tmpl_hdr);
1218 tmpl_hdr->template_id = htons(
1219 ipfix_get_template_id(l2, l3, l4, tunnel));
1221 ipfix_define_template_fields(l2, l3, l4, tunnel, &msg);
1222 tmpl_hdr = (struct ipfix_template_record_header*)
1223 ((uint8_t*)dp_packet_data(&msg) + tmpl_hdr_offset);
1224 tmpl_hdr->field_count = htons(field_count);
1230 /* Send template message. */
1231 ipfix_send_template_msg(exporter->collectors, &msg, set_hdr_offset);
1233 /* XXX: Add Options Template Sets, at least to define a Flow Keys
1234 * Option Template. */
1238 static inline uint32_t
1239 ipfix_hash_flow_key(const struct ipfix_flow_key *flow_key, uint32_t basis)
1242 hash = hash_int(flow_key->obs_domain_id, basis);
1243 hash = hash_int(flow_key->template_id, hash);
1244 hash = hash_bytes(flow_key->flow_key_msg_part,
1245 flow_key->flow_key_msg_part_size, hash);
1250 ipfix_flow_key_equal(const struct ipfix_flow_key *a,
1251 const struct ipfix_flow_key *b)
1253 /* The template ID determines the flow key size, so not need to
1255 return (a->obs_domain_id == b->obs_domain_id
1256 && a->template_id == b->template_id
1257 && memcmp(a->flow_key_msg_part, b->flow_key_msg_part,
1258 a->flow_key_msg_part_size) == 0);
1261 static struct ipfix_flow_cache_entry*
1262 ipfix_cache_find_entry(const struct dpif_ipfix_exporter *exporter,
1263 const struct ipfix_flow_key *flow_key)
1265 struct ipfix_flow_cache_entry *entry;
1267 HMAP_FOR_EACH_WITH_HASH (entry, flow_key_map_node,
1268 ipfix_hash_flow_key(flow_key, 0),
1269 &exporter->cache_flow_key_map) {
1270 if (ipfix_flow_key_equal(&entry->flow_key, flow_key)) {
1279 ipfix_cache_next_timeout_msec(const struct dpif_ipfix_exporter *exporter,
1280 long long int *next_timeout_msec)
1282 struct ipfix_flow_cache_entry *entry;
1284 LIST_FOR_EACH (entry, cache_flow_start_timestamp_list_node,
1285 &exporter->cache_flow_start_timestamp_list) {
1286 *next_timeout_msec = entry->flow_start_timestamp_usec / 1000LL
1287 + 1000LL * exporter->cache_active_timeout;
1295 ipfix_cache_aggregate_entries(struct ipfix_flow_cache_entry *from_entry,
1296 struct ipfix_flow_cache_entry *to_entry)
1298 uint64_t *to_start, *to_end, *from_start, *from_end;
1299 uint16_t *to_min_len, *to_max_len, *from_min_len, *from_max_len;
1301 to_start = &to_entry->flow_start_timestamp_usec;
1302 to_end = &to_entry->flow_end_timestamp_usec;
1303 from_start = &from_entry->flow_start_timestamp_usec;
1304 from_end = &from_entry->flow_end_timestamp_usec;
1306 if (*to_start > *from_start) {
1307 *to_start = *from_start;
1309 if (*to_end < *from_end) {
1310 *to_end = *from_end;
1313 to_entry->packet_delta_count += from_entry->packet_delta_count;
1314 to_entry->layer2_octet_delta_count += from_entry->layer2_octet_delta_count;
1316 to_entry->octet_delta_count += from_entry->octet_delta_count;
1317 to_entry->octet_delta_sum_of_squares +=
1318 from_entry->octet_delta_sum_of_squares;
1320 to_min_len = &to_entry->minimum_ip_total_length;
1321 to_max_len = &to_entry->maximum_ip_total_length;
1322 from_min_len = &from_entry->minimum_ip_total_length;
1323 from_max_len = &from_entry->maximum_ip_total_length;
1325 if (!*to_min_len || (*from_min_len && *to_min_len > *from_min_len)) {
1326 *to_min_len = *from_min_len;
1328 if (*to_max_len < *from_max_len) {
1329 *to_max_len = *from_max_len;
1333 /* Add an entry into a flow cache. The entry is either aggregated into
1334 * an existing entry with the same flow key and free()d, or it is
1335 * inserted into the cache. */
1337 ipfix_cache_update(struct dpif_ipfix_exporter *exporter,
1338 struct ipfix_flow_cache_entry *entry)
1340 struct ipfix_flow_cache_entry *old_entry;
1342 old_entry = ipfix_cache_find_entry(exporter, &entry->flow_key);
1344 if (old_entry == NULL) {
1345 hmap_insert(&exporter->cache_flow_key_map, &entry->flow_key_map_node,
1346 ipfix_hash_flow_key(&entry->flow_key, 0));
1348 /* As the latest entry added into the cache, it should
1349 * logically have the highest flow_start_timestamp_usec, so
1350 * append it at the tail. */
1351 list_push_back(&exporter->cache_flow_start_timestamp_list,
1352 &entry->cache_flow_start_timestamp_list_node);
1354 /* Enforce exporter->cache_max_flows limit. */
1355 if (hmap_count(&exporter->cache_flow_key_map)
1356 > exporter->cache_max_flows) {
1357 dpif_ipfix_cache_expire_now(exporter, false);
1360 ipfix_cache_aggregate_entries(entry, old_entry);
1366 ipfix_cache_entry_init(struct ipfix_flow_cache_entry *entry,
1367 const struct dp_packet *packet, const struct flow *flow,
1368 uint64_t packet_delta_count, uint32_t obs_domain_id,
1369 uint32_t obs_point_id, odp_port_t output_odp_port,
1370 const struct dpif_ipfix_port *tunnel_port,
1371 const struct flow_tnl *tunnel_key)
1373 struct ipfix_flow_key *flow_key;
1374 struct dp_packet msg;
1375 enum ipfix_proto_l2 l2;
1376 enum ipfix_proto_l3 l3;
1377 enum ipfix_proto_l4 l4;
1378 enum ipfix_proto_tunnel tunnel = IPFIX_PROTO_NOT_TUNNELED;
1379 uint8_t ethernet_header_length;
1380 uint16_t ethernet_total_length;
1382 flow_key = &entry->flow_key;
1383 dp_packet_use_stub(&msg, flow_key->flow_key_msg_part,
1384 sizeof flow_key->flow_key_msg_part);
1386 /* Choose the right template ID matching the protocols in the
1387 * sampled packet. */
1388 l2 = (flow->vlan_tci == 0) ? IPFIX_PROTO_L2_ETH : IPFIX_PROTO_L2_VLAN;
1390 switch(ntohs(flow->dl_type)) {
1392 l3 = IPFIX_PROTO_L3_IPV4;
1393 switch(flow->nw_proto) {
1397 l4 = IPFIX_PROTO_L4_TCP_UDP_SCTP;
1400 l4 = IPFIX_PROTO_L4_ICMP;
1403 l4 = IPFIX_PROTO_L4_UNKNOWN;
1407 l3 = IPFIX_PROTO_L3_IPV6;
1408 switch(flow->nw_proto) {
1412 l4 = IPFIX_PROTO_L4_TCP_UDP_SCTP;
1414 case IPPROTO_ICMPV6:
1415 l4 = IPFIX_PROTO_L4_ICMP;
1418 l4 = IPFIX_PROTO_L4_UNKNOWN;
1422 l3 = IPFIX_PROTO_L3_UNKNOWN;
1423 l4 = IPFIX_PROTO_L4_UNKNOWN;
1426 if (tunnel_port && tunnel_key) {
1427 tunnel = IPFIX_PROTO_TUNNELED;
1430 flow_key->obs_domain_id = obs_domain_id;
1431 flow_key->template_id = ipfix_get_template_id(l2, l3, l4, tunnel);
1433 /* The fields defined in the ipfix_data_record_* structs and sent
1434 * below must match exactly the templates defined in
1435 * ipfix_define_template_fields. */
1437 ethernet_header_length = (l2 == IPFIX_PROTO_L2_VLAN)
1438 ? VLAN_ETH_HEADER_LEN : ETH_HEADER_LEN;
1439 ethernet_total_length = dp_packet_size(packet);
1441 /* Common Ethernet entities. */
1443 struct ipfix_data_record_flow_key_common *data_common;
1445 data_common = dp_packet_put_zeros(&msg, sizeof *data_common);
1446 data_common->observation_point_id = htonl(obs_point_id);
1447 data_common->flow_direction =
1448 (output_odp_port == ODPP_NONE) ? INGRESS_FLOW : EGRESS_FLOW;
1449 memcpy(data_common->source_mac_address, flow->dl_src,
1450 sizeof flow->dl_src);
1451 memcpy(data_common->destination_mac_address, flow->dl_dst,
1452 sizeof flow->dl_dst);
1453 data_common->ethernet_type = flow->dl_type;
1454 data_common->ethernet_header_length = ethernet_header_length;
1457 if (l2 == IPFIX_PROTO_L2_VLAN) {
1458 struct ipfix_data_record_flow_key_vlan *data_vlan;
1459 uint16_t vlan_id = vlan_tci_to_vid(flow->vlan_tci);
1460 uint8_t priority = vlan_tci_to_pcp(flow->vlan_tci);
1462 data_vlan = dp_packet_put_zeros(&msg, sizeof *data_vlan);
1463 data_vlan->vlan_id = htons(vlan_id);
1464 data_vlan->dot1q_vlan_id = htons(vlan_id);
1465 data_vlan->dot1q_priority = priority;
1468 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1469 struct ipfix_data_record_flow_key_ip *data_ip;
1471 data_ip = dp_packet_put_zeros(&msg, sizeof *data_ip);
1472 data_ip->ip_version = (l3 == IPFIX_PROTO_L3_IPV4) ? 4 : 6;
1473 data_ip->ip_ttl = flow->nw_ttl;
1474 data_ip->protocol_identifier = flow->nw_proto;
1475 data_ip->ip_diff_serv_code_point = flow->nw_tos >> 2;
1476 data_ip->ip_precedence = flow->nw_tos >> 5;
1477 data_ip->ip_class_of_service = flow->nw_tos;
1479 if (l3 == IPFIX_PROTO_L3_IPV4) {
1480 struct ipfix_data_record_flow_key_ipv4 *data_ipv4;
1482 data_ipv4 = dp_packet_put_zeros(&msg, sizeof *data_ipv4);
1483 data_ipv4->source_ipv4_address = flow->nw_src;
1484 data_ipv4->destination_ipv4_address = flow->nw_dst;
1485 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
1486 struct ipfix_data_record_flow_key_ipv6 *data_ipv6;
1488 data_ipv6 = dp_packet_put_zeros(&msg, sizeof *data_ipv6);
1489 memcpy(data_ipv6->source_ipv6_address, &flow->ipv6_src,
1490 sizeof flow->ipv6_src);
1491 memcpy(data_ipv6->destination_ipv6_address, &flow->ipv6_dst,
1492 sizeof flow->ipv6_dst);
1493 data_ipv6->flow_label_ipv6 = flow->ipv6_label;
1497 if (l4 == IPFIX_PROTO_L4_TCP_UDP_SCTP) {
1498 struct ipfix_data_record_flow_key_transport *data_transport;
1500 data_transport = dp_packet_put_zeros(&msg, sizeof *data_transport);
1501 data_transport->source_transport_port = flow->tp_src;
1502 data_transport->destination_transport_port = flow->tp_dst;
1503 } else if (l4 == IPFIX_PROTO_L4_ICMP) {
1504 struct ipfix_data_record_flow_key_icmp *data_icmp;
1506 data_icmp = dp_packet_put_zeros(&msg, sizeof *data_icmp);
1507 data_icmp->icmp_type = ntohs(flow->tp_src) & 0xff;
1508 data_icmp->icmp_code = ntohs(flow->tp_dst) & 0xff;
1511 if (tunnel == IPFIX_PROTO_TUNNELED) {
1512 struct ipfix_data_record_flow_key_tunnel *data_tunnel;
1513 const uint8_t *tun_id;
1515 data_tunnel = dp_packet_put_zeros(&msg, sizeof *data_tunnel +
1516 tunnel_port->tunnel_key_length);
1517 data_tunnel->tunnel_source_ipv4_address = tunnel_key->ip_src;
1518 data_tunnel->tunnel_destination_ipv4_address = tunnel_key->ip_dst;
1519 /* The tunnel_protocol_identifier is from tunnel_proto array, which
1520 * contains protocol_identifiers of each tunnel type.
1521 * For the tunnel type on the top of IPSec, which uses the protocol
1522 * identifier of the upper tunnel type is used, the tcp_src and tcp_dst
1523 * are decided based on the protocol identifiers.
1525 * The protocol identifier of DPIF_IPFIX_TUNNEL_IPSEC_GRE is IPPROTO_GRE,
1526 * and both tp_src and tp_dst are zero.
1528 data_tunnel->tunnel_protocol_identifier =
1529 tunnel_protocol[tunnel_port->tunnel_type];
1530 data_tunnel->tunnel_source_transport_port = tunnel_key->tp_src;
1531 data_tunnel->tunnel_destination_transport_port = tunnel_key->tp_dst;
1532 data_tunnel->tunnel_type = tunnel_port->tunnel_type;
1533 data_tunnel->tunnel_key_length = tunnel_port->tunnel_key_length;
1534 /* tun_id is in network order, and tunnel key is in low bits. */
1535 tun_id = (const uint8_t *) &tunnel_key->tun_id;
1536 memcpy(data_tunnel->tunnel_key,
1537 &tun_id[8 - tunnel_port->tunnel_key_length],
1538 tunnel_port->tunnel_key_length);
1541 flow_key->flow_key_msg_part_size = dp_packet_size(&msg);
1545 uint64_t layer2_octet_delta_count;
1547 /* Calculate the total matched octet count by considering as
1548 * an approximation that all matched packets have the same
1550 layer2_octet_delta_count = packet_delta_count * ethernet_total_length;
1552 xgettimeofday(&now);
1553 entry->flow_end_timestamp_usec = now.tv_usec + 1000000LL * now.tv_sec;
1554 entry->flow_start_timestamp_usec = entry->flow_end_timestamp_usec;
1555 entry->packet_delta_count = packet_delta_count;
1556 entry->layer2_octet_delta_count = layer2_octet_delta_count;
1559 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
1560 uint16_t ip_total_length =
1561 ethernet_total_length - ethernet_header_length;
1562 uint64_t octet_delta_count;
1564 /* Calculate the total matched octet count by considering as
1565 * an approximation that all matched packets have the same
1567 octet_delta_count = packet_delta_count * ip_total_length;
1569 entry->octet_delta_count = octet_delta_count;
1570 entry->octet_delta_sum_of_squares = octet_delta_count * ip_total_length;
1571 entry->minimum_ip_total_length = ip_total_length;
1572 entry->maximum_ip_total_length = ip_total_length;
1574 entry->octet_delta_sum_of_squares = 0;
1575 entry->minimum_ip_total_length = 0;
1576 entry->maximum_ip_total_length = 0;
1580 /* Send each single data record in its own data set, to simplify the
1581 * implementation by avoiding having to group record by template ID
1582 * before sending. */
1584 ipfix_put_data_set(uint32_t export_time_sec,
1585 struct ipfix_flow_cache_entry *entry,
1586 enum ipfix_flow_end_reason flow_end_reason,
1587 struct dp_packet *msg)
1589 size_t set_hdr_offset;
1590 struct ipfix_set_header *set_hdr;
1592 set_hdr_offset = dp_packet_size(msg);
1594 /* Put a Data Set. */
1595 set_hdr = dp_packet_put_zeros(msg, sizeof *set_hdr);
1596 set_hdr->set_id = htons(entry->flow_key.template_id);
1598 /* Copy the flow key part of the data record. */
1600 dp_packet_put(msg, entry->flow_key.flow_key_msg_part,
1601 entry->flow_key.flow_key_msg_part_size);
1603 /* Put the non-key part of the data record. */
1606 struct ipfix_data_record_aggregated_common *data_aggregated_common;
1607 uint64_t export_time_usec, flow_start_delta_usec, flow_end_delta_usec;
1609 /* Calculate the negative deltas relative to the export time
1610 * in seconds sent in the header, not the exact export
1612 export_time_usec = 1000000LL * export_time_sec;
1613 flow_start_delta_usec = export_time_usec
1614 - entry->flow_start_timestamp_usec;
1615 flow_end_delta_usec = export_time_usec
1616 - entry->flow_end_timestamp_usec;
1618 data_aggregated_common = dp_packet_put_zeros(
1619 msg, sizeof *data_aggregated_common);
1620 data_aggregated_common->flow_start_delta_microseconds = htonl(
1621 flow_start_delta_usec);
1622 data_aggregated_common->flow_end_delta_microseconds = htonl(
1623 flow_end_delta_usec);
1624 data_aggregated_common->packet_delta_count = htonll(
1625 entry->packet_delta_count);
1626 data_aggregated_common->layer2_octet_delta_count = htonll(
1627 entry->layer2_octet_delta_count);
1628 data_aggregated_common->flow_end_reason = flow_end_reason;
1631 if (entry->octet_delta_sum_of_squares) { /* IP packet. */
1632 struct ipfix_data_record_aggregated_ip *data_aggregated_ip;
1634 data_aggregated_ip = dp_packet_put_zeros(
1635 msg, sizeof *data_aggregated_ip);
1636 data_aggregated_ip->octet_delta_count = htonll(
1637 entry->octet_delta_count);
1638 data_aggregated_ip->octet_delta_sum_of_squares = htonll(
1639 entry->octet_delta_sum_of_squares);
1640 data_aggregated_ip->minimum_ip_total_length = htonll(
1641 entry->minimum_ip_total_length);
1642 data_aggregated_ip->maximum_ip_total_length = htonll(
1643 entry->maximum_ip_total_length);
1646 set_hdr = (struct ipfix_set_header*)((uint8_t*)dp_packet_data(msg) + set_hdr_offset);
1647 set_hdr->length = htons(dp_packet_size(msg) - set_hdr_offset);
1650 /* Send an IPFIX message with a single data record. */
1652 ipfix_send_data_msg(struct dpif_ipfix_exporter *exporter,
1653 uint32_t export_time_sec,
1654 struct ipfix_flow_cache_entry *entry,
1655 enum ipfix_flow_end_reason flow_end_reason)
1657 uint64_t msg_stub[DIV_ROUND_UP(MAX_MESSAGE_LEN, 8)];
1658 struct dp_packet msg;
1659 dp_packet_use_stub(&msg, msg_stub, sizeof msg_stub);
1661 ipfix_init_header(export_time_sec, exporter->seq_number++,
1662 entry->flow_key.obs_domain_id, &msg);
1663 ipfix_put_data_set(export_time_sec, entry, flow_end_reason, &msg);
1664 ipfix_send_msg(exporter->collectors, &msg);
1666 dp_packet_uninit(&msg);
1670 dpif_ipfix_sample(struct dpif_ipfix_exporter *exporter,
1671 const struct dp_packet *packet, const struct flow *flow,
1672 uint64_t packet_delta_count, uint32_t obs_domain_id,
1673 uint32_t obs_point_id, odp_port_t output_odp_port,
1674 const struct dpif_ipfix_port *tunnel_port,
1675 const struct flow_tnl *tunnel_key)
1677 struct ipfix_flow_cache_entry *entry;
1679 /* Create a flow cache entry from the sample. */
1680 entry = xmalloc(sizeof *entry);
1681 ipfix_cache_entry_init(entry, packet, flow, packet_delta_count,
1682 obs_domain_id, obs_point_id,
1683 output_odp_port, tunnel_port, tunnel_key);
1684 ipfix_cache_update(exporter, entry);
1688 dpif_ipfix_bridge_sample(struct dpif_ipfix *di, const struct dp_packet *packet,
1689 const struct flow *flow,
1690 odp_port_t input_odp_port, odp_port_t output_odp_port,
1691 const struct flow_tnl *output_tunnel_key)
1694 uint64_t packet_delta_count;
1695 const struct flow_tnl *tunnel_key = NULL;
1696 struct dpif_ipfix_port * tunnel_port = NULL;
1698 ovs_mutex_lock(&mutex);
1699 /* Use the sampling probability as an approximation of the number
1700 * of matched packets. */
1701 packet_delta_count = UINT32_MAX / di->bridge_exporter.probability;
1702 if (di->bridge_exporter.options->enable_tunnel_sampling) {
1703 if (output_odp_port == ODPP_NONE && flow->tunnel.ip_dst) {
1705 tunnel_key = &flow->tunnel;
1706 tunnel_port = dpif_ipfix_find_port(di, input_odp_port);
1708 if (output_odp_port != ODPP_NONE && output_tunnel_key) {
1709 /* Output tunnel, output_tunnel_key must be valid. */
1710 tunnel_key = output_tunnel_key;
1711 tunnel_port = dpif_ipfix_find_port(di, output_odp_port);
1714 dpif_ipfix_sample(&di->bridge_exporter.exporter, packet, flow,
1716 di->bridge_exporter.options->obs_domain_id,
1717 di->bridge_exporter.options->obs_point_id,
1718 output_odp_port, tunnel_port, tunnel_key);
1719 ovs_mutex_unlock(&mutex);
1723 dpif_ipfix_flow_sample(struct dpif_ipfix *di, const struct dp_packet *packet,
1724 const struct flow *flow, uint32_t collector_set_id,
1725 uint16_t probability, uint32_t obs_domain_id,
1726 uint32_t obs_point_id) OVS_EXCLUDED(mutex)
1728 struct dpif_ipfix_flow_exporter_map_node *node;
1729 /* Use the sampling probability as an approximation of the number
1730 * of matched packets. */
1731 uint64_t packet_delta_count = USHRT_MAX / probability;
1733 ovs_mutex_lock(&mutex);
1734 node = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
1736 dpif_ipfix_sample(&node->exporter.exporter, packet, flow,
1737 packet_delta_count, obs_domain_id, obs_point_id,
1738 ODPP_NONE, NULL, NULL);
1740 ovs_mutex_unlock(&mutex);
1744 dpif_ipfix_cache_expire(struct dpif_ipfix_exporter *exporter,
1745 bool forced_end, const uint64_t export_time_usec,
1746 const uint32_t export_time_sec)
1748 struct ipfix_flow_cache_entry *entry, *next_entry;
1749 uint64_t max_flow_start_timestamp_usec;
1750 bool template_msg_sent = false;
1751 enum ipfix_flow_end_reason flow_end_reason;
1753 if (list_is_empty(&exporter->cache_flow_start_timestamp_list)) {
1757 max_flow_start_timestamp_usec = export_time_usec -
1758 1000000LL * exporter->cache_active_timeout;
1760 LIST_FOR_EACH_SAFE (entry, next_entry, cache_flow_start_timestamp_list_node,
1761 &exporter->cache_flow_start_timestamp_list) {
1763 flow_end_reason = FORCED_END;
1764 } else if (entry->flow_start_timestamp_usec
1765 <= max_flow_start_timestamp_usec) {
1766 flow_end_reason = ACTIVE_TIMEOUT;
1767 } else if (hmap_count(&exporter->cache_flow_key_map)
1768 > exporter->cache_max_flows) {
1769 /* Enforce exporter->cache_max_flows. */
1770 flow_end_reason = LACK_OF_RESOURCES;
1772 /* Remaining flows haven't expired yet. */
1776 list_remove(&entry->cache_flow_start_timestamp_list_node);
1777 hmap_remove(&exporter->cache_flow_key_map,
1778 &entry->flow_key_map_node);
1780 if (!template_msg_sent
1781 && (exporter->last_template_set_time + IPFIX_TEMPLATE_INTERVAL)
1782 <= export_time_sec) {
1783 ipfix_send_template_msgs(exporter, export_time_sec,
1784 entry->flow_key.obs_domain_id);
1785 exporter->last_template_set_time = export_time_sec;
1786 template_msg_sent = true;
1789 /* XXX: Group multiple data records for the same obs domain id
1790 * into the same message. */
1791 ipfix_send_data_msg(exporter, export_time_sec, entry, flow_end_reason);
1797 get_export_time_now(uint64_t *export_time_usec, uint32_t *export_time_sec)
1799 struct timeval export_time;
1800 xgettimeofday(&export_time);
1802 *export_time_usec = export_time.tv_usec + 1000000LL * export_time.tv_sec;
1804 /* The IPFIX start and end deltas are negative deltas relative to
1805 * the export time, so set the export time 1 second off to
1806 * calculate those deltas. */
1807 if (export_time.tv_usec == 0) {
1808 *export_time_sec = export_time.tv_sec;
1810 *export_time_sec = export_time.tv_sec + 1;
1815 dpif_ipfix_cache_expire_now(struct dpif_ipfix_exporter *exporter,
1818 uint64_t export_time_usec;
1819 uint32_t export_time_sec;
1821 get_export_time_now(&export_time_usec, &export_time_sec);
1822 dpif_ipfix_cache_expire(exporter, forced_end, export_time_usec,
1827 dpif_ipfix_run(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
1829 uint64_t export_time_usec;
1830 uint32_t export_time_sec;
1831 struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
1833 ovs_mutex_lock(&mutex);
1834 get_export_time_now(&export_time_usec, &export_time_sec);
1835 if (di->bridge_exporter.probability > 0) { /* Bridge exporter enabled. */
1836 dpif_ipfix_cache_expire(
1837 &di->bridge_exporter.exporter, false, export_time_usec,
1840 HMAP_FOR_EACH (flow_exporter_node, node, &di->flow_exporter_map) {
1841 dpif_ipfix_cache_expire(
1842 &flow_exporter_node->exporter.exporter, false, export_time_usec,
1845 ovs_mutex_unlock(&mutex);
1849 dpif_ipfix_wait(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
1851 long long int next_timeout_msec = LLONG_MAX;
1852 struct dpif_ipfix_flow_exporter_map_node *flow_exporter_node;
1854 ovs_mutex_lock(&mutex);
1855 if (di->bridge_exporter.probability > 0) { /* Bridge exporter enabled. */
1856 if (ipfix_cache_next_timeout_msec(
1857 &di->bridge_exporter.exporter, &next_timeout_msec)) {
1858 poll_timer_wait_until(next_timeout_msec);
1861 HMAP_FOR_EACH (flow_exporter_node, node, &di->flow_exporter_map) {
1862 if (ipfix_cache_next_timeout_msec(
1863 &flow_exporter_node->exporter.exporter, &next_timeout_msec)) {
1864 poll_timer_wait_until(next_timeout_msec);
1867 ovs_mutex_unlock(&mutex);