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