Merge "master" branch into "db".
[sliver-openvswitch.git] / ofproto / netflow.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
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 "cfg.h"
24 #include "collectors.h"
25 #include "flow.h"
26 #include "netflow.h"
27 #include "ofpbuf.h"
28 #include "ofproto.h"
29 #include "packets.h"
30 #include "socket-util.h"
31 #include "svec.h"
32 #include "timeval.h"
33 #include "util.h"
34 #include "xtoxll.h"
35
36 #define THIS_MODULE VLM_netflow
37 #include "vlog.h"
38
39 #define NETFLOW_V5_VERSION 5
40
41 static const int ACTIVE_TIMEOUT_DEFAULT = 600;
42
43 /* Every NetFlow v5 message contains the header that follows.  This is
44  * followed by up to thirty records that describe a terminating flow.
45  * We only send a single record per NetFlow message.
46  */
47 struct netflow_v5_header {
48     uint16_t version;              /* NetFlow version is 5. */
49     uint16_t count;                /* Number of records in this message. */
50     uint32_t sysuptime;            /* System uptime in milliseconds. */
51     uint32_t unix_secs;            /* Number of seconds since Unix epoch. */
52     uint32_t unix_nsecs;           /* Number of residual nanoseconds
53                                       after epoch seconds. */
54     uint32_t flow_seq;             /* Number of flows since sending
55                                       messages began. */
56     uint8_t  engine_type;          /* Engine type. */
57     uint8_t  engine_id;            /* Engine id. */
58     uint16_t sampling_interval;    /* Set to zero. */
59 };
60 BUILD_ASSERT_DECL(sizeof(struct netflow_v5_header) == 24);
61
62 /* A NetFlow v5 description of a terminating flow.  It is preceded by a
63  * NetFlow v5 header.
64  */
65 struct netflow_v5_record {
66     uint32_t src_addr;             /* Source IP address. */
67     uint32_t dst_addr;             /* Destination IP address. */
68     uint32_t nexthop;              /* IP address of next hop.  Set to 0. */
69     uint16_t input;                /* Input interface index. */
70     uint16_t output;               /* Output interface index. */
71     uint32_t packet_count;         /* Number of packets. */
72     uint32_t byte_count;           /* Number of bytes. */
73     uint32_t init_time;            /* Value of sysuptime on first packet. */
74     uint32_t used_time;            /* Value of sysuptime on last packet. */
75
76     /* The 'src_port' and 'dst_port' identify the source and destination
77      * port, respectively, for TCP and UDP.  For ICMP, the high-order
78      * byte identifies the type and low-order byte identifies the code
79      * in the 'dst_port' field. */
80     uint16_t src_port;
81     uint16_t dst_port;
82
83     uint8_t  pad1;
84     uint8_t  tcp_flags;            /* Union of seen TCP flags. */
85     uint8_t  ip_proto;             /* IP protocol. */
86     uint8_t  ip_tos;               /* IP TOS value. */
87     uint16_t src_as;               /* Source AS ID.  Set to 0. */
88     uint16_t dst_as;               /* Destination AS ID.  Set to 0. */
89     uint8_t  src_mask;             /* Source mask bits.  Set to 0. */
90     uint8_t  dst_mask;             /* Destination mask bits.  Set to 0. */
91     uint8_t  pad[2];
92 };
93 BUILD_ASSERT_DECL(sizeof(struct netflow_v5_record) == 48);
94
95 struct netflow {
96     uint8_t engine_type;          /* Value of engine_type to use. */
97     uint8_t engine_id;            /* Value of engine_id to use. */
98     long long int boot_time;      /* Time when netflow_create() was called. */
99     struct collectors *collectors; /* NetFlow collectors. */
100     bool add_id_to_iface;         /* Put the 7 least signficiant bits of 
101                                    * 'engine_id' into the most signficant 
102                                    * bits of the interface fields. */
103     uint32_t netflow_cnt;         /* Flow sequence number for NetFlow. */
104     struct ofpbuf packet;         /* NetFlow packet being accumulated. */
105     long long int active_timeout; /* Timeout for flows that are still active. */
106     long long int reconfig_time;  /* When we reconfigured the timeouts. */
107 };
108
109 void
110 netflow_expire(struct netflow *nf, struct netflow_flow *nf_flow,
111                struct ofexpired *expired)
112 {
113     struct netflow_v5_header *nf_hdr;
114     struct netflow_v5_record *nf_rec;
115     struct timeval now;
116
117     nf_flow->last_expired += nf->active_timeout;
118
119     /* NetFlow only reports on IP packets and we should only report flows
120      * that actually have traffic. */
121     if (expired->flow.dl_type != htons(ETH_TYPE_IP) ||
122         expired->packet_count - nf_flow->packet_count_off == 0) {
123         return;
124     }
125
126     time_timeval(&now);
127
128     if (!nf->packet.size) {
129         nf_hdr = ofpbuf_put_zeros(&nf->packet, sizeof *nf_hdr);
130         nf_hdr->version = htons(NETFLOW_V5_VERSION);
131         nf_hdr->count = htons(0);
132         nf_hdr->sysuptime = htonl(time_msec() - nf->boot_time);
133         nf_hdr->unix_secs = htonl(now.tv_sec);
134         nf_hdr->unix_nsecs = htonl(now.tv_usec * 1000);
135         nf_hdr->flow_seq = htonl(nf->netflow_cnt++);
136         nf_hdr->engine_type = nf->engine_type;
137         nf_hdr->engine_id = nf->engine_id;
138         nf_hdr->sampling_interval = htons(0);
139     }
140
141     nf_hdr = nf->packet.data;
142     nf_hdr->count = htons(ntohs(nf_hdr->count) + 1);
143
144     nf_rec = ofpbuf_put_zeros(&nf->packet, sizeof *nf_rec);
145     nf_rec->src_addr = expired->flow.nw_src;
146     nf_rec->dst_addr = expired->flow.nw_dst;
147     nf_rec->nexthop = htons(0);
148     if (nf->add_id_to_iface) {
149         uint16_t iface = (nf->engine_id & 0x7f) << 9;
150         nf_rec->input = htons(iface | (expired->flow.in_port & 0x1ff));
151         nf_rec->output = htons(iface | (nf_flow->output_iface & 0x1ff));
152     } else {
153         nf_rec->input = htons(expired->flow.in_port);
154         nf_rec->output = htons(nf_flow->output_iface);
155     }
156     nf_rec->packet_count = htonl(MIN(expired->packet_count -
157                                      nf_flow->packet_count_off, UINT32_MAX));
158     nf_rec->byte_count = htonl(MIN(expired->byte_count -
159                                    nf_flow->byte_count_off, UINT32_MAX));
160     nf_rec->init_time = htonl(nf_flow->created - nf->boot_time);
161     nf_rec->used_time = htonl(MAX(nf_flow->created, expired->used)
162                              - nf->boot_time);
163     if (expired->flow.nw_proto == IP_TYPE_ICMP) {
164         /* In NetFlow, the ICMP type and code are concatenated and
165          * placed in the 'dst_port' field. */
166         uint8_t type = ntohs(expired->flow.tp_src);
167         uint8_t code = ntohs(expired->flow.tp_dst);
168         nf_rec->src_port = htons(0);
169         nf_rec->dst_port = htons((type << 8) | code);
170     } else {
171         nf_rec->src_port = expired->flow.tp_src;
172         nf_rec->dst_port = expired->flow.tp_dst;
173     }
174     nf_rec->tcp_flags = nf_flow->tcp_flags;
175     nf_rec->ip_proto = expired->flow.nw_proto;
176     nf_rec->ip_tos = nf_flow->ip_tos;
177
178     /* Update flow tracking data. */
179     nf_flow->created = 0;
180     nf_flow->packet_count_off = expired->packet_count;
181     nf_flow->byte_count_off = expired->byte_count;
182     nf_flow->tcp_flags = 0;
183
184     /* NetFlow messages are limited to 30 records. */
185     if (ntohs(nf_hdr->count) >= 30) {
186         netflow_run(nf);
187     }
188 }
189
190 void
191 netflow_run(struct netflow *nf)
192 {
193     if (nf->packet.size) {
194         collectors_send(nf->collectors, nf->packet.data, nf->packet.size);
195         nf->packet.size = 0;
196     }
197 }
198
199 int
200 netflow_set_options(struct netflow *nf,
201                     const struct netflow_options *nf_options)
202 {
203     int error = 0;
204     long long int old_timeout;
205
206     nf->engine_type = nf_options->engine_type;
207     nf->engine_id = nf_options->engine_id;
208     nf->add_id_to_iface = nf_options->add_id_to_iface;
209
210     collectors_destroy(nf->collectors);
211     collectors_create(&nf_options->collectors, 0, &nf->collectors);
212
213     old_timeout = nf->active_timeout;
214     if (nf_options->active_timeout != -1) {
215         nf->active_timeout = nf_options->active_timeout;
216     } else {
217         nf->active_timeout = ACTIVE_TIMEOUT_DEFAULT;
218     }
219     nf->active_timeout *= 1000;
220     if (old_timeout != nf->active_timeout) {
221         nf->reconfig_time = time_msec();
222     }
223
224     return error;
225 }
226
227 struct netflow *
228 netflow_create(void)
229 {
230     struct netflow *nf = xmalloc(sizeof *nf);
231     nf->engine_type = 0;
232     nf->engine_id = 0;
233     nf->boot_time = time_msec();
234     nf->collectors = NULL;
235     nf->add_id_to_iface = false;
236     nf->netflow_cnt = 0;
237     ofpbuf_init(&nf->packet, 1500);
238     return nf;
239 }
240
241 void
242 netflow_destroy(struct netflow *nf)
243 {
244     if (nf) {
245         ofpbuf_uninit(&nf->packet);
246         collectors_destroy(nf->collectors);
247         free(nf);
248     }
249 }
250
251 void
252 netflow_flow_clear(struct netflow_flow *nf_flow)
253 {
254     uint16_t output_iface = nf_flow->output_iface;
255
256     memset(nf_flow, 0, sizeof *nf_flow);
257     nf_flow->output_iface = output_iface;
258 }
259
260 void
261 netflow_flow_update_time(struct netflow *nf, struct netflow_flow *nf_flow,
262                          long long int used)
263 {
264     if (!nf_flow->created) {
265         nf_flow->created = used;
266     }
267
268     if (!nf || !nf->active_timeout || !nf_flow->last_expired ||
269         nf->reconfig_time > nf_flow->last_expired) {
270         /* Keep the time updated to prevent a flood of expiration in
271          * the future. */
272         nf_flow->last_expired = time_msec();
273     }
274 }
275
276 void
277 netflow_flow_update_flags(struct netflow_flow *nf_flow, uint8_t ip_tos,
278                           uint8_t tcp_flags)
279 {
280     nf_flow->ip_tos = ip_tos;
281     nf_flow->tcp_flags |= tcp_flags;
282 }
283
284 bool
285 netflow_active_timeout_expired(struct netflow *nf, struct netflow_flow *nf_flow)
286 {
287     if (nf->active_timeout) {
288         return time_msec() > nf_flow->last_expired + nf->active_timeout;
289     }
290
291     return false;
292 }