Rename UNUSED macro to OVS_UNUSED to avoid naming conflict.
[sliver-openvswitch.git] / ofproto / ofproto-sflow.c
index cf63c65..45015ce 100644 (file)
@@ -54,6 +54,12 @@ struct ofproto_sflow {
 
 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)
@@ -63,14 +69,15 @@ ofproto_sflow_options_equal(const struct ofproto_sflow_options *a,
             && a->polling_interval == b->polling_interval
             && a->header_len == b->header_len
             && a->sub_id == b->sub_id
-            && !strcmp(a->agent_device, b->agent_device)
-            && !strcmp(a->control_ip, b->control_ip));
+            && 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;
@@ -80,6 +87,7 @@ 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);
@@ -88,14 +96,16 @@ ofproto_sflow_options_destroy(struct ofproto_sflow_options *options)
 
 /* sFlow library callback to allocate memory. */
 static void *
-sflow_agent_alloc_cb(void *magic UNUSED, SFLAgent *agent UNUSED, size_t bytes)
+sflow_agent_alloc_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_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)
+sflow_agent_free_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
+                    void *obj)
 {
     free(obj);
     return 0;
@@ -103,15 +113,16 @@ sflow_agent_free_cb(void *magic UNUSED, SFLAgent *agent UNUSED, void *obj)
 
 /* sFlow library callback to report error. */
 static void
-sflow_agent_error_cb(void *magic UNUSED, SFLAgent *agent UNUSED, char *msg)
+sflow_agent_error_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_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,
+sflow_agent_send_packet_cb(void *os_, SFLAgent *agent OVS_UNUSED,
+                           SFLReceiver *receiver OVS_UNUSED, u_char *pkt,
                            uint32_t pktLen)
 {
     struct ofproto_sflow *os = os_;
@@ -140,12 +151,14 @@ sflow_agent_get_counters(void *os_, SFLPoller *poller,
     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 = 1;
+        counters->ifDirection = 0;
     }
     if (!netdev_get_flags(osp->netdev, &flags) && flags & NETDEV_UP) {
         bool carrier;
@@ -286,6 +299,17 @@ ofproto_sflow_add_poller(struct ofproto_sflow *os,
     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)
@@ -328,6 +352,7 @@ ofproto_sflow_del_port(struct ofproto_sflow *os, uint16_t 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);
@@ -340,15 +365,20 @@ ofproto_sflow_set_options(struct ofproto_sflow *os,
                           const struct ofproto_sflow_options *options)
 {
     struct ofproto_sflow_port *osp;
-    SFLDataSource_instance dsi;
     bool options_changed;
-    SFLSampler *sampler;
     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));
 
@@ -361,7 +391,8 @@ ofproto_sflow_set_options(struct ofproto_sflow *os,
         error = collectors_create(&options->targets,
                                   SFL_DEFAULT_COLLECTOR_PORT, &os->collectors);
         if (os->collectors == NULL) {
-            VLOG_WARN_RL(&rl, "no configured collectors, sFlow disabled");
+            VLOG_WARN_RL(&rl, "no collectors could be initialized, "
+                         "sFlow disabled");
             ofproto_sflow_clear(os);
             return;
         }
@@ -400,30 +431,17 @@ ofproto_sflow_set_options(struct ofproto_sflow *os,
                    sflow_agent_send_packet_cb);
 
     receiver = sfl_agent_addReceiver(os->sflow_agent);
-    sfl_receiver_set_sFlowRcvrOwner(receiver, "OpenVSwitch sFlow");
+    sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow");
     sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff);
 
-    /* Add a single sampler to represent the whole switch (special <ifIndex>:0
-     * datasource).  The alternative is to model a physical switch more closely
-     * and instantiate a separate sampler object for each interface, but then
-     * unicasts would have to be offered to two samplers, and
-     * broadcasts/multicasts would have to be offered to all of them.  Doing it
-     * this way with a single <ifindex>:0 sampler is much more efficient for a
-     * virtual switch, and is allowed by the sFlow standard.
-     */
-    SFL_DS_SET(dsi, 0, 0, 0);
-    sampler = sfl_agent_addSampler(os->sflow_agent, &dsi);
-    sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX);
-    sfl_sampler_set_sFlowFsPacketSamplingRate(sampler, options->sampling_rate);
-    sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, options->header_len);
-
     /* Set the sampling_rate down in the datapath. */
     dpif_set_sflow_probability(os->dpif,
                                MAX(1, UINT32_MAX / options->sampling_rate));
 
-    /* Add the currently known ports. */
+    /* Add samplers and pollers for the currently known ports. */
     PORT_ARRAY_FOR_EACH (osp, &os->ports, odp_port) {
-        ofproto_sflow_add_poller(os, osp, odp_port);
+        ofproto_sflow_add_sampler(os, osp,
+                                  options->sampling_rate, options->header_len);
     }
 }
 
@@ -442,7 +460,7 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg)
     SFLFlow_sample_element hdrElem;
     SFLSampled_header *header;
     SFLFlow_sample_element switchElem;
-    SFLSampler *sampler = os->sflow_agent->samplers;
+    SFLSampler *sampler;
     const struct odp_sflow_sample_header *hdr;
     const union odp_action *actions;
     struct ofpbuf payload;
@@ -463,7 +481,7 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg)
     /* Get actions. */
     n_actions = hdr->n_actions;
     if (n_actions > 65536 / sizeof *actions) {
-        VLOG_WARN_RL(&rl, "too many actions in sFlow packet (%"PRIu32" > %zu)",
+        VLOG_WARN_RL(&rl, "too many actions in sFlow packet (%zu > %zu)",
                      65536 / sizeof *actions, n_actions);
         return;
     }
@@ -487,6 +505,16 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg)
     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;
@@ -501,7 +529,7 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg)
     /* Add extended switch element. */
     memset(&switchElem, 0, sizeof(switchElem));
     switchElem.tag = SFLFLOW_EX_SWITCH;
-    switchElem.flowType.sw.src_vlan = flow.dl_vlan;
+    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;
@@ -524,7 +552,7 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg)
             break;
 
         case ODPAT_SET_VLAN_VID:
-            switchElem.flowType.sw.dst_vlan = a->vlan_vid.vlan_vid;
+            switchElem.flowType.sw.dst_vlan = ntohs(a->vlan_vid.vlan_vid);
             break;
 
         case ODPAT_SET_VLAN_PCP:
@@ -535,6 +563,9 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg)
             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. */