summaryrefslogtreecommitdiff
path: root/ofproto
diff options
context:
space:
mode:
authorJustin Pettit <jpettit@nicira.com>2010-02-05 15:58:27 -0800
committerJustin Pettit <jpettit@nicira.com>2010-02-05 17:14:55 -0800
commita4af00400a835eb87569ba40e21874c05e872c0f (patch)
treed9c6db715493bd6fc17c91e9552f8037c3ea7caa /ofproto
parent93ff0290fda0f02904686989243089faaa9229e6 (diff)
parent5819a7cdcf44f0277c538353672fb27701c764f9 (diff)
downloadopenvswitch-a4af00400a835eb87569ba40e21874c05e872c0f.tar.gz
Merge branch 'master' into next
Conflicts: COPYING datapath/datapath.h lib/automake.mk lib/dpif-provider.h lib/dpif.c lib/hmap.h lib/netdev-provider.h lib/netdev.c lib/stream-ssl.h ofproto/executer.c ofproto/ofproto.c ofproto/ofproto.h tests/automake.mk utilities/ovs-ofctl.c utilities/ovs-vsctl.in vswitchd/ovs-vswitchd.conf.5.in xenserver/etc_init.d_vswitch xenserver/etc_xensource_scripts_vif xenserver/opt_xensource_libexec_interface-reconfigure
Diffstat (limited to 'ofproto')
-rw-r--r--ofproto/automake.mk2
-rw-r--r--ofproto/collectors.c6
-rw-r--r--ofproto/collectors.h2
-rw-r--r--ofproto/ofproto-sflow.c607
-rw-r--r--ofproto/ofproto-sflow.h46
-rw-r--r--ofproto/ofproto.c115
-rw-r--r--ofproto/ofproto.h16
7 files changed, 771 insertions, 23 deletions
diff --git a/ofproto/automake.mk b/ofproto/automake.mk
index 0f05e5358..0c99b4907 100644
--- a/ofproto/automake.mk
+++ b/ofproto/automake.mk
@@ -19,6 +19,8 @@ ofproto_libofproto_a_SOURCES = \
ofproto/netflow.h \
ofproto/ofproto.c \
ofproto/ofproto.h \
+ ofproto/ofproto-sflow.c \
+ ofproto/ofproto-sflow.h \
ofproto/pktbuf.c \
ofproto/pktbuf.h \
ofproto/pinsched.c \
diff --git a/ofproto/collectors.c b/ofproto/collectors.c
index f7cb1dbe4..4589f3298 100644
--- a/ofproto/collectors.c
+++ b/ofproto/collectors.c
@@ -121,3 +121,9 @@ collectors_send(const struct collectors *c, const void *payload, size_t n)
}
}
}
+
+int
+collectors_count(const struct collectors *c)
+{
+ return c->n_fds;
+}
diff --git a/ofproto/collectors.h b/ofproto/collectors.h
index a4abb6312..ac70f3759 100644
--- a/ofproto/collectors.h
+++ b/ofproto/collectors.h
@@ -28,4 +28,6 @@ void collectors_destroy(struct collectors *);
void collectors_send(const struct collectors *, const void *, size_t);
+int collectors_count(const struct collectors *);
+
#endif /* collectors.h */
diff --git a/ofproto/ofproto-sflow.c b/ofproto/ofproto-sflow.c
new file mode 100644
index 000000000..1b659d1ab
--- /dev/null
+++ b/ofproto/ofproto-sflow.c
@@ -0,0 +1,607 @@
+/*
+ * Copyright (c) 2009, 2010 InMon Corp.
+ * Copyright (c) 2009 Nicira Networks.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+#include "ofproto-sflow.h"
+#include <inttypes.h>
+#include <stdlib.h>
+#include "collectors.h"
+#include "dpif.h"
+#include "compiler.h"
+#include "netdev.h"
+#include "ofpbuf.h"
+#include "ofproto.h"
+#include "poll-loop.h"
+#include "port-array.h"
+#include "sflow_api.h"
+#include "socket-util.h"
+#include "timeval.h"
+
+#define THIS_MODULE VLM_sflow
+#include "vlog.h"
+
+struct ofproto_sflow_port {
+ struct netdev *netdev; /* Underlying network device, for stats. */
+ SFLDataSource_instance dsi; /* sFlow library's notion of port number. */
+};
+
+struct ofproto_sflow {
+ struct ofproto *ofproto;
+ struct collectors *collectors;
+ SFLAgent *sflow_agent;
+ struct ofproto_sflow_options *options;
+ struct dpif *dpif;
+ time_t next_tick;
+ size_t n_flood, n_all;
+ struct port_array ports; /* Indexed by ODP port number. */
+};
+
+#define RECEIVER_INDEX 1
+
+static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
+
+static bool
+nullable_string_is_equal(const char *a, const char *b)
+{
+ return a ? b && !strcmp(a, b) : !b;
+}
+
+static bool
+ofproto_sflow_options_equal(const struct ofproto_sflow_options *a,
+ const struct ofproto_sflow_options *b)
+{
+ return (svec_equal(&a->targets, &b->targets)
+ && a->sampling_rate == b->sampling_rate
+ && a->polling_interval == b->polling_interval
+ && a->header_len == b->header_len
+ && a->sub_id == b->sub_id
+ && nullable_string_is_equal(a->agent_device, b->agent_device)
+ && nullable_string_is_equal(a->control_ip, b->control_ip));
+}
+
+static struct ofproto_sflow_options *
+ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
+{
+ struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
+ svec_clone(&new->targets, &old->targets);
+ new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
+ new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
+ return new;
+}
+
+static void
+ofproto_sflow_options_destroy(struct ofproto_sflow_options *options)
+{
+ if (options) {
+ svec_destroy(&options->targets);
+ free(options->agent_device);
+ free(options->control_ip);
+ free(options);
+ }
+}
+
+/* sFlow library callback to allocate memory. */
+static void *
+sflow_agent_alloc_cb(void *magic UNUSED, SFLAgent *agent UNUSED, size_t bytes)
+{
+ return calloc(1, bytes);
+}
+
+/* sFlow library callback to free memory. */
+static int
+sflow_agent_free_cb(void *magic UNUSED, SFLAgent *agent UNUSED, void *obj)
+{
+ free(obj);
+ return 0;
+}
+
+/* sFlow library callback to report error. */
+static void
+sflow_agent_error_cb(void *magic UNUSED, SFLAgent *agent UNUSED, char *msg)
+{
+ VLOG_WARN("sFlow agent error: %s", msg);
+}
+
+/* sFlow library callback to send datagram. */
+static void
+sflow_agent_send_packet_cb(void *os_, SFLAgent *agent UNUSED,
+ SFLReceiver *receiver UNUSED, u_char *pkt,
+ uint32_t pktLen)
+{
+ struct ofproto_sflow *os = os_;
+ collectors_send(os->collectors, pkt, pktLen);
+}
+
+static void
+sflow_agent_get_counters(void *os_, SFLPoller *poller,
+ SFL_COUNTERS_SAMPLE_TYPE *cs)
+{
+ struct ofproto_sflow *os = os_;
+ SFLCounters_sample_element elem;
+ struct ofproto_sflow_port *osp;
+ SFLIf_counters *counters;
+ struct netdev_stats stats;
+ enum netdev_flags flags;
+ uint32_t current;
+
+ osp = port_array_get(&os->ports, poller->bridgePort);
+ if (!osp) {
+ return;
+ }
+
+ elem.tag = SFLCOUNTERS_GENERIC;
+ counters = &elem.counterBlock.generic;
+ counters->ifIndex = SFL_DS_INDEX(poller->dsi);
+ counters->ifType = 6;
+ if (!netdev_get_features(osp->netdev, &current, NULL, NULL, NULL)) {
+ /* The values of ifDirection come from MAU MIB (RFC 2668): 0 = unknown,
+ 1 = full-duplex, 2 = half-duplex, 3 = in, 4=out */
+ counters->ifSpeed = netdev_features_to_bps(current);
+ counters->ifDirection = (netdev_features_is_full_duplex(current)
+ ? 1 : 2);
+ } else {
+ counters->ifSpeed = 100000000;
+ counters->ifDirection = 0;
+ }
+ if (!netdev_get_flags(osp->netdev, &flags) && flags & NETDEV_UP) {
+ bool carrier;
+
+ counters->ifStatus = 1; /* ifAdminStatus up. */
+ if (!netdev_get_carrier(osp->netdev, &carrier) && carrier) {
+ counters->ifStatus |= 2; /* ifOperStatus us. */
+ }
+ } else {
+ counters->ifStatus = 0; /* Down. */
+ }
+
+ /* XXX
+ 1. Is the multicast counter filled in?
+ 2. Does the multicast counter include broadcasts?
+ 3. Does the rx_packets counter include multicasts/broadcasts?
+ */
+ netdev_get_stats(osp->netdev, &stats);
+ counters->ifInOctets = stats.rx_bytes;
+ counters->ifInUcastPkts = stats.rx_packets;
+ counters->ifInMulticastPkts = stats.multicast;
+ counters->ifInBroadcastPkts = -1;
+ counters->ifInDiscards = stats.rx_dropped;
+ counters->ifInErrors = stats.rx_errors;
+ counters->ifInUnknownProtos = -1;
+ counters->ifOutOctets = stats.tx_bytes;
+ counters->ifOutUcastPkts = stats.tx_packets;
+ counters->ifOutMulticastPkts = -1;
+ counters->ifOutBroadcastPkts = -1;
+ counters->ifOutDiscards = stats.tx_dropped;
+ counters->ifOutErrors = stats.tx_errors;
+ counters->ifPromiscuousMode = 0;
+
+ SFLADD_ELEMENT(cs, &elem);
+ sfl_poller_writeCountersSample(poller, cs);
+}
+
+/* Obtains an address to use for the local sFlow agent and stores it into
+ * '*agent_addr'. Returns true if successful, false on failure.
+ *
+ * The sFlow agent address should be a local IP address that is persistent and
+ * reachable over the network, if possible. The IP address associated with
+ * 'agent_device' is used if it has one, and otherwise 'control_ip', the IP
+ * address used to talk to the controller. */
+static bool
+sflow_choose_agent_address(const char *agent_device, const char *control_ip,
+ SFLAddress *agent_addr)
+{
+ struct in_addr in4;
+
+ memset(agent_addr, 0, sizeof *agent_addr);
+ agent_addr->type = SFLADDRESSTYPE_IP_V4;
+
+ if (agent_device) {
+ struct netdev *netdev;
+
+ if (!netdev_open_default(agent_device, &netdev)) {
+ int error = netdev_get_in4(netdev, &in4, NULL);
+ netdev_close(netdev);
+ if (!error) {
+ goto success;
+ }
+ }
+ }
+
+ if (control_ip && !lookup_ip(control_ip, &in4)) {
+ goto success;
+ }
+
+ VLOG_ERR("could not determine IP address for sFlow agent");
+ return false;
+
+success:
+ agent_addr->address.ip_v4.addr = in4.s_addr;
+ return true;
+}
+
+void
+ofproto_sflow_clear(struct ofproto_sflow *os)
+{
+ struct ofproto_sflow_port *osp;
+ unsigned int odp_port;
+
+ if (os->sflow_agent) {
+ sfl_agent_release(os->sflow_agent);
+ os->sflow_agent = NULL;
+ }
+ collectors_destroy(os->collectors);
+ os->collectors = NULL;
+ ofproto_sflow_options_destroy(os->options);
+ os->options = NULL;
+
+ PORT_ARRAY_FOR_EACH (osp, &os->ports, odp_port) {
+ ofproto_sflow_del_port(os, odp_port);
+ }
+ port_array_clear(&os->ports);
+
+ /* Turn off sampling to save CPU cycles. */
+ dpif_set_sflow_probability(os->dpif, 0);
+}
+
+bool
+ofproto_sflow_is_enabled(const struct ofproto_sflow *os)
+{
+ return os->collectors != NULL;
+}
+
+struct ofproto_sflow *
+ofproto_sflow_create(struct dpif *dpif)
+{
+ struct ofproto_sflow *os;
+
+ os = xcalloc(1, sizeof *os);
+ os->dpif = dpif;
+ os->next_tick = time_now() + 1;
+ port_array_init(&os->ports);
+ return os;
+}
+
+void
+ofproto_sflow_destroy(struct ofproto_sflow *os)
+{
+ if (os) {
+ ofproto_sflow_clear(os);
+ port_array_destroy(&os->ports);
+ free(os);
+ }
+}
+
+static void
+ofproto_sflow_add_poller(struct ofproto_sflow *os,
+ struct ofproto_sflow_port *osp, uint16_t odp_port)
+{
+ SFLPoller *poller = sfl_agent_addPoller(os->sflow_agent, &osp->dsi, os,
+ sflow_agent_get_counters);
+ sfl_poller_set_sFlowCpInterval(poller, os->options->polling_interval);
+ sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX);
+ sfl_poller_set_bridgePort(poller, odp_port);
+}
+
+static void
+ofproto_sflow_add_sampler(struct ofproto_sflow *os,
+ struct ofproto_sflow_port *osp,
+ u_int32_t sampling_rate, u_int32_t header_len)
+{
+ SFLSampler *sampler = sfl_agent_addSampler(os->sflow_agent, &osp->dsi);
+ sfl_sampler_set_sFlowFsPacketSamplingRate(sampler, sampling_rate);
+ sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, header_len);
+ sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX);
+}
+
+void
+ofproto_sflow_add_port(struct ofproto_sflow *os, uint16_t odp_port,
+ const char *netdev_name)
+{
+ struct ofproto_sflow_port *osp;
+ struct netdev *netdev;
+ uint32_t ifindex;
+ int error;
+
+ ofproto_sflow_del_port(os, odp_port);
+
+ /* Open network device. */
+ error = netdev_open_default(netdev_name, &netdev);
+ if (error) {
+ VLOG_WARN_RL(&rl, "failed to open network device \"%s\": %s",
+ netdev_name, strerror(error));
+ return;
+ }
+
+ /* Add to table of ports. */
+ osp = xmalloc(sizeof *osp);
+ osp->netdev = netdev;
+ ifindex = netdev_get_ifindex(netdev);
+ if (ifindex <= 0) {
+ ifindex = (os->sflow_agent->subId << 16) + odp_port;
+ }
+ SFL_DS_SET(osp->dsi, 0, ifindex, 0);
+ port_array_set(&os->ports, odp_port, osp);
+
+ /* Add poller. */
+ if (os->sflow_agent) {
+ ofproto_sflow_add_poller(os, osp, odp_port);
+ }
+}
+
+void
+ofproto_sflow_del_port(struct ofproto_sflow *os, uint16_t odp_port)
+{
+ struct ofproto_sflow_port *osp = port_array_get(&os->ports, odp_port);
+ if (osp) {
+ if (os->sflow_agent) {
+ sfl_agent_removePoller(os->sflow_agent, &osp->dsi);
+ sfl_agent_removeSampler(os->sflow_agent, &osp->dsi);
+ }
+ netdev_close(osp->netdev);
+ free(osp);
+ port_array_set(&os->ports, odp_port, NULL);
+ }
+}
+
+void
+ofproto_sflow_set_options(struct ofproto_sflow *os,
+ const struct ofproto_sflow_options *options)
+{
+ struct ofproto_sflow_port *osp;
+ bool options_changed;
+ SFLReceiver *receiver;
+ unsigned int odp_port;
+ SFLAddress agentIP;
+ time_t now;
+ int error;
+
+ if (!options->targets.n || !options->sampling_rate) {
+ /* No point in doing any work if there are no targets or nothing to
+ * sample. */
+ ofproto_sflow_clear(os);
+ return;
+ }
+
+ options_changed = (!os->options
+ || !ofproto_sflow_options_equal(options, os->options));
+
+ /* Configure collectors if options have changed or if we're shortchanged in
+ * collectors (which indicates that opening one or more of the configured
+ * collectors failed, so that we should retry). */
+ if (options_changed
+ || collectors_count(os->collectors) < options->targets.n) {
+ collectors_destroy(os->collectors);
+ error = collectors_create(&options->targets,
+ SFL_DEFAULT_COLLECTOR_PORT, &os->collectors);
+ if (os->collectors == NULL) {
+ VLOG_WARN_RL(&rl, "no collectors could be initialized, "
+ "sFlow disabled");
+ ofproto_sflow_clear(os);
+ return;
+ }
+ }
+
+ /* Avoid reconfiguring if options didn't change. */
+ if (!options_changed) {
+ return;
+ }
+ ofproto_sflow_options_destroy(os->options);
+ os->options = ofproto_sflow_options_clone(options);
+
+ /* Choose agent IP address. */
+ if (!sflow_choose_agent_address(options->agent_device,
+ options->control_ip, &agentIP)) {
+ ofproto_sflow_clear(os);
+ return;
+ }
+
+ /* Create agent. */
+ VLOG_INFO("creating sFlow agent %d", options->sub_id);
+ if (os->sflow_agent) {
+ sfl_agent_release(os->sflow_agent);
+ }
+ os->sflow_agent = xcalloc(1, sizeof *os->sflow_agent);
+ now = time_now();
+ sfl_agent_init(os->sflow_agent,
+ &agentIP,
+ options->sub_id,
+ now, /* Boot time. */
+ now, /* Current time. */
+ os, /* Pointer supplied to callbacks. */
+ sflow_agent_alloc_cb,
+ sflow_agent_free_cb,
+ sflow_agent_error_cb,
+ sflow_agent_send_packet_cb);
+
+ receiver = sfl_agent_addReceiver(os->sflow_agent);
+ sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow");
+ sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff);
+
+ /* Set the sampling_rate down in the datapath. */
+ dpif_set_sflow_probability(os->dpif,
+ MAX(1, UINT32_MAX / options->sampling_rate));
+
+ /* Add samplers and pollers for the currently known ports. */
+ PORT_ARRAY_FOR_EACH (osp, &os->ports, odp_port) {
+ ofproto_sflow_add_sampler(os, osp,
+ options->sampling_rate, options->header_len);
+ }
+}
+
+static int
+ofproto_sflow_odp_port_to_ifindex(const struct ofproto_sflow *os,
+ uint16_t odp_port)
+{
+ struct ofproto_sflow_port *osp = port_array_get(&os->ports, odp_port);
+ return osp ? SFL_DS_INDEX(osp->dsi) : 0;
+}
+
+void
+ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg)
+{
+ SFL_FLOW_SAMPLE_TYPE fs;
+ SFLFlow_sample_element hdrElem;
+ SFLSampled_header *header;
+ SFLFlow_sample_element switchElem;
+ SFLSampler *sampler;
+ const struct odp_sflow_sample_header *hdr;
+ const union odp_action *actions;
+ struct ofpbuf payload;
+ size_t n_actions, n_outputs;
+ size_t min_size;
+ flow_t flow;
+ size_t i;
+
+ /* Get odp_sflow_sample_header. */
+ min_size = sizeof *msg + sizeof *hdr;
+ if (min_size > msg->length) {
+ VLOG_WARN_RL(&rl, "sFlow packet too small (%"PRIu32" < %zu)",
+ msg->length, min_size);
+ return;
+ }
+ hdr = (const struct odp_sflow_sample_header *) (msg + 1);
+
+ /* Get actions. */
+ n_actions = hdr->n_actions;
+ if (n_actions > 65536 / sizeof *actions) {
+ VLOG_WARN_RL(&rl, "too many actions in sFlow packet (%zu > %zu)",
+ 65536 / sizeof *actions, n_actions);
+ return;
+ }
+ min_size += n_actions * sizeof *actions;
+ if (min_size > msg->length) {
+ VLOG_WARN_RL(&rl, "sFlow packet with %zu actions too small "
+ "(%"PRIu32" < %zu)",
+ n_actions, msg->length, min_size);
+ return;
+ }
+ actions = (const union odp_action *) (hdr + 1);
+
+ /* Get packet payload and extract flow. */
+ payload.data = (union odp_action *) (actions + n_actions);
+ payload.size = msg->length - min_size;
+ flow_extract(&payload, msg->port, &flow);
+
+ /* Build a flow sample */
+ memset(&fs, 0, sizeof fs);
+ fs.input = ofproto_sflow_odp_port_to_ifindex(os, msg->port);
+ fs.output = 0; /* Filled in correctly below. */
+ fs.sample_pool = hdr->sample_pool;
+
+ /* We are going to give it to the sampler that represents this input port.
+ * By implementing "ingress-only" sampling like this we ensure that we
+ * never have to offer the same sample to more than one sampler. */
+ sampler = sfl_agent_getSamplerByIfIndex(os->sflow_agent, fs.input);
+ if (!sampler) {
+ VLOG_WARN_RL(&rl, "no sampler for input ifIndex (%"PRIu32")",
+ fs.input);
+ return;
+ }
+
+ /* Sampled header. */
+ memset(&hdrElem, 0, sizeof hdrElem);
+ hdrElem.tag = SFLFLOW_HEADER;
+ header = &hdrElem.flowType.header;
+ header->header_protocol = SFLHEADER_ETHERNET_ISO8023;
+ header->frame_length = payload.size;
+ header->stripped = 4; /* Ethernet FCS stripped off. */
+ header->header_length = MIN(payload.size,
+ sampler->sFlowFsMaximumHeaderSize);
+ header->header_bytes = payload.data;
+
+ /* Add extended switch element. */
+ memset(&switchElem, 0, sizeof(switchElem));
+ switchElem.tag = SFLFLOW_EX_SWITCH;
+ switchElem.flowType.sw.src_vlan = ntohs(flow.dl_vlan);
+ switchElem.flowType.sw.src_priority = -1; /* XXX */
+ switchElem.flowType.sw.dst_vlan = -1; /* Filled in correctly below. */
+ switchElem.flowType.sw.dst_priority = switchElem.flowType.sw.src_priority;
+
+ /* Figure out the output ports. */
+ n_outputs = 0;
+ for (i = 0; i < n_actions; i++) {
+ const union odp_action *a = &actions[i];
+
+ switch (a->type) {
+ case ODPAT_OUTPUT:
+ fs.output = ofproto_sflow_odp_port_to_ifindex(os, a->output.port);
+ n_outputs++;
+ break;
+
+ case ODPAT_OUTPUT_GROUP:
+ n_outputs += (a->output_group.group == DP_GROUP_FLOOD ? os->n_flood
+ : a->output_group.group == DP_GROUP_ALL ? os->n_all
+ : 0);
+ break;
+
+ case ODPAT_SET_VLAN_VID:
+ switchElem.flowType.sw.dst_vlan = ntohs(a->vlan_vid.vlan_vid);
+ break;
+
+ case ODPAT_SET_VLAN_PCP:
+ switchElem.flowType.sw.dst_priority = a->vlan_pcp.vlan_pcp;
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ /* Set output port, as defined by http://www.sflow.org/sflow_version_5.txt
+ (search for "Input/output port information"). */
+ if (!n_outputs) {
+ /* This value indicates that the packet was dropped for an unknown
+ * reason. */
+ fs.output = 0x40000000 | 256;
+ } else if (n_outputs > 1 || !fs.output) {
+ /* Setting the high bit means "multiple output ports". */
+ fs.output = 0x80000000 | n_outputs;
+ }
+
+ /* Submit the flow sample to be encoded into the next datagram. */
+ SFLADD_ELEMENT(&fs, &hdrElem);
+ SFLADD_ELEMENT(&fs, &switchElem);
+ sfl_sampler_writeFlowSample(sampler, &fs);
+}
+
+void
+ofproto_sflow_set_group_sizes(struct ofproto_sflow *os,
+ size_t n_flood, size_t n_all)
+{
+ os->n_flood = n_flood;
+ os->n_all = n_all;
+}
+
+void
+ofproto_sflow_run(struct ofproto_sflow *os)
+{
+ if (ofproto_sflow_is_enabled(os)) {
+ time_t now = time_now();
+ if (now >= os->next_tick) {
+ sfl_agent_tick(os->sflow_agent, now);
+ os->next_tick = now + 1;
+ }
+ }
+}
+
+void
+ofproto_sflow_wait(struct ofproto_sflow *os)
+{
+ if (ofproto_sflow_is_enabled(os)) {
+ poll_timer_wait(os->next_tick * 1000 - time_msec());
+ }
+}
diff --git a/ofproto/ofproto-sflow.h b/ofproto/ofproto-sflow.h
new file mode 100644
index 000000000..ec86d1159
--- /dev/null
+++ b/ofproto/ofproto-sflow.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2009 InMon Corp.
+ * Copyright (c) 2009 Nicira Networks.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef OFPROTO_SFLOW_H
+#define OFPROTO_SFLOW_H 1
+
+#include <stdint.h>
+#include "svec.h"
+
+struct dpif;
+struct odp_msg;
+struct ofproto_sflow_options;
+
+struct ofproto_sflow *ofproto_sflow_create(struct dpif *);
+void ofproto_sflow_destroy(struct ofproto_sflow *);
+void ofproto_sflow_set_options(struct ofproto_sflow *,
+ const struct ofproto_sflow_options *);
+void ofproto_sflow_clear(struct ofproto_sflow *);
+bool ofproto_sflow_is_enabled(const struct ofproto_sflow *);
+
+void ofproto_sflow_add_port(struct ofproto_sflow *, uint16_t odp_port,
+ const char *netdev_name);
+void ofproto_sflow_del_port(struct ofproto_sflow *, uint16_t odp_port);
+void ofproto_sflow_set_group_sizes(struct ofproto_sflow *,
+ size_t n_flood, size_t n_all);
+
+void ofproto_sflow_run(struct ofproto_sflow *);
+void ofproto_sflow_wait(struct ofproto_sflow *);
+
+void ofproto_sflow_received(struct ofproto_sflow *, struct odp_msg *);
+
+#endif /* ofproto/ofproto-sflow.h */
diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c
index 10b4796f1..c44762c46 100644
--- a/ofproto/ofproto.c
+++ b/ofproto/ofproto.c
@@ -34,6 +34,7 @@
#include "netflow.h"
#include "odp-util.h"
#include "ofp-print.h"
+#include "ofproto-sflow.h"
#include "ofpbuf.h"
#include "openflow/nicira-ext.h"
#include "openflow/openflow.h"
@@ -58,10 +59,7 @@
#define THIS_MODULE VLM_ofproto
#include "vlog.h"
-enum {
- DP_GROUP_FLOOD = 0,
- DP_GROUP_ALL = 1
-};
+#include "sflow_api.h"
enum {
TABLEID_HASH = 0,
@@ -205,6 +203,7 @@ struct ofproto {
struct fail_open *fail_open;
struct pinsched *miss_sched, *action_sched;
struct netflow *netflow;
+ struct ofproto_sflow *sflow;
/* Flow table. */
struct classifier cls;
@@ -249,7 +248,8 @@ static void handle_odp_msg(struct ofproto *, struct ofpbuf *);
static void handle_openflow(struct ofconn *, struct ofproto *,
struct ofpbuf *);
-static void refresh_port_group(struct ofproto *, unsigned int group);
+static void refresh_port_groups(struct ofproto *);
+
static void update_port(struct ofproto *, const char *devname);
static int init_ports(struct ofproto *);
static void reinit_ports(struct ofproto *);
@@ -279,7 +279,7 @@ ofproto_create(const char *datapath, const char *datapath_type,
dpif_close(dpif);
return error;
}
- error = dpif_recv_set_mask(dpif, ODPL_MISS | ODPL_ACTION);
+ error = dpif_recv_set_mask(dpif, ODPL_MISS | ODPL_ACTION | ODPL_SFLOW);
if (error) {
VLOG_ERR("failed to listen on datapath %s: %s",
datapath, strerror(error));
@@ -312,6 +312,7 @@ ofproto_create(const char *datapath, const char *datapath_type,
p->fail_open = NULL;
p->miss_sched = p->action_sched = NULL;
p->netflow = NULL;
+ p->sflow = NULL;
/* Initialize flow table. */
classifier_init(&p->cls);
@@ -532,6 +533,30 @@ ofproto_set_netflow(struct ofproto *ofproto,
}
void
+ofproto_set_sflow(struct ofproto *ofproto,
+ const struct ofproto_sflow_options *oso)
+{
+ struct ofproto_sflow *os = ofproto->sflow;
+ if (oso) {
+ if (!os) {
+ struct ofport *ofport;
+ unsigned int odp_port;
+
+ os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
+ refresh_port_groups(ofproto);
+ PORT_ARRAY_FOR_EACH (ofport, &ofproto->ports, odp_port) {
+ ofproto_sflow_add_port(os, odp_port,
+ netdev_get_name(ofport->netdev));
+ }
+ }
+ ofproto_sflow_set_options(os, oso);
+ } else {
+ ofproto_sflow_destroy(os);
+ ofproto->sflow = NULL;
+ }
+}
+
+void
ofproto_set_failure(struct ofproto *ofproto, bool fail_open)
{
if (fail_open) {
@@ -676,6 +701,7 @@ ofproto_destroy(struct ofproto *p)
pinsched_destroy(p->miss_sched);
pinsched_destroy(p->action_sched);
netflow_destroy(p->netflow);
+ ofproto_sflow_destroy(p->sflow);
switch_status_unregister(p->ss_cat);
@@ -829,6 +855,9 @@ ofproto_run1(struct ofproto *p)
if (p->netflow) {
netflow_run(p->netflow);
}
+ if (p->sflow) {
+ ofproto_sflow_run(p->sflow);
+ }
return 0;
}
@@ -882,6 +911,9 @@ ofproto_wait(struct ofproto *p)
}
pinsched_wait(p->miss_sched);
pinsched_wait(p->action_sched);
+ if (p->sflow) {
+ ofproto_sflow_wait(p->sflow);
+ }
if (!tag_set_is_empty(&p->revalidate_set)) {
poll_immediate_wake();
}
@@ -1022,7 +1054,7 @@ reinit_ports(struct ofproto *p)
svec_destroy(&devnames);
}
-static void
+static size_t
refresh_port_group(struct ofproto *p, unsigned int group)
{
uint16_t *ports;
@@ -1041,13 +1073,18 @@ refresh_port_group(struct ofproto *p, unsigned int group)
}
dpif_port_group_set(p->dpif, group, ports, n_ports);
free(ports);
+
+ return n_ports;
}
static void
refresh_port_groups(struct ofproto *p)
{
- refresh_port_group(p, DP_GROUP_FLOOD);
- refresh_port_group(p, DP_GROUP_ALL);
+ size_t n_flood = refresh_port_group(p, DP_GROUP_FLOOD);
+ size_t n_all = refresh_port_group(p, DP_GROUP_ALL);
+ if (p->sflow) {
+ ofproto_sflow_set_group_sizes(p->sflow, n_flood, n_all);
+ }
}
static struct ofport *
@@ -1152,19 +1189,29 @@ send_port_status(struct ofproto *p, const struct ofport *ofport,
static void
ofport_install(struct ofproto *p, struct ofport *ofport)
{
+ uint16_t odp_port = ofp_port_to_odp_port(ofport->opp.port_no);
+ const char *netdev_name = (const char *) ofport->opp.name;
+
netdev_monitor_add(p->netdev_monitor, ofport->netdev);
- port_array_set(&p->ports, ofp_port_to_odp_port(ofport->opp.port_no),
- ofport);
- shash_add(&p->port_by_name, (char *) ofport->opp.name, ofport);
+ port_array_set(&p->ports, odp_port, ofport);
+ shash_add(&p->port_by_name, netdev_name, ofport);
+ if (p->sflow) {
+ ofproto_sflow_add_port(p->sflow, odp_port, netdev_name);
+ }
}
static void
ofport_remove(struct ofproto *p, struct ofport *ofport)
{
+ uint16_t odp_port = ofp_port_to_odp_port(ofport->opp.port_no);
+
netdev_monitor_remove(p->netdev_monitor, ofport->netdev);
- port_array_set(&p->ports, ofp_port_to_odp_port(ofport->opp.port_no), NULL);
+ port_array_set(&p->ports, odp_port, NULL);
shash_delete(&p->port_by_name,
shash_find(&p->port_by_name, (char *) ofport->opp.name));
+ if (p->sflow) {
+ ofproto_sflow_del_port(p->sflow, odp_port);
+ }
}
static void
@@ -2249,7 +2296,7 @@ update_port_config(struct ofproto *p, struct ofport *port,
#undef REVALIDATE_BITS
if (mask & OFPPC_NO_FLOOD) {
port->opp.config ^= OFPPC_NO_FLOOD;
- refresh_port_group(p, DP_GROUP_FLOOD);
+ refresh_port_groups(p);
}
if (mask & OFPPC_NO_PACKET_IN) {
port->opp.config ^= OFPPC_NO_PACKET_IN;
@@ -2999,7 +3046,7 @@ handle_openflow(struct ofconn *ofconn, struct ofproto *p,
}
static void
-handle_odp_msg(struct ofproto *p, struct ofpbuf *packet)
+handle_odp_miss_msg(struct ofproto *p, struct ofpbuf *packet)
{
struct odp_msg *msg = packet->data;
uint16_t in_port = odp_port_to_ofp_port(msg->port);
@@ -3007,14 +3054,6 @@ handle_odp_msg(struct ofproto *p, struct ofpbuf *packet)
struct ofpbuf payload;
flow_t flow;
- /* Handle controller actions. */
- if (msg->type == _ODPL_ACTION_NR) {
- COVERAGE_INC(ofproto_ctlr_action);
- pinsched_send(p->action_sched, in_port, packet,
- send_packet_in_action, p);
- return;
- }
-
payload.data = msg + 1;
payload.size = msg->length - sizeof *msg;
flow_extract(&payload, msg->port, &flow);
@@ -3084,6 +3123,36 @@ handle_odp_msg(struct ofproto *p, struct ofpbuf *packet)
ofpbuf_delete(packet);
}
}
+
+static void
+handle_odp_msg(struct ofproto *p, struct ofpbuf *packet)
+{
+ struct odp_msg *msg = packet->data;
+
+ switch (msg->type) {
+ case _ODPL_ACTION_NR:
+ COVERAGE_INC(ofproto_ctlr_action);
+ pinsched_send(p->action_sched, odp_port_to_ofp_port(msg->port), packet,
+ send_packet_in_action, p);
+ break;
+
+ case _ODPL_SFLOW_NR:
+ if (p->sflow) {
+ ofproto_sflow_received(p->sflow, msg);
+ }
+ ofpbuf_delete(packet);
+ break;
+
+ case _ODPL_MISS_NR:
+ handle_odp_miss_msg(p, packet);
+ break;
+
+ default:
+ VLOG_WARN_RL(&rl, "received ODP message of unexpected type %"PRIu32,
+ msg->type);
+ break;
+ }
+}
static void
revalidate_cb(struct cls_rule *sub_, void *cbdata_)
diff --git a/ofproto/ofproto.h b/ofproto/ofproto.h
index ddc34483d..a94c8b59b 100644
--- a/ofproto/ofproto.h
+++ b/ofproto/ofproto.h
@@ -29,6 +29,11 @@ struct ofhooks;
struct ofproto;
struct svec;
+enum {
+ DP_GROUP_FLOOD = 0,
+ DP_GROUP_ALL = 1
+};
+
struct ofexpired {
flow_t flow;
uint64_t packet_count; /* Packets from subrules. */
@@ -36,6 +41,16 @@ struct ofexpired {
long long int used; /* Last-used time (0 if never used). */
};
+struct ofproto_sflow_options {
+ struct svec targets;
+ uint32_t sampling_rate;
+ uint32_t polling_interval;
+ uint32_t header_len;
+ uint32_t sub_id;
+ char *agent_device;
+ char *control_ip;
+};
+
int ofproto_create(const char *datapath, const char *datapath_type,
const struct ofhooks *, void *aux,
struct ofproto **ofprotop);
@@ -62,6 +77,7 @@ int ofproto_set_listeners(struct ofproto *, const struct svec *listeners);
int ofproto_set_snoops(struct ofproto *, const struct svec *snoops);
int ofproto_set_netflow(struct ofproto *,
const struct netflow_options *nf_options);
+void ofproto_set_sflow(struct ofproto *, const struct ofproto_sflow_options *);
void ofproto_set_failure(struct ofproto *, bool fail_open);
void ofproto_set_rate_limit(struct ofproto *, int rate_limit, int burst_limit);
int ofproto_set_stp(struct ofproto *, bool enable_stp);