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);
37 /* Cf. IETF RFC 5101 Section 10.3.4. */
38 #define IPFIX_DEFAULT_COLLECTOR_PORT 4739
40 struct dpif_ipfix_exporter {
41 struct collectors *collectors;
43 time_t last_template_set_time;
46 struct dpif_ipfix_bridge_exporter {
47 struct dpif_ipfix_exporter exporter;
48 struct ofproto_ipfix_bridge_exporter_options *options;
52 struct dpif_ipfix_flow_exporter {
53 struct dpif_ipfix_exporter exporter;
54 struct ofproto_ipfix_flow_exporter_options *options;
57 struct dpif_ipfix_flow_exporter_map_node {
58 struct hmap_node node;
59 struct dpif_ipfix_flow_exporter exporter;
63 struct dpif_ipfix_bridge_exporter bridge_exporter;
64 struct hmap flow_exporter_map; /* dpif_ipfix_flow_exporter_map_nodes. */
68 #define IPFIX_VERSION 0x000a
70 /* When using UDP, IPFIX Template Records must be re-sent regularly.
71 * The standard default interval is 10 minutes (600 seconds).
72 * Cf. IETF RFC 5101 Section 10.3.6. */
73 #define IPFIX_TEMPLATE_INTERVAL 600
75 /* Cf. IETF RFC 5101 Section 3.1. */
77 ovs_be16 version; /* IPFIX_VERSION. */
78 ovs_be16 length; /* Length in bytes including this header. */
79 ovs_be32 export_time; /* Seconds since the epoch. */
80 ovs_be32 seq_number; /* Message sequence number. */
81 ovs_be32 obs_domain_id; /* Observation Domain ID. */
82 } __attribute__((packed));
83 BUILD_ASSERT_DECL(sizeof(struct ipfix_header) == 16);
85 #define IPFIX_SET_ID_TEMPLATE 2
86 #define IPFIX_SET_ID_OPTION_TEMPLATE 3
88 /* Cf. IETF RFC 5101 Section 3.3.2. */
89 struct ipfix_set_header {
90 ovs_be16 set_id; /* IPFIX_SET_ID_* or valid template ID for Data Sets. */
91 ovs_be16 length; /* Length of the set in bytes including header. */
92 } __attribute__((packed));
93 BUILD_ASSERT_DECL(sizeof(struct ipfix_set_header) == 4);
95 /* Alternatives for templates at each layer. A template is defined by
96 * a combination of one value for each layer. */
98 IPFIX_PROTO_L2_ETH = 0, /* No VLAN. */
102 enum ipfix_proto_l3 {
103 IPFIX_PROTO_L3_UNKNOWN = 0,
108 enum ipfix_proto_l4 {
109 IPFIX_PROTO_L4_UNKNOWN = 0,
110 IPFIX_PROTO_L4_TCP_UDP,
114 /* Any Template ID > 255 is usable for Template Records. */
115 #define IPFIX_TEMPLATE_ID_MIN 256
117 /* Cf. IETF RFC 5101 Section 3.4.1. */
118 struct ipfix_template_record_header {
119 ovs_be16 template_id;
120 ovs_be16 field_count;
121 } __attribute__((packed));
122 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_record_header) == 4);
124 enum ipfix_entity_id {
125 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ID_##ENUM = ID,
126 #include "ofproto/ipfix-entities.def"
129 enum ipfix_entity_size {
130 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_SIZE_##ENUM = SIZE,
131 #include "ofproto/ipfix-entities.def"
134 struct ipfix_template_field_specifier {
135 ovs_be16 element_id; /* IPFIX_ENTITY_ID_*. */
136 ovs_be16 field_length; /* Length of the field's value, in bytes. */
137 /* No Enterprise ID, since only standard element IDs are specified. */
138 } __attribute__((packed));
139 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_field_specifier) == 4);
141 /* Part of data record for common metadata and Ethernet entities. */
142 struct ipfix_data_record_common {
143 ovs_be32 observation_point_id; /* OBSERVATION_POINT_ID */
144 ovs_be64 packet_delta_count; /* PACKET_DELTA_COUNT */
145 ovs_be64 layer2_octet_delta_count; /* LAYER2_OCTET_DELTA_COUNT */
146 uint8_t source_mac_address[6]; /* SOURCE_MAC_ADDRESS */
147 uint8_t destination_mac_address[6]; /* DESTINATION_MAC_ADDRESS */
148 ovs_be16 ethernet_type; /* ETHERNET_TYPE */
149 ovs_be16 ethernet_total_length; /* ETHERNET_TOTAL_LENGTH */
150 uint8_t ethernet_header_length; /* ETHERNET_HEADER_LENGTH */
151 } __attribute__((packed));
152 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_common) == 37);
154 /* Part of data record for VLAN entities. */
155 struct ipfix_data_record_vlan {
156 ovs_be16 vlan_id; /* VLAN_ID */
157 ovs_be16 dot1q_vlan_id; /* DOT1Q_VLAN_ID */
158 uint8_t dot1q_priority; /* DOT1Q_PRIORITY */
159 } __attribute__((packed));
160 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_vlan) == 5);
162 /* Part of data record for IP entities. */
163 struct ipfix_data_record_ip {
164 uint8_t ip_version; /* IP_VERSION */
165 uint8_t ip_ttl; /* IP_TTL */
166 uint8_t protocol_identifier; /* PROTOCOL_IDENTIFIER */
167 uint8_t ip_diff_serv_code_point; /* IP_DIFF_SERV_CODE_POINT */
168 uint8_t ip_precedence; /* IP_PRECEDENCE */
169 uint8_t ip_class_of_service; /* IP_CLASS_OF_SERVICE */
170 } __attribute__((packed));
171 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ip) == 6);
173 /* Part of data record for IPv4 entities. */
174 struct ipfix_data_record_ipv4 {
175 ovs_be32 source_ipv4_address; /* SOURCE_IPV4_ADDRESS */
176 ovs_be32 destination_ipv4_address; /* DESTINATION_IPV4_ADDRESS */
177 } __attribute__((packed));
178 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ipv4) == 8);
180 /* Part of data record for IPv4 entities. */
181 struct ipfix_data_record_ipv6 {
182 uint8_t source_ipv6_address[16]; /* SOURCE_IPV6_ADDRESS */
183 uint8_t destination_ipv6_address[16]; /* DESTINATION_IPV6_ADDRESS */
184 ovs_be32 flow_label_ipv6; /* FLOW_LABEL_IPV6 */
185 } __attribute__((packed));
186 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ipv6) == 36);
188 /* Part of data record for TCP/UDP entities. */
189 struct ipfix_data_record_tcpudp {
190 ovs_be16 source_transport_port; /* SOURCE_TRANSPORT_PORT */
191 ovs_be16 destination_transport_port; /* DESTINATION_TRANSPORT_PORT */
192 } __attribute__((packed));
193 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_tcpudp) == 4);
196 ofproto_ipfix_bridge_exporter_options_equal(
197 const struct ofproto_ipfix_bridge_exporter_options *a,
198 const struct ofproto_ipfix_bridge_exporter_options *b)
200 return (a->obs_domain_id == b->obs_domain_id
201 && a->obs_point_id == b->obs_point_id
202 && a->sampling_rate == b->sampling_rate
203 && sset_equals(&a->targets, &b->targets));
206 static struct ofproto_ipfix_bridge_exporter_options *
207 ofproto_ipfix_bridge_exporter_options_clone(
208 const struct ofproto_ipfix_bridge_exporter_options *old)
210 struct ofproto_ipfix_bridge_exporter_options *new =
211 xmemdup(old, sizeof *old);
212 sset_clone(&new->targets, &old->targets);
217 ofproto_ipfix_bridge_exporter_options_destroy(
218 struct ofproto_ipfix_bridge_exporter_options *options)
221 sset_destroy(&options->targets);
227 ofproto_ipfix_flow_exporter_options_equal(
228 const struct ofproto_ipfix_flow_exporter_options *a,
229 const struct ofproto_ipfix_flow_exporter_options *b)
231 return (a->collector_set_id == b->collector_set_id
232 && sset_equals(&a->targets, &b->targets));
235 static struct ofproto_ipfix_flow_exporter_options *
236 ofproto_ipfix_flow_exporter_options_clone(
237 const struct ofproto_ipfix_flow_exporter_options *old)
239 struct ofproto_ipfix_flow_exporter_options *new =
240 xmemdup(old, sizeof *old);
241 sset_clone(&new->targets, &old->targets);
246 ofproto_ipfix_flow_exporter_options_destroy(
247 struct ofproto_ipfix_flow_exporter_options *options)
250 sset_destroy(&options->targets);
256 dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
258 collectors_destroy(exporter->collectors);
259 exporter->collectors = NULL;
260 exporter->seq_number = 1;
261 exporter->last_template_set_time = TIME_MIN;
265 dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter *exporter,
266 const struct sset *targets)
268 collectors_destroy(exporter->collectors);
269 collectors_create(targets, IPFIX_DEFAULT_COLLECTOR_PORT,
270 &exporter->collectors);
271 if (exporter->collectors == NULL) {
272 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
273 "IPFIX exporter disabled");
274 dpif_ipfix_exporter_clear(exporter);
281 dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
283 dpif_ipfix_exporter_clear(&exporter->exporter);
284 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
285 exporter->options = NULL;
286 exporter->probability = 0;
290 dpif_ipfix_bridge_exporter_set_options(
291 struct dpif_ipfix_bridge_exporter *exporter,
292 const struct ofproto_ipfix_bridge_exporter_options *options)
294 bool options_changed;
296 if (!options || sset_is_empty(&options->targets)) {
297 /* No point in doing any work if there are no targets. */
298 dpif_ipfix_bridge_exporter_clear(exporter);
304 || !ofproto_ipfix_bridge_exporter_options_equal(
305 options, exporter->options));
307 /* Configure collectors if options have changed or if we're
308 * shortchanged in collectors (which indicates that opening one or
309 * more of the configured collectors failed, so that we should
312 || collectors_count(exporter->exporter.collectors)
313 < sset_count(&options->targets)) {
314 if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
315 &options->targets)) {
320 /* Avoid reconfiguring if options didn't change. */
321 if (!options_changed) {
325 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
326 exporter->options = ofproto_ipfix_bridge_exporter_options_clone(options);
327 exporter->probability =
328 MAX(1, UINT32_MAX / exporter->options->sampling_rate);
331 static struct dpif_ipfix_flow_exporter_map_node*
332 dpif_ipfix_find_flow_exporter_map_node(
333 const struct dpif_ipfix *di, const uint32_t collector_set_id)
335 struct dpif_ipfix_flow_exporter_map_node *exporter_node;
337 HMAP_FOR_EACH_WITH_HASH (exporter_node, node,
338 hash_int(collector_set_id, 0),
339 &di->flow_exporter_map) {
340 if (exporter_node->exporter.options->collector_set_id
341 == collector_set_id) {
342 return exporter_node;
350 dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
352 dpif_ipfix_exporter_clear(&exporter->exporter);
353 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
354 exporter->options = NULL;
358 dpif_ipfix_flow_exporter_set_options(
359 struct dpif_ipfix_flow_exporter *exporter,
360 const struct ofproto_ipfix_flow_exporter_options *options)
362 bool options_changed;
364 if (sset_is_empty(&options->targets)) {
365 /* No point in doing any work if there are no targets. */
366 dpif_ipfix_flow_exporter_clear(exporter);
372 || !ofproto_ipfix_flow_exporter_options_equal(
373 options, exporter->options));
375 /* Configure collectors if options have changed or if we're
376 * shortchanged in collectors (which indicates that opening one or
377 * more of the configured collectors failed, so that we should
380 || collectors_count(exporter->exporter.collectors)
381 < sset_count(&options->targets)) {
382 if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
383 &options->targets)) {
388 /* Avoid reconfiguring if options didn't change. */
389 if (!options_changed) {
393 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
394 exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
400 dpif_ipfix_set_options(
401 struct dpif_ipfix *di,
402 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
403 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
404 size_t n_flow_exporters_options)
407 struct ofproto_ipfix_flow_exporter_options *options;
408 struct dpif_ipfix_flow_exporter_map_node *node, *next;
409 size_t n_broken_flow_exporters_options = 0;
411 dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
412 bridge_exporter_options);
414 /* Add new flow exporters and update current flow exporters. */
415 options = (struct ofproto_ipfix_flow_exporter_options *)
416 flow_exporters_options;
417 for (i = 0; i < n_flow_exporters_options; i++) {
418 node = dpif_ipfix_find_flow_exporter_map_node(
419 di, options->collector_set_id);
421 node = xzalloc(sizeof *node);
422 dpif_ipfix_exporter_clear(&node->exporter.exporter);
423 hmap_insert(&di->flow_exporter_map, &node->node,
424 hash_int(options->collector_set_id, 0));
426 if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
427 n_broken_flow_exporters_options++;
432 ovs_assert(hmap_count(&di->flow_exporter_map) >=
433 (n_flow_exporters_options - n_broken_flow_exporters_options));
435 /* Remove dropped flow exporters, if any needs to be removed. */
436 if (hmap_count(&di->flow_exporter_map) > n_flow_exporters_options) {
437 HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
438 /* This is slow but doesn't take any extra memory, and
439 * this table is not supposed to contain many rows anyway. */
440 options = (struct ofproto_ipfix_flow_exporter_options *)
441 flow_exporters_options;
442 for (i = 0; i < n_flow_exporters_options; i++) {
443 if (node->exporter.options->collector_set_id
444 == options->collector_set_id) {
449 if (i == n_flow_exporters_options) { // Not found.
450 hmap_remove(&di->flow_exporter_map, &node->node);
451 dpif_ipfix_flow_exporter_clear(&node->exporter);
457 ovs_assert(hmap_count(&di->flow_exporter_map) ==
458 (n_flow_exporters_options - n_broken_flow_exporters_options));
462 dpif_ipfix_create(void)
464 struct dpif_ipfix *di;
465 di = xzalloc(sizeof *di);
466 dpif_ipfix_exporter_clear(&di->bridge_exporter.exporter);
467 hmap_init(&di->flow_exporter_map);
473 dpif_ipfix_ref(const struct dpif_ipfix *di_)
475 struct dpif_ipfix *di = CONST_CAST(struct dpif_ipfix *, di_);
477 ovs_assert(di->ref_cnt > 0);
484 dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
486 return di->bridge_exporter.probability;
490 dpif_ipfix_clear(struct dpif_ipfix *di)
492 struct dpif_ipfix_flow_exporter_map_node *node, *next;
494 dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
496 HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
497 hmap_remove(&di->flow_exporter_map, &node->node);
498 dpif_ipfix_flow_exporter_clear(&node->exporter);
504 dpif_ipfix_unref(struct dpif_ipfix *di)
510 ovs_assert(di->ref_cnt > 0);
511 if (!--di->ref_cnt) {
512 dpif_ipfix_clear(di);
513 hmap_destroy(&di->flow_exporter_map);
519 ipfix_init_header(uint32_t seq_number, uint32_t obs_domain_id,
522 struct ipfix_header *hdr;
524 hdr = ofpbuf_put_zeros(msg, sizeof *hdr);
525 hdr->version = htons(IPFIX_VERSION);
526 hdr->length = htons(sizeof *hdr); /* Updated in ipfix_send_msg. */
527 hdr->export_time = htonl(time_wall());
528 hdr->seq_number = htonl(seq_number);
529 hdr->obs_domain_id = htonl(obs_domain_id);
533 ipfix_send_msg(const struct collectors *collectors, struct ofpbuf *msg)
535 struct ipfix_header *hdr;
537 /* Adjust the length in the header. */
539 hdr->length = htons(msg->size);
541 collectors_send(collectors, msg->data, msg->size);
546 ipfix_get_template_id(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
547 enum ipfix_proto_l4 l4)
549 uint16_t template_id;
551 template_id = template_id * NUM_IPFIX_PROTO_L3 + l3;
552 template_id = template_id * NUM_IPFIX_PROTO_L4 + l4;
553 return IPFIX_TEMPLATE_ID_MIN + template_id;
557 ipfix_define_template_entity(enum ipfix_entity_id id,
558 enum ipfix_entity_size size, struct ofpbuf *msg)
560 struct ipfix_template_field_specifier *field;
562 field = ofpbuf_put_zeros(msg, sizeof *field);
563 field->element_id = htons(id);
564 field->field_length = htons(size);
568 ipfix_define_template_fields(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
569 enum ipfix_proto_l4 l4, struct ofpbuf *msg)
575 ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
576 IPFIX_ENTITY_SIZE_##ID, msg); \
580 DEF(OBSERVATION_POINT_ID);
581 DEF(PACKET_DELTA_COUNT);
582 DEF(LAYER2_OCTET_DELTA_COUNT);
584 /* Common Ethernet entities. */
585 DEF(SOURCE_MAC_ADDRESS);
586 DEF(DESTINATION_MAC_ADDRESS);
588 DEF(ETHERNET_TOTAL_LENGTH);
589 DEF(ETHERNET_HEADER_LENGTH);
591 if (l2 == IPFIX_PROTO_L2_VLAN) {
597 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
600 DEF(PROTOCOL_IDENTIFIER);
601 DEF(IP_DIFF_SERV_CODE_POINT);
603 DEF(IP_CLASS_OF_SERVICE);
605 if (l3 == IPFIX_PROTO_L3_IPV4) {
606 DEF(SOURCE_IPV4_ADDRESS);
607 DEF(DESTINATION_IPV4_ADDRESS);
608 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
609 DEF(SOURCE_IPV6_ADDRESS);
610 DEF(DESTINATION_IPV6_ADDRESS);
611 DEF(FLOW_LABEL_IPV6);
615 if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
616 DEF(SOURCE_TRANSPORT_PORT);
617 DEF(DESTINATION_TRANSPORT_PORT);
626 ipfix_send_template_msg(struct dpif_ipfix_exporter *exporter,
627 uint32_t obs_domain_id)
629 uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
631 size_t set_hdr_offset, tmpl_hdr_offset;
632 struct ipfix_set_header *set_hdr;
633 struct ipfix_template_record_header *tmpl_hdr;
634 uint16_t field_count;
635 enum ipfix_proto_l2 l2;
636 enum ipfix_proto_l3 l3;
637 enum ipfix_proto_l4 l4;
639 ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
641 ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
642 set_hdr_offset = msg.size;
644 /* Add a Template Set. */
645 set_hdr = ofpbuf_put_zeros(&msg, sizeof *set_hdr);
646 set_hdr->set_id = htons(IPFIX_SET_ID_TEMPLATE);
648 /* Define one template for each possible combination of
650 for (l2 = 0; l2 < NUM_IPFIX_PROTO_L2; l2++) {
651 for (l3 = 0; l3 < NUM_IPFIX_PROTO_L3; l3++) {
652 for (l4 = 0; l4 < NUM_IPFIX_PROTO_L4; l4++) {
653 if (l3 == IPFIX_PROTO_L3_UNKNOWN &&
654 l4 != IPFIX_PROTO_L4_UNKNOWN) {
657 tmpl_hdr_offset = msg.size;
658 tmpl_hdr = ofpbuf_put_zeros(&msg, sizeof *tmpl_hdr);
659 tmpl_hdr->template_id = htons(
660 ipfix_get_template_id(l2, l3, l4));
661 field_count = ipfix_define_template_fields(l2, l3, l4, &msg);
662 tmpl_hdr = (struct ipfix_template_record_header*)
663 ((uint8_t*)msg.data + tmpl_hdr_offset);
664 tmpl_hdr->field_count = htons(field_count);
669 set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
670 set_hdr->length = htons(msg.size - set_hdr_offset);
672 /* XXX: Add Options Template Sets, at least to define a Flow Keys
673 * Option Template. */
675 ipfix_send_msg(exporter->collectors, &msg);
681 ipfix_send_data_msg(struct dpif_ipfix_exporter *exporter, struct ofpbuf *packet,
682 const struct flow *flow, uint64_t packet_delta_count,
683 uint32_t obs_domain_id, uint32_t obs_point_id)
685 uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
687 size_t set_hdr_offset;
688 struct ipfix_set_header *set_hdr;
689 enum ipfix_proto_l2 l2;
690 enum ipfix_proto_l3 l3;
691 enum ipfix_proto_l4 l4;
693 ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
695 ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
696 exporter->seq_number++;
697 set_hdr_offset = msg.size;
699 /* Choose the right template ID matching the protocols in the
701 l2 = (flow->vlan_tci == 0) ? IPFIX_PROTO_L2_ETH : IPFIX_PROTO_L2_VLAN;
703 switch(ntohs(flow->dl_type)) {
705 l3 = IPFIX_PROTO_L3_IPV4;
708 l3 = IPFIX_PROTO_L3_IPV6;
711 l3 = IPFIX_PROTO_L3_UNKNOWN;
714 l4 = IPFIX_PROTO_L4_UNKNOWN;
715 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
716 switch(flow->nw_proto) {
717 case IPPROTO_TCP: /* TCP */
718 case IPPROTO_UDP: /* UDP */
719 l4 = IPFIX_PROTO_L4_TCP_UDP;
724 /* Add a Data Set. */
725 set_hdr = ofpbuf_put_zeros(&msg, sizeof *set_hdr);
726 set_hdr->set_id = htons(ipfix_get_template_id(l2, l3, l4));
728 /* The fields defined in the ipfix_data_record_* structs and sent
729 * below must match exactly the templates defined in
730 * ipfix_define_template_fields. */
732 /* Common Ethernet entities. */
734 struct ipfix_data_record_common *data_common;
735 uint16_t ethernet_total_length;
736 uint8_t ethernet_header_length;
737 uint64_t layer2_octet_delta_count;
739 ethernet_total_length = packet->size;
740 ethernet_header_length = (l2 == IPFIX_PROTO_L2_VLAN)
741 ? VLAN_ETH_HEADER_LEN : ETH_HEADER_LEN;
743 /* Calculate the total matched octet count by considering as
744 * an approximation that all matched packets have the same
746 layer2_octet_delta_count = packet_delta_count * ethernet_total_length;
748 data_common = ofpbuf_put_zeros(&msg, sizeof *data_common);
749 data_common->observation_point_id = htonl(obs_point_id);
750 data_common->packet_delta_count = htonll(packet_delta_count);
751 data_common->layer2_octet_delta_count =
752 htonll(layer2_octet_delta_count);
753 memcpy(data_common->source_mac_address, flow->dl_src,
754 sizeof flow->dl_src);
755 memcpy(data_common->destination_mac_address, flow->dl_dst,
756 sizeof flow->dl_dst);
757 data_common->ethernet_type = flow->dl_type;
758 data_common->ethernet_total_length = htons(ethernet_total_length);
759 data_common->ethernet_header_length = ethernet_header_length;
762 if (l2 == IPFIX_PROTO_L2_VLAN) {
763 struct ipfix_data_record_vlan *data_vlan;
764 uint16_t vlan_id = vlan_tci_to_vid(flow->vlan_tci);
765 uint8_t priority = vlan_tci_to_pcp(flow->vlan_tci);
767 data_vlan = ofpbuf_put_zeros(&msg, sizeof *data_vlan);
768 data_vlan->vlan_id = htons(vlan_id);
769 data_vlan->dot1q_vlan_id = htons(vlan_id);
770 data_vlan->dot1q_priority = priority;
773 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
774 struct ipfix_data_record_ip *data_ip;
776 data_ip = ofpbuf_put_zeros(&msg, sizeof *data_ip);
777 data_ip->ip_version = (l3 == IPFIX_PROTO_L3_IPV4) ? 4 : 6;
778 data_ip->ip_ttl = flow->nw_ttl;
779 data_ip->protocol_identifier = flow->nw_proto;
780 data_ip->ip_diff_serv_code_point = flow->nw_tos >> 2;
781 data_ip->ip_precedence = flow->nw_tos >> 5;
782 data_ip->ip_class_of_service = flow->nw_tos;
784 if (l3 == IPFIX_PROTO_L3_IPV4) {
785 struct ipfix_data_record_ipv4 *data_ipv4;
786 data_ipv4 = ofpbuf_put_zeros(&msg, sizeof *data_ipv4);
787 data_ipv4->source_ipv4_address = flow->nw_src;
788 data_ipv4->destination_ipv4_address = flow->nw_dst;
789 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
790 struct ipfix_data_record_ipv6 *data_ipv6;
792 data_ipv6 = ofpbuf_put_zeros(&msg, sizeof *data_ipv6);
793 memcpy(data_ipv6->source_ipv6_address, &flow->ipv6_src,
794 sizeof flow->ipv6_src);
795 memcpy(data_ipv6->destination_ipv6_address, &flow->ipv6_dst,
796 sizeof flow->ipv6_dst);
797 data_ipv6->flow_label_ipv6 = flow->ipv6_label;
801 if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
802 struct ipfix_data_record_tcpudp *data_tcpudp;
804 data_tcpudp = ofpbuf_put_zeros(&msg, sizeof *data_tcpudp);
805 data_tcpudp->source_transport_port = flow->tp_src;
806 data_tcpudp->destination_transport_port = flow->tp_dst;
809 set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
810 set_hdr->length = htons(msg.size - set_hdr_offset);
812 ipfix_send_msg(exporter->collectors, &msg);
818 dpif_ipfix_sample(struct dpif_ipfix_exporter *exporter,
819 struct ofpbuf *packet, const struct flow *flow,
820 uint64_t packet_delta_count, uint32_t obs_domain_id,
821 uint32_t obs_point_id)
823 time_t now = time_wall();
824 if ((exporter->last_template_set_time + IPFIX_TEMPLATE_INTERVAL) <= now) {
825 ipfix_send_template_msg(exporter, obs_domain_id);
826 exporter->last_template_set_time = now;
829 ipfix_send_data_msg(exporter, packet, flow, packet_delta_count,
830 obs_domain_id, obs_point_id);
834 dpif_ipfix_bridge_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
835 const struct flow *flow)
837 /* Use the sampling probability as an approximation of the number
838 * of matched packets. */
839 uint64_t packet_delta_count = UINT32_MAX / di->bridge_exporter.probability;
841 dpif_ipfix_sample(&di->bridge_exporter.exporter, packet, flow,
843 di->bridge_exporter.options->obs_domain_id,
844 di->bridge_exporter.options->obs_point_id);
848 dpif_ipfix_flow_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
849 const struct flow *flow, uint32_t collector_set_id,
850 uint16_t probability, uint32_t obs_domain_id,
851 uint32_t obs_point_id)
853 struct dpif_ipfix_flow_exporter_map_node *node;
854 /* Use the sampling probability as an approximation of the number
855 * of matched packets. */
856 uint64_t packet_delta_count = USHRT_MAX / probability;
858 node = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
864 dpif_ipfix_sample(&node->exporter.exporter, packet, flow,
865 packet_delta_count, obs_domain_id, obs_point_id);