2 * Copyright (c) 2009, 2010 InMon Corp.
3 * Copyright (c) 2009 Nicira Networks.
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-sflow.h"
22 #include "collectors.h"
28 #include "poll-loop.h"
29 #include "port-array.h"
30 #include "sflow_api.h"
31 #include "socket-util.h"
34 #define THIS_MODULE VLM_sflow
37 struct ofproto_sflow_port {
38 struct netdev *netdev; /* Underlying network device, for stats. */
39 SFLDataSource_instance dsi; /* sFlow library's notion of port number. */
42 struct ofproto_sflow {
43 struct ofproto *ofproto;
44 struct collectors *collectors;
45 SFLAgent *sflow_agent;
46 struct ofproto_sflow_options *options;
49 size_t n_flood, n_all;
50 struct port_array ports; /* Indexed by ODP port number. */
53 #define RECEIVER_INDEX 1
55 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
58 nullable_string_is_equal(const char *a, const char *b)
60 return a ? b && !strcmp(a, b) : !b;
64 ofproto_sflow_options_equal(const struct ofproto_sflow_options *a,
65 const struct ofproto_sflow_options *b)
67 return (svec_equal(&a->targets, &b->targets)
68 && a->sampling_rate == b->sampling_rate
69 && a->polling_interval == b->polling_interval
70 && a->header_len == b->header_len
71 && a->sub_id == b->sub_id
72 && nullable_string_is_equal(a->agent_device, b->agent_device)
73 && nullable_string_is_equal(a->control_ip, b->control_ip));
76 static struct ofproto_sflow_options *
77 ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
79 struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
80 svec_clone(&new->targets, &old->targets);
81 new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
82 new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
87 ofproto_sflow_options_destroy(struct ofproto_sflow_options *options)
90 svec_destroy(&options->targets);
91 free(options->agent_device);
92 free(options->control_ip);
97 /* sFlow library callback to allocate memory. */
99 sflow_agent_alloc_cb(void *magic UNUSED, SFLAgent *agent UNUSED, size_t bytes)
101 return calloc(1, bytes);
104 /* sFlow library callback to free memory. */
106 sflow_agent_free_cb(void *magic UNUSED, SFLAgent *agent UNUSED, void *obj)
112 /* sFlow library callback to report error. */
114 sflow_agent_error_cb(void *magic UNUSED, SFLAgent *agent UNUSED, char *msg)
116 VLOG_WARN("sFlow agent error: %s", msg);
119 /* sFlow library callback to send datagram. */
121 sflow_agent_send_packet_cb(void *os_, SFLAgent *agent UNUSED,
122 SFLReceiver *receiver UNUSED, u_char *pkt,
125 struct ofproto_sflow *os = os_;
126 collectors_send(os->collectors, pkt, pktLen);
130 sflow_agent_get_counters(void *os_, SFLPoller *poller,
131 SFL_COUNTERS_SAMPLE_TYPE *cs)
133 struct ofproto_sflow *os = os_;
134 SFLCounters_sample_element elem;
135 struct ofproto_sflow_port *osp;
136 SFLIf_counters *counters;
137 struct netdev_stats stats;
138 enum netdev_flags flags;
141 osp = port_array_get(&os->ports, poller->bridgePort);
146 elem.tag = SFLCOUNTERS_GENERIC;
147 counters = &elem.counterBlock.generic;
148 counters->ifIndex = SFL_DS_INDEX(poller->dsi);
149 counters->ifType = 6;
150 if (!netdev_get_features(osp->netdev, ¤t, NULL, NULL, NULL)) {
151 /* The values of ifDirection come from MAU MIB (RFC 2668): 0 = unknown,
152 1 = full-duplex, 2 = half-duplex, 3 = in, 4=out */
153 counters->ifSpeed = netdev_features_to_bps(current);
154 counters->ifDirection = (netdev_features_is_full_duplex(current)
157 counters->ifSpeed = 100000000;
158 counters->ifDirection = 0;
160 if (!netdev_get_flags(osp->netdev, &flags) && flags & NETDEV_UP) {
163 counters->ifStatus = 1; /* ifAdminStatus up. */
164 if (!netdev_get_carrier(osp->netdev, &carrier) && carrier) {
165 counters->ifStatus |= 2; /* ifOperStatus us. */
168 counters->ifStatus = 0; /* Down. */
172 1. Is the multicast counter filled in?
173 2. Does the multicast counter include broadcasts?
174 3. Does the rx_packets counter include multicasts/broadcasts?
176 netdev_get_stats(osp->netdev, &stats);
177 counters->ifInOctets = stats.rx_bytes;
178 counters->ifInUcastPkts = stats.rx_packets;
179 counters->ifInMulticastPkts = stats.multicast;
180 counters->ifInBroadcastPkts = -1;
181 counters->ifInDiscards = stats.rx_dropped;
182 counters->ifInErrors = stats.rx_errors;
183 counters->ifInUnknownProtos = -1;
184 counters->ifOutOctets = stats.tx_bytes;
185 counters->ifOutUcastPkts = stats.tx_packets;
186 counters->ifOutMulticastPkts = -1;
187 counters->ifOutBroadcastPkts = -1;
188 counters->ifOutDiscards = stats.tx_dropped;
189 counters->ifOutErrors = stats.tx_errors;
190 counters->ifPromiscuousMode = 0;
192 SFLADD_ELEMENT(cs, &elem);
193 sfl_poller_writeCountersSample(poller, cs);
196 /* Obtains an address to use for the local sFlow agent and stores it into
197 * '*agent_addr'. Returns true if successful, false on failure.
199 * The sFlow agent address should be a local IP address that is persistent and
200 * reachable over the network, if possible. The IP address associated with
201 * 'agent_device' is used if it has one, and otherwise 'control_ip', the IP
202 * address used to talk to the controller. */
204 sflow_choose_agent_address(const char *agent_device, const char *control_ip,
205 SFLAddress *agent_addr)
209 memset(agent_addr, 0, sizeof *agent_addr);
210 agent_addr->type = SFLADDRESSTYPE_IP_V4;
213 struct netdev *netdev;
215 if (!netdev_open_default(agent_device, &netdev)) {
216 int error = netdev_get_in4(netdev, &in4, NULL);
217 netdev_close(netdev);
224 if (control_ip && !lookup_ip(control_ip, &in4)) {
228 VLOG_ERR("could not determine IP address for sFlow agent");
232 agent_addr->address.ip_v4.addr = in4.s_addr;
237 ofproto_sflow_clear(struct ofproto_sflow *os)
239 struct ofproto_sflow_port *osp;
240 unsigned int odp_port;
242 if (os->sflow_agent) {
243 sfl_agent_release(os->sflow_agent);
244 os->sflow_agent = NULL;
246 collectors_destroy(os->collectors);
247 os->collectors = NULL;
248 ofproto_sflow_options_destroy(os->options);
251 PORT_ARRAY_FOR_EACH (osp, &os->ports, odp_port) {
252 ofproto_sflow_del_port(os, odp_port);
254 port_array_clear(&os->ports);
256 /* Turn off sampling to save CPU cycles. */
257 dpif_set_sflow_probability(os->dpif, 0);
261 ofproto_sflow_is_enabled(const struct ofproto_sflow *os)
263 return os->collectors != NULL;
266 struct ofproto_sflow *
267 ofproto_sflow_create(struct dpif *dpif)
269 struct ofproto_sflow *os;
271 os = xcalloc(1, sizeof *os);
273 os->next_tick = time_now() + 1;
274 port_array_init(&os->ports);
279 ofproto_sflow_destroy(struct ofproto_sflow *os)
282 ofproto_sflow_clear(os);
283 port_array_destroy(&os->ports);
289 ofproto_sflow_add_poller(struct ofproto_sflow *os,
290 struct ofproto_sflow_port *osp, uint16_t odp_port)
292 SFLPoller *poller = sfl_agent_addPoller(os->sflow_agent, &osp->dsi, os,
293 sflow_agent_get_counters);
294 sfl_poller_set_sFlowCpInterval(poller, os->options->polling_interval);
295 sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX);
296 sfl_poller_set_bridgePort(poller, odp_port);
300 ofproto_sflow_add_sampler(struct ofproto_sflow *os,
301 struct ofproto_sflow_port *osp,
302 u_int32_t sampling_rate, u_int32_t header_len)
304 SFLSampler *sampler = sfl_agent_addSampler(os->sflow_agent, &osp->dsi);
305 sfl_sampler_set_sFlowFsPacketSamplingRate(sampler, sampling_rate);
306 sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, header_len);
307 sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX);
311 ofproto_sflow_add_port(struct ofproto_sflow *os, uint16_t odp_port,
312 const char *netdev_name)
314 struct ofproto_sflow_port *osp;
315 struct netdev *netdev;
319 ofproto_sflow_del_port(os, odp_port);
321 /* Open network device. */
322 error = netdev_open_default(netdev_name, &netdev);
324 VLOG_WARN_RL(&rl, "failed to open network device \"%s\": %s",
325 netdev_name, strerror(error));
329 /* Add to table of ports. */
330 osp = xmalloc(sizeof *osp);
331 osp->netdev = netdev;
332 ifindex = netdev_get_ifindex(netdev);
334 ifindex = (os->sflow_agent->subId << 16) + odp_port;
336 SFL_DS_SET(osp->dsi, 0, ifindex, 0);
337 port_array_set(&os->ports, odp_port, osp);
340 if (os->sflow_agent) {
341 ofproto_sflow_add_poller(os, osp, odp_port);
346 ofproto_sflow_del_port(struct ofproto_sflow *os, uint16_t odp_port)
348 struct ofproto_sflow_port *osp = port_array_get(&os->ports, odp_port);
350 if (os->sflow_agent) {
351 sfl_agent_removePoller(os->sflow_agent, &osp->dsi);
352 sfl_agent_removeSampler(os->sflow_agent, &osp->dsi);
354 netdev_close(osp->netdev);
356 port_array_set(&os->ports, odp_port, NULL);
361 ofproto_sflow_set_options(struct ofproto_sflow *os,
362 const struct ofproto_sflow_options *options)
364 struct ofproto_sflow_port *osp;
365 bool options_changed;
366 SFLReceiver *receiver;
367 unsigned int odp_port;
371 if (!options->targets.n || !options->sampling_rate) {
372 /* No point in doing any work if there are no targets or nothing to
374 ofproto_sflow_clear(os);
378 options_changed = (!os->options
379 || !ofproto_sflow_options_equal(options, os->options));
381 /* Configure collectors if options have changed or if we're shortchanged in
382 * collectors (which indicates that opening one or more of the configured
383 * collectors failed, so that we should retry). */
385 || collectors_count(os->collectors) < options->targets.n) {
386 collectors_destroy(os->collectors);
387 collectors_create(&options->targets, SFL_DEFAULT_COLLECTOR_PORT,
389 if (os->collectors == NULL) {
390 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
392 ofproto_sflow_clear(os);
397 /* Avoid reconfiguring if options didn't change. */
398 if (!options_changed) {
401 ofproto_sflow_options_destroy(os->options);
402 os->options = ofproto_sflow_options_clone(options);
404 /* Choose agent IP address. */
405 if (!sflow_choose_agent_address(options->agent_device,
406 options->control_ip, &agentIP)) {
407 ofproto_sflow_clear(os);
412 VLOG_INFO("creating sFlow agent %d", options->sub_id);
413 if (os->sflow_agent) {
414 sfl_agent_release(os->sflow_agent);
416 os->sflow_agent = xcalloc(1, sizeof *os->sflow_agent);
418 sfl_agent_init(os->sflow_agent,
421 now, /* Boot time. */
422 now, /* Current time. */
423 os, /* Pointer supplied to callbacks. */
424 sflow_agent_alloc_cb,
426 sflow_agent_error_cb,
427 sflow_agent_send_packet_cb);
429 receiver = sfl_agent_addReceiver(os->sflow_agent);
430 sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow");
431 sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff);
433 /* Set the sampling_rate down in the datapath. */
434 dpif_set_sflow_probability(os->dpif,
435 MAX(1, UINT32_MAX / options->sampling_rate));
437 /* Add samplers and pollers for the currently known ports. */
438 PORT_ARRAY_FOR_EACH (osp, &os->ports, odp_port) {
439 ofproto_sflow_add_sampler(os, osp,
440 options->sampling_rate, options->header_len);
445 ofproto_sflow_odp_port_to_ifindex(const struct ofproto_sflow *os,
448 struct ofproto_sflow_port *osp = port_array_get(&os->ports, odp_port);
449 return osp ? SFL_DS_INDEX(osp->dsi) : 0;
453 ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg)
455 SFL_FLOW_SAMPLE_TYPE fs;
456 SFLFlow_sample_element hdrElem;
457 SFLSampled_header *header;
458 SFLFlow_sample_element switchElem;
460 const struct odp_sflow_sample_header *hdr;
461 const union odp_action *actions;
462 struct ofpbuf payload;
463 size_t n_actions, n_outputs;
468 /* Get odp_sflow_sample_header. */
469 min_size = sizeof *msg + sizeof *hdr;
470 if (min_size > msg->length) {
471 VLOG_WARN_RL(&rl, "sFlow packet too small (%"PRIu32" < %zu)",
472 msg->length, min_size);
475 hdr = (const struct odp_sflow_sample_header *) (msg + 1);
478 n_actions = hdr->n_actions;
479 if (n_actions > 65536 / sizeof *actions) {
480 VLOG_WARN_RL(&rl, "too many actions in sFlow packet (%zu > %zu)",
481 65536 / sizeof *actions, n_actions);
484 min_size += n_actions * sizeof *actions;
485 if (min_size > msg->length) {
486 VLOG_WARN_RL(&rl, "sFlow packet with %zu actions too small "
488 n_actions, msg->length, min_size);
491 actions = (const union odp_action *) (hdr + 1);
493 /* Get packet payload and extract flow. */
494 payload.data = (union odp_action *) (actions + n_actions);
495 payload.size = msg->length - min_size;
496 flow_extract(&payload, msg->port, &flow);
498 /* Build a flow sample */
499 memset(&fs, 0, sizeof fs);
500 fs.input = ofproto_sflow_odp_port_to_ifindex(os, msg->port);
501 fs.output = 0; /* Filled in correctly below. */
502 fs.sample_pool = hdr->sample_pool;
504 /* We are going to give it to the sampler that represents this input port.
505 * By implementing "ingress-only" sampling like this we ensure that we
506 * never have to offer the same sample to more than one sampler. */
507 sampler = sfl_agent_getSamplerByIfIndex(os->sflow_agent, fs.input);
509 VLOG_WARN_RL(&rl, "no sampler for input ifIndex (%"PRIu32")",
514 /* Sampled header. */
515 memset(&hdrElem, 0, sizeof hdrElem);
516 hdrElem.tag = SFLFLOW_HEADER;
517 header = &hdrElem.flowType.header;
518 header->header_protocol = SFLHEADER_ETHERNET_ISO8023;
519 header->frame_length = payload.size;
520 header->stripped = 4; /* Ethernet FCS stripped off. */
521 header->header_length = MIN(payload.size,
522 sampler->sFlowFsMaximumHeaderSize);
523 header->header_bytes = payload.data;
525 /* Add extended switch element. */
526 memset(&switchElem, 0, sizeof(switchElem));
527 switchElem.tag = SFLFLOW_EX_SWITCH;
528 switchElem.flowType.sw.src_vlan = ntohs(flow.dl_vlan);
529 switchElem.flowType.sw.src_priority = -1; /* XXX */
530 switchElem.flowType.sw.dst_vlan = -1; /* Filled in correctly below. */
531 switchElem.flowType.sw.dst_priority = switchElem.flowType.sw.src_priority;
533 /* Figure out the output ports. */
535 for (i = 0; i < n_actions; i++) {
536 const union odp_action *a = &actions[i];
540 fs.output = ofproto_sflow_odp_port_to_ifindex(os, a->output.port);
544 case ODPAT_OUTPUT_GROUP:
545 n_outputs += (a->output_group.group == DP_GROUP_FLOOD ? os->n_flood
546 : a->output_group.group == DP_GROUP_ALL ? os->n_all
550 case ODPAT_SET_VLAN_VID:
551 switchElem.flowType.sw.dst_vlan = ntohs(a->vlan_vid.vlan_vid);
554 case ODPAT_SET_VLAN_PCP:
555 switchElem.flowType.sw.dst_priority = a->vlan_pcp.vlan_pcp;
563 /* Set output port, as defined by http://www.sflow.org/sflow_version_5.txt
564 (search for "Input/output port information"). */
566 /* This value indicates that the packet was dropped for an unknown
568 fs.output = 0x40000000 | 256;
569 } else if (n_outputs > 1 || !fs.output) {
570 /* Setting the high bit means "multiple output ports". */
571 fs.output = 0x80000000 | n_outputs;
574 /* Submit the flow sample to be encoded into the next datagram. */
575 SFLADD_ELEMENT(&fs, &hdrElem);
576 SFLADD_ELEMENT(&fs, &switchElem);
577 sfl_sampler_writeFlowSample(sampler, &fs);
581 ofproto_sflow_set_group_sizes(struct ofproto_sflow *os,
582 size_t n_flood, size_t n_all)
584 os->n_flood = n_flood;
589 ofproto_sflow_run(struct ofproto_sflow *os)
591 if (ofproto_sflow_is_enabled(os)) {
592 time_t now = time_now();
593 if (now >= os->next_tick) {
594 sfl_agent_tick(os->sflow_agent, now);
595 os->next_tick = now + 1;
601 ofproto_sflow_wait(struct ofproto_sflow *os)
603 if (ofproto_sflow_is_enabled(os)) {
604 poll_timer_wait(os->next_tick * 1000 - time_msec());