ofproto-dpif-ipfix: Reference count 'struct dpif_ipfix'.
[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     int ref_cnt;
66 };
67
68 #define IPFIX_VERSION 0x000a
69
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
74
75 /* Cf. IETF RFC 5101 Section 3.1. */
76 struct ipfix_header {
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);
84
85 #define IPFIX_SET_ID_TEMPLATE 2
86 #define IPFIX_SET_ID_OPTION_TEMPLATE 3
87
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);
94
95 /* Alternatives for templates at each layer.  A template is defined by
96  * a combination of one value for each layer. */
97 enum ipfix_proto_l2 {
98     IPFIX_PROTO_L2_ETH = 0,  /* No VLAN. */
99     IPFIX_PROTO_L2_VLAN,
100     NUM_IPFIX_PROTO_L2
101 };
102 enum ipfix_proto_l3 {
103     IPFIX_PROTO_L3_UNKNOWN = 0,
104     IPFIX_PROTO_L3_IPV4,
105     IPFIX_PROTO_L3_IPV6,
106     NUM_IPFIX_PROTO_L3
107 };
108 enum ipfix_proto_l4 {
109     IPFIX_PROTO_L4_UNKNOWN = 0,
110     IPFIX_PROTO_L4_TCP_UDP,
111     NUM_IPFIX_PROTO_L4
112 };
113
114 /* Any Template ID > 255 is usable for Template Records. */
115 #define IPFIX_TEMPLATE_ID_MIN 256
116
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);
123
124 enum ipfix_entity_id {
125 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME)  IPFIX_ENTITY_ID_##ENUM = ID,
126 #include "ofproto/ipfix-entities.def"
127 };
128
129 enum ipfix_entity_size {
130 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME)  IPFIX_ENTITY_SIZE_##ENUM = SIZE,
131 #include "ofproto/ipfix-entities.def"
132 };
133
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);
140
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);
153
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);
161
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);
172
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);
179
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);
187
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);
194
195 static bool
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)
199 {
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));
204 }
205
206 static struct ofproto_ipfix_bridge_exporter_options *
207 ofproto_ipfix_bridge_exporter_options_clone(
208     const struct ofproto_ipfix_bridge_exporter_options *old)
209 {
210     struct ofproto_ipfix_bridge_exporter_options *new =
211         xmemdup(old, sizeof *old);
212     sset_clone(&new->targets, &old->targets);
213     return new;
214 }
215
216 static void
217 ofproto_ipfix_bridge_exporter_options_destroy(
218     struct ofproto_ipfix_bridge_exporter_options *options)
219 {
220     if (options) {
221         sset_destroy(&options->targets);
222         free(options);
223     }
224 }
225
226 static bool
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)
230 {
231     return (a->collector_set_id == b->collector_set_id
232             && sset_equals(&a->targets, &b->targets));
233 }
234
235 static struct ofproto_ipfix_flow_exporter_options *
236 ofproto_ipfix_flow_exporter_options_clone(
237     const struct ofproto_ipfix_flow_exporter_options *old)
238 {
239     struct ofproto_ipfix_flow_exporter_options *new =
240         xmemdup(old, sizeof *old);
241     sset_clone(&new->targets, &old->targets);
242     return new;
243 }
244
245 static void
246 ofproto_ipfix_flow_exporter_options_destroy(
247     struct ofproto_ipfix_flow_exporter_options *options)
248 {
249     if (options) {
250         sset_destroy(&options->targets);
251         free(options);
252     }
253 }
254
255 static void
256 dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
257 {
258     collectors_destroy(exporter->collectors);
259     exporter->collectors = NULL;
260     exporter->seq_number = 1;
261     exporter->last_template_set_time = TIME_MIN;
262 }
263
264 static bool
265 dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter *exporter,
266                                 const struct sset *targets)
267 {
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);
275         return false;
276     }
277     return true;
278 }
279
280 static void
281 dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
282 {
283     dpif_ipfix_exporter_clear(&exporter->exporter);
284     ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
285     exporter->options = NULL;
286     exporter->probability = 0;
287 }
288
289 static void
290 dpif_ipfix_bridge_exporter_set_options(
291     struct dpif_ipfix_bridge_exporter *exporter,
292     const struct ofproto_ipfix_bridge_exporter_options *options)
293 {
294     bool options_changed;
295
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);
299         return;
300     }
301
302     options_changed = (
303         !exporter->options
304         || !ofproto_ipfix_bridge_exporter_options_equal(
305             options, exporter->options));
306
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
310      * retry). */
311     if (options_changed
312         || collectors_count(exporter->exporter.collectors)
313             < sset_count(&options->targets)) {
314         if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
315                                              &options->targets)) {
316             return;
317         }
318     }
319
320     /* Avoid reconfiguring if options didn't change. */
321     if (!options_changed) {
322         return;
323     }
324
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);
329 }
330
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)
334 {
335     struct dpif_ipfix_flow_exporter_map_node *exporter_node;
336
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;
343         }
344     }
345
346     return NULL;
347 }
348
349 static void
350 dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
351 {
352     dpif_ipfix_exporter_clear(&exporter->exporter);
353     ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
354     exporter->options = NULL;
355 }
356
357 static bool
358 dpif_ipfix_flow_exporter_set_options(
359     struct dpif_ipfix_flow_exporter *exporter,
360     const struct ofproto_ipfix_flow_exporter_options *options)
361 {
362     bool options_changed;
363
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);
367         return true;
368     }
369
370     options_changed = (
371         !exporter->options
372         || !ofproto_ipfix_flow_exporter_options_equal(
373             options, exporter->options));
374
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
378      * retry). */
379     if (options_changed
380         || collectors_count(exporter->exporter.collectors)
381             < sset_count(&options->targets)) {
382         if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
383                                              &options->targets)) {
384             return false;
385         }
386     }
387
388     /* Avoid reconfiguring if options didn't change. */
389     if (!options_changed) {
390         return true;
391     }
392
393     ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
394     exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
395
396     return true;
397 }
398
399 void
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)
405 {
406     int i;
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;
410
411     dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
412                                            bridge_exporter_options);
413
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);
420         if (!node) {
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));
425         }
426         if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
427             n_broken_flow_exporters_options++;
428         }
429         options++;
430     }
431
432     ovs_assert(hmap_count(&di->flow_exporter_map) >=
433                (n_flow_exporters_options - n_broken_flow_exporters_options));
434
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) {
445                   break;
446               }
447               options++;
448             }
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);
452                 free(node);
453             }
454         }
455     }
456
457     ovs_assert(hmap_count(&di->flow_exporter_map) ==
458                (n_flow_exporters_options - n_broken_flow_exporters_options));
459 }
460
461 struct dpif_ipfix *
462 dpif_ipfix_create(void)
463 {
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);
468     di->ref_cnt = 1;
469     return di;
470 }
471
472 struct dpif_ipfix *
473 dpif_ipfix_ref(const struct dpif_ipfix *di_)
474 {
475     struct dpif_ipfix *di = CONST_CAST(struct dpif_ipfix *, di_);
476     if (di) {
477         ovs_assert(di->ref_cnt > 0);
478         di->ref_cnt++;
479     }
480     return di;
481 }
482
483 uint32_t
484 dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
485 {
486     return di->bridge_exporter.probability;
487 }
488
489 static void
490 dpif_ipfix_clear(struct dpif_ipfix *di)
491 {
492     struct dpif_ipfix_flow_exporter_map_node *node, *next;
493
494     dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
495
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);
499         free(node);
500     }
501 }
502
503 void
504 dpif_ipfix_unref(struct dpif_ipfix *di)
505 {
506     if (!di) {
507         return;
508     }
509
510     ovs_assert(di->ref_cnt > 0);
511     if (!--di->ref_cnt) {
512         dpif_ipfix_clear(di);
513         hmap_destroy(&di->flow_exporter_map);
514         free(di);
515     }
516 }
517
518 static void
519 ipfix_init_header(uint32_t seq_number, uint32_t obs_domain_id,
520                   struct ofpbuf *msg)
521 {
522     struct ipfix_header *hdr;
523
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);
530 }
531
532 static void
533 ipfix_send_msg(const struct collectors *collectors, struct ofpbuf *msg)
534 {
535     struct ipfix_header *hdr;
536
537     /* Adjust the length in the header. */
538     hdr = msg->data;
539     hdr->length = htons(msg->size);
540
541     collectors_send(collectors, msg->data, msg->size);
542     msg->size = 0;
543 }
544
545 static uint16_t
546 ipfix_get_template_id(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
547                       enum ipfix_proto_l4 l4)
548 {
549     uint16_t template_id;
550     template_id = l2;
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;
554 }
555
556 static void
557 ipfix_define_template_entity(enum ipfix_entity_id id,
558                              enum ipfix_entity_size size, struct ofpbuf *msg)
559 {
560     struct ipfix_template_field_specifier *field;
561
562     field = ofpbuf_put_zeros(msg, sizeof *field);
563     field->element_id = htons(id);
564     field->field_length = htons(size);
565 }
566
567 static uint16_t
568 ipfix_define_template_fields(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
569                              enum ipfix_proto_l4 l4, struct ofpbuf *msg)
570 {
571     uint16_t count = 0;
572
573 #define DEF(ID) \
574     { \
575         ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
576                                      IPFIX_ENTITY_SIZE_##ID, msg); \
577         count++; \
578     }
579
580     DEF(OBSERVATION_POINT_ID);
581     DEF(PACKET_DELTA_COUNT);
582     DEF(LAYER2_OCTET_DELTA_COUNT);
583
584     /* Common Ethernet entities. */
585     DEF(SOURCE_MAC_ADDRESS);
586     DEF(DESTINATION_MAC_ADDRESS);
587     DEF(ETHERNET_TYPE);
588     DEF(ETHERNET_TOTAL_LENGTH);
589     DEF(ETHERNET_HEADER_LENGTH);
590
591     if (l2 == IPFIX_PROTO_L2_VLAN) {
592         DEF(VLAN_ID);
593         DEF(DOT1Q_VLAN_ID);
594         DEF(DOT1Q_PRIORITY);
595     }
596
597     if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
598         DEF(IP_VERSION);
599         DEF(IP_TTL);
600         DEF(PROTOCOL_IDENTIFIER);
601         DEF(IP_DIFF_SERV_CODE_POINT);
602         DEF(IP_PRECEDENCE);
603         DEF(IP_CLASS_OF_SERVICE);
604
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);
612         }
613     }
614
615     if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
616         DEF(SOURCE_TRANSPORT_PORT);
617         DEF(DESTINATION_TRANSPORT_PORT);
618     }
619
620 #undef DEF
621
622     return count;
623 }
624
625 static void
626 ipfix_send_template_msg(struct dpif_ipfix_exporter *exporter,
627                         uint32_t obs_domain_id)
628 {
629     uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
630     struct ofpbuf msg;
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;
638
639     ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
640
641     ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
642     set_hdr_offset = msg.size;
643
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);
647
648     /* Define one template for each possible combination of
649      * protocols. */
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) {
655                     continue;
656                 }
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);
665             }
666         }
667     }
668
669     set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
670     set_hdr->length = htons(msg.size - set_hdr_offset);
671
672     /* XXX: Add Options Template Sets, at least to define a Flow Keys
673      * Option Template. */
674
675     ipfix_send_msg(exporter->collectors, &msg);
676
677     ofpbuf_uninit(&msg);
678 }
679
680 static void
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)
684 {
685     uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
686     struct ofpbuf msg;
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;
692
693     ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
694
695     ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
696     exporter->seq_number++;
697     set_hdr_offset = msg.size;
698
699     /* Choose the right template ID matching the protocols in the
700      * sampled packet. */
701     l2 = (flow->vlan_tci == 0) ? IPFIX_PROTO_L2_ETH : IPFIX_PROTO_L2_VLAN;
702
703     switch(ntohs(flow->dl_type)) {
704     case ETH_TYPE_IP:
705         l3 = IPFIX_PROTO_L3_IPV4;
706         break;
707     case ETH_TYPE_IPV6:
708         l3 = IPFIX_PROTO_L3_IPV6;
709         break;
710     default:
711         l3 = IPFIX_PROTO_L3_UNKNOWN;
712     }
713
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;
720             break;
721         }
722     }
723
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));
727
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. */
731
732     /* Common Ethernet entities. */
733     {
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;
738
739         ethernet_total_length = packet->size;
740         ethernet_header_length = (l2 == IPFIX_PROTO_L2_VLAN)
741             ? VLAN_ETH_HEADER_LEN : ETH_HEADER_LEN;
742
743         /* Calculate the total matched octet count by considering as
744          * an approximation that all matched packets have the same
745          * length. */
746         layer2_octet_delta_count = packet_delta_count * ethernet_total_length;
747
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;
760     }
761
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);
766
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;
771     }
772
773     if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
774         struct ipfix_data_record_ip *data_ip;
775
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;
783
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;
791
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;
798         }
799     }
800
801     if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
802         struct ipfix_data_record_tcpudp *data_tcpudp;
803
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;
807     }
808
809     set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
810     set_hdr->length = htons(msg.size - set_hdr_offset);
811
812     ipfix_send_msg(exporter->collectors, &msg);
813
814     ofpbuf_uninit(&msg);
815 }
816
817 static void
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)
822 {
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;
827     }
828
829     ipfix_send_data_msg(exporter, packet, flow, packet_delta_count,
830                         obs_domain_id, obs_point_id);
831 }
832
833 void
834 dpif_ipfix_bridge_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
835                          const struct flow *flow)
836 {
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;
840
841     dpif_ipfix_sample(&di->bridge_exporter.exporter, packet, flow,
842                       packet_delta_count,
843                       di->bridge_exporter.options->obs_domain_id,
844                       di->bridge_exporter.options->obs_point_id);
845 }
846
847 void
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)
852 {
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;
857
858     node = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
859
860     if (!node) {
861         return;
862     }
863
864     dpif_ipfix_sample(&node->exporter.exporter, packet, flow,
865                       packet_delta_count, obs_domain_id, obs_point_id);
866 }