2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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"
22 #include "collectors.h"
32 #include "poll-loop.h"
33 #include "sflow_api.h"
34 #include "socket-util.h"
38 VLOG_DEFINE_THIS_MODULE(sflow);
40 struct dpif_sflow_port {
41 struct hmap_node hmap_node; /* In struct dpif_sflow's "ports" hmap. */
42 struct netdev *netdev; /* Underlying network device, for stats. */
43 SFLDataSource_instance dsi; /* sFlow library's notion of port number. */
44 uint16_t odp_port; /* ODP port number. */
48 struct ofproto *ofproto;
49 struct collectors *collectors;
50 SFLAgent *sflow_agent;
51 struct ofproto_sflow_options *options;
54 size_t n_flood, n_all;
55 struct hmap ports; /* Contains "struct dpif_sflow_port"s. */
58 static void dpif_sflow_del_port__(struct dpif_sflow *,
59 struct dpif_sflow_port *);
61 #define RECEIVER_INDEX 1
63 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
66 nullable_string_is_equal(const char *a, const char *b)
68 return a ? b && !strcmp(a, b) : !b;
72 ofproto_sflow_options_equal(const struct ofproto_sflow_options *a,
73 const struct ofproto_sflow_options *b)
75 return (sset_equals(&a->targets, &b->targets)
76 && a->sampling_rate == b->sampling_rate
77 && a->polling_interval == b->polling_interval
78 && a->header_len == b->header_len
79 && a->sub_id == b->sub_id
80 && nullable_string_is_equal(a->agent_device, b->agent_device)
81 && nullable_string_is_equal(a->control_ip, b->control_ip));
84 static struct ofproto_sflow_options *
85 ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
87 struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
88 sset_clone(&new->targets, &old->targets);
89 new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
90 new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
95 ofproto_sflow_options_destroy(struct ofproto_sflow_options *options)
98 sset_destroy(&options->targets);
99 free(options->agent_device);
100 free(options->control_ip);
105 /* sFlow library callback to allocate memory. */
107 sflow_agent_alloc_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
110 return calloc(1, bytes);
113 /* sFlow library callback to free memory. */
115 sflow_agent_free_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
122 /* sFlow library callback to report error. */
124 sflow_agent_error_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
127 VLOG_WARN("sFlow agent error: %s", msg);
130 /* sFlow library callback to send datagram. */
132 sflow_agent_send_packet_cb(void *ds_, SFLAgent *agent OVS_UNUSED,
133 SFLReceiver *receiver OVS_UNUSED, u_char *pkt,
136 struct dpif_sflow *ds = ds_;
137 collectors_send(ds->collectors, pkt, pktLen);
140 static struct dpif_sflow_port *
141 dpif_sflow_find_port(const struct dpif_sflow *ds, uint16_t odp_port)
143 struct dpif_sflow_port *dsp;
145 HMAP_FOR_EACH_IN_BUCKET (dsp, hmap_node,
146 hash_int(odp_port, 0), &ds->ports) {
147 if (dsp->odp_port == odp_port) {
155 sflow_agent_get_counters(void *ds_, SFLPoller *poller,
156 SFL_COUNTERS_SAMPLE_TYPE *cs)
158 struct dpif_sflow *ds = ds_;
159 SFLCounters_sample_element elem;
160 struct dpif_sflow_port *dsp;
161 SFLIf_counters *counters;
162 struct netdev_stats stats;
163 enum netdev_flags flags;
166 dsp = dpif_sflow_find_port(ds, poller->bridgePort);
171 elem.tag = SFLCOUNTERS_GENERIC;
172 counters = &elem.counterBlock.generic;
173 counters->ifIndex = SFL_DS_INDEX(poller->dsi);
174 counters->ifType = 6;
175 if (!netdev_get_features(dsp->netdev, ¤t, NULL, NULL, NULL)) {
176 /* The values of ifDirection come from MAU MIB (RFC 2668): 0 = unknown,
177 1 = full-duplex, 2 = half-duplex, 3 = in, 4=out */
178 counters->ifSpeed = netdev_features_to_bps(current);
179 counters->ifDirection = (netdev_features_is_full_duplex(current)
182 counters->ifSpeed = 100000000;
183 counters->ifDirection = 0;
185 if (!netdev_get_flags(dsp->netdev, &flags) && flags & NETDEV_UP) {
186 counters->ifStatus = 1; /* ifAdminStatus up. */
187 if (netdev_get_carrier(dsp->netdev)) {
188 counters->ifStatus |= 2; /* ifOperStatus us. */
191 counters->ifStatus = 0; /* Down. */
195 1. Is the multicast counter filled in?
196 2. Does the multicast counter include broadcasts?
197 3. Does the rx_packets counter include multicasts/broadcasts?
199 netdev_get_stats(dsp->netdev, &stats);
200 counters->ifInOctets = stats.rx_bytes;
201 counters->ifInUcastPkts = stats.rx_packets;
202 counters->ifInMulticastPkts = stats.multicast;
203 counters->ifInBroadcastPkts = -1;
204 counters->ifInDiscards = stats.rx_dropped;
205 counters->ifInErrors = stats.rx_errors;
206 counters->ifInUnknownProtos = -1;
207 counters->ifOutOctets = stats.tx_bytes;
208 counters->ifOutUcastPkts = stats.tx_packets;
209 counters->ifOutMulticastPkts = -1;
210 counters->ifOutBroadcastPkts = -1;
211 counters->ifOutDiscards = stats.tx_dropped;
212 counters->ifOutErrors = stats.tx_errors;
213 counters->ifPromiscuousMode = 0;
215 SFLADD_ELEMENT(cs, &elem);
216 sfl_poller_writeCountersSample(poller, cs);
219 /* Obtains an address to use for the local sFlow agent and stores it into
220 * '*agent_addr'. Returns true if successful, false on failure.
222 * The sFlow agent address should be a local IP address that is persistent and
223 * reachable over the network, if possible. The IP address associated with
224 * 'agent_device' is used if it has one, and otherwise 'control_ip', the IP
225 * address used to talk to the controller. */
227 sflow_choose_agent_address(const char *agent_device, const char *control_ip,
228 SFLAddress *agent_addr)
232 memset(agent_addr, 0, sizeof *agent_addr);
233 agent_addr->type = SFLADDRESSTYPE_IP_V4;
236 struct netdev *netdev;
238 if (!netdev_open_default(agent_device, &netdev)) {
239 int error = netdev_get_in4(netdev, &in4, NULL);
240 netdev_close(netdev);
247 if (control_ip && !lookup_ip(control_ip, &in4)) {
251 VLOG_ERR("could not determine IP address for sFlow agent");
255 agent_addr->address.ip_v4.addr = (OVS_FORCE uint32_t) in4.s_addr;
260 dpif_sflow_clear(struct dpif_sflow *ds)
262 if (ds->sflow_agent) {
263 sfl_agent_release(ds->sflow_agent);
264 ds->sflow_agent = NULL;
266 collectors_destroy(ds->collectors);
267 ds->collectors = NULL;
268 ofproto_sflow_options_destroy(ds->options);
271 /* Turn off sampling to save CPU cycles. */
272 dpif_set_sflow_probability(ds->dpif, 0);
276 dpif_sflow_is_enabled(const struct dpif_sflow *ds)
278 return ds->collectors != NULL;
282 dpif_sflow_create(struct dpif *dpif)
284 struct dpif_sflow *ds;
286 ds = xcalloc(1, sizeof *ds);
288 ds->next_tick = time_now() + 1;
289 hmap_init(&ds->ports);
294 dpif_sflow_destroy(struct dpif_sflow *ds)
297 struct dpif_sflow_port *dsp, *next;
299 dpif_sflow_clear(ds);
300 HMAP_FOR_EACH_SAFE (dsp, next, hmap_node, &ds->ports) {
301 dpif_sflow_del_port__(ds, dsp);
303 hmap_destroy(&ds->ports);
309 dpif_sflow_add_poller(struct dpif_sflow *ds,
310 struct dpif_sflow_port *dsp, uint16_t odp_port)
312 SFLPoller *poller = sfl_agent_addPoller(ds->sflow_agent, &dsp->dsi, ds,
313 sflow_agent_get_counters);
314 sfl_poller_set_sFlowCpInterval(poller, ds->options->polling_interval);
315 sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX);
316 sfl_poller_set_bridgePort(poller, odp_port);
320 dpif_sflow_add_sampler(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
322 SFLSampler *sampler = sfl_agent_addSampler(ds->sflow_agent, &dsp->dsi);
323 sfl_sampler_set_sFlowFsPacketSamplingRate(sampler, ds->options->sampling_rate);
324 sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, ds->options->header_len);
325 sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX);
329 dpif_sflow_add_port(struct dpif_sflow *ds, uint16_t odp_port,
330 const char *netdev_name)
332 struct dpif_sflow_port *dsp;
333 struct netdev *netdev;
337 dpif_sflow_del_port(ds, odp_port);
339 /* Open network device. */
340 error = netdev_open_default(netdev_name, &netdev);
342 VLOG_WARN_RL(&rl, "failed to open network device \"%s\": %s",
343 netdev_name, strerror(error));
347 /* Add to table of ports. */
348 dsp = xmalloc(sizeof *dsp);
349 dsp->netdev = netdev;
350 ifindex = netdev_get_ifindex(netdev);
352 ifindex = (ds->sflow_agent->subId << 16) + odp_port;
354 SFL_DS_SET(dsp->dsi, 0, ifindex, 0);
355 dsp->odp_port = odp_port;
356 hmap_insert(&ds->ports, &dsp->hmap_node, hash_int(odp_port, 0));
358 /* Add poller and sampler. */
359 if (ds->sflow_agent) {
360 dpif_sflow_add_poller(ds, dsp, odp_port);
361 dpif_sflow_add_sampler(ds, dsp);
366 dpif_sflow_del_port__(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
368 if (ds->sflow_agent) {
369 sfl_agent_removePoller(ds->sflow_agent, &dsp->dsi);
370 sfl_agent_removeSampler(ds->sflow_agent, &dsp->dsi);
372 netdev_close(dsp->netdev);
373 hmap_remove(&ds->ports, &dsp->hmap_node);
378 dpif_sflow_del_port(struct dpif_sflow *ds, uint16_t odp_port)
380 struct dpif_sflow_port *dsp = dpif_sflow_find_port(ds, odp_port);
382 dpif_sflow_del_port__(ds, dsp);
387 dpif_sflow_set_options(struct dpif_sflow *ds,
388 const struct ofproto_sflow_options *options)
390 struct dpif_sflow_port *dsp;
391 bool options_changed;
392 SFLReceiver *receiver;
396 if (sset_is_empty(&options->targets) || !options->sampling_rate) {
397 /* No point in doing any work if there are no targets or nothing to
399 dpif_sflow_clear(ds);
403 options_changed = (!ds->options
404 || !ofproto_sflow_options_equal(options, ds->options));
406 /* Configure collectors if options have changed or if we're shortchanged in
407 * collectors (which indicates that opening one or more of the configured
408 * collectors failed, so that we should retry). */
410 || collectors_count(ds->collectors) < sset_count(&options->targets)) {
411 collectors_destroy(ds->collectors);
412 collectors_create(&options->targets, SFL_DEFAULT_COLLECTOR_PORT,
414 if (ds->collectors == NULL) {
415 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
417 dpif_sflow_clear(ds);
422 /* Avoid reconfiguring if options didn't change. */
423 if (!options_changed) {
426 ofproto_sflow_options_destroy(ds->options);
427 ds->options = ofproto_sflow_options_clone(options);
429 /* Choose agent IP address. */
430 if (!sflow_choose_agent_address(options->agent_device,
431 options->control_ip, &agentIP)) {
432 dpif_sflow_clear(ds);
437 VLOG_INFO("creating sFlow agent %d", options->sub_id);
438 if (ds->sflow_agent) {
439 sfl_agent_release(ds->sflow_agent);
441 ds->sflow_agent = xcalloc(1, sizeof *ds->sflow_agent);
443 sfl_agent_init(ds->sflow_agent,
446 now, /* Boot time. */
447 now, /* Current time. */
448 ds, /* Pointer supplied to callbacks. */
449 sflow_agent_alloc_cb,
451 sflow_agent_error_cb,
452 sflow_agent_send_packet_cb);
454 receiver = sfl_agent_addReceiver(ds->sflow_agent);
455 sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow");
456 sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff);
458 /* Set the sampling_rate down in the datapath. */
459 dpif_set_sflow_probability(ds->dpif,
460 MAX(1, UINT32_MAX / options->sampling_rate));
462 /* Add samplers and pollers for the currently known ports. */
463 HMAP_FOR_EACH (dsp, hmap_node, &ds->ports) {
464 dpif_sflow_add_poller(ds, dsp, dsp->odp_port);
465 dpif_sflow_add_sampler(ds, dsp);
470 dpif_sflow_odp_port_to_ifindex(const struct dpif_sflow *ds,
473 struct dpif_sflow_port *dsp = dpif_sflow_find_port(ds, odp_port);
474 return dsp ? SFL_DS_INDEX(dsp->dsi) : 0;
478 dpif_sflow_received(struct dpif_sflow *ds, const struct dpif_upcall *upcall,
479 const struct flow *flow)
481 SFL_FLOW_SAMPLE_TYPE fs;
482 SFLFlow_sample_element hdrElem;
483 SFLSampled_header *header;
484 SFLFlow_sample_element switchElem;
490 /* Build a flow sample */
491 memset(&fs, 0, sizeof fs);
492 fs.input = dpif_sflow_odp_port_to_ifindex(ds, flow->in_port);
493 fs.output = 0; /* Filled in correctly below. */
494 fs.sample_pool = upcall->sample_pool;
496 /* We are going to give it to the sampler that represents this input port.
497 * By implementing "ingress-only" sampling like this we ensure that we
498 * never have to offer the same sample to more than one sampler. */
499 sampler = sfl_agent_getSamplerByIfIndex(ds->sflow_agent, fs.input);
501 VLOG_WARN_RL(&rl, "no sampler for input ifIndex (%"PRIu32")",
506 /* Sampled header. */
507 memset(&hdrElem, 0, sizeof hdrElem);
508 hdrElem.tag = SFLFLOW_HEADER;
509 header = &hdrElem.flowType.header;
510 header->header_protocol = SFLHEADER_ETHERNET_ISO8023;
511 /* The frame_length should include the Ethernet FCS (4 bytes),
512 but it has already been stripped, so we need to add 4 here. */
513 header->frame_length = upcall->packet->size + 4;
514 /* Ethernet FCS stripped off. */
515 header->stripped = 4;
516 header->header_length = MIN(upcall->packet->size,
517 sampler->sFlowFsMaximumHeaderSize);
518 header->header_bytes = upcall->packet->data;
520 /* Add extended switch element. */
521 memset(&switchElem, 0, sizeof(switchElem));
522 switchElem.tag = SFLFLOW_EX_SWITCH;
523 switchElem.flowType.sw.src_vlan = vlan_tci_to_vid(flow->vlan_tci);
524 switchElem.flowType.sw.src_priority = vlan_tci_to_pcp(flow->vlan_tci);
525 /* Initialize the output VLAN and priority to be the same as the input,
526 but these fields can be overriden below if affected by an action. */
527 switchElem.flowType.sw.dst_vlan = switchElem.flowType.sw.src_vlan;
528 switchElem.flowType.sw.dst_priority = switchElem.flowType.sw.src_priority;
530 /* Figure out the output ports. */
532 NL_ATTR_FOR_EACH_UNSAFE (a, left, upcall->actions, upcall->actions_len) {
535 switch (nl_attr_type(a)) {
536 case ODP_ACTION_ATTR_OUTPUT:
537 fs.output = dpif_sflow_odp_port_to_ifindex(ds, nl_attr_get_u32(a));
541 case ODP_ACTION_ATTR_SET_DL_TCI:
542 tci = nl_attr_get_be16(a);
543 switchElem.flowType.sw.dst_vlan = vlan_tci_to_vid(tci);
544 switchElem.flowType.sw.dst_priority = vlan_tci_to_pcp(tci);
552 /* Set output port, as defined by http://www.sflow.org/sflow_version_5.txt
553 (search for "Input/output port information"). */
555 /* This value indicates that the packet was dropped for an unknown
557 fs.output = 0x40000000 | 256;
558 } else if (n_outputs > 1 || !fs.output) {
559 /* Setting the high bit means "multiple output ports". */
560 fs.output = 0x80000000 | n_outputs;
563 /* Submit the flow sample to be encoded into the next datagram. */
564 SFLADD_ELEMENT(&fs, &hdrElem);
565 SFLADD_ELEMENT(&fs, &switchElem);
566 sfl_sampler_writeFlowSample(sampler, &fs);
570 dpif_sflow_run(struct dpif_sflow *ds)
572 if (dpif_sflow_is_enabled(ds)) {
573 time_t now = time_now();
574 if (now >= ds->next_tick) {
575 sfl_agent_tick(ds->sflow_agent, time_wall());
576 ds->next_tick = now + 1;
582 dpif_sflow_wait(struct dpif_sflow *ds)
584 if (dpif_sflow_is_enabled(ds)) {
585 poll_timer_wait_until(ds->next_tick * 1000LL);