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. */
67 #define IPFIX_VERSION 0x000a
69 /* When using UDP, IPFIX Template Records must be re-sent regularly.
70 * The standard default interval is 10 minutes (600 seconds).
71 * Cf. IETF RFC 5101 Section 10.3.6. */
72 #define IPFIX_TEMPLATE_INTERVAL 600
74 /* Cf. IETF RFC 5101 Section 3.1. */
76 ovs_be16 version; /* IPFIX_VERSION. */
77 ovs_be16 length; /* Length in bytes including this header. */
78 ovs_be32 export_time; /* Seconds since the epoch. */
79 ovs_be32 seq_number; /* Message sequence number. */
80 ovs_be32 obs_domain_id; /* Observation Domain ID. */
81 } __attribute__((packed));
82 BUILD_ASSERT_DECL(sizeof(struct ipfix_header) == 16);
84 #define IPFIX_SET_ID_TEMPLATE 2
85 #define IPFIX_SET_ID_OPTION_TEMPLATE 3
87 /* Cf. IETF RFC 5101 Section 3.3.2. */
88 struct ipfix_set_header {
89 ovs_be16 set_id; /* IPFIX_SET_ID_* or valid template ID for Data Sets. */
90 ovs_be16 length; /* Length of the set in bytes including header. */
91 } __attribute__((packed));
92 BUILD_ASSERT_DECL(sizeof(struct ipfix_set_header) == 4);
94 /* Alternatives for templates at each layer. A template is defined by
95 * a combination of one value for each layer. */
97 IPFIX_PROTO_L2_ETH = 0, /* No VLAN. */
101 enum ipfix_proto_l3 {
102 IPFIX_PROTO_L3_UNKNOWN = 0,
107 enum ipfix_proto_l4 {
108 IPFIX_PROTO_L4_UNKNOWN = 0,
109 IPFIX_PROTO_L4_TCP_UDP,
113 /* Any Template ID > 255 is usable for Template Records. */
114 #define IPFIX_TEMPLATE_ID_MIN 256
116 /* Cf. IETF RFC 5101 Section 3.4.1. */
117 struct ipfix_template_record_header {
118 ovs_be16 template_id;
119 ovs_be16 field_count;
120 } __attribute__((packed));
121 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_record_header) == 4);
123 enum ipfix_entity_id {
124 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_ID_##ENUM = ID,
125 #include "ofproto/ipfix-entities.def"
128 enum ipfix_entity_size {
129 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME) IPFIX_ENTITY_SIZE_##ENUM = SIZE,
130 #include "ofproto/ipfix-entities.def"
133 struct ipfix_template_field_specifier {
134 ovs_be16 element_id; /* IPFIX_ENTITY_ID_*. */
135 ovs_be16 field_length; /* Length of the field's value, in bytes. */
136 /* No Enterprise ID, since only standard element IDs are specified. */
137 } __attribute__((packed));
138 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_field_specifier) == 4);
140 /* Part of data record for common metadata and Ethernet entities. */
141 struct ipfix_data_record_common {
142 ovs_be32 observation_point_id; /* OBSERVATION_POINT_ID */
143 ovs_be64 packet_delta_count; /* PACKET_DELTA_COUNT */
144 ovs_be64 layer2_octet_delta_count; /* LAYER2_OCTET_DELTA_COUNT */
145 uint8_t source_mac_address[6]; /* SOURCE_MAC_ADDRESS */
146 uint8_t destination_mac_address[6]; /* DESTINATION_MAC_ADDRESS */
147 ovs_be16 ethernet_type; /* ETHERNET_TYPE */
148 ovs_be16 ethernet_total_length; /* ETHERNET_TOTAL_LENGTH */
149 uint8_t ethernet_header_length; /* ETHERNET_HEADER_LENGTH */
150 } __attribute__((packed));
151 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_common) == 37);
153 /* Part of data record for VLAN entities. */
154 struct ipfix_data_record_vlan {
155 ovs_be16 vlan_id; /* VLAN_ID */
156 ovs_be16 dot1q_vlan_id; /* DOT1Q_VLAN_ID */
157 uint8_t dot1q_priority; /* DOT1Q_PRIORITY */
158 } __attribute__((packed));
159 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_vlan) == 5);
161 /* Part of data record for IP entities. */
162 struct ipfix_data_record_ip {
163 uint8_t ip_version; /* IP_VERSION */
164 uint8_t ip_ttl; /* IP_TTL */
165 uint8_t protocol_identifier; /* PROTOCOL_IDENTIFIER */
166 uint8_t ip_diff_serv_code_point; /* IP_DIFF_SERV_CODE_POINT */
167 uint8_t ip_precedence; /* IP_PRECEDENCE */
168 uint8_t ip_class_of_service; /* IP_CLASS_OF_SERVICE */
169 } __attribute__((packed));
170 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ip) == 6);
172 /* Part of data record for IPv4 entities. */
173 struct ipfix_data_record_ipv4 {
174 ovs_be32 source_ipv4_address; /* SOURCE_IPV4_ADDRESS */
175 ovs_be32 destination_ipv4_address; /* DESTINATION_IPV4_ADDRESS */
176 } __attribute__((packed));
177 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ipv4) == 8);
179 /* Part of data record for IPv4 entities. */
180 struct ipfix_data_record_ipv6 {
181 uint8_t source_ipv6_address[16]; /* SOURCE_IPV6_ADDRESS */
182 uint8_t destination_ipv6_address[16]; /* DESTINATION_IPV6_ADDRESS */
183 ovs_be32 flow_label_ipv6; /* FLOW_LABEL_IPV6 */
184 } __attribute__((packed));
185 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ipv6) == 36);
187 /* Part of data record for TCP/UDP entities. */
188 struct ipfix_data_record_tcpudp {
189 ovs_be16 source_transport_port; /* SOURCE_TRANSPORT_PORT */
190 ovs_be16 destination_transport_port; /* DESTINATION_TRANSPORT_PORT */
191 } __attribute__((packed));
192 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_tcpudp) == 4);
195 ofproto_ipfix_bridge_exporter_options_equal(
196 const struct ofproto_ipfix_bridge_exporter_options *a,
197 const struct ofproto_ipfix_bridge_exporter_options *b)
199 return (a->obs_domain_id == b->obs_domain_id
200 && a->obs_point_id == b->obs_point_id
201 && a->sampling_rate == b->sampling_rate
202 && sset_equals(&a->targets, &b->targets));
205 static struct ofproto_ipfix_bridge_exporter_options *
206 ofproto_ipfix_bridge_exporter_options_clone(
207 const struct ofproto_ipfix_bridge_exporter_options *old)
209 struct ofproto_ipfix_bridge_exporter_options *new =
210 xmemdup(old, sizeof *old);
211 sset_clone(&new->targets, &old->targets);
216 ofproto_ipfix_bridge_exporter_options_destroy(
217 struct ofproto_ipfix_bridge_exporter_options *options)
220 sset_destroy(&options->targets);
226 ofproto_ipfix_flow_exporter_options_equal(
227 const struct ofproto_ipfix_flow_exporter_options *a,
228 const struct ofproto_ipfix_flow_exporter_options *b)
230 return (a->collector_set_id == b->collector_set_id
231 && sset_equals(&a->targets, &b->targets));
234 static struct ofproto_ipfix_flow_exporter_options *
235 ofproto_ipfix_flow_exporter_options_clone(
236 const struct ofproto_ipfix_flow_exporter_options *old)
238 struct ofproto_ipfix_flow_exporter_options *new =
239 xmemdup(old, sizeof *old);
240 sset_clone(&new->targets, &old->targets);
245 ofproto_ipfix_flow_exporter_options_destroy(
246 struct ofproto_ipfix_flow_exporter_options *options)
249 sset_destroy(&options->targets);
255 dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
257 collectors_destroy(exporter->collectors);
258 exporter->collectors = NULL;
259 exporter->seq_number = 1;
260 exporter->last_template_set_time = TIME_MIN;
264 dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter *exporter,
265 const struct sset *targets)
267 collectors_destroy(exporter->collectors);
268 collectors_create(targets, IPFIX_DEFAULT_COLLECTOR_PORT,
269 &exporter->collectors);
270 if (exporter->collectors == NULL) {
271 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
272 "IPFIX exporter disabled");
273 dpif_ipfix_exporter_clear(exporter);
280 dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
282 dpif_ipfix_exporter_clear(&exporter->exporter);
283 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
284 exporter->options = NULL;
285 exporter->probability = 0;
289 dpif_ipfix_bridge_exporter_set_options(
290 struct dpif_ipfix_bridge_exporter *exporter,
291 const struct ofproto_ipfix_bridge_exporter_options *options)
293 bool options_changed;
295 if (!options || sset_is_empty(&options->targets)) {
296 /* No point in doing any work if there are no targets. */
297 dpif_ipfix_bridge_exporter_clear(exporter);
303 || !ofproto_ipfix_bridge_exporter_options_equal(
304 options, exporter->options));
306 /* Configure collectors if options have changed or if we're
307 * shortchanged in collectors (which indicates that opening one or
308 * more of the configured collectors failed, so that we should
311 || collectors_count(exporter->exporter.collectors)
312 < sset_count(&options->targets)) {
313 if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
314 &options->targets)) {
319 /* Avoid reconfiguring if options didn't change. */
320 if (!options_changed) {
324 ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
325 exporter->options = ofproto_ipfix_bridge_exporter_options_clone(options);
326 exporter->probability =
327 MAX(1, UINT32_MAX / exporter->options->sampling_rate);
330 static struct dpif_ipfix_flow_exporter_map_node*
331 dpif_ipfix_find_flow_exporter_map_node(
332 const struct dpif_ipfix *di, const uint32_t collector_set_id)
334 struct dpif_ipfix_flow_exporter_map_node *exporter_node;
336 HMAP_FOR_EACH_WITH_HASH (exporter_node, node,
337 hash_int(collector_set_id, 0),
338 &di->flow_exporter_map) {
339 if (exporter_node->exporter.options->collector_set_id
340 == collector_set_id) {
341 return exporter_node;
349 dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
351 dpif_ipfix_exporter_clear(&exporter->exporter);
352 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
353 exporter->options = NULL;
357 dpif_ipfix_flow_exporter_set_options(
358 struct dpif_ipfix_flow_exporter *exporter,
359 const struct ofproto_ipfix_flow_exporter_options *options)
361 bool options_changed;
363 if (sset_is_empty(&options->targets)) {
364 /* No point in doing any work if there are no targets. */
365 dpif_ipfix_flow_exporter_clear(exporter);
371 || !ofproto_ipfix_flow_exporter_options_equal(
372 options, exporter->options));
374 /* Configure collectors if options have changed or if we're
375 * shortchanged in collectors (which indicates that opening one or
376 * more of the configured collectors failed, so that we should
379 || collectors_count(exporter->exporter.collectors)
380 < sset_count(&options->targets)) {
381 if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
382 &options->targets)) {
387 /* Avoid reconfiguring if options didn't change. */
388 if (!options_changed) {
392 ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
393 exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
399 dpif_ipfix_set_options(
400 struct dpif_ipfix *di,
401 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
402 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
403 size_t n_flow_exporters_options)
406 struct ofproto_ipfix_flow_exporter_options *options;
407 struct dpif_ipfix_flow_exporter_map_node *node, *next;
408 size_t n_broken_flow_exporters_options = 0;
410 dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
411 bridge_exporter_options);
413 /* Add new flow exporters and update current flow exporters. */
414 options = (struct ofproto_ipfix_flow_exporter_options *)
415 flow_exporters_options;
416 for (i = 0; i < n_flow_exporters_options; i++) {
417 node = dpif_ipfix_find_flow_exporter_map_node(
418 di, options->collector_set_id);
420 node = xzalloc(sizeof *node);
421 dpif_ipfix_exporter_clear(&node->exporter.exporter);
422 hmap_insert(&di->flow_exporter_map, &node->node,
423 hash_int(options->collector_set_id, 0));
425 if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
426 n_broken_flow_exporters_options++;
431 ovs_assert(hmap_count(&di->flow_exporter_map) >=
432 (n_flow_exporters_options - n_broken_flow_exporters_options));
434 /* Remove dropped flow exporters, if any needs to be removed. */
435 if (hmap_count(&di->flow_exporter_map) > n_flow_exporters_options) {
436 HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
437 /* This is slow but doesn't take any extra memory, and
438 * this table is not supposed to contain many rows anyway. */
439 options = (struct ofproto_ipfix_flow_exporter_options *)
440 flow_exporters_options;
441 for (i = 0; i < n_flow_exporters_options; i++) {
442 if (node->exporter.options->collector_set_id
443 == options->collector_set_id) {
448 if (i == n_flow_exporters_options) { // Not found.
449 hmap_remove(&di->flow_exporter_map, &node->node);
450 dpif_ipfix_flow_exporter_clear(&node->exporter);
456 ovs_assert(hmap_count(&di->flow_exporter_map) ==
457 (n_flow_exporters_options - n_broken_flow_exporters_options));
461 dpif_ipfix_create(void)
463 struct dpif_ipfix *di;
464 di = xzalloc(sizeof *di);
465 dpif_ipfix_exporter_clear(&di->bridge_exporter.exporter);
466 hmap_init(&di->flow_exporter_map);
471 dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
473 return di->bridge_exporter.probability;
477 dpif_ipfix_clear(struct dpif_ipfix *di)
479 struct dpif_ipfix_flow_exporter_map_node *node, *next;
481 dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
483 HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
484 hmap_remove(&di->flow_exporter_map, &node->node);
485 dpif_ipfix_flow_exporter_clear(&node->exporter);
491 dpif_ipfix_destroy(struct dpif_ipfix *di)
494 dpif_ipfix_clear(di);
495 hmap_destroy(&di->flow_exporter_map);
501 ipfix_init_header(uint32_t seq_number, uint32_t obs_domain_id,
504 struct ipfix_header *hdr;
506 hdr = ofpbuf_put_zeros(msg, sizeof *hdr);
507 hdr->version = htons(IPFIX_VERSION);
508 hdr->length = htons(sizeof *hdr); /* Updated in ipfix_send_msg. */
509 hdr->export_time = htonl(time_wall());
510 hdr->seq_number = htonl(seq_number);
511 hdr->obs_domain_id = htonl(obs_domain_id);
515 ipfix_send_msg(const struct collectors *collectors, struct ofpbuf *msg)
517 struct ipfix_header *hdr;
519 /* Adjust the length in the header. */
521 hdr->length = htons(msg->size);
523 collectors_send(collectors, msg->data, msg->size);
528 ipfix_get_template_id(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
529 enum ipfix_proto_l4 l4)
531 uint16_t template_id;
533 template_id = template_id * NUM_IPFIX_PROTO_L3 + l3;
534 template_id = template_id * NUM_IPFIX_PROTO_L4 + l4;
535 return IPFIX_TEMPLATE_ID_MIN + template_id;
539 ipfix_define_template_entity(enum ipfix_entity_id id,
540 enum ipfix_entity_size size, struct ofpbuf *msg)
542 struct ipfix_template_field_specifier *field;
544 field = ofpbuf_put_zeros(msg, sizeof *field);
545 field->element_id = htons(id);
546 field->field_length = htons(size);
550 ipfix_define_template_fields(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
551 enum ipfix_proto_l4 l4, struct ofpbuf *msg)
557 ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
558 IPFIX_ENTITY_SIZE_##ID, msg); \
562 DEF(OBSERVATION_POINT_ID);
563 DEF(PACKET_DELTA_COUNT);
564 DEF(LAYER2_OCTET_DELTA_COUNT);
566 /* Common Ethernet entities. */
567 DEF(SOURCE_MAC_ADDRESS);
568 DEF(DESTINATION_MAC_ADDRESS);
570 DEF(ETHERNET_TOTAL_LENGTH);
571 DEF(ETHERNET_HEADER_LENGTH);
573 if (l2 == IPFIX_PROTO_L2_VLAN) {
579 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
582 DEF(PROTOCOL_IDENTIFIER);
583 DEF(IP_DIFF_SERV_CODE_POINT);
585 DEF(IP_CLASS_OF_SERVICE);
587 if (l3 == IPFIX_PROTO_L3_IPV4) {
588 DEF(SOURCE_IPV4_ADDRESS);
589 DEF(DESTINATION_IPV4_ADDRESS);
590 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
591 DEF(SOURCE_IPV6_ADDRESS);
592 DEF(DESTINATION_IPV6_ADDRESS);
593 DEF(FLOW_LABEL_IPV6);
597 if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
598 DEF(SOURCE_TRANSPORT_PORT);
599 DEF(DESTINATION_TRANSPORT_PORT);
608 ipfix_send_template_msg(struct dpif_ipfix_exporter *exporter,
609 uint32_t obs_domain_id)
611 uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
613 size_t set_hdr_offset, tmpl_hdr_offset;
614 struct ipfix_set_header *set_hdr;
615 struct ipfix_template_record_header *tmpl_hdr;
616 uint16_t field_count;
617 enum ipfix_proto_l2 l2;
618 enum ipfix_proto_l3 l3;
619 enum ipfix_proto_l4 l4;
621 ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
623 ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
624 set_hdr_offset = msg.size;
626 /* Add a Template Set. */
627 set_hdr = ofpbuf_put_zeros(&msg, sizeof *set_hdr);
628 set_hdr->set_id = htons(IPFIX_SET_ID_TEMPLATE);
630 /* Define one template for each possible combination of
632 for (l2 = 0; l2 < NUM_IPFIX_PROTO_L2; l2++) {
633 for (l3 = 0; l3 < NUM_IPFIX_PROTO_L3; l3++) {
634 for (l4 = 0; l4 < NUM_IPFIX_PROTO_L4; l4++) {
635 if (l3 == IPFIX_PROTO_L3_UNKNOWN &&
636 l4 != IPFIX_PROTO_L4_UNKNOWN) {
639 tmpl_hdr_offset = msg.size;
640 tmpl_hdr = ofpbuf_put_zeros(&msg, sizeof *tmpl_hdr);
641 tmpl_hdr->template_id = htons(
642 ipfix_get_template_id(l2, l3, l4));
643 field_count = ipfix_define_template_fields(l2, l3, l4, &msg);
644 tmpl_hdr = (struct ipfix_template_record_header*)
645 ((uint8_t*)msg.data + tmpl_hdr_offset);
646 tmpl_hdr->field_count = htons(field_count);
651 set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
652 set_hdr->length = htons(msg.size - set_hdr_offset);
654 /* TODO: Add Options Template Sets, at least to define a Flow Keys
655 * Option Template. */
657 ipfix_send_msg(exporter->collectors, &msg);
663 ipfix_send_data_msg(struct dpif_ipfix_exporter *exporter, struct ofpbuf *packet,
664 const struct flow *flow, uint64_t packet_delta_count,
665 uint32_t obs_domain_id, uint32_t obs_point_id)
667 uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
669 size_t set_hdr_offset;
670 struct ipfix_set_header *set_hdr;
671 enum ipfix_proto_l2 l2;
672 enum ipfix_proto_l3 l3;
673 enum ipfix_proto_l4 l4;
675 ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
677 ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
678 exporter->seq_number++;
679 set_hdr_offset = msg.size;
681 /* Choose the right template ID matching the protocols in the
683 l2 = (flow->vlan_tci == 0) ? IPFIX_PROTO_L2_ETH : IPFIX_PROTO_L2_VLAN;
685 switch(ntohs(flow->dl_type)) {
687 l3 = IPFIX_PROTO_L3_IPV4;
690 l3 = IPFIX_PROTO_L3_IPV6;
693 l3 = IPFIX_PROTO_L3_UNKNOWN;
696 l4 = IPFIX_PROTO_L4_UNKNOWN;
697 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
698 switch(flow->nw_proto) {
699 case IPPROTO_TCP: /* TCP */
700 case IPPROTO_UDP: /* UDP */
701 l4 = IPFIX_PROTO_L4_TCP_UDP;
706 /* Add a Data Set. */
707 set_hdr = ofpbuf_put_zeros(&msg, sizeof *set_hdr);
708 set_hdr->set_id = htons(ipfix_get_template_id(l2, l3, l4));
710 /* The fields defined in the ipfix_data_record_* structs and sent
711 * below must match exactly the templates defined in
712 * ipfix_define_template_fields. */
714 /* Common Ethernet entities. */
716 struct ipfix_data_record_common *data_common;
717 uint16_t ethernet_total_length;
718 uint8_t ethernet_header_length;
719 uint64_t layer2_octet_delta_count;
721 ethernet_total_length = packet->size;
722 ethernet_header_length = (l2 == IPFIX_PROTO_L2_VLAN)
723 ? VLAN_ETH_HEADER_LEN : ETH_HEADER_LEN;
725 /* Calculate the total matched octet count by considering as
726 * an approximation that all matched packets have the same
728 layer2_octet_delta_count = packet_delta_count * ethernet_total_length;
730 data_common = ofpbuf_put_zeros(&msg, sizeof *data_common);
731 data_common->observation_point_id = htonl(obs_point_id);
732 data_common->packet_delta_count = htonll(packet_delta_count);
733 data_common->layer2_octet_delta_count =
734 htonll(layer2_octet_delta_count);
735 memcpy(data_common->source_mac_address, flow->dl_src,
736 sizeof flow->dl_src);
737 memcpy(data_common->destination_mac_address, flow->dl_dst,
738 sizeof flow->dl_dst);
739 data_common->ethernet_type = flow->dl_type;
740 data_common->ethernet_total_length = htons(ethernet_total_length);
741 data_common->ethernet_header_length = ethernet_header_length;
744 if (l2 == IPFIX_PROTO_L2_VLAN) {
745 struct ipfix_data_record_vlan *data_vlan;
746 uint16_t vlan_id = vlan_tci_to_vid(flow->vlan_tci);
747 uint8_t priority = vlan_tci_to_pcp(flow->vlan_tci);
749 data_vlan = ofpbuf_put_zeros(&msg, sizeof *data_vlan);
750 data_vlan->vlan_id = htons(vlan_id);
751 data_vlan->dot1q_vlan_id = htons(vlan_id);
752 data_vlan->dot1q_priority = priority;
755 if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
756 struct ipfix_data_record_ip *data_ip;
758 data_ip = ofpbuf_put_zeros(&msg, sizeof *data_ip);
759 data_ip->ip_version = (l3 == IPFIX_PROTO_L3_IPV4) ? 4 : 6;
760 data_ip->ip_ttl = flow->nw_ttl;
761 data_ip->protocol_identifier = flow->nw_proto;
762 data_ip->ip_diff_serv_code_point = flow->nw_tos >> 2;
763 data_ip->ip_precedence = flow->nw_tos >> 5;
764 data_ip->ip_class_of_service = flow->nw_tos;
766 if (l3 == IPFIX_PROTO_L3_IPV4) {
767 struct ipfix_data_record_ipv4 *data_ipv4;
768 data_ipv4 = ofpbuf_put_zeros(&msg, sizeof *data_ipv4);
769 data_ipv4->source_ipv4_address = flow->nw_src;
770 data_ipv4->destination_ipv4_address = flow->nw_dst;
771 } else { /* l3 == IPFIX_PROTO_L3_IPV6 */
772 struct ipfix_data_record_ipv6 *data_ipv6;
774 data_ipv6 = ofpbuf_put_zeros(&msg, sizeof *data_ipv6);
775 memcpy(data_ipv6->source_ipv6_address, &flow->ipv6_src,
776 sizeof flow->ipv6_src);
777 memcpy(data_ipv6->destination_ipv6_address, &flow->ipv6_dst,
778 sizeof flow->ipv6_dst);
779 data_ipv6->flow_label_ipv6 = flow->ipv6_label;
783 if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
784 struct ipfix_data_record_tcpudp *data_tcpudp;
786 data_tcpudp = ofpbuf_put_zeros(&msg, sizeof *data_tcpudp);
787 data_tcpudp->source_transport_port = flow->tp_src;
788 data_tcpudp->destination_transport_port = flow->tp_dst;
791 set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
792 set_hdr->length = htons(msg.size - set_hdr_offset);
794 ipfix_send_msg(exporter->collectors, &msg);
800 dpif_ipfix_sample(struct dpif_ipfix_exporter *exporter,
801 struct ofpbuf *packet, const struct flow *flow,
802 uint64_t packet_delta_count, uint32_t obs_domain_id,
803 uint32_t obs_point_id)
805 time_t now = time_wall();
806 if ((exporter->last_template_set_time + IPFIX_TEMPLATE_INTERVAL) <= now) {
807 ipfix_send_template_msg(exporter, obs_domain_id);
808 exporter->last_template_set_time = now;
811 ipfix_send_data_msg(exporter, packet, flow, packet_delta_count,
812 obs_domain_id, obs_point_id);
816 dpif_ipfix_bridge_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
817 const struct flow *flow)
819 /* Use the sampling probability as an approximation of the number
820 * of matched packets. */
821 uint64_t packet_delta_count = UINT32_MAX / di->bridge_exporter.probability;
823 dpif_ipfix_sample(&di->bridge_exporter.exporter, packet, flow,
825 di->bridge_exporter.options->obs_domain_id,
826 di->bridge_exporter.options->obs_point_id);
830 dpif_ipfix_flow_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
831 const struct flow *flow, uint32_t collector_set_id,
832 uint16_t probability, uint32_t obs_domain_id,
833 uint32_t obs_point_id)
835 struct dpif_ipfix_flow_exporter_map_node *node;
836 /* Use the sampling probability as an approximation of the number
837 * of matched packets. */
838 uint64_t packet_delta_count = USHRT_MAX / probability;
840 node = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
846 dpif_ipfix_sample(&node->exporter.exporter, packet, flow,
847 packet_delta_count, obs_domain_id, obs_point_id);