X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=ofproto%2Fofproto-sflow.c;h=b37db42d049c45fd49e0e3ffce677a770dfc0d71;hb=56fd8edf80b6098289f9ddd94a6a4be3be648472;hp=45c6c08ade240d1134d99fccd218a07a73a4be03;hpb=b6dab095874bef8c59e9c1304cfc49f55a8259fb;p=openvswitch diff --git a/ofproto/ofproto-sflow.c b/ofproto/ofproto-sflow.c index 45c6c08a..b37db42d 100644 --- a/ofproto/ofproto-sflow.c +++ b/ofproto/ofproto-sflow.c @@ -296,6 +296,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) @@ -338,6 +349,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); @@ -350,15 +362,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)); @@ -371,7 +388,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; } @@ -410,30 +428,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 :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, + options->sampling_rate, options->header_len); } } @@ -452,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; @@ -473,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,6 +502,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; @@ -511,7 +526,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; @@ -534,7 +549,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: @@ -545,6 +560,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. */