2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3 * Copyright (c) 2009 InMon Corp.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "ofproto-dpif-sflow.h"
23 #include "collectors.h"
33 #include "poll-loop.h"
34 #include "route-table.h"
35 #include "sflow_api.h"
36 #include "socket-util.h"
39 #include "lib/odp-util.h"
40 #include "ofproto-provider.h"
42 VLOG_DEFINE_THIS_MODULE(sflow);
44 struct dpif_sflow_port {
45 struct hmap_node hmap_node; /* In struct dpif_sflow's "ports" hmap. */
46 SFLDataSource_instance dsi; /* sFlow library's notion of port number. */
47 struct ofport *ofport; /* To retrive port stats. */
51 struct collectors *collectors;
52 SFLAgent *sflow_agent;
53 struct ofproto_sflow_options *options;
56 size_t n_flood, n_all;
57 struct hmap ports; /* Contains "struct dpif_sflow_port"s. */
61 static void dpif_sflow_del_port__(struct dpif_sflow *,
62 struct dpif_sflow_port *);
64 #define RECEIVER_INDEX 1
66 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
69 nullable_string_is_equal(const char *a, const char *b)
71 return a ? b && !strcmp(a, b) : !b;
75 ofproto_sflow_options_equal(const struct ofproto_sflow_options *a,
76 const struct ofproto_sflow_options *b)
78 return (sset_equals(&a->targets, &b->targets)
79 && a->sampling_rate == b->sampling_rate
80 && a->polling_interval == b->polling_interval
81 && a->header_len == b->header_len
82 && a->sub_id == b->sub_id
83 && nullable_string_is_equal(a->agent_device, b->agent_device)
84 && nullable_string_is_equal(a->control_ip, b->control_ip));
87 static struct ofproto_sflow_options *
88 ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
90 struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
91 sset_clone(&new->targets, &old->targets);
92 new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
93 new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
98 ofproto_sflow_options_destroy(struct ofproto_sflow_options *options)
101 sset_destroy(&options->targets);
102 free(options->agent_device);
103 free(options->control_ip);
108 /* sFlow library callback to allocate memory. */
110 sflow_agent_alloc_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
113 return calloc(1, bytes);
116 /* sFlow library callback to free memory. */
118 sflow_agent_free_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
125 /* sFlow library callback to report error. */
127 sflow_agent_error_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
130 VLOG_WARN("sFlow agent error: %s", msg);
133 /* sFlow library callback to send datagram. */
135 sflow_agent_send_packet_cb(void *ds_, SFLAgent *agent OVS_UNUSED,
136 SFLReceiver *receiver OVS_UNUSED, u_char *pkt,
139 struct dpif_sflow *ds = ds_;
140 collectors_send(ds->collectors, pkt, pktLen);
143 static struct dpif_sflow_port *
144 dpif_sflow_find_port(const struct dpif_sflow *ds, uint16_t odp_port)
146 struct dpif_sflow_port *dsp;
148 HMAP_FOR_EACH_IN_BUCKET (dsp, hmap_node,
149 hash_int(odp_port, 0), &ds->ports) {
150 if (ofp_port_to_odp_port(dsp->ofport->ofp_port) == odp_port) {
158 sflow_agent_get_counters(void *ds_, SFLPoller *poller,
159 SFL_COUNTERS_SAMPLE_TYPE *cs)
161 struct dpif_sflow *ds = ds_;
162 SFLCounters_sample_element elem;
163 enum netdev_features current;
164 struct dpif_sflow_port *dsp;
165 SFLIf_counters *counters;
166 struct netdev_stats stats;
167 enum netdev_flags flags;
169 dsp = dpif_sflow_find_port(ds, poller->bridgePort);
174 elem.tag = SFLCOUNTERS_GENERIC;
175 counters = &elem.counterBlock.generic;
176 counters->ifIndex = SFL_DS_INDEX(poller->dsi);
177 counters->ifType = 6;
178 if (!netdev_get_features(dsp->ofport->netdev, ¤t, NULL, NULL, NULL)) {
179 /* The values of ifDirection come from MAU MIB (RFC 2668): 0 = unknown,
180 1 = full-duplex, 2 = half-duplex, 3 = in, 4=out */
181 counters->ifSpeed = netdev_features_to_bps(current);
182 counters->ifDirection = (netdev_features_is_full_duplex(current)
185 counters->ifSpeed = 100000000;
186 counters->ifDirection = 0;
188 if (!netdev_get_flags(dsp->ofport->netdev, &flags) && flags & NETDEV_UP) {
189 counters->ifStatus = 1; /* ifAdminStatus up. */
190 if (netdev_get_carrier(dsp->ofport->netdev)) {
191 counters->ifStatus |= 2; /* ifOperStatus us. */
194 counters->ifStatus = 0; /* Down. */
198 1. Is the multicast counter filled in?
199 2. Does the multicast counter include broadcasts?
200 3. Does the rx_packets counter include multicasts/broadcasts?
202 ofproto_port_get_stats(dsp->ofport, &stats);
203 counters->ifInOctets = stats.rx_bytes;
204 counters->ifInUcastPkts = stats.rx_packets;
205 counters->ifInMulticastPkts = stats.multicast;
206 counters->ifInBroadcastPkts = -1;
207 counters->ifInDiscards = stats.rx_dropped;
208 counters->ifInErrors = stats.rx_errors;
209 counters->ifInUnknownProtos = -1;
210 counters->ifOutOctets = stats.tx_bytes;
211 counters->ifOutUcastPkts = stats.tx_packets;
212 counters->ifOutMulticastPkts = -1;
213 counters->ifOutBroadcastPkts = -1;
214 counters->ifOutDiscards = stats.tx_dropped;
215 counters->ifOutErrors = stats.tx_errors;
216 counters->ifPromiscuousMode = 0;
218 SFLADD_ELEMENT(cs, &elem);
219 sfl_poller_writeCountersSample(poller, cs);
222 /* Obtains an address to use for the local sFlow agent and stores it into
223 * '*agent_addr'. Returns true if successful, false on failure.
225 * The sFlow agent address should be a local IP address that is persistent and
226 * reachable over the network, if possible. The IP address associated with
227 * 'agent_device' is used if it has one, and otherwise 'control_ip', the IP
228 * address used to talk to the controller. If the agent device is not
229 * specified then it is figured out by taking a look at the routing table based
232 sflow_choose_agent_address(const char *agent_device,
233 const struct sset *targets,
234 const char *control_ip,
235 SFLAddress *agent_addr)
240 memset(agent_addr, 0, sizeof *agent_addr);
241 agent_addr->type = SFLADDRESSTYPE_IP_V4;
244 if (!netdev_get_in4_by_name(agent_device, &in4)) {
249 SSET_FOR_EACH (target, targets) {
250 struct sockaddr_in sin;
253 if (inet_parse_active(target, SFL_DEFAULT_COLLECTOR_PORT, &sin)
254 && route_table_get_name(sin.sin_addr.s_addr, name)
255 && !netdev_get_in4_by_name(name, &in4)) {
260 if (control_ip && !lookup_ip(control_ip, &in4)) {
264 VLOG_ERR("could not determine IP address for sFlow agent");
268 agent_addr->address.ip_v4.addr = (OVS_FORCE uint32_t) in4.s_addr;
273 dpif_sflow_clear(struct dpif_sflow *ds)
275 if (ds->sflow_agent) {
276 sfl_agent_release(ds->sflow_agent);
277 ds->sflow_agent = NULL;
279 collectors_destroy(ds->collectors);
280 ds->collectors = NULL;
281 ofproto_sflow_options_destroy(ds->options);
284 /* Turn off sampling to save CPU cycles. */
289 dpif_sflow_is_enabled(const struct dpif_sflow *ds)
291 return ds->collectors != NULL;
295 dpif_sflow_create(struct dpif *dpif)
297 struct dpif_sflow *ds;
299 ds = xcalloc(1, sizeof *ds);
301 ds->next_tick = time_now() + 1;
302 hmap_init(&ds->ports);
304 route_table_register();
309 /* 32-bit fraction of packets to sample with. A value of 0 samples no packets,
310 * a value of %UINT32_MAX samples all packets and intermediate values sample
311 * intermediate fractions of packets. */
313 dpif_sflow_get_probability(const struct dpif_sflow *ds)
315 return ds->probability;
319 dpif_sflow_destroy(struct dpif_sflow *ds)
322 struct dpif_sflow_port *dsp, *next;
324 route_table_unregister();
325 dpif_sflow_clear(ds);
326 HMAP_FOR_EACH_SAFE (dsp, next, hmap_node, &ds->ports) {
327 dpif_sflow_del_port__(ds, dsp);
329 hmap_destroy(&ds->ports);
335 dpif_sflow_add_poller(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
337 SFLPoller *poller = sfl_agent_addPoller(ds->sflow_agent, &dsp->dsi, ds,
338 sflow_agent_get_counters);
339 sfl_poller_set_sFlowCpInterval(poller, ds->options->polling_interval);
340 sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX);
341 sfl_poller_set_bridgePort(poller,
342 ofp_port_to_odp_port(dsp->ofport->ofp_port));
346 dpif_sflow_add_sampler(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
348 SFLSampler *sampler = sfl_agent_addSampler(ds->sflow_agent, &dsp->dsi);
349 sfl_sampler_set_sFlowFsPacketSamplingRate(sampler, ds->options->sampling_rate);
350 sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, ds->options->header_len);
351 sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX);
355 dpif_sflow_add_port(struct dpif_sflow *ds, struct ofport *ofport)
357 struct dpif_sflow_port *dsp;
358 uint16_t odp_port = ofp_port_to_odp_port(ofport->ofp_port);
361 dpif_sflow_del_port(ds, odp_port);
363 /* Add to table of ports. */
364 dsp = xmalloc(sizeof *dsp);
365 ifindex = netdev_get_ifindex(ofport->netdev);
367 ifindex = (ds->sflow_agent->subId << 16) + odp_port;
369 dsp->ofport = ofport;
370 SFL_DS_SET(dsp->dsi, 0, ifindex, 0);
371 hmap_insert(&ds->ports, &dsp->hmap_node, hash_int(odp_port, 0));
373 /* Add poller and sampler. */
374 if (ds->sflow_agent) {
375 dpif_sflow_add_poller(ds, dsp);
376 dpif_sflow_add_sampler(ds, dsp);
381 dpif_sflow_del_port__(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
383 if (ds->sflow_agent) {
384 sfl_agent_removePoller(ds->sflow_agent, &dsp->dsi);
385 sfl_agent_removeSampler(ds->sflow_agent, &dsp->dsi);
387 hmap_remove(&ds->ports, &dsp->hmap_node);
392 dpif_sflow_del_port(struct dpif_sflow *ds, uint16_t odp_port)
394 struct dpif_sflow_port *dsp = dpif_sflow_find_port(ds, odp_port);
396 dpif_sflow_del_port__(ds, dsp);
401 dpif_sflow_set_options(struct dpif_sflow *ds,
402 const struct ofproto_sflow_options *options)
404 struct dpif_sflow_port *dsp;
405 bool options_changed;
406 SFLReceiver *receiver;
410 if (sset_is_empty(&options->targets) || !options->sampling_rate) {
411 /* No point in doing any work if there are no targets or nothing to
413 dpif_sflow_clear(ds);
417 options_changed = (!ds->options
418 || !ofproto_sflow_options_equal(options, ds->options));
420 /* Configure collectors if options have changed or if we're shortchanged in
421 * collectors (which indicates that opening one or more of the configured
422 * collectors failed, so that we should retry). */
424 || collectors_count(ds->collectors) < sset_count(&options->targets)) {
425 collectors_destroy(ds->collectors);
426 collectors_create(&options->targets, SFL_DEFAULT_COLLECTOR_PORT,
428 if (ds->collectors == NULL) {
429 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
431 dpif_sflow_clear(ds);
436 /* Choose agent IP address and agent device (if not yet setup) */
437 if (!sflow_choose_agent_address(options->agent_device,
439 options->control_ip, &agentIP)) {
440 dpif_sflow_clear(ds);
444 /* Avoid reconfiguring if options didn't change. */
445 if (!options_changed) {
448 ofproto_sflow_options_destroy(ds->options);
449 ds->options = ofproto_sflow_options_clone(options);
452 VLOG_INFO("creating sFlow agent %d", options->sub_id);
453 if (ds->sflow_agent) {
454 sfl_agent_release(ds->sflow_agent);
456 ds->sflow_agent = xcalloc(1, sizeof *ds->sflow_agent);
458 sfl_agent_init(ds->sflow_agent,
461 now, /* Boot time. */
462 now, /* Current time. */
463 ds, /* Pointer supplied to callbacks. */
464 sflow_agent_alloc_cb,
466 sflow_agent_error_cb,
467 sflow_agent_send_packet_cb);
469 receiver = sfl_agent_addReceiver(ds->sflow_agent);
470 sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow");
471 sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff);
473 /* Set the sampling_rate down in the datapath. */
474 ds->probability = MAX(1, UINT32_MAX / ds->options->sampling_rate);
476 /* Add samplers and pollers for the currently known ports. */
477 HMAP_FOR_EACH (dsp, hmap_node, &ds->ports) {
478 dpif_sflow_add_poller(ds, dsp);
479 dpif_sflow_add_sampler(ds, dsp);
484 dpif_sflow_odp_port_to_ifindex(const struct dpif_sflow *ds,
487 struct dpif_sflow_port *dsp = dpif_sflow_find_port(ds, odp_port);
488 return dsp ? SFL_DS_INDEX(dsp->dsi) : 0;
492 dpif_sflow_received(struct dpif_sflow *ds, struct ofpbuf *packet,
493 const struct flow *flow,
494 const union user_action_cookie *cookie)
496 SFL_FLOW_SAMPLE_TYPE fs;
497 SFLFlow_sample_element hdrElem;
498 SFLSampled_header *header;
499 SFLFlow_sample_element switchElem;
501 struct dpif_sflow_port *in_dsp;
502 struct netdev_stats stats;
506 /* Build a flow sample */
507 memset(&fs, 0, sizeof fs);
509 in_dsp = dpif_sflow_find_port(ds, ofp_port_to_odp_port(flow->in_port));
513 fs.input = SFL_DS_INDEX(in_dsp->dsi);
515 error = ofproto_port_get_stats(in_dsp->ofport, &stats);
517 VLOG_WARN_RL(&rl, "netdev get-stats error %s", strerror(error));
520 fs.sample_pool = stats.rx_packets;
522 /* We are going to give it to the sampler that represents this input port.
523 * By implementing "ingress-only" sampling like this we ensure that we
524 * never have to offer the same sample to more than one sampler. */
525 sampler = sfl_agent_getSamplerByIfIndex(ds->sflow_agent, fs.input);
527 VLOG_WARN_RL(&rl, "no sampler for input ifIndex (%"PRIu32")",
532 /* Sampled header. */
533 memset(&hdrElem, 0, sizeof hdrElem);
534 hdrElem.tag = SFLFLOW_HEADER;
535 header = &hdrElem.flowType.header;
536 header->header_protocol = SFLHEADER_ETHERNET_ISO8023;
537 /* The frame_length should include the Ethernet FCS (4 bytes),
538 but it has already been stripped, so we need to add 4 here. */
539 header->frame_length = packet->size + 4;
540 /* Ethernet FCS stripped off. */
541 header->stripped = 4;
542 header->header_length = MIN(packet->size,
543 sampler->sFlowFsMaximumHeaderSize);
544 header->header_bytes = packet->data;
546 /* Add extended switch element. */
547 memset(&switchElem, 0, sizeof(switchElem));
548 switchElem.tag = SFLFLOW_EX_SWITCH;
549 switchElem.flowType.sw.src_vlan = vlan_tci_to_vid(flow->vlan_tci);
550 switchElem.flowType.sw.src_priority = vlan_tci_to_pcp(flow->vlan_tci);
552 /* Retrieve data from user_action_cookie. */
553 vlan_tci = cookie->sflow.vlan_tci;
554 switchElem.flowType.sw.dst_vlan = vlan_tci_to_vid(vlan_tci);
555 switchElem.flowType.sw.dst_priority = vlan_tci_to_pcp(vlan_tci);
557 fs.output = cookie->sflow.output;
559 /* Submit the flow sample to be encoded into the next datagram. */
560 SFLADD_ELEMENT(&fs, &hdrElem);
561 SFLADD_ELEMENT(&fs, &switchElem);
562 sfl_sampler_writeFlowSample(sampler, &fs);
566 dpif_sflow_run(struct dpif_sflow *ds)
568 if (dpif_sflow_is_enabled(ds)) {
569 time_t now = time_now();
571 if (now >= ds->next_tick) {
572 sfl_agent_tick(ds->sflow_agent, time_wall());
573 ds->next_tick = now + 1;
579 dpif_sflow_wait(struct dpif_sflow *ds)
581 if (dpif_sflow_is_enabled(ds)) {
582 poll_timer_wait_until(ds->next_tick * 1000LL);