/*
- * 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.
#include "collectors.h"
#include "dpif.h"
#include "compiler.h"
+#include "hash.h"
+#include "hmap.h"
#include "netdev.h"
+#include "netlink.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 {
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);
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)
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;
}
counters->ifDirection = 0;
}
if (!netdev_get_flags(osp->netdev, &flags) && flags & NETDEV_UP) {
- bool carrier;
-
counters->ifStatus = 1; /* ifAdminStatus up. */
- if (!netdev_get_carrier(osp->netdev, &carrier) && carrier) {
+ if (netdev_get_carrier(osp->netdev)) {
counters->ifStatus |= 2; /* ifOperStatus us. */
}
} else {
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;
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);
}
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;
}
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);
}
}
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 and sampler. */
if (os->sflow_agent) {
}
}
+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);
- sfl_agent_removeSampler(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);
}
}
struct ofproto_sflow_port *osp;
bool options_changed;
SFLReceiver *receiver;
- unsigned int odp_port;
SFLAddress agentIP;
time_t now;
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,
MAX(1, UINT32_MAX / options->sampling_rate));
/* 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);
+ HMAP_FOR_EACH (osp, hmap_node, &os->ports) {
+ ofproto_sflow_add_poller(os, osp, osp->odp_port);
ofproto_sflow_add_sampler(os, osp);
}
}
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;
}
void
-ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg)
+ofproto_sflow_received(struct ofproto_sflow *os,
+ const struct dpif_upcall *upcall,
+ const struct flow *flow)
{
SFL_FLOW_SAMPLE_TYPE fs;
SFLFlow_sample_element hdrElem;
SFLSampled_header *header;
SFLFlow_sample_element switchElem;
SFLSampler *sampler;
- const struct odp_sflow_sample_header *hdr;
- const union odp_action *actions;
- struct ofpbuf payload;
- size_t n_actions, n_outputs;
- size_t min_size;
- flow_t flow;
- size_t i;
-
- /* Get odp_sflow_sample_header. */
- min_size = sizeof *msg + sizeof *hdr;
- if (min_size > msg->length) {
- VLOG_WARN_RL(&rl, "sFlow packet too small (%"PRIu32" < %zu)",
- msg->length, min_size);
- return;
- }
- hdr = (const struct odp_sflow_sample_header *) (msg + 1);
-
- /* Get actions. */
- n_actions = hdr->n_actions;
- if (n_actions > 65536 / sizeof *actions) {
- VLOG_WARN_RL(&rl, "too many actions in sFlow packet (%zu > %zu)",
- 65536 / sizeof *actions, n_actions);
- return;
- }
- min_size += n_actions * sizeof *actions;
- if (min_size > msg->length) {
- VLOG_WARN_RL(&rl, "sFlow packet with %zu actions too small "
- "(%"PRIu32" < %zu)",
- n_actions, msg->length, min_size);
- return;
- }
- actions = (const union odp_action *) (hdr + 1);
-
- /* Get packet payload and extract flow. */
- payload.data = (union odp_action *) (actions + n_actions);
- payload.size = msg->length - min_size;
- flow_extract(&payload, 0, msg->port, &flow);
+ unsigned int left;
+ struct nlattr *a;
+ size_t n_outputs;
/* Build a flow sample */
memset(&fs, 0, sizeof fs);
- fs.input = ofproto_sflow_odp_port_to_ifindex(os, msg->port);
+ fs.input = ofproto_sflow_odp_port_to_ifindex(os, flow->in_port);
fs.output = 0; /* Filled in correctly below. */
- fs.sample_pool = hdr->sample_pool;
+ fs.sample_pool = upcall->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
header->header_protocol = SFLHEADER_ETHERNET_ISO8023;
/* 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;
+ header->frame_length = upcall->packet->size + 4;
/* Ethernet FCS stripped off. */
header->stripped = 4;
- header->header_length = MIN(payload.size,
+ header->header_length = MIN(upcall->packet->size,
sampler->sFlowFsMaximumHeaderSize);
- header->header_bytes = payload.data;
+ header->header_bytes = upcall->packet->data;
/* Add extended switch element. */
memset(&switchElem, 0, sizeof(switchElem));
switchElem.tag = SFLFLOW_EX_SWITCH;
- switchElem.flowType.sw.src_vlan = ntohs(flow.dl_vlan);
- switchElem.flowType.sw.src_priority = -1; /* XXX */
+ switchElem.flowType.sw.src_vlan = vlan_tci_to_vid(flow->vlan_tci);
+ switchElem.flowType.sw.src_priority = vlan_tci_to_pcp(flow->vlan_tci);
/* 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;
/* Figure out the output ports. */
n_outputs = 0;
- for (i = 0; i < n_actions; i++) {
- const union odp_action *a = &actions[i];
+ NL_ATTR_FOR_EACH_UNSAFE (a, left, upcall->actions, upcall->actions_len) {
+ ovs_be16 tci;
- switch (a->type) {
+ switch (nl_attr_type(a)) {
case ODPAT_OUTPUT:
- fs.output = ofproto_sflow_odp_port_to_ifindex(os, a->output.port);
+ fs.output = ofproto_sflow_odp_port_to_ifindex(os,
+ nl_attr_get_u32(a));
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 = ntohs(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 = nl_attr_get_be16(a);
+ switchElem.flowType.sw.dst_vlan = vlan_tci_to_vid(tci);
+ switchElem.flowType.sw.dst_priority = vlan_tci_to_pcp(tci);
break;
default:
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;
}
}
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);
}
}