2e32f219cb5c10848dba79b9333e656381a562b3
[sliver-openvswitch.git] / ofproto / netflow.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 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 "netflow.h"
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include "byte-order.h"
24 #include "collectors.h"
25 #include "flow.h"
26 #include "lib/netflow.h"
27 #include "ofpbuf.h"
28 #include "ofproto.h"
29 #include "ofproto/netflow.h"
30 #include "packets.h"
31 #include "poll-loop.h"
32 #include "socket-util.h"
33 #include "timeval.h"
34 #include "util.h"
35 #include "vlog.h"
36
37 VLOG_DEFINE_THIS_MODULE(netflow);
38
39 struct netflow {
40     uint8_t engine_type;          /* Value of engine_type to use. */
41     uint8_t engine_id;            /* Value of engine_id to use. */
42     long long int boot_time;      /* Time when netflow_create() was called. */
43     struct collectors *collectors; /* NetFlow collectors. */
44     bool add_id_to_iface;         /* Put the 7 least significiant bits of
45                                    * 'engine_id' into the most significant
46                                    * bits of the interface fields. */
47     uint32_t netflow_cnt;         /* Flow sequence number for NetFlow. */
48     struct ofpbuf packet;         /* NetFlow packet being accumulated. */
49     long long int active_timeout; /* Timeout for flows that are still active. */
50     long long int next_timeout;   /* Next scheduled active timeout. */
51     long long int reconfig_time;  /* When we reconfigured the timeouts. */
52 };
53
54 void
55 netflow_mask_wc(struct flow_wildcards *wc)
56 {
57     memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
58     memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
59     memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
60     memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
61     memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
62     memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
63     wc->masks.nw_tos |= IP_DSCP_MASK;
64 }
65
66 static void
67 gen_netflow_rec(struct netflow *nf, struct netflow_flow *nf_flow,
68                 struct ofexpired *expired,
69                 uint32_t packet_count, uint32_t byte_count)
70 {
71     struct netflow_v5_header *nf_hdr;
72     struct netflow_v5_record *nf_rec;
73
74     if (!nf->packet.size) {
75         struct timespec now;
76
77         time_wall_timespec(&now);
78
79         nf_hdr = ofpbuf_put_zeros(&nf->packet, sizeof *nf_hdr);
80         nf_hdr->version = htons(NETFLOW_V5_VERSION);
81         nf_hdr->count = htons(0);
82         nf_hdr->sysuptime = htonl(time_msec() - nf->boot_time);
83         nf_hdr->unix_secs = htonl(now.tv_sec);
84         nf_hdr->unix_nsecs = htonl(now.tv_nsec);
85         nf_hdr->flow_seq = htonl(nf->netflow_cnt++);
86         nf_hdr->engine_type = nf->engine_type;
87         nf_hdr->engine_id = nf->engine_id;
88         nf_hdr->sampling_interval = htons(0);
89     }
90
91     nf_hdr = nf->packet.data;
92     nf_hdr->count = htons(ntohs(nf_hdr->count) + 1);
93
94     nf_rec = ofpbuf_put_zeros(&nf->packet, sizeof *nf_rec);
95     nf_rec->src_addr = expired->flow.nw_src;
96     nf_rec->dst_addr = expired->flow.nw_dst;
97     nf_rec->nexthop = htonl(0);
98     if (nf->add_id_to_iface) {
99         uint16_t iface = (nf->engine_id & 0x7f) << 9;
100         nf_rec->input = htons(iface
101             | (ofp_to_u16(expired->flow.in_port.ofp_port) & 0x1ff));
102         nf_rec->output = htons(iface
103             | (ofp_to_u16(nf_flow->output_iface) & 0x1ff));
104     } else {
105         nf_rec->input = htons(ofp_to_u16(expired->flow.in_port.ofp_port));
106         nf_rec->output = htons(ofp_to_u16(nf_flow->output_iface));
107     }
108     nf_rec->packet_count = htonl(packet_count);
109     nf_rec->byte_count = htonl(byte_count);
110     nf_rec->init_time = htonl(nf_flow->created - nf->boot_time);
111     nf_rec->used_time = htonl(MAX(nf_flow->created, expired->used)
112                              - nf->boot_time);
113     if (expired->flow.nw_proto == IPPROTO_ICMP) {
114         /* In NetFlow, the ICMP type and code are concatenated and
115          * placed in the 'dst_port' field. */
116         uint8_t type = ntohs(expired->flow.tp_src);
117         uint8_t code = ntohs(expired->flow.tp_dst);
118         nf_rec->src_port = htons(0);
119         nf_rec->dst_port = htons((type << 8) | code);
120     } else {
121         nf_rec->src_port = expired->flow.tp_src;
122         nf_rec->dst_port = expired->flow.tp_dst;
123     }
124     nf_rec->tcp_flags = nf_flow->tcp_flags;
125     nf_rec->ip_proto = expired->flow.nw_proto;
126     nf_rec->ip_tos = expired->flow.nw_tos & IP_DSCP_MASK;
127
128     /* NetFlow messages are limited to 30 records. */
129     if (ntohs(nf_hdr->count) >= 30) {
130         netflow_run(nf);
131     }
132 }
133
134 void
135 netflow_expire(struct netflow *nf, struct netflow_flow *nf_flow,
136                struct ofexpired *expired)
137 {
138     uint64_t pkt_delta = expired->packet_count - nf_flow->packet_count_off;
139     uint64_t byte_delta = expired->byte_count - nf_flow->byte_count_off;
140
141     nf_flow->last_expired += nf->active_timeout;
142
143     /* NetFlow only reports on IP packets and we should only report flows
144      * that actually have traffic. */
145     if (expired->flow.dl_type != htons(ETH_TYPE_IP) || pkt_delta == 0) {
146         return;
147     }
148
149     if ((byte_delta >> 32) <= 175) {
150         /* NetFlow v5 records are limited to 32-bit counters.  If we've wrapped
151          * a counter, send as multiple records so we don't lose track of any
152          * traffic.  We try to evenly distribute the packet and byte counters,
153          * so that the bytes-per-packet lengths don't look wonky across the
154          * records. */
155         while (byte_delta) {
156             int n_recs = (byte_delta + UINT32_MAX - 1) / UINT32_MAX;
157             uint32_t pkt_count = pkt_delta / n_recs;
158             uint32_t byte_count = byte_delta / n_recs;
159
160             gen_netflow_rec(nf, nf_flow, expired, pkt_count, byte_count);
161
162             pkt_delta -= pkt_count;
163             byte_delta -= byte_count;
164         }
165     } else {
166         /* In 600 seconds, a 10GbE link can theoretically transmit 75 * 10**10
167          * == 175 * 2**32 bytes.  The byte counter is bigger than that, so it's
168          * probably a bug--for example, the netdev code uses UINT64_MAX to
169          * report "unknown value", and perhaps that has leaked through to here.
170          *
171          * We wouldn't want to hit the loop above in this case, because it
172          * would try to send up to UINT32_MAX netflow records, which would take
173          * a long time.
174          */
175         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
176
177         VLOG_WARN_RL(&rl, "impossible byte counter %"PRIu64, byte_delta);
178     }
179
180     /* Update flow tracking data. */
181     nf_flow->created = 0;
182     nf_flow->packet_count_off = expired->packet_count;
183     nf_flow->byte_count_off = expired->byte_count;
184     nf_flow->tcp_flags = 0;
185 }
186
187 /* Returns true if it's time to send out a round of NetFlow active timeouts,
188  * false otherwise. */
189 bool
190 netflow_run(struct netflow *nf)
191 {
192     if (nf->packet.size) {
193         collectors_send(nf->collectors, nf->packet.data, nf->packet.size);
194         nf->packet.size = 0;
195     }
196
197     if (nf->active_timeout && time_msec() >= nf->next_timeout) {
198         nf->next_timeout = time_msec() + 1000;
199         return true;
200     } else {
201         return false;
202     }
203 }
204
205 void
206 netflow_wait(struct netflow *nf)
207 {
208     if (nf->active_timeout) {
209         poll_timer_wait_until(nf->next_timeout);
210     }
211     if (nf->packet.size) {
212         poll_immediate_wake();
213     }
214 }
215
216 int
217 netflow_set_options(struct netflow *nf,
218                     const struct netflow_options *nf_options)
219 {
220     int error = 0;
221     long long int old_timeout;
222
223     nf->engine_type = nf_options->engine_type;
224     nf->engine_id = nf_options->engine_id;
225     nf->add_id_to_iface = nf_options->add_id_to_iface;
226
227     collectors_destroy(nf->collectors);
228     collectors_create(&nf_options->collectors, 0, &nf->collectors);
229
230     old_timeout = nf->active_timeout;
231     if (nf_options->active_timeout >= 0) {
232         nf->active_timeout = nf_options->active_timeout;
233     } else {
234         nf->active_timeout = NF_ACTIVE_TIMEOUT_DEFAULT;
235     }
236     nf->active_timeout *= 1000;
237     if (old_timeout != nf->active_timeout) {
238         nf->reconfig_time = time_msec();
239         nf->next_timeout = time_msec();
240     }
241
242     return error;
243 }
244
245 struct netflow *
246 netflow_create(void)
247 {
248     struct netflow *nf = xzalloc(sizeof *nf);
249     nf->engine_type = 0;
250     nf->engine_id = 0;
251     nf->boot_time = time_msec();
252     nf->collectors = NULL;
253     nf->add_id_to_iface = false;
254     nf->netflow_cnt = 0;
255     ofpbuf_init(&nf->packet, 1500);
256     return nf;
257 }
258
259 void
260 netflow_destroy(struct netflow *nf)
261 {
262     if (nf) {
263         ofpbuf_uninit(&nf->packet);
264         collectors_destroy(nf->collectors);
265         free(nf);
266     }
267 }
268
269 /* Initializes a new 'nf_flow' given that the caller has already cleared it to
270  * all-zero-bits. */
271 void
272 netflow_flow_init(struct netflow_flow *nf_flow OVS_UNUSED)
273 {
274     /* Nothing to do. */
275 }
276
277 void
278 netflow_flow_clear(struct netflow_flow *nf_flow)
279 {
280     ofp_port_t output_iface = nf_flow->output_iface;
281
282     memset(nf_flow, 0, sizeof *nf_flow);
283     nf_flow->output_iface = output_iface;
284 }
285
286 void
287 netflow_flow_update_time(struct netflow *nf, struct netflow_flow *nf_flow,
288                          long long int used)
289 {
290     if (!nf_flow->created) {
291         nf_flow->created = used;
292     }
293
294     if (!nf || !nf->active_timeout || !nf_flow->last_expired ||
295         nf->reconfig_time > nf_flow->last_expired) {
296         /* Keep the time updated to prevent a flood of expiration in
297          * the future. */
298         nf_flow->last_expired = time_msec();
299     }
300 }
301
302 void
303 netflow_flow_update_flags(struct netflow_flow *nf_flow, uint8_t tcp_flags)
304 {
305     nf_flow->tcp_flags |= tcp_flags;
306 }
307
308 bool
309 netflow_active_timeout_expired(struct netflow *nf, struct netflow_flow *nf_flow)
310 {
311     if (nf->active_timeout) {
312         return time_msec() > nf_flow->last_expired + nf->active_timeout;
313     }
314
315     return false;
316 }