#include <string.h>
#include "buffer.h"
#include "chain.h"
+#include "csum.h"
#include "flow.h"
#include "netdev.h"
#include "packets.h"
#define BRIDGE_PORT_NO_FLOOD 0x00000001
/* Capabilities supported by this implementation. */
-#define OFP_SUPPORTED_CAPABILITIES (OFPC_MULTI_PHY_TX)
+#define OFP_SUPPORTED_CAPABILITIES ( OFPC_FLOW_STATS \
+ | OFPC_TABLE_STATS \
+ | OFPC_PORT_STATS \
+ | OFPC_MULTI_PHY_TX )
/* Actions supported by this implementation. */
#define OFP_SUPPORTED_ACTIONS ( (1 << OFPAT_OUTPUT) \
struct sw_chain *chain; /* Forwarding rules. */
- struct ofp_switch_config config;
+ /* Configuration set from controller. */
+ uint16_t flags;
+ uint16_t miss_send_len;
/* Switch ports. */
struct sw_port ports[OFPP_MAX];
#define PKT_COOKIE_BITS (32 - PKT_BUFFER_BITS)
+int run_flow_through_tables(struct datapath *, struct buffer *, int in_port);
void fwd_port_input(struct datapath *, struct buffer *, int in_port);
int fwd_control_input(struct datapath *, const struct sender *,
const void *, size_t);
return p - dp->ports;
}
-/* Generates a unique datapath id. It incorporates the datapath index
- * and a hardware address, if available. If not, it generates a random
- * one.
- */
+/* Generates and returns a random datapath id. */
static uint64_t
gen_datapath_id(void)
{
- /* Choose a random datapath id. */
- uint64_t id = 0;
- int i;
-
- srand(time(0));
-
- for (i = 0; i < ETH_ADDR_LEN; i++) {
- id |= (uint64_t)(rand() & 0xff) << (8*(ETH_ADDR_LEN-1 - i));
- }
-
- return id;
+ uint8_t ea[ETH_ADDR_LEN];
+ eth_addr_random(ea);
+ return eth_addr_to_uint64(ea);
}
int
}
list_init(&dp->port_list);
- dp->config.flags = 0;
- dp->config.miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
+ dp->flags = 0;
+ dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
*dp_ = dp;
return 0;
}
dp_add_port(struct datapath *dp, const char *name)
{
struct netdev *netdev;
+ struct in6_addr in6;
+ struct in_addr in4;
struct sw_port *p;
int error;
- error = netdev_open(name, &netdev);
+ error = netdev_open(name, NETDEV_ETH_TYPE_ANY, &netdev);
if (error) {
return error;
}
+ error = netdev_set_flags(netdev, NETDEV_UP | NETDEV_PROMISC, false);
+ if (error) {
+ VLOG_ERR("Couldn't set promiscuous mode on %s device", name);
+ netdev_close(netdev);
+ return error;
+ }
+ if (netdev_get_in4(netdev, &in4)) {
+ VLOG_ERR("%s device has assigned IP address %s", name, inet_ntoa(in4));
+ }
+ if (netdev_get_in6(netdev, &in6)) {
+ char in6_name[INET6_ADDRSTRLEN + 1];
+ inet_ntop(AF_INET6, &in6, in6_name, sizeof in6_name);
+ VLOG_ERR("%s device has assigned IPv6 address %s", name, in6_name);
+ }
for (p = dp->ports; ; p++) {
if (p >= &dp->ports[ARRAY_SIZE(dp->ports)]) {
free(dp);
}
+/* Send packets out all the ports except the originating one. If the
+ * "flood" argument is set, don't send out ports with flooding disabled.
+ */
static int
-flood(struct datapath *dp, struct buffer *buffer, int in_port)
+output_all(struct datapath *dp, struct buffer *buffer, int in_port, int flood)
{
struct sw_port *p;
int prev_port;
prev_port = -1;
LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
- if (port_no(dp, p) == in_port || p->flags & BRIDGE_PORT_NO_FLOOD) {
+ if (port_no(dp, p) == in_port) {
+ continue;
+ }
+ if (flood && p->flags & BRIDGE_PORT_NO_FLOOD) {
continue;
}
if (prev_port != -1) {
assert(buffer);
if (out_port == OFPP_FLOOD) {
- flood(dp, buffer, in_port);
+ output_all(dp, buffer, in_port, 1);
+ } else if (out_port == OFPP_ALL) {
+ output_all(dp, buffer, in_port, 0);
} else if (out_port == OFPP_CONTROLLER) {
dp_output_control(dp, buffer, in_port, 0, OFPR_ACTION);
} else if (out_port == OFPP_TABLE) {
- struct sw_flow_key key;
- struct sw_flow *flow;
-
- key.wildcards = 0;
- flow_extract(buffer, in_port, &key.flow);
- flow = chain_lookup(dp->chain, &key);
- if (flow != NULL) {
- flow_used(flow, buffer);
- execute_actions(dp, buffer, in_port, &key,
- flow->actions, flow->n_actions);
+ if (run_flow_through_tables(dp, buffer, in_port)) {
+ buffer_delete(buffer);
}
} else {
output_packet(dp, buffer, out_port);
}
static void *
-alloc_openflow_buffer(struct datapath *dp, size_t openflow_len, uint8_t type,
- const struct sender *sender, struct buffer **bufferp)
+make_openflow_reply(size_t openflow_len, uint8_t type,
+ const struct sender *sender, struct buffer **bufferp)
{
- struct buffer *buffer;
- struct ofp_header *oh;
-
- buffer = *bufferp = buffer_new(openflow_len);
- oh = buffer_put_uninit(buffer, openflow_len);
- oh->version = OFP_VERSION;
- oh->type = type;
- oh->length = 0; /* Filled in by send_openflow_buffer(). */
- oh->xid = sender ? sender->xid : 0;
- return oh;
+ return make_openflow_xid(openflow_len, type, sender ? sender->xid : 0,
+ bufferp);
}
static int
{
struct remote *remote = sender ? sender->remote : dp->controller;
struct rconn *rconn = remote->rconn;
- struct ofp_header *oh;
int retval;
- oh = buffer_at_assert(buffer, 0, sizeof *oh);
- oh->length = htons(buffer->size);
-
+ update_openflow_length(buffer);
retval = rconn_send(rconn, buffer);
if (retval) {
VLOG_WARN("send to %s failed: %s",
struct ofp_switch_features *ofr;
struct sw_port *p;
- ofr = alloc_openflow_buffer(dp, sizeof *ofr, OFPT_FEATURES_REPLY,
- sender, &buffer);
+ ofr = make_openflow_reply(sizeof *ofr, OFPT_FEATURES_REPLY,
+ sender, &buffer);
ofr->datapath_id = htonll(dp->id);
ofr->n_exact = htonl(2 * TABLE_HASH_MAX_FLOWS);
ofr->n_compression = 0; /* Not supported */
void
dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
{
- struct sw_port *p;
-
- p = &dp->ports[htons(opp->port_no)];
+ int port_no = ntohs(opp->port_no);
+ if (port_no < OFPP_MAX) {
+ struct sw_port *p = &dp->ports[port_no];
- /* Make sure the port id hasn't changed since this was sent */
- if (!p || memcmp(opp->hw_addr, netdev_get_etheraddr(p->netdev),
- ETH_ADDR_LEN) != 0)
- return;
-
- p->flags = htonl(opp->flags);
+ /* Make sure the port id hasn't changed since this was sent */
+ if (!p || memcmp(opp->hw_addr, netdev_get_etheraddr(p->netdev),
+ ETH_ADDR_LEN) != 0) {
+ return;
+ }
+ p->flags = htonl(opp->flags);
+ }
}
static void
{
struct buffer *buffer;
struct ofp_port_status *ops;
- ops = alloc_openflow_buffer(p->dp, sizeof *ops, OFPT_PORT_STATUS, NULL,
- &buffer);
+ ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &buffer);
ops->reason = status;
memset(ops->pad, 0, sizeof ops->pad);
fill_port_desc(p->dp, p, &ops->desc);
{
struct buffer *buffer;
struct ofp_flow_expired *ofe;
- ofe = alloc_openflow_buffer(dp, sizeof *ofe, OFPT_FLOW_EXPIRED, NULL,
- &buffer);
+ ofe = make_openflow_xid(sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &buffer);
flow_fill_match(&ofe->match, &flow->key);
memset(ofe->pad, 0, sizeof ofe->pad);
{
struct buffer *buffer;
struct ofp_error_msg *oem;
- oem = alloc_openflow_buffer(dp, sizeof(*oem)+len, OFPT_ERROR_MSG,
- sender, &buffer);
+ oem = make_openflow_reply(sizeof(*oem)+len, OFPT_ERROR_MSG,
+ sender, &buffer);
oem->type = htons(type);
oem->code = htons(code);
memcpy(oem->data, data, len);
\f
/* 'buffer' was received on 'in_port', a physical switch port between 0 and
- * OFPP_MAX. Process it according to 'chain'. */
-void fwd_port_input(struct datapath *dp, struct buffer *buffer, int in_port)
+ * OFPP_MAX. Process it according to 'dp''s flow table. Returns 0 if
+ * successful, in which case 'buffer' is destroyed, or -ESRCH if there is no
+ * matching flow, in which case 'buffer' still belongs to the caller. */
+int run_flow_through_tables(struct datapath *dp, struct buffer *buffer,
+ int in_port)
{
struct sw_flow_key key;
struct sw_flow *flow;
key.wildcards = 0;
- flow_extract(buffer, in_port, &key.flow);
+ if (flow_extract(buffer, in_port, &key.flow)
+ && (dp->flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP) {
+ /* Drop fragment. */
+ buffer_delete(buffer);
+ return 0;
+ }
+
flow = chain_lookup(dp->chain, &key);
if (flow != NULL) {
flow_used(flow, buffer);
execute_actions(dp, buffer, in_port, &key,
flow->actions, flow->n_actions);
+ return 0;
} else {
- dp_output_control(dp, buffer, in_port, ntohs(dp->config.miss_send_len),
+ return -ESRCH;
+ }
+}
+
+/* 'buffer' was received on 'in_port', a physical switch port between 0 and
+ * OFPP_MAX. Process it according to 'dp''s flow table, sending it up to the
+ * controller if no flow matches. Takes ownership of 'buffer'. */
+void fwd_port_input(struct datapath *dp, struct buffer *buffer, int in_port)
+{
+ if (run_flow_through_tables(dp, buffer, in_port)) {
+ dp_output_control(dp, buffer, in_port, dp->miss_send_len,
OFPR_NO_MATCH);
}
}
buffer_delete(buffer);
}
-/* Returns the new checksum for a packet in which the checksum field previously
- * contained 'old_csum' and in which a field that contained 'old_u16' was
- * changed to contain 'new_u16'. */
-static uint16_t
-recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
-{
- /* Ones-complement arithmetic is endian-independent, so this code does not
- * use htons() or ntohs().
- *
- * See RFC 1624 for formula and explanation. */
- uint16_t hc_complement = ~old_csum;
- uint16_t m_complement = ~old_u16;
- uint16_t m_prime = new_u16;
- uint32_t sum = hc_complement + m_complement + m_prime;
- uint16_t hc_prime_complement = sum + (sum >> 16);
- return ~hc_prime_complement;
-}
-
-/* Returns the new checksum for a packet in which the checksum field previously
- * contained 'old_csum' and in which a field that contained 'old_u32' was
- * changed to contain 'new_u32'. */
-static uint16_t
-recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
-{
- return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
- old_u32 >> 16, new_u32 >> 16);
-}
-
static void modify_nh(struct buffer *buffer, uint16_t eth_proto,
uint8_t nw_proto, const struct ofp_action *a)
{
uint16_t new_id = a->arg.vlan_id;
struct vlan_eth_header *veh;
- if (new_id != OFP_VLAN_NONE) {
+ if (new_id != htons(OFP_VLAN_NONE)) {
if (key->flow.dl_vlan != htons(OFP_VLAN_NONE)) {
/* Modify vlan id, but maintain other TCI values */
veh = buffer->l2;
veh->veth_tci &= ~htons(VLAN_VID);
- veh->veth_tci |= htons(new_id);
+ veh->veth_tci |= new_id;
} else {
/* Insert new vlan id. */
struct eth_header *eh = buffer->l2;
struct buffer *buffer;
struct ofp_switch_config *osc;
- osc = alloc_openflow_buffer(dp, sizeof *osc, OFPT_GET_CONFIG_REPLY,
- sender, &buffer);
+ osc = make_openflow_reply(sizeof *osc, OFPT_GET_CONFIG_REPLY,
+ sender, &buffer);
- assert(sizeof *osc == sizeof dp->config);
- memcpy(((char *)osc) + sizeof osc->header,
- ((char *)&dp->config) + sizeof dp->config.header,
- sizeof dp->config - sizeof dp->config.header);
+ osc->flags = htons(dp->flags);
+ osc->miss_send_len = htons(dp->miss_send_len);
return send_openflow_buffer(dp, buffer, sender);
}
const void *msg)
{
const struct ofp_switch_config *osc = msg;
- dp->config = *osc;
+ dp->flags = ntohs(osc->flags);
+ dp->miss_send_len = ntohs(osc->miss_send_len);
return 0;
}
const struct ofp_action *a = &ofm->actions[i];
if (a->type == htons(OFPAT_OUTPUT)
- && a->arg.output.port == htons(OFPP_TABLE)) {
+ && (a->arg.output.port == htons(OFPP_TABLE)
+ || a->arg.output.port == htons(OFPP_NONE))) {
/* xxx Send fancy new error message? */
goto error;
}
memset(ots->pad, 0, sizeof ots->pad);
ots->max_entries = htonl(stats.max_flows);
ots->active_count = htonl(stats.n_flows);
- ots->matched_count = htonll(0); /* FIXME */
+ ots->matched_count = htonll(stats.n_matched);
}
return 0;
}
return 0;
}
- osr = alloc_openflow_buffer(dp, sizeof *osr, OFPT_STATS_REPLY, &cb->sender,
- &buffer);
+ osr = make_openflow_reply(sizeof *osr, OFPT_STATS_REPLY, &cb->sender,
+ &buffer);
osr->type = htons(cb->s - stats);
osr->flags = 0;
return err;
}
+static int
+recv_echo_request(struct datapath *dp, const struct sender *sender,
+ const void *oh)
+{
+ return send_openflow_buffer(dp, make_echo_reply(oh), sender);
+}
+
+static int
+recv_echo_reply(struct datapath *dp UNUSED, const struct sender *sender UNUSED,
+ const void *oh UNUSED)
+{
+ return 0;
+}
+
/* 'msg', which is 'length' bytes long, was received from the control path.
* Apply it to 'chain'. */
int
sizeof (struct ofp_stats_request),
recv_stats_request,
},
+ [OFPT_ECHO_REQUEST] = {
+ sizeof (struct ofp_header),
+ recv_echo_request,
+ },
+ [OFPT_ECHO_REPLY] = {
+ sizeof (struct ofp_header),
+ recv_echo_reply,
+ },
};
const struct openflow_packet *pkt;
struct ofp_header *oh;
oh = (struct ofp_header *) msg;
- if (oh->version != OFP_VERSION || oh->type >= ARRAY_SIZE(packets)
- || ntohs(oh->length) > length)
+ assert(oh->version == OFP_VERSION);
+ if (oh->type >= ARRAY_SIZE(packets) || ntohs(oh->length) > length)
return -EINVAL;
pkt = &packets[oh->type];