]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/ofproto-dpif-sflow.c
aacb3e81c65fbaf2361b6c1a7b735e09f18a27a4
[mirror_ovs.git] / ofproto / ofproto-dpif-sflow.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3 * Copyright (c) 2009 InMon Corp.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <config.h>
19 #include "ofproto-dpif-sflow.h"
20 #include <inttypes.h>
21 #include <sys/socket.h>
22 #include <net/if.h>
23 #include <stdlib.h>
24 #include "collectors.h"
25 #include "compiler.h"
26 #include "dpif.h"
27 #include "hash.h"
28 #include "hmap.h"
29 #include "netdev.h"
30 #include "netlink.h"
31 #include "ofpbuf.h"
32 #include "ofproto.h"
33 #include "packets.h"
34 #include "poll-loop.h"
35 #include "route-table.h"
36 #include "sflow_api.h"
37 #include "socket-util.h"
38 #include "timeval.h"
39 #include "vlog.h"
40 #include "lib/odp-util.h"
41 #include "ofproto-provider.h"
42
43 VLOG_DEFINE_THIS_MODULE(sflow);
44
45 struct dpif_sflow_port {
46 struct hmap_node hmap_node; /* In struct dpif_sflow's "ports" hmap. */
47 SFLDataSource_instance dsi; /* sFlow library's notion of port number. */
48 struct ofport *ofport; /* To retrive port stats. */
49 uint32_t odp_port;
50 };
51
52 struct dpif_sflow {
53 struct collectors *collectors;
54 SFLAgent *sflow_agent;
55 struct ofproto_sflow_options *options;
56 struct dpif *dpif;
57 time_t next_tick;
58 size_t n_flood, n_all;
59 struct hmap ports; /* Contains "struct dpif_sflow_port"s. */
60 uint32_t probability;
61 };
62
63 static void dpif_sflow_del_port__(struct dpif_sflow *,
64 struct dpif_sflow_port *);
65
66 #define RECEIVER_INDEX 1
67
68 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
69
70 static bool
71 nullable_string_is_equal(const char *a, const char *b)
72 {
73 return a ? b && !strcmp(a, b) : !b;
74 }
75
76 static bool
77 ofproto_sflow_options_equal(const struct ofproto_sflow_options *a,
78 const struct ofproto_sflow_options *b)
79 {
80 return (sset_equals(&a->targets, &b->targets)
81 && a->sampling_rate == b->sampling_rate
82 && a->polling_interval == b->polling_interval
83 && a->header_len == b->header_len
84 && a->sub_id == b->sub_id
85 && nullable_string_is_equal(a->agent_device, b->agent_device)
86 && nullable_string_is_equal(a->control_ip, b->control_ip));
87 }
88
89 static struct ofproto_sflow_options *
90 ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
91 {
92 struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
93 sset_clone(&new->targets, &old->targets);
94 new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
95 new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
96 return new;
97 }
98
99 static void
100 ofproto_sflow_options_destroy(struct ofproto_sflow_options *options)
101 {
102 if (options) {
103 sset_destroy(&options->targets);
104 free(options->agent_device);
105 free(options->control_ip);
106 free(options);
107 }
108 }
109
110 /* sFlow library callback to allocate memory. */
111 static void *
112 sflow_agent_alloc_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
113 size_t bytes)
114 {
115 return calloc(1, bytes);
116 }
117
118 /* sFlow library callback to free memory. */
119 static int
120 sflow_agent_free_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
121 void *obj)
122 {
123 free(obj);
124 return 0;
125 }
126
127 /* sFlow library callback to report error. */
128 static void
129 sflow_agent_error_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
130 char *msg)
131 {
132 VLOG_WARN("sFlow agent error: %s", msg);
133 }
134
135 /* sFlow library callback to send datagram. */
136 static void
137 sflow_agent_send_packet_cb(void *ds_, SFLAgent *agent OVS_UNUSED,
138 SFLReceiver *receiver OVS_UNUSED, u_char *pkt,
139 uint32_t pktLen)
140 {
141 struct dpif_sflow *ds = ds_;
142 collectors_send(ds->collectors, pkt, pktLen);
143 }
144
145 static struct dpif_sflow_port *
146 dpif_sflow_find_port(const struct dpif_sflow *ds, uint32_t odp_port)
147 {
148 struct dpif_sflow_port *dsp;
149
150 HMAP_FOR_EACH_IN_BUCKET (dsp, hmap_node,
151 hash_int(odp_port, 0), &ds->ports) {
152 if (dsp->odp_port == odp_port) {
153 return dsp;
154 }
155 }
156 return NULL;
157 }
158
159 static void
160 sflow_agent_get_counters(void *ds_, SFLPoller *poller,
161 SFL_COUNTERS_SAMPLE_TYPE *cs)
162 {
163 struct dpif_sflow *ds = ds_;
164 SFLCounters_sample_element elem;
165 enum netdev_features current;
166 struct dpif_sflow_port *dsp;
167 SFLIf_counters *counters;
168 struct netdev_stats stats;
169 enum netdev_flags flags;
170
171 dsp = dpif_sflow_find_port(ds, poller->bridgePort);
172 if (!dsp) {
173 return;
174 }
175
176 elem.tag = SFLCOUNTERS_GENERIC;
177 counters = &elem.counterBlock.generic;
178 counters->ifIndex = SFL_DS_INDEX(poller->dsi);
179 counters->ifType = 6;
180 if (!netdev_get_features(dsp->ofport->netdev, &current, NULL, NULL, NULL)) {
181 /* The values of ifDirection come from MAU MIB (RFC 2668): 0 = unknown,
182 1 = full-duplex, 2 = half-duplex, 3 = in, 4=out */
183 counters->ifSpeed = netdev_features_to_bps(current);
184 counters->ifDirection = (netdev_features_is_full_duplex(current)
185 ? 1 : 2);
186 } else {
187 counters->ifSpeed = 100000000;
188 counters->ifDirection = 0;
189 }
190 if (!netdev_get_flags(dsp->ofport->netdev, &flags) && flags & NETDEV_UP) {
191 counters->ifStatus = 1; /* ifAdminStatus up. */
192 if (netdev_get_carrier(dsp->ofport->netdev)) {
193 counters->ifStatus |= 2; /* ifOperStatus us. */
194 }
195 } else {
196 counters->ifStatus = 0; /* Down. */
197 }
198
199 /* XXX
200 1. Is the multicast counter filled in?
201 2. Does the multicast counter include broadcasts?
202 3. Does the rx_packets counter include multicasts/broadcasts?
203 */
204 ofproto_port_get_stats(dsp->ofport, &stats);
205 counters->ifInOctets = stats.rx_bytes;
206 counters->ifInUcastPkts = stats.rx_packets;
207 counters->ifInMulticastPkts = stats.multicast;
208 counters->ifInBroadcastPkts = -1;
209 counters->ifInDiscards = stats.rx_dropped;
210 counters->ifInErrors = stats.rx_errors;
211 counters->ifInUnknownProtos = -1;
212 counters->ifOutOctets = stats.tx_bytes;
213 counters->ifOutUcastPkts = stats.tx_packets;
214 counters->ifOutMulticastPkts = -1;
215 counters->ifOutBroadcastPkts = -1;
216 counters->ifOutDiscards = stats.tx_dropped;
217 counters->ifOutErrors = stats.tx_errors;
218 counters->ifPromiscuousMode = 0;
219
220 SFLADD_ELEMENT(cs, &elem);
221 sfl_poller_writeCountersSample(poller, cs);
222 }
223
224 /* Obtains an address to use for the local sFlow agent and stores it into
225 * '*agent_addr'. Returns true if successful, false on failure.
226 *
227 * The sFlow agent address should be a local IP address that is persistent and
228 * reachable over the network, if possible. The IP address associated with
229 * 'agent_device' is used if it has one, and otherwise 'control_ip', the IP
230 * address used to talk to the controller. If the agent device is not
231 * specified then it is figured out by taking a look at the routing table based
232 * on 'targets'. */
233 static bool
234 sflow_choose_agent_address(const char *agent_device,
235 const struct sset *targets,
236 const char *control_ip,
237 SFLAddress *agent_addr)
238 {
239 const char *target;
240 struct in_addr in4;
241
242 memset(agent_addr, 0, sizeof *agent_addr);
243 agent_addr->type = SFLADDRESSTYPE_IP_V4;
244
245 if (agent_device) {
246 if (!netdev_get_in4_by_name(agent_device, &in4)) {
247 goto success;
248 }
249 }
250
251 SSET_FOR_EACH (target, targets) {
252 struct sockaddr_in sin;
253 char name[IFNAMSIZ];
254
255 if (inet_parse_active(target, SFL_DEFAULT_COLLECTOR_PORT, &sin)
256 && route_table_get_name(sin.sin_addr.s_addr, name)
257 && !netdev_get_in4_by_name(name, &in4)) {
258 goto success;
259 }
260 }
261
262 if (control_ip && !lookup_ip(control_ip, &in4)) {
263 goto success;
264 }
265
266 VLOG_ERR("could not determine IP address for sFlow agent");
267 return false;
268
269 success:
270 agent_addr->address.ip_v4.addr = (OVS_FORCE uint32_t) in4.s_addr;
271 return true;
272 }
273
274 void
275 dpif_sflow_clear(struct dpif_sflow *ds)
276 {
277 if (ds->sflow_agent) {
278 sfl_agent_release(ds->sflow_agent);
279 ds->sflow_agent = NULL;
280 }
281 collectors_destroy(ds->collectors);
282 ds->collectors = NULL;
283 ofproto_sflow_options_destroy(ds->options);
284 ds->options = NULL;
285
286 /* Turn off sampling to save CPU cycles. */
287 ds->probability = 0;
288 }
289
290 bool
291 dpif_sflow_is_enabled(const struct dpif_sflow *ds)
292 {
293 return ds->collectors != NULL;
294 }
295
296 struct dpif_sflow *
297 dpif_sflow_create(struct dpif *dpif)
298 {
299 struct dpif_sflow *ds;
300
301 ds = xcalloc(1, sizeof *ds);
302 ds->dpif = dpif;
303 ds->next_tick = time_now() + 1;
304 hmap_init(&ds->ports);
305 ds->probability = 0;
306 route_table_register();
307
308 return ds;
309 }
310
311 /* 32-bit fraction of packets to sample with. A value of 0 samples no packets,
312 * a value of %UINT32_MAX samples all packets and intermediate values sample
313 * intermediate fractions of packets. */
314 uint32_t
315 dpif_sflow_get_probability(const struct dpif_sflow *ds)
316 {
317 return ds->probability;
318 }
319
320 void
321 dpif_sflow_destroy(struct dpif_sflow *ds)
322 {
323 if (ds) {
324 struct dpif_sflow_port *dsp, *next;
325
326 route_table_unregister();
327 dpif_sflow_clear(ds);
328 HMAP_FOR_EACH_SAFE (dsp, next, hmap_node, &ds->ports) {
329 dpif_sflow_del_port__(ds, dsp);
330 }
331 hmap_destroy(&ds->ports);
332 free(ds);
333 }
334 }
335
336 static void
337 dpif_sflow_add_poller(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
338 {
339 SFLPoller *poller = sfl_agent_addPoller(ds->sflow_agent, &dsp->dsi, ds,
340 sflow_agent_get_counters);
341 sfl_poller_set_sFlowCpInterval(poller, ds->options->polling_interval);
342 sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX);
343 sfl_poller_set_bridgePort(poller, dsp->odp_port);
344 }
345
346 static void
347 dpif_sflow_add_sampler(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
348 {
349 SFLSampler *sampler = sfl_agent_addSampler(ds->sflow_agent, &dsp->dsi);
350 sfl_sampler_set_sFlowFsPacketSamplingRate(sampler, ds->options->sampling_rate);
351 sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, ds->options->header_len);
352 sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX);
353 }
354
355 void
356 dpif_sflow_add_port(struct dpif_sflow *ds, struct ofport *ofport,
357 uint32_t odp_port)
358 {
359 struct dpif_sflow_port *dsp;
360 uint32_t ifindex;
361
362 dpif_sflow_del_port(ds, odp_port);
363
364 /* Add to table of ports. */
365 dsp = xmalloc(sizeof *dsp);
366 ifindex = netdev_get_ifindex(ofport->netdev);
367 if (ifindex <= 0) {
368 ifindex = (ds->sflow_agent->subId << 16) + odp_port;
369 }
370 dsp->ofport = ofport;
371 dsp->odp_port = odp_port;
372 SFL_DS_SET(dsp->dsi, 0, ifindex, 0);
373 hmap_insert(&ds->ports, &dsp->hmap_node, hash_int(odp_port, 0));
374
375 /* Add poller and sampler. */
376 if (ds->sflow_agent) {
377 dpif_sflow_add_poller(ds, dsp);
378 dpif_sflow_add_sampler(ds, dsp);
379 }
380 }
381
382 static void
383 dpif_sflow_del_port__(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
384 {
385 if (ds->sflow_agent) {
386 sfl_agent_removePoller(ds->sflow_agent, &dsp->dsi);
387 sfl_agent_removeSampler(ds->sflow_agent, &dsp->dsi);
388 }
389 hmap_remove(&ds->ports, &dsp->hmap_node);
390 free(dsp);
391 }
392
393 void
394 dpif_sflow_del_port(struct dpif_sflow *ds, uint32_t odp_port)
395 {
396 struct dpif_sflow_port *dsp = dpif_sflow_find_port(ds, odp_port);
397 if (dsp) {
398 dpif_sflow_del_port__(ds, dsp);
399 }
400 }
401
402 void
403 dpif_sflow_set_options(struct dpif_sflow *ds,
404 const struct ofproto_sflow_options *options)
405 {
406 struct dpif_sflow_port *dsp;
407 bool options_changed;
408 SFLReceiver *receiver;
409 SFLAddress agentIP;
410 time_t now;
411
412 if (sset_is_empty(&options->targets) || !options->sampling_rate) {
413 /* No point in doing any work if there are no targets or nothing to
414 * sample. */
415 dpif_sflow_clear(ds);
416 return;
417 }
418
419 options_changed = (!ds->options
420 || !ofproto_sflow_options_equal(options, ds->options));
421
422 /* Configure collectors if options have changed or if we're shortchanged in
423 * collectors (which indicates that opening one or more of the configured
424 * collectors failed, so that we should retry). */
425 if (options_changed
426 || collectors_count(ds->collectors) < sset_count(&options->targets)) {
427 collectors_destroy(ds->collectors);
428 collectors_create(&options->targets, SFL_DEFAULT_COLLECTOR_PORT,
429 &ds->collectors);
430 if (ds->collectors == NULL) {
431 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
432 "sFlow disabled");
433 dpif_sflow_clear(ds);
434 return;
435 }
436 }
437
438 /* Choose agent IP address and agent device (if not yet setup) */
439 if (!sflow_choose_agent_address(options->agent_device,
440 &options->targets,
441 options->control_ip, &agentIP)) {
442 dpif_sflow_clear(ds);
443 return;
444 }
445
446 /* Avoid reconfiguring if options didn't change. */
447 if (!options_changed) {
448 return;
449 }
450 ofproto_sflow_options_destroy(ds->options);
451 ds->options = ofproto_sflow_options_clone(options);
452
453 /* Create agent. */
454 VLOG_INFO("creating sFlow agent %d", options->sub_id);
455 if (ds->sflow_agent) {
456 sfl_agent_release(ds->sflow_agent);
457 }
458 ds->sflow_agent = xcalloc(1, sizeof *ds->sflow_agent);
459 now = time_wall();
460 sfl_agent_init(ds->sflow_agent,
461 &agentIP,
462 options->sub_id,
463 now, /* Boot time. */
464 now, /* Current time. */
465 ds, /* Pointer supplied to callbacks. */
466 sflow_agent_alloc_cb,
467 sflow_agent_free_cb,
468 sflow_agent_error_cb,
469 sflow_agent_send_packet_cb);
470
471 receiver = sfl_agent_addReceiver(ds->sflow_agent);
472 sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow");
473 sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff);
474
475 /* Set the sampling_rate down in the datapath. */
476 ds->probability = MAX(1, UINT32_MAX / ds->options->sampling_rate);
477
478 /* Add samplers and pollers for the currently known ports. */
479 HMAP_FOR_EACH (dsp, hmap_node, &ds->ports) {
480 dpif_sflow_add_poller(ds, dsp);
481 dpif_sflow_add_sampler(ds, dsp);
482 }
483 }
484
485 int
486 dpif_sflow_odp_port_to_ifindex(const struct dpif_sflow *ds,
487 uint32_t odp_port)
488 {
489 struct dpif_sflow_port *dsp = dpif_sflow_find_port(ds, odp_port);
490 return dsp ? SFL_DS_INDEX(dsp->dsi) : 0;
491 }
492
493 void
494 dpif_sflow_received(struct dpif_sflow *ds, struct ofpbuf *packet,
495 const struct flow *flow, uint32_t odp_in_port,
496 const union user_action_cookie *cookie)
497 {
498 SFL_FLOW_SAMPLE_TYPE fs;
499 SFLFlow_sample_element hdrElem;
500 SFLSampled_header *header;
501 SFLFlow_sample_element switchElem;
502 SFLSampler *sampler;
503 struct dpif_sflow_port *in_dsp;
504 struct netdev_stats stats;
505 ovs_be16 vlan_tci;
506 int error;
507
508 /* Build a flow sample */
509 memset(&fs, 0, sizeof fs);
510
511 in_dsp = dpif_sflow_find_port(ds, odp_in_port);
512 if (!in_dsp) {
513 return;
514 }
515 fs.input = SFL_DS_INDEX(in_dsp->dsi);
516
517 error = ofproto_port_get_stats(in_dsp->ofport, &stats);
518 if (error) {
519 VLOG_WARN_RL(&rl, "netdev get-stats error %s", strerror(error));
520 return;
521 }
522 fs.sample_pool = stats.rx_packets;
523
524 /* We are going to give it to the sampler that represents this input port.
525 * By implementing "ingress-only" sampling like this we ensure that we
526 * never have to offer the same sample to more than one sampler. */
527 sampler = sfl_agent_getSamplerByIfIndex(ds->sflow_agent, fs.input);
528 if (!sampler) {
529 VLOG_WARN_RL(&rl, "no sampler for input ifIndex (%"PRIu32")",
530 fs.input);
531 return;
532 }
533
534 /* Sampled header. */
535 memset(&hdrElem, 0, sizeof hdrElem);
536 hdrElem.tag = SFLFLOW_HEADER;
537 header = &hdrElem.flowType.header;
538 header->header_protocol = SFLHEADER_ETHERNET_ISO8023;
539 /* The frame_length should include the Ethernet FCS (4 bytes),
540 but it has already been stripped, so we need to add 4 here. */
541 header->frame_length = packet->size + 4;
542 /* Ethernet FCS stripped off. */
543 header->stripped = 4;
544 header->header_length = MIN(packet->size,
545 sampler->sFlowFsMaximumHeaderSize);
546 header->header_bytes = packet->data;
547
548 /* Add extended switch element. */
549 memset(&switchElem, 0, sizeof(switchElem));
550 switchElem.tag = SFLFLOW_EX_SWITCH;
551 switchElem.flowType.sw.src_vlan = vlan_tci_to_vid(flow->vlan_tci);
552 switchElem.flowType.sw.src_priority = vlan_tci_to_pcp(flow->vlan_tci);
553
554 /* Retrieve data from user_action_cookie. */
555 vlan_tci = cookie->sflow.vlan_tci;
556 switchElem.flowType.sw.dst_vlan = vlan_tci_to_vid(vlan_tci);
557 switchElem.flowType.sw.dst_priority = vlan_tci_to_pcp(vlan_tci);
558
559 fs.output = cookie->sflow.output;
560
561 /* Submit the flow sample to be encoded into the next datagram. */
562 SFLADD_ELEMENT(&fs, &hdrElem);
563 SFLADD_ELEMENT(&fs, &switchElem);
564 sfl_sampler_writeFlowSample(sampler, &fs);
565 }
566
567 void
568 dpif_sflow_run(struct dpif_sflow *ds)
569 {
570 if (dpif_sflow_is_enabled(ds)) {
571 time_t now = time_now();
572 route_table_run();
573 if (now >= ds->next_tick) {
574 sfl_agent_tick(ds->sflow_agent, time_wall());
575 ds->next_tick = now + 1;
576 }
577 }
578 }
579
580 void
581 dpif_sflow_wait(struct dpif_sflow *ds)
582 {
583 if (dpif_sflow_is_enabled(ds)) {
584 poll_timer_wait_until(ds->next_tick * 1000LL);
585 }
586 }