X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=ofproto%2Fofproto-sflow.c;h=784e5528f85472abea96585eeba427c08ff3553f;hb=27bcf966b4057623f7b4d856c0348a1e0eb452e0;hp=cf63c65501d5cb1168cff978133efd636f7de13c;hpb=216114e062b4ee108ff38c6959ff81d166d51368;p=sliver-openvswitch.git diff --git a/ofproto/ofproto-sflow.c b/ofproto/ofproto-sflow.c index cf63c6550..784e5528f 100644 --- a/ofproto/ofproto-sflow.c +++ b/ofproto/ofproto-sflow.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2009, 2010 InMon Corp. - * Copyright (c) 2009 Nicira Networks. + * Copyright (c) 2009, 2010 Nicira Networks. + * Copyright (c) 2009 InMon Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,21 +22,25 @@ #include "collectors.h" #include "dpif.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" - -#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 odp_port; /* ODP port number. */ }; struct ofproto_sflow { @@ -47,13 +51,22 @@ struct ofproto_sflow { struct dpif *dpif; time_t next_tick; size_t n_flood, n_all; - struct port_array ports; /* Indexed by ODP 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); +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 +76,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 +94,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 +103,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,21 +120,36 @@ 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_; collectors_send(os->collectors, pkt, pktLen); } +static struct ofproto_sflow_port * +ofproto_sflow_find_port(const struct ofproto_sflow *os, uint16_t odp_port) +{ + struct ofproto_sflow_port *osp; + + HMAP_FOR_EACH_IN_BUCKET (osp, hmap_node, + hash_int(odp_port, 0), &os->ports) { + if (osp->odp_port == odp_port) { + return osp; + } + } + return NULL; +} + static void sflow_agent_get_counters(void *os_, SFLPoller *poller, SFL_COUNTERS_SAMPLE_TYPE *cs) @@ -130,7 +162,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; } @@ -140,12 +172,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, ¤t, 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; @@ -202,7 +236,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) { @@ -226,9 +260,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; @@ -238,11 +269,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); } @@ -261,7 +287,7 @@ ofproto_sflow_create(struct dpif *dpif) os = xcalloc(1, sizeof *os); os->dpif = dpif; os->next_tick = time_now() + 1; - port_array_init(&os->ports); + hmap_init(&os->ports); return os; } @@ -269,8 +295,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); } } @@ -286,6 +317,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) @@ -298,7 +339,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)); @@ -313,25 +354,35 @@ ofproto_sflow_add_port(struct ofproto_sflow *os, uint16_t odp_port, ifindex = (os->sflow_agent->subId << 16) + odp_port; } SFL_DS_SET(osp->dsi, 0, ifindex, 0); - port_array_set(&os->ports, odp_port, osp); + osp->odp_port = odp_port; + hmap_insert(&os->ports, &osp->hmap_node, hash_int(odp_port, 0)); - /* Add poller. */ + /* Add poller and sampler. */ if (os->sflow_agent) { ofproto_sflow_add_poller(os, osp, odp_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 odp_port) { - struct ofproto_sflow_port *osp = port_array_get(&os->ports, odp_port); + struct ofproto_sflow_port *osp = ofproto_sflow_find_port(os, odp_port); if (osp) { - if (os->sflow_agent) { - sfl_agent_removePoller(os->sflow_agent, &osp->dsi); - } - netdev_close(osp->netdev); - free(osp); - port_array_set(&os->ports, odp_port, NULL); + ofproto_sflow_del_port__(os, osp); } } @@ -340,14 +391,17 @@ 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)); @@ -358,10 +412,11 @@ 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 configured collectors, sFlow disabled"); + VLOG_WARN_RL(&rl, "no collectors could be initialized, " + "sFlow disabled"); ofproto_sflow_clear(os); return; } @@ -387,7 +442,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, @@ -400,30 +455,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. */ - PORT_ARRAY_FOR_EACH (osp, &os->ports, odp_port) { - ofproto_sflow_add_poller(os, osp, odp_port); + /* Add samplers and pollers for the currently known ports. */ + HMAP_FOR_EACH (osp, hmap_node, &os->ports) { + ofproto_sflow_add_poller(os, osp, osp->odp_port); + ofproto_sflow_add_sampler(os, osp); } } @@ -431,7 +473,7 @@ 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); + struct ofproto_sflow_port *osp = ofproto_sflow_find_port(os, odp_port); return osp ? SFL_DS_INDEX(osp->dsi) : 0; } @@ -442,13 +484,13 @@ 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; size_t n_actions, n_outputs; + struct flow flow; size_t min_size; - flow_t flow; size_t i; /* Get odp_sflow_sample_header. */ @@ -463,7 +505,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; } @@ -479,7 +521,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); @@ -487,13 +529,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; @@ -501,15 +556,18 @@ 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. */ + /* 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. */ n_outputs = 0; for (i = 0; i < n_actions; i++) { const union odp_action *a = &actions[i]; + uint16_t tci; switch (a->type) { case ODPAT_OUTPUT: @@ -517,24 +575,19 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg) 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 = a->vlan_vid.vlan_vid; - break; - - case ODPAT_SET_VLAN_PCP: - switchElem.flowType.sw.dst_priority = a->vlan_pcp.vlan_pcp; + case ODPAT_SET_DL_TCI: + tci = a->dl_tci.tci; + switchElem.flowType.sw.dst_vlan = vlan_tci_to_vid(tci); + switchElem.flowType.sw.dst_priority = vlan_tci_to_pcp(tci); 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. */ @@ -550,21 +603,13 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg) 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); + sfl_agent_tick(os->sflow_agent, time_wall()); os->next_tick = now + 1; } } @@ -574,6 +619,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); } }