X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=ofproto%2Fofproto-sflow.c;h=c74c7360ef5e4db6b738421655cd693880ad943f;hb=33ef7695b71a71f2d0bb0e00aab46855afb8c2a6;hp=1ad252302ef9745a4ef372194472cdacc55c7f3e;hpb=40b3b2ff4e155d1245330c6366cedd90930ce599;p=openvswitch diff --git a/ofproto/ofproto-sflow.c b/ofproto/ofproto-sflow.c index 1ad25230..c74c7360 100644 --- a/ofproto/ofproto-sflow.c +++ b/ofproto/ofproto-sflow.c @@ -30,10 +30,10 @@ #include "sflow_api.h" #include "socket-util.h" #include "timeval.h" - -#define THIS_MODULE VLM_sflow #include "vlog.h" +VLOG_DEFINE_THIS_MODULE(sflow) + struct ofproto_sflow_port { struct netdev *netdev; /* Underlying network device, for stats. */ SFLDataSource_instance dsi; /* sFlow library's notion of port number. */ @@ -96,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; @@ -111,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_; @@ -212,7 +215,7 @@ sflow_choose_agent_address(const char *agent_device, const char *control_ip, if (agent_device) { struct netdev *netdev; - if (!netdev_open(agent_device, NETDEV_ETH_TYPE_NONE, &netdev)) { + if (!netdev_open_default(agent_device, &netdev)) { int error = netdev_get_in4(netdev, &in4, NULL); netdev_close(netdev); if (!error) { @@ -236,9 +239,6 @@ success: 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; @@ -248,11 +248,6 @@ ofproto_sflow_clear(struct ofproto_sflow *os) 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); } @@ -279,7 +274,13 @@ void ofproto_sflow_destroy(struct ofproto_sflow *os) { if (os) { + struct ofproto_sflow_port *osp; + unsigned int odp_port; + ofproto_sflow_clear(os); + PORT_ARRAY_FOR_EACH (osp, &os->ports, odp_port) { + ofproto_sflow_del_port(os, odp_port); + } port_array_destroy(&os->ports); free(os); } @@ -296,6 +297,16 @@ 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) +{ + SFLSampler *sampler = sfl_agent_addSampler(os->sflow_agent, &osp->dsi); + 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); +} + void ofproto_sflow_add_port(struct ofproto_sflow *os, uint16_t odp_port, const char *netdev_name) @@ -308,7 +319,7 @@ ofproto_sflow_add_port(struct ofproto_sflow *os, uint16_t odp_port, ofproto_sflow_del_port(os, odp_port); /* Open network device. */ - error = netdev_open(netdev_name, NETDEV_ETH_TYPE_NONE, &netdev); + error = netdev_open_default(netdev_name, &netdev); if (error) { VLOG_WARN_RL(&rl, "failed to open network device \"%s\": %s", netdev_name, strerror(error)); @@ -325,9 +336,10 @@ ofproto_sflow_add_port(struct ofproto_sflow *os, uint16_t odp_port, SFL_DS_SET(osp->dsi, 0, ifindex, 0); port_array_set(&os->ports, odp_port, osp); - /* Add poller. */ + /* Add poller and sampler. */ if (os->sflow_agent) { ofproto_sflow_add_poller(os, osp, odp_port); + ofproto_sflow_add_sampler(os, osp); } } @@ -338,10 +350,11 @@ 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); - port_array_set(&os->ports, odp_port, NULL); + port_array_delete(&os->ports, odp_port); } } @@ -350,14 +363,11 @@ 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 @@ -375,8 +385,8 @@ ofproto_sflow_set_options(struct ofproto_sflow *os, 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); + 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"); @@ -405,7 +415,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, @@ -421,27 +431,14 @@ ofproto_sflow_set_options(struct ofproto_sflow *os, sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow"); sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff); - /* Add a single sampler to represent the datapath (special :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 :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); } } @@ -460,7 +457,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; @@ -481,7 +478,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; } @@ -497,7 +494,7 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg) /* 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); + flow_extract(&payload, 0, msg->port, &flow); /* Build a flow sample */ memset(&fs, 0, sizeof fs); @@ -505,13 +502,26 @@ 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; 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; @@ -521,7 +531,9 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct odp_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. */ @@ -585,7 +597,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; } } @@ -595,6 +607,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); } }