Merge "master" into "wdp".
[sliver-openvswitch.git] / ofproto / ofproto-sflow.c
index eb70df4..3724911 100644 (file)
 #include <stdlib.h>
 #include "collectors.h"
 #include "compiler.h"
+#include "hash.h"
+#include "hmap.h"
 #include "netdev.h"
 #include "ofpbuf.h"
 #include "ofproto.h"
 #include "packets.h"
 #include "poll-loop.h"
-#include "port-array.h"
 #include "sflow_api.h"
 #include "socket-util.h"
 #include "timeval.h"
+#include "vlog.h"
 #include "wdp.h"
 #include "xfif.h"
 
-#define THIS_MODULE VLM_sflow
-#include "vlog.h"
+VLOG_DEFINE_THIS_MODULE(sflow)
 
 struct ofproto_sflow_port {
+    struct hmap_node hmap_node; /* In struct ofproto_sflow's "ports" hmap. */
     struct netdev *netdev;      /* Underlying network device, for stats. */
     SFLDataSource_instance dsi; /* sFlow library's notion of port number. */
+    uint16_t xflow_port;        /* xflow port number. */
 };
 
 struct ofproto_sflow {
@@ -49,9 +52,12 @@ struct ofproto_sflow {
     struct wdp *wdp;
     time_t next_tick;
     size_t n_flood, n_all;
-    struct port_array ports;    /* Indexed by XFLOW port number. */
+    struct hmap ports;          /* Contains "struct ofproto_sflow_port"s. */
 };
 
+static void ofproto_sflow_del_port__(struct ofproto_sflow *,
+                                     struct ofproto_sflow_port *);
+
 #define RECEIVER_INDEX 1
 
 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
@@ -131,6 +137,20 @@ sflow_agent_send_packet_cb(void *os_, SFLAgent *agent OVS_UNUSED,
     collectors_send(os->collectors, pkt, pktLen);
 }
 
+static struct ofproto_sflow_port *
+ofproto_sflow_find_port(const struct ofproto_sflow *os, uint16_t xflow_port)
+{
+    struct ofproto_sflow_port *osp;
+
+    HMAP_FOR_EACH_IN_BUCKET (osp, hmap_node,
+                             hash_int(xflow_port, 0), &os->ports) {
+        if (osp->xflow_port == xflow_port) {
+            return osp;
+        }
+    }
+    return NULL;
+}
+
 static void
 sflow_agent_get_counters(void *os_, SFLPoller *poller,
                          SFL_COUNTERS_SAMPLE_TYPE *cs)
@@ -143,7 +163,7 @@ sflow_agent_get_counters(void *os_, SFLPoller *poller,
     enum netdev_flags flags;
     uint32_t current;
 
-    osp = port_array_get(&os->ports, poller->bridgePort);
+    osp = ofproto_sflow_find_port(os, poller->bridgePort);
     if (!osp) {
         return;
     }
@@ -241,9 +261,6 @@ success:
 void
 ofproto_sflow_clear(struct ofproto_sflow *os)
 {
-    struct ofproto_sflow_port *osp;
-    unsigned int xflow_port;
-
     if (os->sflow_agent) {
         sfl_agent_release(os->sflow_agent);
         os->sflow_agent = NULL;
@@ -253,11 +270,6 @@ ofproto_sflow_clear(struct ofproto_sflow *os)
     ofproto_sflow_options_destroy(os->options);
     os->options = NULL;
 
-    PORT_ARRAY_FOR_EACH (osp, &os->ports, xflow_port) {
-        ofproto_sflow_del_port(os, xflow_port);
-    }
-    port_array_clear(&os->ports);
-
     /* Turn off sampling to save CPU cycles. */
     wdp_set_sflow_probability(os->wdp, 0);
 }
@@ -276,7 +288,7 @@ ofproto_sflow_create(struct wdp *wdp)
     os = xcalloc(1, sizeof *os);
     os->wdp = wdp;
     os->next_tick = time_now() + 1;
-    port_array_init(&os->ports);
+    hmap_init(&os->ports);
     return os;
 }
 
@@ -284,8 +296,13 @@ void
 ofproto_sflow_destroy(struct ofproto_sflow *os)
 {
     if (os) {
+        struct ofproto_sflow_port *osp, *next;
+
         ofproto_sflow_clear(os);
-        port_array_destroy(&os->ports);
+        HMAP_FOR_EACH_SAFE (osp, next, hmap_node, &os->ports) {
+            ofproto_sflow_del_port__(os, osp);
+        }
+        hmap_destroy(&os->ports);
         free(os);
     }
 }
@@ -303,12 +320,11 @@ ofproto_sflow_add_poller(struct ofproto_sflow *os,
 
 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)
+                         struct ofproto_sflow_port *osp)
 {
     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_sFlowFsPacketSamplingRate(sampler, os->options->sampling_rate);
+    sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, os->options->header_len);
     sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX);
 }
 
@@ -339,26 +355,35 @@ ofproto_sflow_add_port(struct ofproto_sflow *os, uint16_t xflow_port,
         ifindex = (os->sflow_agent->subId << 16) + xflow_port;
     }
     SFL_DS_SET(osp->dsi, 0, ifindex, 0);
-    port_array_set(&os->ports, xflow_port, osp);
+    osp->xflow_port = xflow_port;
+    hmap_insert(&os->ports, &osp->hmap_node, hash_int(xflow_port, 0));
 
-    /* Add poller. */
+    /* Add poller and sampler. */
     if (os->sflow_agent) {
         ofproto_sflow_add_poller(os, osp, xflow_port);
+        ofproto_sflow_add_sampler(os, osp);
     }
 }
 
+static void
+ofproto_sflow_del_port__(struct ofproto_sflow *os,
+                         struct ofproto_sflow_port *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);
+    hmap_remove(&os->ports, &osp->hmap_node);
+    free(osp);
+}
+
 void
 ofproto_sflow_del_port(struct ofproto_sflow *os, uint16_t xflow_port)
 {
-    struct ofproto_sflow_port *osp = port_array_get(&os->ports, xflow_port);
+    struct ofproto_sflow_port *osp = ofproto_sflow_find_port(os, xflow_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, xflow_port, NULL);
+        ofproto_sflow_del_port__(os, osp);
     }
 }
 
@@ -369,7 +394,6 @@ ofproto_sflow_set_options(struct ofproto_sflow *os,
     struct ofproto_sflow_port *osp;
     bool options_changed;
     SFLReceiver *receiver;
-    unsigned int xflow_port;
     SFLAddress agentIP;
     time_t now;
 
@@ -419,7 +443,7 @@ ofproto_sflow_set_options(struct ofproto_sflow *os,
         sfl_agent_release(os->sflow_agent);
     }
     os->sflow_agent = xcalloc(1, sizeof *os->sflow_agent);
-    now = time_now();
+    now = time_wall();
     sfl_agent_init(os->sflow_agent,
                    &agentIP,
                    options->sub_id,
@@ -440,17 +464,17 @@ ofproto_sflow_set_options(struct ofproto_sflow *os,
                               MAX(1, UINT32_MAX / options->sampling_rate));
 
     /* Add samplers and pollers for the currently known ports. */
-    PORT_ARRAY_FOR_EACH (osp, &os->ports, xflow_port) {
-        ofproto_sflow_add_sampler(os, osp,
-                                  options->sampling_rate, options->header_len);
+    HMAP_FOR_EACH (osp, hmap_node, &os->ports) {
+        ofproto_sflow_add_poller(os, osp, osp->xflow_port);
+        ofproto_sflow_add_sampler(os, osp);
     }
 }
 
 static int
 ofproto_sflow_xflow_port_to_ifindex(const struct ofproto_sflow *os,
-                                  uint16_t xflow_port)
+                                    uint16_t xflow_port)
 {
-    struct ofproto_sflow_port *osp = port_array_get(&os->ports, xflow_port);
+    struct ofproto_sflow_port *osp = ofproto_sflow_find_port(os, xflow_port);
     return osp ? SFL_DS_INDEX(osp->dsi) : 0;
 }
 
@@ -498,7 +522,7 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct xflow_msg *msg)
     /* Get packet payload and extract flow. */
     payload.data = (union xflow_action *) (actions + n_actions);
     payload.size = msg->length - min_size;
-    flow_extract(&payload, msg->port, &flow);
+    flow_extract(&payload, 0, msg->port, &flow);
 
     /* Build a flow sample */
     memset(&fs, 0, sizeof fs);
@@ -521,8 +545,11 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct xflow_msg *msg)
     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. */
+    /* The frame_length should include the Ethernet FCS (4 bytes),
+       but it has already been stripped,  so we need to add 4 here. */
+    header->frame_length = payload.size + 4;
+    /* Ethernet FCS stripped off. */
+    header->stripped = 4;
     header->header_length = MIN(payload.size,
                                 sampler->sFlowFsMaximumHeaderSize);
     header->header_bytes = payload.data;
@@ -532,7 +559,9 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct xflow_msg *msg)
     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. */
+     /* Initialize the output VLAN and priority to be the same as the input,
+        but these fields can be overriden below if affected by an action. */
+    switchElem.flowType.sw.dst_vlan = switchElem.flowType.sw.src_vlan;
     switchElem.flowType.sw.dst_priority = switchElem.flowType.sw.src_priority;
 
     /* Figure out the output ports. */
@@ -599,7 +628,7 @@ 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);
+            sfl_agent_tick(os->sflow_agent, time_wall());
             os->next_tick = now + 1;
         }
     }
@@ -609,6 +638,6 @@ void
 ofproto_sflow_wait(struct ofproto_sflow *os)
 {
     if (ofproto_sflow_is_enabled(os)) {
-        poll_timer_wait(os->next_tick * 1000 - time_msec());
+        poll_timer_wait_until(os->next_tick * 1000LL);
     }
 }