2 * Copyright (c) 2012 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"
19 #include "byte-order.h"
20 #include "collectors.h"
33 VLOG_DEFINE_THIS_MODULE(ipfix);
35 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
36 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
38 /* Cf. IETF RFC 5101 Section 10.3.4. */
39 #define IPFIX_DEFAULT_COLLECTOR_PORT 4739
41 struct dpif_ipfix_exporter {
42 struct collectors *collectors;
44 time_t last_template_set_time;
47 struct dpif_ipfix_bridge_exporter {
48 struct dpif_ipfix_exporter exporter;
49 struct ofproto_ipfix_bridge_exporter_options *options;
53 struct dpif_ipfix_flow_exporter {
54 struct dpif_ipfix_exporter exporter;
55 struct ofproto_ipfix_flow_exporter_options *options;
58 struct dpif_ipfix_flow_exporter_map_node {
59 struct hmap_node node;
60 struct dpif_ipfix_flow_exporter exporter;
64 struct dpif_ipfix_bridge_exporter bridge_exporter;
65 struct hmap flow_exporter_map; /* dpif_ipfix_flow_exporter_map_nodes. */
69 #define IPFIX_VERSION 0x000a
71 /* When using UDP, IPFIX Template Records must be re-sent regularly.
72 * The standard default interval is 10 minutes (600 seconds).
73 * Cf. IETF RFC 5101 Section 10.3.6. */
74 #define IPFIX_TEMPLATE_INTERVAL 600
76 /* Cf. IETF RFC 5101 Section 3.1. */
79 ovs_be16 version; /* IPFIX_VERSION. */
80 ovs_be16 length; /* Length in bytes including this header. */
81 ovs_be32 export_time; /* Seconds since the epoch. */
82 ovs_be32 seq_number; /* Message sequence number. */
83 ovs_be32 obs_domain_id; /* Observation Domain ID. */
85 BUILD_ASSERT_DECL(sizeof(struct ipfix_header) == 16);
87 #define IPFIX_SET_ID_TEMPLATE 2
88 #define IPFIX_SET_ID_OPTION_TEMPLATE 3
90 /* Cf. IETF RFC 5101 Section 3.3.2. */
92 struct ipfix_set_header {
93 ovs_be16 set_id; /* IPFIX_SET_ID_* or valid template ID for Data Sets. */
94 ovs_be16 length; /* Length of the set in bytes including header. */
96 BUILD_ASSERT_DECL(sizeof(struct ipfix_set_header) == 4);
98 /* Alternatives for templates at each layer. A template is defined by
99 * a combination of one value for each layer. */
100 enum ipfix_proto_l2 {
101 IPFIX_PROTO_L2_ETH = 0, /* No VLAN. */
105 enum ipfix_proto_l3 {
106 IPFIX_PROTO_L3_UNKNOWN = 0,
111 enum ipfix_proto_l4 {
112 IPFIX_PROTO_L4_UNKNOWN = 0,
113 IPFIX_PROTO_L4_TCP_UDP,
117 /* Any Template ID > 255 is usable for Template Records. */
118 #define IPFIX_TEMPLATE_ID_MIN 256
120 /* Cf. IETF RFC 5101 Section 3.4.1. */
122 struct ipfix_template_record_header {
123 ovs_be16 template_id;
124 ovs_be16 field_count;
126 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_record_header) == 4);
128 enum ipfix_entity_id {
129 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ID_##ENUM = ID,
130 #include "ofproto/ipfix-entities.def"
133 enum ipfix_entity_size {
134 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_SIZE_##ENUM = SIZE,
135 #include "ofproto/ipfix-entities.def"
139 struct ipfix_template_field_specifier {
140 ovs_be16 element_id; /* IPFIX_ENTITY_ID_*. */
141 ovs_be16 field_length; /* Length of the field's value, in bytes. */
142 /* No Enterprise ID, since only standard element IDs are specified. */
144 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_field_specifier) == 4);
146 /* Part of data record for common metadata and Ethernet entities. */
148 struct ipfix_data_record_common {
149 ovs_be32 observation_point_id; /* OBSERVATION_POINT_ID */
150 ovs_be64 packet_delta_count; /* PACKET_DELTA_COUNT */
151 ovs_be64 layer2_octet_delta_count; /* LAYER2_OCTET_DELTA_COUNT */
152 uint8_t source_mac_address[6]; /* SOURCE_MAC_ADDRESS */
153 uint8_t destination_mac_address[6]; /* DESTINATION_MAC_ADDRESS */
154 ovs_be16 ethernet_type; /* ETHERNET_TYPE */
155 ovs_be16 ethernet_total_length; /* ETHERNET_TOTAL_LENGTH */
156 uint8_t ethernet_header_length; /* ETHERNET_HEADER_LENGTH */
158 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_common) == 37);
160 /* Part of data record for VLAN entities. */
162 struct ipfix_data_record_vlan {
163 ovs_be16 vlan_id; /* VLAN_ID */
164 ovs_be16 dot1q_vlan_id; /* DOT1Q_VLAN_ID */
165 uint8_t dot1q_priority; /* DOT1Q_PRIORITY */
167 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_vlan) == 5);
169 /* Part of data record for IP entities. */
171 struct ipfix_data_record_ip {
172 uint8_t ip_version; /* IP_VERSION */
173 uint8_t ip_ttl; /* IP_TTL */
174 uint8_t protocol_identifier; /* PROTOCOL_IDENTIFIER */
175 uint8_t ip_diff_serv_code_point; /* IP_DIFF_SERV_CODE_POINT */
176 uint8_t ip_precedence; /* IP_PRECEDENCE */
177 uint8_t ip_class_of_service; /* IP_CLASS_OF_SERVICE */
179 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ip) == 6);
181 /* Part of data record for IPv4 entities. */
183 struct ipfix_data_record_ipv4 {
184 ovs_be32 source_ipv4_address; /* SOURCE_IPV4_ADDRESS */
185 ovs_be32 destination_ipv4_address; /* DESTINATION_IPV4_ADDRESS */
187 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ipv4) == 8);
189 /* Part of data record for IPv4 entities. */
191 struct ipfix_data_record_ipv6 {
192 uint8_t source_ipv6_address[16]; /* SOURCE_IPV6_ADDRESS */
193 uint8_t destination_ipv6_address[16]; /* DESTINATION_IPV6_ADDRESS */
194 ovs_be32 flow_label_ipv6; /* FLOW_LABEL_IPV6 */
196 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ipv6) == 36);
198 /* Part of data record for TCP/UDP entities. */
200 struct ipfix_data_record_tcpudp {
201 ovs_be16 source_transport_port; /* SOURCE_TRANSPORT_PORT */
202 ovs_be16 destination_transport_port; /* DESTINATION_TRANSPORT_PORT */
204 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_tcpudp) == 4);
207 ofproto_ipfix_bridge_exporter_options_equal(
208 const struct ofproto_ipfix_bridge_exporter_options *a,
209 const struct ofproto_ipfix_bridge_exporter_options *b)
211 return (a->obs_domain_id == b->obs_domain_id
212 && a->obs_point_id == b->obs_point_id
213 && a->sampling_rate == b->sampling_rate
214 && sset_equals(&a->targets, &b->targets));
217 static struct ofproto_ipfix_bridge_exporter_options *
218 ofproto_ipfix_bridge_exporter_options_clone(
219 const struct ofproto_ipfix_bridge_exporter_options *old)
221 struct ofproto_ipfix_bridge_exporter_options *new =
222 xmemdup(old, sizeof *old);
223 sset_clone(&new->targets, &old->targets);
228 ofproto_ipfix_bridge_exporter_options_destroy(
229 struct ofproto_ipfix_bridge_exporter_options *options)
232 sset_destroy(&options->targets);
238 ofproto_ipfix_flow_exporter_options_equal(
239 const struct ofproto_ipfix_flow_exporter_options *a,
240 const struct ofproto_ipfix_flow_exporter_options *b)
242 return (a->collector_set_id == b->collector_set_id
243 && sset_equals(&a->targets, &b->targets));
246 static struct ofproto_ipfix_flow_exporter_options *
247 ofproto_ipfix_flow_exporter_options_clone(
248 const struct ofproto_ipfix_flow_exporter_options *old)
250 struct ofproto_ipfix_flow_exporter_options *new =
251 xmemdup(old, sizeof *old);
252 sset_clone(&new->targets, &old->targets);
257 ofproto_ipfix_flow_exporter_options_destroy(
258 struct ofproto_ipfix_flow_exporter_options *options)
261 sset_destroy(&options->targets);
267 dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
269 collectors_destroy(exporter->collectors);
270 exporter->collectors = NULL;
271 exporter->seq_number = 1;
272 exporter->last_template_set_time = TIME_MIN;
276 dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter *exporter,
277 const struct sset *targets)
279 collectors_destroy(exporter->collectors);
280 collectors_create(targets, IPFIX_DEFAULT_COLLECTOR_PORT,
281 &exporter->collectors);
282 if (exporter->collectors == NULL) {
283 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
284 "IPFIX exporter disabled");
285 dpif_ipfix_exporter_clear(exporter);
292 dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
294 dpif_ipfix_exporter_clear(&exporter->exporter);
295 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
296 exporter->options = NULL;
297 exporter->probability = 0;
301 dpif_ipfix_bridge_exporter_set_options(
302 struct dpif_ipfix_bridge_exporter *exporter,
303 const struct ofproto_ipfix_bridge_exporter_options *options)
305 bool options_changed;
307 if (!options || sset_is_empty(&options->targets)) {
308 /* No point in doing any work if there are no targets. */
309 dpif_ipfix_bridge_exporter_clear(exporter);
315 || !ofproto_ipfix_bridge_exporter_options_equal(
316 options, exporter->options));
318 /* Configure collectors if options have changed or if we're
319 * shortchanged in collectors (which indicates that opening one or
320 * more of the configured collectors failed, so that we should
323 || collectors_count(exporter->exporter.collectors)
324 < sset_count(&options->targets)) {
325 if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
326 &options->targets)) {
331 /* Avoid reconfiguring if options didn't change. */
332 if (!options_changed) {
336 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
337 exporter->options = ofproto_ipfix_bridge_exporter_options_clone(options);
338 exporter->probability =
339 MAX(1, UINT32_MAX / exporter->options->sampling_rate);
342 static struct dpif_ipfix_flow_exporter_map_node*
343 dpif_ipfix_find_flow_exporter_map_node(
344 const struct dpif_ipfix *di, const uint32_t collector_set_id)
346 struct dpif_ipfix_flow_exporter_map_node *exporter_node;
348 HMAP_FOR_EACH_WITH_HASH (exporter_node, node,
349 hash_int(collector_set_id, 0),
350 &di->flow_exporter_map) {
351 if (exporter_node->exporter.options->collector_set_id
352 == collector_set_id) {
353 return exporter_node;
361 dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
363 dpif_ipfix_exporter_clear(&exporter->exporter);
364 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
365 exporter->options = NULL;
369 dpif_ipfix_flow_exporter_set_options(
370 struct dpif_ipfix_flow_exporter *exporter,
371 const struct ofproto_ipfix_flow_exporter_options *options)
373 bool options_changed;
375 if (sset_is_empty(&options->targets)) {
376 /* No point in doing any work if there are no targets. */
377 dpif_ipfix_flow_exporter_clear(exporter);
383 || !ofproto_ipfix_flow_exporter_options_equal(
384 options, exporter->options));
386 /* Configure collectors if options have changed or if we're
387 * shortchanged in collectors (which indicates that opening one or
388 * more of the configured collectors failed, so that we should
391 || collectors_count(exporter->exporter.collectors)
392 < sset_count(&options->targets)) {
393 if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
394 &options->targets)) {
399 /* Avoid reconfiguring if options didn't change. */
400 if (!options_changed) {
404 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
405 exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
411 dpif_ipfix_set_options(
412 struct dpif_ipfix *di,
413 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
414 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
415 size_t n_flow_exporters_options) OVS_EXCLUDED(mutex)
418 struct ofproto_ipfix_flow_exporter_options *options;
419 struct dpif_ipfix_flow_exporter_map_node *node, *next;
420 size_t n_broken_flow_exporters_options = 0;
422 ovs_mutex_lock(&mutex);
423 dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
424 bridge_exporter_options);
426 /* Add new flow exporters and update current flow exporters. */
427 options = (struct ofproto_ipfix_flow_exporter_options *)
428 flow_exporters_options;
429 for (i = 0; i < n_flow_exporters_options; i++) {
430 node = dpif_ipfix_find_flow_exporter_map_node(
431 di, options->collector_set_id);
433 node = xzalloc(sizeof *node);
434 dpif_ipfix_exporter_clear(&node->exporter.exporter);
435 hmap_insert(&di->flow_exporter_map, &node->node,
436 hash_int(options->collector_set_id, 0));
438 if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
439 n_broken_flow_exporters_options++;
444 ovs_assert(hmap_count(&di->flow_exporter_map) >=
445 (n_flow_exporters_options - n_broken_flow_exporters_options));
447 /* Remove dropped flow exporters, if any needs to be removed. */
448 if (hmap_count(&di->flow_exporter_map) > n_flow_exporters_options) {
449 HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
450 /* This is slow but doesn't take any extra memory, and
451 * this table is not supposed to contain many rows anyway. */
452 options = (struct ofproto_ipfix_flow_exporter_options *)
453 flow_exporters_options;
454 for (i = 0; i < n_flow_exporters_options; i++) {
455 if (node->exporter.options->collector_set_id
456 == options->collector_set_id) {
461 if (i == n_flow_exporters_options) { // Not found.
462 hmap_remove(&di->flow_exporter_map, &node->node);
463 dpif_ipfix_flow_exporter_clear(&node->exporter);
469 ovs_assert(hmap_count(&di->flow_exporter_map) ==
470 (n_flow_exporters_options - n_broken_flow_exporters_options));
471 ovs_mutex_unlock(&mutex);
475 dpif_ipfix_create(void)
477 struct dpif_ipfix *di;
478 di = xzalloc(sizeof *di);
479 dpif_ipfix_exporter_clear(&di->bridge_exporter.exporter);
480 hmap_init(&di->flow_exporter_map);
481 atomic_init(&di->ref_cnt, 1);
486 dpif_ipfix_ref(const struct dpif_ipfix *di_)
488 struct dpif_ipfix *di = CONST_CAST(struct dpif_ipfix *, di_);
491 atomic_add(&di->ref_cnt, 1, &orig);
492 ovs_assert(orig > 0);
498 dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
502 ovs_mutex_lock(&mutex);
503 ret = di->bridge_exporter.probability;
504 ovs_mutex_unlock(&mutex);
509 dpif_ipfix_clear(struct dpif_ipfix *di) OVS_REQUIRES(mutex)
511 struct dpif_ipfix_flow_exporter_map_node *node, *next;
513 dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
515 HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
516 hmap_remove(&di->flow_exporter_map, &node->node);
517 dpif_ipfix_flow_exporter_clear(&node->exporter);
523 dpif_ipfix_unref(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
531 atomic_sub(&di->ref_cnt, 1, &orig);
532 ovs_assert(orig > 0);
534 ovs_mutex_lock(&mutex);
535 dpif_ipfix_clear(di);
536 hmap_destroy(&di->flow_exporter_map);
538 ovs_mutex_unlock(&mutex);
543 ipfix_init_header(uint32_t seq_number, uint32_t obs_domain_id,
546 struct ipfix_header *hdr;
548 hdr = ofpbuf_put_zeros(msg, sizeof *hdr);
549 hdr->version = htons(IPFIX_VERSION);
550 hdr->length = htons(sizeof *hdr); /* Updated in ipfix_send_msg. */
551 hdr->export_time = htonl(time_wall());
552 hdr->seq_number = htonl(seq_number);
553 hdr->obs_domain_id = htonl(obs_domain_id);
557 ipfix_send_msg(const struct collectors *collectors, struct ofpbuf *msg)
559 struct ipfix_header *hdr;
561 /* Adjust the length in the header. */
563 hdr->length = htons(msg->size);
565 collectors_send(collectors, msg->data, msg->size);
570 ipfix_get_template_id(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
571 enum ipfix_proto_l4 l4)
573 uint16_t template_id;
575 template_id = template_id * NUM_IPFIX_PROTO_L3 + l3;
576 template_id = template_id * NUM_IPFIX_PROTO_L4 + l4;
577 return IPFIX_TEMPLATE_ID_MIN + template_id;
581 ipfix_define_template_entity(enum ipfix_entity_id id,
582 enum ipfix_entity_size size, struct ofpbuf *msg)
584 struct ipfix_template_field_specifier *field;
586 field = ofpbuf_put_zeros(msg, sizeof *field);
587 field->element_id = htons(id);
588 field->field_length = htons(size);
592 ipfix_define_template_fields(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
593 enum ipfix_proto_l4 l4, struct ofpbuf *msg)
599 ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
600 IPFIX_ENTITY_SIZE_##ID, msg); \
604 DEF(OBSERVATION_POINT_ID);
605 DEF(PACKET_DELTA_COUNT);
606 DEF(LAYER2_OCTET_DELTA_COUNT);
608 /* Common Ethernet entities. */
609 DEF(SOURCE_MAC_ADDRESS);
610 DEF(DESTINATION_MAC_ADDRESS);
612 DEF(ETHERNET_TOTAL_LENGTH);
613 DEF(ETHERNET_HEADER_LENGTH);
615 if (l2 == IPFIX_PROTO_L2_VLAN) {
621 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
624 DEF(PROTOCOL_IDENTIFIER);
625 DEF(IP_DIFF_SERV_CODE_POINT);
627 DEF(IP_CLASS_OF_SERVICE);
629 if (l3 == IPFIX_PROTO_L3_IPV4) {
630 DEF(SOURCE_IPV4_ADDRESS);
631 DEF(DESTINATION_IPV4_ADDRESS);
632 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
633 DEF(SOURCE_IPV6_ADDRESS);
634 DEF(DESTINATION_IPV6_ADDRESS);
635 DEF(FLOW_LABEL_IPV6);
639 if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
640 DEF(SOURCE_TRANSPORT_PORT);
641 DEF(DESTINATION_TRANSPORT_PORT);
650 ipfix_send_template_msg(struct dpif_ipfix_exporter *exporter,
651 uint32_t obs_domain_id)
653 uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
655 size_t set_hdr_offset, tmpl_hdr_offset;
656 struct ipfix_set_header *set_hdr;
657 struct ipfix_template_record_header *tmpl_hdr;
658 uint16_t field_count;
659 enum ipfix_proto_l2 l2;
660 enum ipfix_proto_l3 l3;
661 enum ipfix_proto_l4 l4;
663 ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
665 ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
666 set_hdr_offset = msg.size;
668 /* Add a Template Set. */
669 set_hdr = ofpbuf_put_zeros(&msg, sizeof *set_hdr);
670 set_hdr->set_id = htons(IPFIX_SET_ID_TEMPLATE);
672 /* Define one template for each possible combination of
674 for (l2 = 0; l2 < NUM_IPFIX_PROTO_L2; l2++) {
675 for (l3 = 0; l3 < NUM_IPFIX_PROTO_L3; l3++) {
676 for (l4 = 0; l4 < NUM_IPFIX_PROTO_L4; l4++) {
677 if (l3 == IPFIX_PROTO_L3_UNKNOWN &&
678 l4 != IPFIX_PROTO_L4_UNKNOWN) {
681 tmpl_hdr_offset = msg.size;
682 tmpl_hdr = ofpbuf_put_zeros(&msg, sizeof *tmpl_hdr);
683 tmpl_hdr->template_id = htons(
684 ipfix_get_template_id(l2, l3, l4));
685 field_count = ipfix_define_template_fields(l2, l3, l4, &msg);
686 tmpl_hdr = (struct ipfix_template_record_header*)
687 ((uint8_t*)msg.data + tmpl_hdr_offset);
688 tmpl_hdr->field_count = htons(field_count);
693 set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
694 set_hdr->length = htons(msg.size - set_hdr_offset);
696 /* XXX: Add Options Template Sets, at least to define a Flow Keys
697 * Option Template. */
699 ipfix_send_msg(exporter->collectors, &msg);
705 ipfix_send_data_msg(struct dpif_ipfix_exporter *exporter, struct ofpbuf *packet,
706 const struct flow *flow, uint64_t packet_delta_count,
707 uint32_t obs_domain_id, uint32_t obs_point_id)
709 uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
711 size_t set_hdr_offset;
712 struct ipfix_set_header *set_hdr;
713 enum ipfix_proto_l2 l2;
714 enum ipfix_proto_l3 l3;
715 enum ipfix_proto_l4 l4;
717 ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
719 ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
720 exporter->seq_number++;
721 set_hdr_offset = msg.size;
723 /* Choose the right template ID matching the protocols in the
725 l2 = (flow->vlan_tci == 0) ? IPFIX_PROTO_L2_ETH : IPFIX_PROTO_L2_VLAN;
727 switch(ntohs(flow->dl_type)) {
729 l3 = IPFIX_PROTO_L3_IPV4;
732 l3 = IPFIX_PROTO_L3_IPV6;
735 l3 = IPFIX_PROTO_L3_UNKNOWN;
738 l4 = IPFIX_PROTO_L4_UNKNOWN;
739 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
740 switch(flow->nw_proto) {
741 case IPPROTO_TCP: /* TCP */
742 case IPPROTO_UDP: /* UDP */
743 l4 = IPFIX_PROTO_L4_TCP_UDP;
748 /* Add a Data Set. */
749 set_hdr = ofpbuf_put_zeros(&msg, sizeof *set_hdr);
750 set_hdr->set_id = htons(ipfix_get_template_id(l2, l3, l4));
752 /* The fields defined in the ipfix_data_record_* structs and sent
753 * below must match exactly the templates defined in
754 * ipfix_define_template_fields. */
756 /* Common Ethernet entities. */
758 struct ipfix_data_record_common *data_common;
759 uint16_t ethernet_total_length;
760 uint8_t ethernet_header_length;
761 uint64_t layer2_octet_delta_count;
763 ethernet_total_length = packet->size;
764 ethernet_header_length = (l2 == IPFIX_PROTO_L2_VLAN)
765 ? VLAN_ETH_HEADER_LEN : ETH_HEADER_LEN;
767 /* Calculate the total matched octet count by considering as
768 * an approximation that all matched packets have the same
770 layer2_octet_delta_count = packet_delta_count * ethernet_total_length;
772 data_common = ofpbuf_put_zeros(&msg, sizeof *data_common);
773 data_common->observation_point_id = htonl(obs_point_id);
774 data_common->packet_delta_count = htonll(packet_delta_count);
775 data_common->layer2_octet_delta_count =
776 htonll(layer2_octet_delta_count);
777 memcpy(data_common->source_mac_address, flow->dl_src,
778 sizeof flow->dl_src);
779 memcpy(data_common->destination_mac_address, flow->dl_dst,
780 sizeof flow->dl_dst);
781 data_common->ethernet_type = flow->dl_type;
782 data_common->ethernet_total_length = htons(ethernet_total_length);
783 data_common->ethernet_header_length = ethernet_header_length;
786 if (l2 == IPFIX_PROTO_L2_VLAN) {
787 struct ipfix_data_record_vlan *data_vlan;
788 uint16_t vlan_id = vlan_tci_to_vid(flow->vlan_tci);
789 uint8_t priority = vlan_tci_to_pcp(flow->vlan_tci);
791 data_vlan = ofpbuf_put_zeros(&msg, sizeof *data_vlan);
792 data_vlan->vlan_id = htons(vlan_id);
793 data_vlan->dot1q_vlan_id = htons(vlan_id);
794 data_vlan->dot1q_priority = priority;
797 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
798 struct ipfix_data_record_ip *data_ip;
800 data_ip = ofpbuf_put_zeros(&msg, sizeof *data_ip);
801 data_ip->ip_version = (l3 == IPFIX_PROTO_L3_IPV4) ? 4 : 6;
802 data_ip->ip_ttl = flow->nw_ttl;
803 data_ip->protocol_identifier = flow->nw_proto;
804 data_ip->ip_diff_serv_code_point = flow->nw_tos >> 2;
805 data_ip->ip_precedence = flow->nw_tos >> 5;
806 data_ip->ip_class_of_service = flow->nw_tos;
808 if (l3 == IPFIX_PROTO_L3_IPV4) {
809 struct ipfix_data_record_ipv4 *data_ipv4;
810 data_ipv4 = ofpbuf_put_zeros(&msg, sizeof *data_ipv4);
811 data_ipv4->source_ipv4_address = flow->nw_src;
812 data_ipv4->destination_ipv4_address = flow->nw_dst;
813 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
814 struct ipfix_data_record_ipv6 *data_ipv6;
816 data_ipv6 = ofpbuf_put_zeros(&msg, sizeof *data_ipv6);
817 memcpy(data_ipv6->source_ipv6_address, &flow->ipv6_src,
818 sizeof flow->ipv6_src);
819 memcpy(data_ipv6->destination_ipv6_address, &flow->ipv6_dst,
820 sizeof flow->ipv6_dst);
821 data_ipv6->flow_label_ipv6 = flow->ipv6_label;
825 if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
826 struct ipfix_data_record_tcpudp *data_tcpudp;
828 data_tcpudp = ofpbuf_put_zeros(&msg, sizeof *data_tcpudp);
829 data_tcpudp->source_transport_port = flow->tp_src;
830 data_tcpudp->destination_transport_port = flow->tp_dst;
833 set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
834 set_hdr->length = htons(msg.size - set_hdr_offset);
836 ipfix_send_msg(exporter->collectors, &msg);
842 dpif_ipfix_sample(struct dpif_ipfix_exporter *exporter,
843 struct ofpbuf *packet, const struct flow *flow,
844 uint64_t packet_delta_count, uint32_t obs_domain_id,
845 uint32_t obs_point_id)
847 time_t now = time_wall();
848 if ((exporter->last_template_set_time + IPFIX_TEMPLATE_INTERVAL) <= now) {
849 ipfix_send_template_msg(exporter, obs_domain_id);
850 exporter->last_template_set_time = now;
853 ipfix_send_data_msg(exporter, packet, flow, packet_delta_count,
854 obs_domain_id, obs_point_id);
858 dpif_ipfix_bridge_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
859 const struct flow *flow) OVS_EXCLUDED(mutex)
861 uint64_t packet_delta_count;
863 ovs_mutex_lock(&mutex);
864 /* Use the sampling probability as an approximation of the number
865 * of matched packets. */
866 packet_delta_count = UINT32_MAX / di->bridge_exporter.probability;
867 dpif_ipfix_sample(&di->bridge_exporter.exporter, packet, flow,
869 di->bridge_exporter.options->obs_domain_id,
870 di->bridge_exporter.options->obs_point_id);
871 ovs_mutex_unlock(&mutex);
875 dpif_ipfix_flow_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
876 const struct flow *flow, uint32_t collector_set_id,
877 uint16_t probability, uint32_t obs_domain_id,
878 uint32_t obs_point_id) OVS_EXCLUDED(mutex)
880 struct dpif_ipfix_flow_exporter_map_node *node;
881 /* Use the sampling probability as an approximation of the number
882 * of matched packets. */
883 uint64_t packet_delta_count = USHRT_MAX / probability;
885 ovs_mutex_lock(&mutex);
886 node = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
888 dpif_ipfix_sample(&node->exporter.exporter, packet, flow,
889 packet_delta_count, obs_domain_id, obs_point_id);
891 ovs_mutex_unlock(&mutex);