Merge remote-tracking branch 'ovs-dev/master'
[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 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
37
38 /* Cf. IETF RFC 5101 Section 10.3.4. */
39 #define IPFIX_DEFAULT_COLLECTOR_PORT 4739
40
41 struct dpif_ipfix_exporter {
42     struct collectors *collectors;
43     uint32_t seq_number;
44     time_t last_template_set_time;
45 };
46
47 struct dpif_ipfix_bridge_exporter {
48     struct dpif_ipfix_exporter exporter;
49     struct ofproto_ipfix_bridge_exporter_options *options;
50     uint32_t probability;
51 };
52
53 struct dpif_ipfix_flow_exporter {
54     struct dpif_ipfix_exporter exporter;
55     struct ofproto_ipfix_flow_exporter_options *options;
56 };
57
58 struct dpif_ipfix_flow_exporter_map_node {
59     struct hmap_node node;
60     struct dpif_ipfix_flow_exporter exporter;
61 };
62
63 struct dpif_ipfix {
64     struct dpif_ipfix_bridge_exporter bridge_exporter;
65     struct hmap flow_exporter_map;  /* dpif_ipfix_flow_exporter_map_nodes. */
66     atomic_int ref_cnt;
67 };
68
69 #define IPFIX_VERSION 0x000a
70
71 /* When using UDP, IPFIX Template Records must be re-sent regularly.
72  * The standard default interval is 10 minutes (600 seconds).
73  * Cf. IETF RFC 5101 Section 10.3.6. */
74 #define IPFIX_TEMPLATE_INTERVAL 600
75
76 /* Cf. IETF RFC 5101 Section 3.1. */
77 OVS_PACKED(
78 struct ipfix_header {
79     ovs_be16 version;  /* IPFIX_VERSION. */
80     ovs_be16 length;  /* Length in bytes including this header. */
81     ovs_be32 export_time;  /* Seconds since the epoch. */
82     ovs_be32 seq_number;  /* Message sequence number. */
83     ovs_be32 obs_domain_id;  /* Observation Domain ID. */
84 });
85 BUILD_ASSERT_DECL(sizeof(struct ipfix_header) == 16);
86
87 #define IPFIX_SET_ID_TEMPLATE 2
88 #define IPFIX_SET_ID_OPTION_TEMPLATE 3
89
90 /* Cf. IETF RFC 5101 Section 3.3.2. */
91 OVS_PACKED(
92 struct ipfix_set_header {
93     ovs_be16 set_id;  /* IPFIX_SET_ID_* or valid template ID for Data Sets. */
94     ovs_be16 length;  /* Length of the set in bytes including header. */
95 });
96 BUILD_ASSERT_DECL(sizeof(struct ipfix_set_header) == 4);
97
98 /* Alternatives for templates at each layer.  A template is defined by
99  * a combination of one value for each layer. */
100 enum ipfix_proto_l2 {
101     IPFIX_PROTO_L2_ETH = 0,  /* No VLAN. */
102     IPFIX_PROTO_L2_VLAN,
103     NUM_IPFIX_PROTO_L2
104 };
105 enum ipfix_proto_l3 {
106     IPFIX_PROTO_L3_UNKNOWN = 0,
107     IPFIX_PROTO_L3_IPV4,
108     IPFIX_PROTO_L3_IPV6,
109     NUM_IPFIX_PROTO_L3
110 };
111 enum ipfix_proto_l4 {
112     IPFIX_PROTO_L4_UNKNOWN = 0,
113     IPFIX_PROTO_L4_TCP_UDP,
114     NUM_IPFIX_PROTO_L4
115 };
116
117 /* Any Template ID > 255 is usable for Template Records. */
118 #define IPFIX_TEMPLATE_ID_MIN 256
119
120 /* Cf. IETF RFC 5101 Section 3.4.1. */
121 OVS_PACKED(
122 struct ipfix_template_record_header {
123     ovs_be16 template_id;
124     ovs_be16 field_count;
125 });
126 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_record_header) == 4);
127
128 enum ipfix_entity_id {
129 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME)  IPFIX_ENTITY_ID_##ENUM = ID,
130 #include "ofproto/ipfix-entities.def"
131 };
132
133 enum ipfix_entity_size {
134 #define IPFIX_ENTITY(ENUM, ID, SIZE, NAME)  IPFIX_ENTITY_SIZE_##ENUM = SIZE,
135 #include "ofproto/ipfix-entities.def"
136 };
137
138 OVS_PACKED(
139 struct ipfix_template_field_specifier {
140     ovs_be16 element_id;  /* IPFIX_ENTITY_ID_*. */
141     ovs_be16 field_length;  /* Length of the field's value, in bytes. */
142     /* No Enterprise ID, since only standard element IDs are specified. */
143 });
144 BUILD_ASSERT_DECL(sizeof(struct ipfix_template_field_specifier) == 4);
145
146 /* Part of data record for common metadata and Ethernet entities. */
147 OVS_PACKED(
148 struct ipfix_data_record_common {
149     ovs_be32 observation_point_id;  /* OBSERVATION_POINT_ID */
150     ovs_be64 packet_delta_count;  /* PACKET_DELTA_COUNT */
151     ovs_be64 layer2_octet_delta_count;  /* LAYER2_OCTET_DELTA_COUNT */
152     uint8_t source_mac_address[6];  /* SOURCE_MAC_ADDRESS */
153     uint8_t destination_mac_address[6];  /* DESTINATION_MAC_ADDRESS */
154     ovs_be16 ethernet_type;  /* ETHERNET_TYPE */
155     ovs_be16 ethernet_total_length;  /* ETHERNET_TOTAL_LENGTH */
156     uint8_t ethernet_header_length;  /* ETHERNET_HEADER_LENGTH */
157 });
158 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_common) == 37);
159
160 /* Part of data record for VLAN entities. */
161 OVS_PACKED(
162 struct ipfix_data_record_vlan {
163     ovs_be16 vlan_id;  /* VLAN_ID */
164     ovs_be16 dot1q_vlan_id;  /* DOT1Q_VLAN_ID */
165     uint8_t dot1q_priority;  /* DOT1Q_PRIORITY */
166 });
167 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_vlan) == 5);
168
169 /* Part of data record for IP entities. */
170 OVS_PACKED(
171 struct ipfix_data_record_ip {
172     uint8_t ip_version;  /* IP_VERSION */
173     uint8_t ip_ttl;  /* IP_TTL */
174     uint8_t protocol_identifier;  /* PROTOCOL_IDENTIFIER */
175     uint8_t ip_diff_serv_code_point;  /* IP_DIFF_SERV_CODE_POINT */
176     uint8_t ip_precedence;  /* IP_PRECEDENCE */
177     uint8_t ip_class_of_service;  /* IP_CLASS_OF_SERVICE */
178 });
179 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ip) == 6);
180
181 /* Part of data record for IPv4 entities. */
182 OVS_PACKED(
183 struct ipfix_data_record_ipv4 {
184     ovs_be32 source_ipv4_address;  /* SOURCE_IPV4_ADDRESS */
185     ovs_be32 destination_ipv4_address;  /* DESTINATION_IPV4_ADDRESS */
186 });
187 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ipv4) == 8);
188
189 /* Part of data record for IPv4 entities. */
190 OVS_PACKED(
191 struct ipfix_data_record_ipv6 {
192     uint8_t source_ipv6_address[16];  /* SOURCE_IPV6_ADDRESS */
193     uint8_t destination_ipv6_address[16];  /* DESTINATION_IPV6_ADDRESS */
194     ovs_be32 flow_label_ipv6;  /* FLOW_LABEL_IPV6 */
195 });
196 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_ipv6) == 36);
197
198 /* Part of data record for TCP/UDP entities. */
199 OVS_PACKED(
200 struct ipfix_data_record_tcpudp {
201     ovs_be16 source_transport_port;  /* SOURCE_TRANSPORT_PORT */
202     ovs_be16 destination_transport_port;  /* DESTINATION_TRANSPORT_PORT */
203 });
204 BUILD_ASSERT_DECL(sizeof(struct ipfix_data_record_tcpudp) == 4);
205
206 static bool
207 ofproto_ipfix_bridge_exporter_options_equal(
208     const struct ofproto_ipfix_bridge_exporter_options *a,
209     const struct ofproto_ipfix_bridge_exporter_options *b)
210 {
211     return (a->obs_domain_id == b->obs_domain_id
212             && a->obs_point_id == b->obs_point_id
213             && a->sampling_rate == b->sampling_rate
214             && sset_equals(&a->targets, &b->targets));
215 }
216
217 static struct ofproto_ipfix_bridge_exporter_options *
218 ofproto_ipfix_bridge_exporter_options_clone(
219     const struct ofproto_ipfix_bridge_exporter_options *old)
220 {
221     struct ofproto_ipfix_bridge_exporter_options *new =
222         xmemdup(old, sizeof *old);
223     sset_clone(&new->targets, &old->targets);
224     return new;
225 }
226
227 static void
228 ofproto_ipfix_bridge_exporter_options_destroy(
229     struct ofproto_ipfix_bridge_exporter_options *options)
230 {
231     if (options) {
232         sset_destroy(&options->targets);
233         free(options);
234     }
235 }
236
237 static bool
238 ofproto_ipfix_flow_exporter_options_equal(
239     const struct ofproto_ipfix_flow_exporter_options *a,
240     const struct ofproto_ipfix_flow_exporter_options *b)
241 {
242     return (a->collector_set_id == b->collector_set_id
243             && sset_equals(&a->targets, &b->targets));
244 }
245
246 static struct ofproto_ipfix_flow_exporter_options *
247 ofproto_ipfix_flow_exporter_options_clone(
248     const struct ofproto_ipfix_flow_exporter_options *old)
249 {
250     struct ofproto_ipfix_flow_exporter_options *new =
251         xmemdup(old, sizeof *old);
252     sset_clone(&new->targets, &old->targets);
253     return new;
254 }
255
256 static void
257 ofproto_ipfix_flow_exporter_options_destroy(
258     struct ofproto_ipfix_flow_exporter_options *options)
259 {
260     if (options) {
261         sset_destroy(&options->targets);
262         free(options);
263     }
264 }
265
266 static void
267 dpif_ipfix_exporter_clear(struct dpif_ipfix_exporter *exporter)
268 {
269     collectors_destroy(exporter->collectors);
270     exporter->collectors = NULL;
271     exporter->seq_number = 1;
272     exporter->last_template_set_time = TIME_MIN;
273 }
274
275 static bool
276 dpif_ipfix_exporter_set_options(struct dpif_ipfix_exporter *exporter,
277                                 const struct sset *targets)
278 {
279     collectors_destroy(exporter->collectors);
280     collectors_create(targets, IPFIX_DEFAULT_COLLECTOR_PORT,
281                       &exporter->collectors);
282     if (exporter->collectors == NULL) {
283         VLOG_WARN_RL(&rl, "no collectors could be initialized, "
284                      "IPFIX exporter disabled");
285         dpif_ipfix_exporter_clear(exporter);
286         return false;
287     }
288     return true;
289 }
290
291 static void
292 dpif_ipfix_bridge_exporter_clear(struct dpif_ipfix_bridge_exporter *exporter)
293 {
294     dpif_ipfix_exporter_clear(&exporter->exporter);
295     ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
296     exporter->options = NULL;
297     exporter->probability = 0;
298 }
299
300 static void
301 dpif_ipfix_bridge_exporter_set_options(
302     struct dpif_ipfix_bridge_exporter *exporter,
303     const struct ofproto_ipfix_bridge_exporter_options *options)
304 {
305     bool options_changed;
306
307     if (!options || sset_is_empty(&options->targets)) {
308         /* No point in doing any work if there are no targets. */
309         dpif_ipfix_bridge_exporter_clear(exporter);
310         return;
311     }
312
313     options_changed = (
314         !exporter->options
315         || !ofproto_ipfix_bridge_exporter_options_equal(
316             options, exporter->options));
317
318     /* Configure collectors if options have changed or if we're
319      * shortchanged in collectors (which indicates that opening one or
320      * more of the configured collectors failed, so that we should
321      * retry). */
322     if (options_changed
323         || collectors_count(exporter->exporter.collectors)
324             < sset_count(&options->targets)) {
325         if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
326                                              &options->targets)) {
327             return;
328         }
329     }
330
331     /* Avoid reconfiguring if options didn't change. */
332     if (!options_changed) {
333         return;
334     }
335
336     ofproto_ipfix_bridge_exporter_options_destroy(exporter->options);
337     exporter->options = ofproto_ipfix_bridge_exporter_options_clone(options);
338     exporter->probability =
339         MAX(1, UINT32_MAX / exporter->options->sampling_rate);
340 }
341
342 static struct dpif_ipfix_flow_exporter_map_node*
343 dpif_ipfix_find_flow_exporter_map_node(
344     const struct dpif_ipfix *di, const uint32_t collector_set_id)
345 {
346     struct dpif_ipfix_flow_exporter_map_node *exporter_node;
347
348     HMAP_FOR_EACH_WITH_HASH (exporter_node, node,
349                              hash_int(collector_set_id, 0),
350                              &di->flow_exporter_map) {
351         if (exporter_node->exporter.options->collector_set_id
352             == collector_set_id) {
353             return exporter_node;
354         }
355     }
356
357     return NULL;
358 }
359
360 static void
361 dpif_ipfix_flow_exporter_clear(struct dpif_ipfix_flow_exporter *exporter)
362 {
363     dpif_ipfix_exporter_clear(&exporter->exporter);
364     ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
365     exporter->options = NULL;
366 }
367
368 static bool
369 dpif_ipfix_flow_exporter_set_options(
370     struct dpif_ipfix_flow_exporter *exporter,
371     const struct ofproto_ipfix_flow_exporter_options *options)
372 {
373     bool options_changed;
374
375     if (sset_is_empty(&options->targets)) {
376         /* No point in doing any work if there are no targets. */
377         dpif_ipfix_flow_exporter_clear(exporter);
378         return true;
379     }
380
381     options_changed = (
382         !exporter->options
383         || !ofproto_ipfix_flow_exporter_options_equal(
384             options, exporter->options));
385
386     /* Configure collectors if options have changed or if we're
387      * shortchanged in collectors (which indicates that opening one or
388      * more of the configured collectors failed, so that we should
389      * retry). */
390     if (options_changed
391         || collectors_count(exporter->exporter.collectors)
392             < sset_count(&options->targets)) {
393         if (!dpif_ipfix_exporter_set_options(&exporter->exporter,
394                                              &options->targets)) {
395             return false;
396         }
397     }
398
399     /* Avoid reconfiguring if options didn't change. */
400     if (!options_changed) {
401         return true;
402     }
403
404     ofproto_ipfix_flow_exporter_options_destroy(exporter->options);
405     exporter->options = ofproto_ipfix_flow_exporter_options_clone(options);
406
407     return true;
408 }
409
410 void
411 dpif_ipfix_set_options(
412     struct dpif_ipfix *di,
413     const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
414     const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
415     size_t n_flow_exporters_options) OVS_EXCLUDED(mutex)
416 {
417     int i;
418     struct ofproto_ipfix_flow_exporter_options *options;
419     struct dpif_ipfix_flow_exporter_map_node *node, *next;
420     size_t n_broken_flow_exporters_options = 0;
421
422     ovs_mutex_lock(&mutex);
423     dpif_ipfix_bridge_exporter_set_options(&di->bridge_exporter,
424                                            bridge_exporter_options);
425
426     /* Add new flow exporters and update current flow exporters. */
427     options = (struct ofproto_ipfix_flow_exporter_options *)
428         flow_exporters_options;
429     for (i = 0; i < n_flow_exporters_options; i++) {
430         node = dpif_ipfix_find_flow_exporter_map_node(
431             di, options->collector_set_id);
432         if (!node) {
433             node = xzalloc(sizeof *node);
434             dpif_ipfix_exporter_clear(&node->exporter.exporter);
435             hmap_insert(&di->flow_exporter_map, &node->node,
436                         hash_int(options->collector_set_id, 0));
437         }
438         if (!dpif_ipfix_flow_exporter_set_options(&node->exporter, options)) {
439             n_broken_flow_exporters_options++;
440         }
441         options++;
442     }
443
444     ovs_assert(hmap_count(&di->flow_exporter_map) >=
445                (n_flow_exporters_options - n_broken_flow_exporters_options));
446
447     /* Remove dropped flow exporters, if any needs to be removed. */
448     if (hmap_count(&di->flow_exporter_map) > n_flow_exporters_options) {
449         HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
450             /* This is slow but doesn't take any extra memory, and
451              * this table is not supposed to contain many rows anyway. */
452             options = (struct ofproto_ipfix_flow_exporter_options *)
453                 flow_exporters_options;
454             for (i = 0; i < n_flow_exporters_options; i++) {
455               if (node->exporter.options->collector_set_id
456                   == options->collector_set_id) {
457                   break;
458               }
459               options++;
460             }
461             if (i == n_flow_exporters_options) {  // Not found.
462                 hmap_remove(&di->flow_exporter_map, &node->node);
463                 dpif_ipfix_flow_exporter_clear(&node->exporter);
464                 free(node);
465             }
466         }
467     }
468
469     ovs_assert(hmap_count(&di->flow_exporter_map) ==
470                (n_flow_exporters_options - n_broken_flow_exporters_options));
471     ovs_mutex_unlock(&mutex);
472 }
473
474 struct dpif_ipfix *
475 dpif_ipfix_create(void)
476 {
477     struct dpif_ipfix *di;
478     di = xzalloc(sizeof *di);
479     dpif_ipfix_exporter_clear(&di->bridge_exporter.exporter);
480     hmap_init(&di->flow_exporter_map);
481     atomic_init(&di->ref_cnt, 1);
482     return di;
483 }
484
485 struct dpif_ipfix *
486 dpif_ipfix_ref(const struct dpif_ipfix *di_)
487 {
488     struct dpif_ipfix *di = CONST_CAST(struct dpif_ipfix *, di_);
489     if (di) {
490         int orig;
491         atomic_add(&di->ref_cnt, 1, &orig);
492         ovs_assert(orig > 0);
493     }
494     return di;
495 }
496
497 uint32_t
498 dpif_ipfix_get_bridge_exporter_probability(const struct dpif_ipfix *di)
499     OVS_EXCLUDED(mutex)
500 {
501     uint32_t ret;
502     ovs_mutex_lock(&mutex);
503     ret = di->bridge_exporter.probability;
504     ovs_mutex_unlock(&mutex);
505     return ret;
506 }
507
508 static void
509 dpif_ipfix_clear(struct dpif_ipfix *di) OVS_REQ_WRLOCK(mutex)
510 {
511     struct dpif_ipfix_flow_exporter_map_node *node, *next;
512
513     dpif_ipfix_bridge_exporter_clear(&di->bridge_exporter);
514
515     HMAP_FOR_EACH_SAFE (node, next, node, &di->flow_exporter_map) {
516         hmap_remove(&di->flow_exporter_map, &node->node);
517         dpif_ipfix_flow_exporter_clear(&node->exporter);
518         free(node);
519     }
520 }
521
522 void
523 dpif_ipfix_unref(struct dpif_ipfix *di) OVS_EXCLUDED(mutex)
524 {
525     int orig;
526
527     if (!di) {
528         return;
529     }
530
531     atomic_sub(&di->ref_cnt, 1, &orig);
532     ovs_assert(orig > 0);
533     if (orig == 1) {
534         ovs_mutex_lock(&mutex);
535         dpif_ipfix_clear(di);
536         hmap_destroy(&di->flow_exporter_map);
537         free(di);
538         ovs_mutex_unlock(&mutex);
539     }
540 }
541
542 static void
543 ipfix_init_header(uint32_t seq_number, uint32_t obs_domain_id,
544                   struct ofpbuf *msg)
545 {
546     struct ipfix_header *hdr;
547
548     hdr = ofpbuf_put_zeros(msg, sizeof *hdr);
549     hdr->version = htons(IPFIX_VERSION);
550     hdr->length = htons(sizeof *hdr);  /* Updated in ipfix_send_msg. */
551     hdr->export_time = htonl(time_wall());
552     hdr->seq_number = htonl(seq_number);
553     hdr->obs_domain_id = htonl(obs_domain_id);
554 }
555
556 static void
557 ipfix_send_msg(const struct collectors *collectors, struct ofpbuf *msg)
558 {
559     struct ipfix_header *hdr;
560
561     /* Adjust the length in the header. */
562     hdr = msg->data;
563     hdr->length = htons(msg->size);
564
565     collectors_send(collectors, msg->data, msg->size);
566     msg->size = 0;
567 }
568
569 static uint16_t
570 ipfix_get_template_id(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
571                       enum ipfix_proto_l4 l4)
572 {
573     uint16_t template_id;
574     template_id = l2;
575     template_id = template_id * NUM_IPFIX_PROTO_L3 + l3;
576     template_id = template_id * NUM_IPFIX_PROTO_L4 + l4;
577     return IPFIX_TEMPLATE_ID_MIN + template_id;
578 }
579
580 static void
581 ipfix_define_template_entity(enum ipfix_entity_id id,
582                              enum ipfix_entity_size size, struct ofpbuf *msg)
583 {
584     struct ipfix_template_field_specifier *field;
585
586     field = ofpbuf_put_zeros(msg, sizeof *field);
587     field->element_id = htons(id);
588     field->field_length = htons(size);
589 }
590
591 static uint16_t
592 ipfix_define_template_fields(enum ipfix_proto_l2 l2, enum ipfix_proto_l3 l3,
593                              enum ipfix_proto_l4 l4, struct ofpbuf *msg)
594 {
595     uint16_t count = 0;
596
597 #define DEF(ID) \
598     { \
599         ipfix_define_template_entity(IPFIX_ENTITY_ID_##ID, \
600                                      IPFIX_ENTITY_SIZE_##ID, msg); \
601         count++; \
602     }
603
604     DEF(OBSERVATION_POINT_ID);
605     DEF(PACKET_DELTA_COUNT);
606     DEF(LAYER2_OCTET_DELTA_COUNT);
607
608     /* Common Ethernet entities. */
609     DEF(SOURCE_MAC_ADDRESS);
610     DEF(DESTINATION_MAC_ADDRESS);
611     DEF(ETHERNET_TYPE);
612     DEF(ETHERNET_TOTAL_LENGTH);
613     DEF(ETHERNET_HEADER_LENGTH);
614
615     if (l2 == IPFIX_PROTO_L2_VLAN) {
616         DEF(VLAN_ID);
617         DEF(DOT1Q_VLAN_ID);
618         DEF(DOT1Q_PRIORITY);
619     }
620
621     if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
622         DEF(IP_VERSION);
623         DEF(IP_TTL);
624         DEF(PROTOCOL_IDENTIFIER);
625         DEF(IP_DIFF_SERV_CODE_POINT);
626         DEF(IP_PRECEDENCE);
627         DEF(IP_CLASS_OF_SERVICE);
628
629         if (l3 == IPFIX_PROTO_L3_IPV4) {
630             DEF(SOURCE_IPV4_ADDRESS);
631             DEF(DESTINATION_IPV4_ADDRESS);
632         } else {  /* l3 == IPFIX_PROTO_L3_IPV6 */
633             DEF(SOURCE_IPV6_ADDRESS);
634             DEF(DESTINATION_IPV6_ADDRESS);
635             DEF(FLOW_LABEL_IPV6);
636         }
637     }
638
639     if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
640         DEF(SOURCE_TRANSPORT_PORT);
641         DEF(DESTINATION_TRANSPORT_PORT);
642     }
643
644 #undef DEF
645
646     return count;
647 }
648
649 static void
650 ipfix_send_template_msg(struct dpif_ipfix_exporter *exporter,
651                         uint32_t obs_domain_id)
652 {
653     uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
654     struct ofpbuf msg;
655     size_t set_hdr_offset, tmpl_hdr_offset;
656     struct ipfix_set_header *set_hdr;
657     struct ipfix_template_record_header *tmpl_hdr;
658     uint16_t field_count;
659     enum ipfix_proto_l2 l2;
660     enum ipfix_proto_l3 l3;
661     enum ipfix_proto_l4 l4;
662
663     ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
664
665     ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
666     set_hdr_offset = msg.size;
667
668     /* Add a Template Set. */
669     set_hdr = ofpbuf_put_zeros(&msg, sizeof *set_hdr);
670     set_hdr->set_id = htons(IPFIX_SET_ID_TEMPLATE);
671
672     /* Define one template for each possible combination of
673      * protocols. */
674     for (l2 = 0; l2 < NUM_IPFIX_PROTO_L2; l2++) {
675         for (l3 = 0; l3 < NUM_IPFIX_PROTO_L3; l3++) {
676             for (l4 = 0; l4 < NUM_IPFIX_PROTO_L4; l4++) {
677                 if (l3 == IPFIX_PROTO_L3_UNKNOWN &&
678                     l4 != IPFIX_PROTO_L4_UNKNOWN) {
679                     continue;
680                 }
681                 tmpl_hdr_offset = msg.size;
682                 tmpl_hdr = ofpbuf_put_zeros(&msg, sizeof *tmpl_hdr);
683                 tmpl_hdr->template_id = htons(
684                     ipfix_get_template_id(l2, l3, l4));
685                 field_count = ipfix_define_template_fields(l2, l3, l4, &msg);
686                 tmpl_hdr = (struct ipfix_template_record_header*)
687                     ((uint8_t*)msg.data + tmpl_hdr_offset);
688                 tmpl_hdr->field_count = htons(field_count);
689             }
690         }
691     }
692
693     set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
694     set_hdr->length = htons(msg.size - set_hdr_offset);
695
696     /* XXX: Add Options Template Sets, at least to define a Flow Keys
697      * Option Template. */
698
699     ipfix_send_msg(exporter->collectors, &msg);
700
701     ofpbuf_uninit(&msg);
702 }
703
704 static void
705 ipfix_send_data_msg(struct dpif_ipfix_exporter *exporter, struct ofpbuf *packet,
706                     const struct flow *flow, uint64_t packet_delta_count,
707                     uint32_t obs_domain_id, uint32_t obs_point_id)
708 {
709     uint64_t msg_stub[DIV_ROUND_UP(1500, 8)];
710     struct ofpbuf msg;
711     size_t set_hdr_offset;
712     struct ipfix_set_header *set_hdr;
713     enum ipfix_proto_l2 l2;
714     enum ipfix_proto_l3 l3;
715     enum ipfix_proto_l4 l4;
716
717     ofpbuf_use_stub(&msg, msg_stub, sizeof msg_stub);
718
719     ipfix_init_header(exporter->seq_number, obs_domain_id, &msg);
720     exporter->seq_number++;
721     set_hdr_offset = msg.size;
722
723     /* Choose the right template ID matching the protocols in the
724      * sampled packet. */
725     l2 = (flow->vlan_tci == 0) ? IPFIX_PROTO_L2_ETH : IPFIX_PROTO_L2_VLAN;
726
727     switch(ntohs(flow->dl_type)) {
728     case ETH_TYPE_IP:
729         l3 = IPFIX_PROTO_L3_IPV4;
730         break;
731     case ETH_TYPE_IPV6:
732         l3 = IPFIX_PROTO_L3_IPV6;
733         break;
734     default:
735         l3 = IPFIX_PROTO_L3_UNKNOWN;
736     }
737
738     l4 = IPFIX_PROTO_L4_UNKNOWN;
739     if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
740         switch(flow->nw_proto) {
741         case IPPROTO_TCP:  /* TCP */
742         case IPPROTO_UDP:  /* UDP */
743             l4 = IPFIX_PROTO_L4_TCP_UDP;
744             break;
745         }
746     }
747
748     /* Add a Data Set. */
749     set_hdr = ofpbuf_put_zeros(&msg, sizeof *set_hdr);
750     set_hdr->set_id = htons(ipfix_get_template_id(l2, l3, l4));
751
752     /* The fields defined in the ipfix_data_record_* structs and sent
753      * below must match exactly the templates defined in
754      * ipfix_define_template_fields. */
755
756     /* Common Ethernet entities. */
757     {
758         struct ipfix_data_record_common *data_common;
759         uint16_t ethernet_total_length;
760         uint8_t ethernet_header_length;
761         uint64_t layer2_octet_delta_count;
762
763         ethernet_total_length = packet->size;
764         ethernet_header_length = (l2 == IPFIX_PROTO_L2_VLAN)
765             ? VLAN_ETH_HEADER_LEN : ETH_HEADER_LEN;
766
767         /* Calculate the total matched octet count by considering as
768          * an approximation that all matched packets have the same
769          * length. */
770         layer2_octet_delta_count = packet_delta_count * ethernet_total_length;
771
772         data_common = ofpbuf_put_zeros(&msg, sizeof *data_common);
773         data_common->observation_point_id = htonl(obs_point_id);
774         data_common->packet_delta_count = htonll(packet_delta_count);
775         data_common->layer2_octet_delta_count =
776             htonll(layer2_octet_delta_count);
777         memcpy(data_common->source_mac_address, flow->dl_src,
778                sizeof flow->dl_src);
779         memcpy(data_common->destination_mac_address, flow->dl_dst,
780                sizeof flow->dl_dst);
781         data_common->ethernet_type = flow->dl_type;
782         data_common->ethernet_total_length = htons(ethernet_total_length);
783         data_common->ethernet_header_length = ethernet_header_length;
784     }
785
786     if (l2 == IPFIX_PROTO_L2_VLAN) {
787         struct ipfix_data_record_vlan *data_vlan;
788         uint16_t vlan_id = vlan_tci_to_vid(flow->vlan_tci);
789         uint8_t priority = vlan_tci_to_pcp(flow->vlan_tci);
790
791         data_vlan = ofpbuf_put_zeros(&msg, sizeof *data_vlan);
792         data_vlan->vlan_id = htons(vlan_id);
793         data_vlan->dot1q_vlan_id = htons(vlan_id);
794         data_vlan->dot1q_priority = priority;
795     }
796
797     if (l3 != IPFIX_PROTO_L3_UNKNOWN) {
798         struct ipfix_data_record_ip *data_ip;
799
800         data_ip = ofpbuf_put_zeros(&msg, sizeof *data_ip);
801         data_ip->ip_version = (l3 == IPFIX_PROTO_L3_IPV4) ? 4 : 6;
802         data_ip->ip_ttl = flow->nw_ttl;
803         data_ip->protocol_identifier = flow->nw_proto;
804         data_ip->ip_diff_serv_code_point = flow->nw_tos >> 2;
805         data_ip->ip_precedence = flow->nw_tos >> 5;
806         data_ip->ip_class_of_service = flow->nw_tos;
807
808         if (l3 == IPFIX_PROTO_L3_IPV4) {
809             struct ipfix_data_record_ipv4 *data_ipv4;
810             data_ipv4 = ofpbuf_put_zeros(&msg, sizeof *data_ipv4);
811             data_ipv4->source_ipv4_address = flow->nw_src;
812             data_ipv4->destination_ipv4_address = flow->nw_dst;
813         } else {  /* l3 == IPFIX_PROTO_L3_IPV6 */
814             struct ipfix_data_record_ipv6 *data_ipv6;
815
816             data_ipv6 = ofpbuf_put_zeros(&msg, sizeof *data_ipv6);
817             memcpy(data_ipv6->source_ipv6_address, &flow->ipv6_src,
818                    sizeof flow->ipv6_src);
819             memcpy(data_ipv6->destination_ipv6_address, &flow->ipv6_dst,
820                    sizeof flow->ipv6_dst);
821             data_ipv6->flow_label_ipv6 = flow->ipv6_label;
822         }
823     }
824
825     if (l4 != IPFIX_PROTO_L4_UNKNOWN) {
826         struct ipfix_data_record_tcpudp *data_tcpudp;
827
828         data_tcpudp = ofpbuf_put_zeros(&msg, sizeof *data_tcpudp);
829         data_tcpudp->source_transport_port = flow->tp_src;
830         data_tcpudp->destination_transport_port = flow->tp_dst;
831     }
832
833     set_hdr = (struct ipfix_set_header*)((uint8_t*)msg.data + set_hdr_offset);
834     set_hdr->length = htons(msg.size - set_hdr_offset);
835
836     ipfix_send_msg(exporter->collectors, &msg);
837
838     ofpbuf_uninit(&msg);
839 }
840
841 static void
842 dpif_ipfix_sample(struct dpif_ipfix_exporter *exporter,
843                   struct ofpbuf *packet, const struct flow *flow,
844                   uint64_t packet_delta_count, uint32_t obs_domain_id,
845                   uint32_t obs_point_id)
846 {
847     time_t now = time_wall();
848     if ((exporter->last_template_set_time + IPFIX_TEMPLATE_INTERVAL) <= now) {
849         ipfix_send_template_msg(exporter, obs_domain_id);
850         exporter->last_template_set_time = now;
851     }
852
853     ipfix_send_data_msg(exporter, packet, flow, packet_delta_count,
854                         obs_domain_id, obs_point_id);
855 }
856
857 void
858 dpif_ipfix_bridge_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
859                          const struct flow *flow) OVS_EXCLUDED(mutex)
860 {
861     uint64_t packet_delta_count;
862
863     ovs_mutex_lock(&mutex);
864     /* Use the sampling probability as an approximation of the number
865      * of matched packets. */
866     packet_delta_count = UINT32_MAX / di->bridge_exporter.probability;
867     dpif_ipfix_sample(&di->bridge_exporter.exporter, packet, flow,
868                       packet_delta_count,
869                       di->bridge_exporter.options->obs_domain_id,
870                       di->bridge_exporter.options->obs_point_id);
871     ovs_mutex_unlock(&mutex);
872 }
873
874 void
875 dpif_ipfix_flow_sample(struct dpif_ipfix *di, struct ofpbuf *packet,
876                        const struct flow *flow, uint32_t collector_set_id,
877                        uint16_t probability, uint32_t obs_domain_id,
878                        uint32_t obs_point_id) OVS_EXCLUDED(mutex)
879 {
880     struct dpif_ipfix_flow_exporter_map_node *node;
881     /* Use the sampling probability as an approximation of the number
882      * of matched packets. */
883     uint64_t packet_delta_count = USHRT_MAX / probability;
884
885     ovs_mutex_lock(&mutex);
886     node = dpif_ipfix_find_flow_exporter_map_node(di, collector_set_id);
887     if (node) {
888         dpif_ipfix_sample(&node->exporter.exporter, packet, flow,
889                           packet_delta_count, obs_domain_id, obs_point_id);
890     }
891     ovs_mutex_unlock(&mutex);
892 }