ofp-print: ofp_packet_to_string() Do not emit extra trailing newline
[sliver-openvswitch.git] / ofproto / ofproto-dpif-ipfix.c
1 /*
2  * Copyright (c) 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ofproto-dpif-ipfix.h"
19 #include "byte-order.h"
20 #include "collectors.h"
21 #include "flow.h"
22 #include "hash.h"
23 #include "hmap.h"
24 #include "ofpbuf.h"
25 #include "ofproto.h"
26 #include "packets.h"
27 #include "sset.h"
28 #include "util.h"
29 #include "timeval.h"
30 #include "util.h"
31 #include "vlog.h"
32
33 VLOG_DEFINE_THIS_MODULE(ipfix);
34
35 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
36
37 /* Cf. IETF RFC 5101 Section 10.3.4. */
38 #define IPFIX_DEFAULT_COLLECTOR_PORT 4739
39
40 struct dpif_ipfix_exporter {
41     struct collectors *collectors;
42     uint32_t seq_number;
43     time_t last_template_set_time;
44 };
45
46 struct dpif_ipfix_bridge_exporter {
47     struct dpif_ipfix_exporter exporter;
48     struct ofproto_ipfix_bridge_exporter_options *options;
49     uint32_t probability;
50 };
51
52 struct dpif_ipfix_flow_exporter {
53     struct dpif_ipfix_exporter exporter;
54     struct ofproto_ipfix_flow_exporter_options *options;
55 };
56
57 struct dpif_ipfix_flow_exporter_map_node {
58     struct hmap_node node;
59     struct dpif_ipfix_flow_exporter exporter;
60 };
61
62 struct dpif_ipfix {
63     struct dpif_ipfix_bridge_exporter bridge_exporter;
64     struct hmap flow_exporter_map;  /* dpif_ipfix_flow_exporter_map_nodes. */
65 };
66
67 #define IPFIX_VERSION 0x000a
68
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
73
74 /* Cf. IETF RFC 5101 Section 3.1. */
75 struct ipfix_header {
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);
83
84 #define IPFIX_SET_ID_TEMPLATE 2
85 #define IPFIX_SET_ID_OPTION_TEMPLATE 3
86
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);
93
94 /* Alternatives for templates at each layer.  A template is defined by
95  * a combination of one value for each layer. */
96 enum ipfix_proto_l2 {
97     IPFIX_PROTO_L2_ETH = 0,  /* No VLAN. */
98     IPFIX_PROTO_L2_VLAN,
99     NUM_IPFIX_PROTO_L2
100 };
101 enum ipfix_proto_l3 {
102     IPFIX_PROTO_L3_UNKNOWN = 0,
103     IPFIX_PROTO_L3_IPV4,
104     IPFIX_PROTO_L3_IPV6,
105     NUM_IPFIX_PROTO_L3
106 };
107 enum ipfix_proto_l4 {
108     IPFIX_PROTO_L4_UNKNOWN = 0,
109     IPFIX_PROTO_L4_TCP_UDP,
110     NUM_IPFIX_PROTO_L4
111 };
112
113 /* Any Template ID > 255 is usable for Template Records. */
114 #define IPFIX_TEMPLATE_ID_MIN 256
115
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);
122
123 enum ipfix_entity_id {
124 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME)  IPFIX_ENTITY_ID_##ENUM = ID,
125 #include "ofproto/ipfix-entities.def"
126 };
127
128 enum ipfix_entity_size {
129 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME)  IPFIX_ENTITY_SIZE_##ENUM = SIZE,
130 #include "ofproto/ipfix-entities.def"
131 };
132
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);
139
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);
152
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);
160
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);
171
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);
178
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);
186
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);
193
194 static bool
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)
198 {
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));
203 }
204
205 static struct ofproto_ipfix_bridge_exporter_options *
206 ofproto_ipfix_bridge_exporter_options_clone(
207     const struct ofproto_ipfix_bridge_exporter_options *old)
208 {
209     struct ofproto_ipfix_bridge_exporter_options *new =
210         xmemdup(old, sizeof *old);
211     sset_clone(&new->targets, &old->targets);
212     return new;
213 }
214
215 static void
216 ofproto_ipfix_bridge_exporter_options_destroy(
217     struct ofproto_ipfix_bridge_exporter_options *options)
218 {
219     if (options) {
220         sset_destroy(&options->targets);
221         free(options);
222     }
223 }
224
225 static bool
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)
229 {
230     return (a->collector_set_id == b->collector_set_id
231             && sset_equals(&a->targets, &b->targets));
232 }
233
234 static struct ofproto_ipfix_flow_exporter_options *
235 ofproto_ipfix_flow_exporter_options_clone(
236     const struct ofproto_ipfix_flow_exporter_options *old)
237 {
238     struct ofproto_ipfix_flow_exporter_options *new =
239         xmemdup(old, sizeof *old);
240     sset_clone(&new->targets, &old->targets);
241     return new;
242 }
243
244 static void
245 ofproto_ipfix_flow_exporter_options_destroy(
246     struct ofproto_ipfix_flow_exporter_options *options)
247 {
248     if (options) {
249         sset_destroy(&options->targets);
250         free(options);
251     }
252 }
253
254 static void
255 dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
256 {
257     collectors_destroy(exporter->collectors);
258     exporter->collectors = NULL;
259     exporter->seq_number = 1;
260     exporter->last_template_set_time = TIME_MIN;
261 }
262
263 static bool
264 dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter *exporter,
265                                 const struct sset *targets)
266 {
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);
274         return false;
275     }
276     return true;
277 }
278
279 static void
280 dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
281 {
282     dpif_ipfix_exporter_clear(&exporter->exporter);
283     ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
284     exporter->options = NULL;
285     exporter->probability = 0;
286 }
287
288 static void
289 dpif_ipfix_bridge_exporter_set_options(
290     struct dpif_ipfix_bridge_exporter *exporter,
291     const struct ofproto_ipfix_bridge_exporter_options *options)
292 {
293     bool options_changed;
294
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);
298         return;
299     }
300
301     options_changed = (
302         !exporter->options
303         || !ofproto_ipfix_bridge_exporter_options_equal(
304             options, exporter->options));
305
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
309      * retry). */
310     if (options_changed
311         || collectors_count(exporter->exporter.collectors)
312             < sset_count(&options->targets)) {
313         if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
314                                              &options->targets)) {
315             return;
316         }
317     }
318
319     /* Avoid reconfiguring if options didn't change. */
320     if (!options_changed) {
321         return;
322     }
323
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);
328 }
329
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)
333 {
334     struct dpif_ipfix_flow_exporter_map_node *exporter_node;
335
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;
342         }
343     }
344
345     return NULL;
346 }
347
348 static void
349 dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
350 {
351     dpif_ipfix_exporter_clear(&exporter->exporter);
352     ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
353     exporter->options = NULL;
354 }
355
356 static bool
357 dpif_ipfix_flow_exporter_set_options(
358     struct dpif_ipfix_flow_exporter *exporter,
359     const struct ofproto_ipfix_flow_exporter_options *options)
360 {
361     bool options_changed;
362
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);
366         return true;
367     }
368
369     options_changed = (
370         !exporter->options
371         || !ofproto_ipfix_flow_exporter_options_equal(
372             options, exporter->options));
373
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
377      * retry). */
378     if (options_changed
379         || collectors_count(exporter->exporter.collectors)
380             < sset_count(&options->targets)) {
381         if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
382                                              &options->targets)) {
383             return false;
384         }
385     }
386
387     /* Avoid reconfiguring if options didn't change. */
388     if (!options_changed) {
389         return true;
390     }
391
392     ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
393     exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
394
395     return true;
396 }
397
398 void
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)
404 {
405     int i;
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;
409
410     dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
411                                            bridge_exporter_options);
412
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);
419         if (!node) {
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));
424         }
425         if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
426             n_broken_flow_exporters_options++;
427         }
428         options++;
429     }
430
431     ovs_assert(hmap_count(&di->flow_exporter_map) >=
432                (n_flow_exporters_options - n_broken_flow_exporters_options));
433
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) {
444                   break;
445               }
446               options++;
447             }
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);
451                 free(node);
452             }
453         }
454     }
455
456     ovs_assert(hmap_count(&di->flow_exporter_map) ==
457                (n_flow_exporters_options - n_broken_flow_exporters_options));
458 }
459
460 struct dpif_ipfix *
461 dpif_ipfix_create(void)
462 {
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);
467     return di;
468 }
469
470 uint32_t
471 dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
472 {
473     return di->bridge_exporter.probability;
474 }
475
476 static void
477 dpif_ipfix_clear(struct dpif_ipfix *di)
478 {
479     struct dpif_ipfix_flow_exporter_map_node *node, *next;
480
481     dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
482
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);
486         free(node);
487     }
488 }
489
490 void
491 dpif_ipfix_destroy(struct dpif_ipfix *di)
492 {
493     if (di) {
494         dpif_ipfix_clear(di);
495         hmap_destroy(&di->flow_exporter_map);
496         free(di);
497     }
498 }
499
500 static void
501 ipfix_init_header(uint32_t seq_number, uint32_t obs_domain_id,
502                   struct ofpbuf *msg)
503 {
504     struct ipfix_header *hdr;
505
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);
512 }
513
514 static void
515 ipfix_send_msg(const struct collectors *collectors, struct ofpbuf *msg)
516 {
517     struct ipfix_header *hdr;
518
519     /* Adjust the length in the header. */
520     hdr = msg->data;
521     hdr->length = htons(msg->size);
522
523     collectors_send(collectors, msg->data, msg->size);
524     msg->size = 0;
525 }
526
527 static uint16_t
528 ipfix_get_template_id(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
529                       enum ipfix_proto_l4 l4)
530 {
531     uint16_t template_id;
532     template_id = l2;
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;
536 }
537
538 static void
539 ipfix_define_template_entity(enum ipfix_entity_id id,
540                              enum ipfix_entity_size size, struct ofpbuf *msg)
541 {
542     struct ipfix_template_field_specifier *field;
543
544     field = ofpbuf_put_zeros(msg, sizeof *field);
545     field->element_id = htons(id);
546     field->field_length = htons(size);
547 }
548
549 static uint16_t
550 ipfix_define_template_fields(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
551                              enum ipfix_proto_l4 l4, struct ofpbuf *msg)
552 {
553     uint16_t count = 0;
554
555 #define DEF(ID) \
556     { \
557         ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
558                                      IPFIX_ENTITY_SIZE_##ID, msg); \
559         count++; \
560     }
561
562     DEF(OBSERVATION_POINT_ID);
563     DEF(PACKET_DELTA_COUNT);
564     DEF(LAYER2_OCTET_DELTA_COUNT);
565
566     /* Common Ethernet entities. */
567     DEF(SOURCE_MAC_ADDRESS);
568     DEF(DESTINATION_MAC_ADDRESS);
569     DEF(ETHERNET_TYPE);
570     DEF(ETHERNET_TOTAL_LENGTH);
571     DEF(ETHERNET_HEADER_LENGTH);
572
573     if (l2 == IPFIX_PROTO_L2_VLAN) {
574         DEF(VLAN_ID);
575         DEF(DOT1Q_VLAN_ID);
576         DEF(DOT1Q_PRIORITY);
577     }
578
579     if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
580         DEF(IP_VERSION);
581         DEF(IP_TTL);
582         DEF(PROTOCOL_IDENTIFIER);
583         DEF(IP_DIFF_SERV_CODE_POINT);
584         DEF(IP_PRECEDENCE);
585         DEF(IP_CLASS_OF_SERVICE);
586
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);
594         }
595     }
596
597     if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
598         DEF(SOURCE_TRANSPORT_PORT);
599         DEF(DESTINATION_TRANSPORT_PORT);
600     }
601
602 #undef DEF
603
604     return count;
605 }
606
607 static void
608 ipfix_send_template_msg(struct dpif_ipfix_exporter *exporter,
609                         uint32_t obs_domain_id)
610 {
611     uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
612     struct ofpbuf msg;
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;
620
621     ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
622
623     ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
624     set_hdr_offset = msg.size;
625
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);
629
630     /* Define one template for each possible combination of
631      * protocols. */
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) {
637                     continue;
638                 }
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);
647             }
648         }
649     }
650
651     set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
652     set_hdr->length = htons(msg.size - set_hdr_offset);
653
654     /* TODO: Add Options Template Sets, at least to define a Flow Keys
655      * Option Template. */
656
657     ipfix_send_msg(exporter->collectors, &msg);
658
659     ofpbuf_uninit(&msg);
660 }
661
662 static void
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)
666 {
667     uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
668     struct ofpbuf msg;
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;
674
675     ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
676
677     ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
678     exporter->seq_number++;
679     set_hdr_offset = msg.size;
680
681     /* Choose the right template ID matching the protocols in the
682      * sampled packet. */
683     l2 = (flow->vlan_tci == 0) ? IPFIX_PROTO_L2_ETH : IPFIX_PROTO_L2_VLAN;
684
685     switch(ntohs(flow->dl_type)) {
686     case ETH_TYPE_IP:
687         l3 = IPFIX_PROTO_L3_IPV4;
688         break;
689     case ETH_TYPE_IPV6:
690         l3 = IPFIX_PROTO_L3_IPV6;
691         break;
692     default:
693         l3 = IPFIX_PROTO_L3_UNKNOWN;
694     }
695
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;
702             break;
703         }
704     }
705
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));
709
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. */
713
714     /* Common Ethernet entities. */
715     {
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;
720
721         ethernet_total_length = packet->size;
722         ethernet_header_length = (l2 == IPFIX_PROTO_L2_VLAN)
723             ? VLAN_ETH_HEADER_LEN : ETH_HEADER_LEN;
724
725         /* Calculate the total matched octet count by considering as
726          * an approximation that all matched packets have the same
727          * length. */
728         layer2_octet_delta_count = packet_delta_count * ethernet_total_length;
729
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;
742     }
743
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);
748
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;
753     }
754
755     if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
756         struct ipfix_data_record_ip *data_ip;
757
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;
765
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;
773
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;
780         }
781     }
782
783     if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
784         struct ipfix_data_record_tcpudp *data_tcpudp;
785
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;
789     }
790
791     set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
792     set_hdr->length = htons(msg.size - set_hdr_offset);
793
794     ipfix_send_msg(exporter->collectors, &msg);
795
796     ofpbuf_uninit(&msg);
797 }
798
799 static void
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)
804 {
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;
809     }
810
811     ipfix_send_data_msg(exporter, packet, flow, packet_delta_count,
812                         obs_domain_id, obs_point_id);
813 }
814
815 void
816 dpif_ipfix_bridge_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
817                          const struct flow *flow)
818 {
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;
822
823     dpif_ipfix_sample(&di->bridge_exporter.exporter, packet, flow,
824                       packet_delta_count,
825                       di->bridge_exporter.options->obs_domain_id,
826                       di->bridge_exporter.options->obs_point_id);
827 }
828
829 void
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)
834 {
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;
839
840     node = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
841
842     if (!node) {
843         return;
844     }
845
846     dpif_ipfix_sample(&node->exporter.exporter, packet, flow,
847                       packet_delta_count, obs_domain_id, obs_point_id);
848 }