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"
37 #include "lib/odp-util.h"
39 VLOG_DEFINE_THIS_MODULE(sflow);
41 struct dpif_sflow_port {
42 struct hmap_node hmap_node; /* In struct dpif_sflow's "ports" hmap. */
43 struct netdev *netdev; /* Underlying network device, for stats. */
44 SFLDataSource_instance dsi; /* sFlow library's notion of port number. */
45 uint16_t odp_port; /* Datapath port number. */
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. */
59 static void dpif_sflow_del_port__(struct dpif_sflow *,
60 struct dpif_sflow_port *);
62 #define RECEIVER_INDEX 1
64 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
67 nullable_string_is_equal(const char *a, const char *b)
69 return a ? b && !strcmp(a, b) : !b;
73 ofproto_sflow_options_equal(const struct ofproto_sflow_options *a,
74 const struct ofproto_sflow_options *b)
76 return (sset_equals(&a->targets, &b->targets)
77 && a->sampling_rate == b->sampling_rate
78 && a->polling_interval == b->polling_interval
79 && a->header_len == b->header_len
80 && a->sub_id == b->sub_id
81 && nullable_string_is_equal(a->agent_device, b->agent_device)
82 && nullable_string_is_equal(a->control_ip, b->control_ip));
85 static struct ofproto_sflow_options *
86 ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
88 struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
89 sset_clone(&new->targets, &old->targets);
90 new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
91 new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
96 ofproto_sflow_options_destroy(struct ofproto_sflow_options *options)
99 sset_destroy(&options->targets);
100 free(options->agent_device);
101 free(options->control_ip);
106 /* sFlow library callback to allocate memory. */
108 sflow_agent_alloc_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
111 return calloc(1, bytes);
114 /* sFlow library callback to free memory. */
116 sflow_agent_free_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
123 /* sFlow library callback to report error. */
125 sflow_agent_error_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
128 VLOG_WARN("sFlow agent error: %s", msg);
131 /* sFlow library callback to send datagram. */
133 sflow_agent_send_packet_cb(void *ds_, SFLAgent *agent OVS_UNUSED,
134 SFLReceiver *receiver OVS_UNUSED, u_char *pkt,
137 struct dpif_sflow *ds = ds_;
138 collectors_send(ds->collectors, pkt, pktLen);
141 static struct dpif_sflow_port *
142 dpif_sflow_find_port(const struct dpif_sflow *ds, uint16_t odp_port)
144 struct dpif_sflow_port *dsp;
146 HMAP_FOR_EACH_IN_BUCKET (dsp, hmap_node,
147 hash_int(odp_port, 0), &ds->ports) {
148 if (dsp->odp_port == odp_port) {
156 sflow_agent_get_counters(void *ds_, SFLPoller *poller,
157 SFL_COUNTERS_SAMPLE_TYPE *cs)
159 struct dpif_sflow *ds = ds_;
160 SFLCounters_sample_element elem;
161 struct dpif_sflow_port *dsp;
162 SFLIf_counters *counters;
163 struct netdev_stats stats;
164 enum netdev_flags flags;
167 dsp = dpif_sflow_find_port(ds, poller->bridgePort);
172 elem.tag = SFLCOUNTERS_GENERIC;
173 counters = &elem.counterBlock.generic;
174 counters->ifIndex = SFL_DS_INDEX(poller->dsi);
175 counters->ifType = 6;
176 if (!netdev_get_features(dsp->netdev, ¤t, NULL, NULL, NULL)) {
177 /* The values of ifDirection come from MAU MIB (RFC 2668): 0 = unknown,
178 1 = full-duplex, 2 = half-duplex, 3 = in, 4=out */
179 counters->ifSpeed = netdev_features_to_bps(current);
180 counters->ifDirection = (netdev_features_is_full_duplex(current)
183 counters->ifSpeed = 100000000;
184 counters->ifDirection = 0;
186 if (!netdev_get_flags(dsp->netdev, &flags) && flags & NETDEV_UP) {
187 counters->ifStatus = 1; /* ifAdminStatus up. */
188 if (netdev_get_carrier(dsp->netdev)) {
189 counters->ifStatus |= 2; /* ifOperStatus us. */
192 counters->ifStatus = 0; /* Down. */
196 1. Is the multicast counter filled in?
197 2. Does the multicast counter include broadcasts?
198 3. Does the rx_packets counter include multicasts/broadcasts?
200 netdev_get_stats(dsp->netdev, &stats);
201 counters->ifInOctets = stats.rx_bytes;
202 counters->ifInUcastPkts = stats.rx_packets;
203 counters->ifInMulticastPkts = stats.multicast;
204 counters->ifInBroadcastPkts = -1;
205 counters->ifInDiscards = stats.rx_dropped;
206 counters->ifInErrors = stats.rx_errors;
207 counters->ifInUnknownProtos = -1;
208 counters->ifOutOctets = stats.tx_bytes;
209 counters->ifOutUcastPkts = stats.tx_packets;
210 counters->ifOutMulticastPkts = -1;
211 counters->ifOutBroadcastPkts = -1;
212 counters->ifOutDiscards = stats.tx_dropped;
213 counters->ifOutErrors = stats.tx_errors;
214 counters->ifPromiscuousMode = 0;
216 SFLADD_ELEMENT(cs, &elem);
217 sfl_poller_writeCountersSample(poller, cs);
220 /* Obtains an address to use for the local sFlow agent and stores it into
221 * '*agent_addr'. Returns true if successful, false on failure.
223 * The sFlow agent address should be a local IP address that is persistent and
224 * reachable over the network, if possible. The IP address associated with
225 * 'agent_device' is used if it has one, and otherwise 'control_ip', the IP
226 * address used to talk to the controller. */
228 sflow_choose_agent_address(const char *agent_device, const char *control_ip,
229 SFLAddress *agent_addr)
233 memset(agent_addr, 0, sizeof *agent_addr);
234 agent_addr->type = SFLADDRESSTYPE_IP_V4;
237 struct netdev *netdev;
239 if (!netdev_open(agent_device, "system", &netdev)) {
240 int error = netdev_get_in4(netdev, &in4, NULL);
241 netdev_close(netdev);
248 if (control_ip && !lookup_ip(control_ip, &in4)) {
252 VLOG_ERR("could not determine IP address for sFlow agent");
256 agent_addr->address.ip_v4.addr = (OVS_FORCE uint32_t) in4.s_addr;
261 dpif_sflow_clear(struct dpif_sflow *ds)
263 if (ds->sflow_agent) {
264 sfl_agent_release(ds->sflow_agent);
265 ds->sflow_agent = NULL;
267 collectors_destroy(ds->collectors);
268 ds->collectors = NULL;
269 ofproto_sflow_options_destroy(ds->options);
272 /* Turn off sampling to save CPU cycles. */
277 dpif_sflow_is_enabled(const struct dpif_sflow *ds)
279 return ds->collectors != NULL;
283 dpif_sflow_create(struct dpif *dpif)
285 struct dpif_sflow *ds;
287 ds = xcalloc(1, sizeof *ds);
289 ds->next_tick = time_now() + 1;
290 hmap_init(&ds->ports);
295 /* 32-bit fraction of packets to sample with. A value of 0 samples no packets,
296 * a value of %UINT32_MAX samples all packets and intermediate values sample
297 * intermediate fractions of packets. */
299 dpif_sflow_get_probability(const struct dpif_sflow *ds)
301 return ds->probability;
305 dpif_sflow_destroy(struct dpif_sflow *ds)
308 struct dpif_sflow_port *dsp, *next;
310 dpif_sflow_clear(ds);
311 HMAP_FOR_EACH_SAFE (dsp, next, hmap_node, &ds->ports) {
312 dpif_sflow_del_port__(ds, dsp);
314 hmap_destroy(&ds->ports);
320 dpif_sflow_add_poller(struct dpif_sflow *ds,
321 struct dpif_sflow_port *dsp, uint16_t odp_port)
323 SFLPoller *poller = sfl_agent_addPoller(ds->sflow_agent, &dsp->dsi, ds,
324 sflow_agent_get_counters);
325 sfl_poller_set_sFlowCpInterval(poller, ds->options->polling_interval);
326 sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX);
327 sfl_poller_set_bridgePort(poller, odp_port);
331 dpif_sflow_add_sampler(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
333 SFLSampler *sampler = sfl_agent_addSampler(ds->sflow_agent, &dsp->dsi);
334 sfl_sampler_set_sFlowFsPacketSamplingRate(sampler, ds->options->sampling_rate);
335 sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, ds->options->header_len);
336 sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX);
340 dpif_sflow_add_port(struct dpif_sflow *ds, uint16_t odp_port,
341 const char *netdev_name)
343 struct dpif_sflow_port *dsp;
344 struct netdev *netdev;
348 dpif_sflow_del_port(ds, odp_port);
350 /* Open network device. */
351 error = netdev_open(netdev_name, "system", &netdev);
353 VLOG_WARN_RL(&rl, "failed to open network device \"%s\": %s",
354 netdev_name, strerror(error));
358 /* Add to table of ports. */
359 dsp = xmalloc(sizeof *dsp);
360 dsp->netdev = netdev;
361 ifindex = netdev_get_ifindex(netdev);
363 ifindex = (ds->sflow_agent->subId << 16) + odp_port;
365 SFL_DS_SET(dsp->dsi, 0, ifindex, 0);
366 dsp->odp_port = odp_port;
367 hmap_insert(&ds->ports, &dsp->hmap_node, hash_int(odp_port, 0));
369 /* Add poller and sampler. */
370 if (ds->sflow_agent) {
371 dpif_sflow_add_poller(ds, dsp, odp_port);
372 dpif_sflow_add_sampler(ds, dsp);
377 dpif_sflow_del_port__(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
379 if (ds->sflow_agent) {
380 sfl_agent_removePoller(ds->sflow_agent, &dsp->dsi);
381 sfl_agent_removeSampler(ds->sflow_agent, &dsp->dsi);
383 netdev_close(dsp->netdev);
384 hmap_remove(&ds->ports, &dsp->hmap_node);
389 dpif_sflow_del_port(struct dpif_sflow *ds, uint16_t odp_port)
391 struct dpif_sflow_port *dsp = dpif_sflow_find_port(ds, odp_port);
393 dpif_sflow_del_port__(ds, dsp);
398 dpif_sflow_set_options(struct dpif_sflow *ds,
399 const struct ofproto_sflow_options *options)
401 struct dpif_sflow_port *dsp;
402 bool options_changed;
403 SFLReceiver *receiver;
407 if (sset_is_empty(&options->targets) || !options->sampling_rate) {
408 /* No point in doing any work if there are no targets or nothing to
410 dpif_sflow_clear(ds);
414 options_changed = (!ds->options
415 || !ofproto_sflow_options_equal(options, ds->options));
417 /* Configure collectors if options have changed or if we're shortchanged in
418 * collectors (which indicates that opening one or more of the configured
419 * collectors failed, so that we should retry). */
421 || collectors_count(ds->collectors) < sset_count(&options->targets)) {
422 collectors_destroy(ds->collectors);
423 collectors_create(&options->targets, SFL_DEFAULT_COLLECTOR_PORT,
425 if (ds->collectors == NULL) {
426 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
428 dpif_sflow_clear(ds);
433 /* Avoid reconfiguring if options didn't change. */
434 if (!options_changed) {
437 ofproto_sflow_options_destroy(ds->options);
438 ds->options = ofproto_sflow_options_clone(options);
440 /* Choose agent IP address. */
441 if (!sflow_choose_agent_address(options->agent_device,
442 options->control_ip, &agentIP)) {
443 dpif_sflow_clear(ds);
448 VLOG_INFO("creating sFlow agent %d", options->sub_id);
449 if (ds->sflow_agent) {
450 sfl_agent_release(ds->sflow_agent);
452 ds->sflow_agent = xcalloc(1, sizeof *ds->sflow_agent);
454 sfl_agent_init(ds->sflow_agent,
457 now, /* Boot time. */
458 now, /* Current time. */
459 ds, /* Pointer supplied to callbacks. */
460 sflow_agent_alloc_cb,
462 sflow_agent_error_cb,
463 sflow_agent_send_packet_cb);
465 receiver = sfl_agent_addReceiver(ds->sflow_agent);
466 sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow");
467 sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff);
469 /* Set the sampling_rate down in the datapath. */
470 ds->probability = MAX(1, UINT32_MAX / ds->options->sampling_rate);
472 /* Add samplers and pollers for the currently known ports. */
473 HMAP_FOR_EACH (dsp, hmap_node, &ds->ports) {
474 dpif_sflow_add_poller(ds, dsp, dsp->odp_port);
475 dpif_sflow_add_sampler(ds, dsp);
480 dpif_sflow_odp_port_to_ifindex(const struct dpif_sflow *ds,
483 struct dpif_sflow_port *dsp = dpif_sflow_find_port(ds, odp_port);
484 return dsp ? SFL_DS_INDEX(dsp->dsi) : 0;
488 dpif_sflow_received(struct dpif_sflow *ds, struct ofpbuf *packet,
489 const struct flow *flow,
490 const struct user_action_cookie *cookie)
492 SFL_FLOW_SAMPLE_TYPE fs;
493 SFLFlow_sample_element hdrElem;
494 SFLSampled_header *header;
495 SFLFlow_sample_element switchElem;
497 struct dpif_sflow_port *in_dsp;
498 struct netdev_stats stats;
501 /* Build a flow sample */
502 memset(&fs, 0, sizeof fs);
504 in_dsp = dpif_sflow_find_port(ds, ofp_port_to_odp_port(flow->in_port));
508 fs.input = SFL_DS_INDEX(in_dsp->dsi);
510 error = netdev_get_stats(in_dsp->netdev, &stats);
512 VLOG_WARN_RL(&rl, "netdev get-stats error %s", strerror(error));
515 fs.sample_pool = stats.rx_packets;
517 /* We are going to give it to the sampler that represents this input port.
518 * By implementing "ingress-only" sampling like this we ensure that we
519 * never have to offer the same sample to more than one sampler. */
520 sampler = sfl_agent_getSamplerByIfIndex(ds->sflow_agent, fs.input);
522 VLOG_WARN_RL(&rl, "no sampler for input ifIndex (%"PRIu32")",
527 /* Sampled header. */
528 memset(&hdrElem, 0, sizeof hdrElem);
529 hdrElem.tag = SFLFLOW_HEADER;
530 header = &hdrElem.flowType.header;
531 header->header_protocol = SFLHEADER_ETHERNET_ISO8023;
532 /* The frame_length should include the Ethernet FCS (4 bytes),
533 but it has already been stripped, so we need to add 4 here. */
534 header->frame_length = packet->size + 4;
535 /* Ethernet FCS stripped off. */
536 header->stripped = 4;
537 header->header_length = MIN(packet->size,
538 sampler->sFlowFsMaximumHeaderSize);
539 header->header_bytes = packet->data;
541 /* Add extended switch element. */
542 memset(&switchElem, 0, sizeof(switchElem));
543 switchElem.tag = SFLFLOW_EX_SWITCH;
544 switchElem.flowType.sw.src_vlan = vlan_tci_to_vid(flow->vlan_tci);
545 switchElem.flowType.sw.src_priority = vlan_tci_to_pcp(flow->vlan_tci);
547 /* Retrieve data from user_action_cookie. */
548 switchElem.flowType.sw.dst_vlan = vlan_tci_to_vid(cookie->vlan_tci);
549 switchElem.flowType.sw.dst_priority = vlan_tci_to_pcp(cookie->vlan_tci);
551 /* Set output port, as defined by http://www.sflow.org/sflow_version_5.txt
552 (search for "Input/output port information"). */
553 if (!cookie->n_output) {
554 /* This value indicates that the packet was dropped for an unknown
556 fs.output = 0x40000000 | 256;
557 } else if (cookie->n_output > 1 || !cookie->data) {
558 /* Setting the high bit means "multiple output ports". */
559 fs.output = 0x80000000 | cookie->n_output;
561 fs.output = cookie->data;
564 /* Submit the flow sample to be encoded into the next datagram. */
565 SFLADD_ELEMENT(&fs, &hdrElem);
566 SFLADD_ELEMENT(&fs, &switchElem);
567 sfl_sampler_writeFlowSample(sampler, &fs);
571 dpif_sflow_run(struct dpif_sflow *ds)
573 if (dpif_sflow_is_enabled(ds)) {
574 time_t now = time_now();
575 if (now >= ds->next_tick) {
576 sfl_agent_tick(ds->sflow_agent, time_wall());
577 ds->next_tick = now + 1;
583 dpif_sflow_wait(struct dpif_sflow *ds)
585 if (dpif_sflow_is_enabled(ds)) {
586 poll_timer_wait_until(ds->next_tick * 1000LL);