X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=switch%2Fdatapath.c;h=fb688af60e2bfb3c13c987e2b69e38e3246f7b55;hb=4d90ee6aef909d1a982164694d9e5902035730bf;hp=2055e094f1884e35d50dedb827402c911325c09a;hpb=d2552c15f34eb5ed86db02820599a65493e39ca7;p=sliver-openvswitch.git diff --git a/switch/datapath.c b/switch/datapath.c index 2055e094f..fb688af60 100644 --- a/switch/datapath.c +++ b/switch/datapath.c @@ -35,10 +35,12 @@ #include #include #include +#include #include #include #include "buffer.h" #include "chain.h" +#include "csum.h" #include "flow.h" #include "netdev.h" #include "packets.h" @@ -54,7 +56,10 @@ #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) \ @@ -74,16 +79,25 @@ struct sw_port { unsigned long long int rx_count, tx_count, drop_count; }; +/* The origin of a received OpenFlow message, to enable sending a reply. */ +struct sender { + struct remote *remote; /* The device that sent the message. */ + uint32_t xid; /* The OpenFlow transaction ID. */ +}; + /* A connection to a controller or a management device. */ struct remote { struct list node; struct rconn *rconn; -}; -/* The origin of a received OpenFlow message, to enable sending a reply. */ -struct sender { - struct remote *remote; /* The device that sent the message. */ - uint32_t xid; /* The OpenFlow transaction ID. */ + /* Support for reliable, multi-message replies to requests. + * + * If an incoming request needs to have a reliable reply that might + * require multiple messages, it can use remote_start_dump() to set up + * a callback that will be called as buffer space for replies. */ + int (*cb_dump)(struct datapath *, void *aux); + void (*cb_done)(void *aux); + void *cb_aux; }; struct datapath { @@ -154,24 +168,13 @@ static int port_no(struct datapath *dp, struct sw_port *p) 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 @@ -207,13 +210,29 @@ int 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)]) { @@ -244,7 +263,7 @@ dp_add_listen_vconn(struct datapath *dp, struct vconn *listen_vconn) } void -dp_run(struct datapath *dp) +dp_run(struct datapath *dp) { time_t now = time(0); struct sw_port *p, *pn; @@ -319,28 +338,43 @@ remote_run(struct datapath *dp, struct remote *r) rconn_run(r->rconn); - /* Process a number of commands from the remote, but cap them at a - * reasonable number so that other processing doesn't starve. */ + /* Do some remote processing, but cap it at a reasonable amount so that + * other processing doesn't starve. */ for (i = 0; i < 50; i++) { - struct buffer *buffer; - struct ofp_header *oh; + if (!r->cb_dump) { + struct buffer *buffer; + struct ofp_header *oh; - buffer = rconn_recv(r->rconn); - if (!buffer) { - break; - } + buffer = rconn_recv(r->rconn); + if (!buffer) { + break; + } - if (buffer->size >= sizeof *oh) { - struct sender sender; + if (buffer->size >= sizeof *oh) { + struct sender sender; - oh = buffer->data; - sender.remote = r; - sender.xid = oh->xid; - fwd_control_input(dp, &sender, buffer->data, buffer->size); + oh = buffer->data; + sender.remote = r; + sender.xid = oh->xid; + fwd_control_input(dp, &sender, buffer->data, buffer->size); + } else { + VLOG_WARN("received too-short OpenFlow message"); + } + buffer_delete(buffer); } else { - VLOG_WARN("received too-short OpenFlow message"); + if (!rconn_is_full(r->rconn)) { + int error = r->cb_dump(dp, r->cb_aux); + if (error <= 0) { + if (error) { + VLOG_WARN("dump callback error: %s", strerror(-error)); + } + r->cb_done(r->cb_aux); + r->cb_dump = NULL; + } + } else { + break; + } } - buffer_delete(buffer); } if (!rconn_is_alive(r->rconn)) { @@ -359,6 +393,9 @@ static void remote_destroy(struct remote *r) { if (r) { + if (r->cb_dump && r->cb_done) { + r->cb_done(r->cb_aux); + } list_remove(&r->node); rconn_destroy(r->rconn); free(r); @@ -371,9 +408,36 @@ remote_create(struct datapath *dp, struct rconn *rconn) struct remote *remote = xmalloc(sizeof *remote); list_push_back(&dp->remotes, &remote->node); remote->rconn = rconn; + remote->cb_dump = NULL; return remote; } +/* Starts a callback-based, reliable, possibly multi-message reply to a + * request made by 'remote'. + * + * 'dump' designates a function that will be called when the 'remote' send + * queue has an empty slot. It should compose a message and send it on + * 'remote'. On success, it should return 1 if it should be called again when + * another send queue slot opens up, 0 if its transmissions are complete, or a + * negative errno value on failure. + * + * 'done' designates a function to clean up any resources allocated for the + * dump. It must handle being called before the dump is complete (which will + * happen if 'remote' is closed unexpectedly). + * + * 'aux' is passed to 'dump' and 'done'. */ +static void +remote_start_dump(struct remote *remote, + int (*dump)(struct datapath *, void *), + void (*done)(void *), + void *aux) +{ + assert(!remote->cb_dump); + remote->cb_dump = dump; + remote->cb_done = done; + remote->cb_aux = aux; +} + void dp_wait(struct datapath *dp) { @@ -417,15 +481,21 @@ dp_destroy(struct datapath *dp) 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) { @@ -470,28 +540,34 @@ dp_output_port(struct datapath *dp, struct buffer *buffer, 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); + } } 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 @@ -500,12 +576,9 @@ send_openflow_buffer(struct datapath *dp, struct buffer *buffer, { 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", @@ -568,12 +641,11 @@ dp_send_features_reply(struct datapath *dp, const struct sender *sender) 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_mac_only = htonl(TABLE_MAC_MAX_FLOWS); - ofr->n_compression = 0; /* Not supported */ + ofr->n_compression = 0; /* Not supported */ ofr->n_general = htonl(TABLE_LINEAR_MAX_FLOWS); ofr->buffer_mb = htonl(UINT32_MAX); ofr->n_buffers = htonl(N_PKT_BUFFERS); @@ -590,16 +662,17 @@ dp_send_features_reply(struct datapath *dp, const struct sender *sender) 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 @@ -607,10 +680,11 @@ send_port_status(struct sw_port *p, uint8_t status) { struct buffer *buffer; struct ofp_port_status *ops; - ops = alloc_openflow_buffer(p->dp, sizeof *ops, OFPT_PORT_STATUS, NULL, - &buffer); - ops->reason = status; + 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); + send_openflow_buffer(p->dp, buffer, NULL); } @@ -619,130 +693,63 @@ send_flow_expired(struct datapath *dp, struct sw_flow *flow) { 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); - ofe->duration = htonl(flow->timeout - flow->max_idle - flow->created); - ofe->packet_count = htonll(flow->packet_count); - ofe->byte_count = htonll(flow->byte_count); - send_openflow_buffer(dp, buffer, NULL); -} - -static void -fill_flow_stats(struct ofp_flow_stats *ofs, struct sw_flow *flow, - int table_idx, time_t now) -{ - ofs->match.wildcards = htons(flow->key.wildcards); - ofs->match.in_port = flow->key.flow.in_port; - memcpy(ofs->match.dl_src, flow->key.flow.dl_src, ETH_ADDR_LEN); - memcpy(ofs->match.dl_dst, flow->key.flow.dl_dst, ETH_ADDR_LEN); - ofs->match.dl_vlan = flow->key.flow.dl_vlan; - ofs->match.dl_type = flow->key.flow.dl_type; - ofs->match.nw_src = flow->key.flow.nw_src; - ofs->match.nw_dst = flow->key.flow.nw_dst; - ofs->match.nw_proto = flow->key.flow.nw_proto; - memset(ofs->match.pad, 0, sizeof ofs->match.pad); - ofs->match.tp_src = flow->key.flow.tp_src; - ofs->match.tp_dst = flow->key.flow.tp_dst; - ofs->duration = htonl(now - flow->created); - ofs->table_id = table_idx; - ofs->packet_count = htonll(flow->packet_count); - ofs->byte_count = htonll(flow->byte_count); -} -int -dp_send_flow_stats(struct datapath *dp, const struct sender *sender, - const struct ofp_match *match) -{ - struct buffer *buffer; - struct ofp_flow_stat_reply *fsr; - size_t header_size, fudge, flow_size; - struct sw_flow_key match_key; - int table_idx, n_flows, max_flows; - time_t now; + memset(ofe->pad, 0, sizeof ofe->pad); + ofe->priority = htons(flow->priority); - header_size = offsetof(struct ofp_flow_stat_reply, flows); - fudge = 128; - flow_size = sizeof fsr->flows[0]; - max_flows = (65536 - header_size - fudge) / flow_size; - fsr = alloc_openflow_buffer(dp, header_size, - OFPT_FLOW_STAT_REPLY, sender, &buffer); - - n_flows = 0; - flow_extract_match(&match_key, match); - now = time(0); - for (table_idx = 0; table_idx < dp->chain->n_tables; table_idx++) { - struct sw_table *table = dp->chain->tables[table_idx]; - struct swt_iterator iter; - - if (n_flows >= max_flows) { - break; - } - - if (!table->iterator(table, &iter)) { - printf("iterator failed for table %d\n", table_idx); - continue; - } - - for (; iter.flow; table->iterator_next(&iter)) { - if (flow_matches(&match_key, &iter.flow->key)) { - struct ofp_flow_stats *ofs = buffer_put_uninit(buffer, - sizeof *ofs); - fill_flow_stats(ofs, iter.flow, table_idx, now); - if (++n_flows >= max_flows) { - break; - } - } - } - table->iterator_destroy(&iter); - } - return send_openflow_buffer(dp, buffer, sender); + ofe->duration = htonl(flow->timeout - flow->max_idle - flow->created); + ofe->packet_count = htonll(flow->packet_count); + ofe->byte_count = htonll(flow->byte_count); + send_openflow_buffer(dp, buffer, NULL); } -int -dp_send_port_stats(struct datapath *dp, const struct sender *sender) +void +dp_send_error_msg(struct datapath *dp, const struct sender *sender, + uint16_t type, uint16_t code, const uint8_t *data, size_t len) { - struct buffer *buffer; - struct ofp_port_stat_reply *psr; - struct sw_port *p; - - psr = alloc_openflow_buffer(dp, offsetof(struct ofp_port_stat_reply, - ports), - OFPT_PORT_STAT_REPLY, sender, &buffer); - LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) { - struct ofp_port_stats *ps = buffer_put_uninit(buffer, sizeof *ps); - ps->port_no = htons(port_no(dp, p)); - memset(ps->pad, 0, sizeof ps->pad); - ps->rx_count = htonll(p->rx_count); - ps->tx_count = htonll(p->tx_count); - ps->drop_count = htonll(p->drop_count); - } - return send_openflow_buffer(dp, buffer, sender); + struct buffer *buffer; + struct ofp_error_msg *oem; + 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); + send_openflow_buffer(dp, buffer, sender); } -int -dp_send_table_stats(struct datapath *dp, const struct sender *sender) +static void +fill_flow_stats(struct buffer *buffer, struct sw_flow *flow, + int table_idx, time_t now) { - struct buffer *buffer; - struct ofp_table_stat_reply *tsr; - int i; - - tsr = alloc_openflow_buffer(dp, offsetof(struct ofp_table_stat_reply, - tables), - OFPT_TABLE_STAT_REPLY, sender, &buffer); - for (i = 0; i < dp->chain->n_tables; i++) { - struct ofp_table_stats *ots = buffer_put_uninit(buffer, sizeof *ots); - struct sw_table_stats stats; - dp->chain->tables[i]->stats(dp->chain->tables[i], &stats); - strncpy(ots->name, stats.name, sizeof ots->name); - ots->table_id = i; - ots->pad[0] = ots->pad[1] = 0; - ots->max_entries = htonl(stats.max_flows); - ots->active_count = htonl(stats.n_flows); - ots->matched_count = htonll(0); /* FIXME */ - } - return send_openflow_buffer(dp, buffer, sender); + struct ofp_flow_stats *ofs; + int length = sizeof *ofs + sizeof *ofs->actions * flow->n_actions; + ofs = buffer_put_uninit(buffer, length); + ofs->length = htons(length); + ofs->table_id = table_idx; + ofs->pad = 0; + ofs->match.wildcards = htons(flow->key.wildcards); + ofs->match.in_port = flow->key.flow.in_port; + memcpy(ofs->match.dl_src, flow->key.flow.dl_src, ETH_ADDR_LEN); + memcpy(ofs->match.dl_dst, flow->key.flow.dl_dst, ETH_ADDR_LEN); + ofs->match.dl_vlan = flow->key.flow.dl_vlan; + ofs->match.dl_type = flow->key.flow.dl_type; + ofs->match.nw_src = flow->key.flow.nw_src; + ofs->match.nw_dst = flow->key.flow.nw_dst; + ofs->match.nw_proto = flow->key.flow.nw_proto; + memset(ofs->match.pad, 0, sizeof ofs->match.pad); + ofs->match.tp_src = flow->key.flow.tp_src; + ofs->match.tp_dst = flow->key.flow.tp_dst; + ofs->duration = htonl(now - flow->created); + ofs->packet_count = htonll(flow->packet_count); + ofs->byte_count = htonll(flow->byte_count); + ofs->priority = htons(flow->priority); + ofs->max_idle = htons(flow->max_idle); + memcpy(ofs->actions, flow->actions, + sizeof *ofs->actions * flow->n_actions); } + /* 'buffer' was received on 'in_port', a physical switch port between 0 and * OFPP_MAX. Process it according to 'chain'. */ @@ -839,34 +846,6 @@ execute_actions(struct datapath *dp, struct buffer *buffer, 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) { @@ -922,12 +901,12 @@ modify_vlan(struct buffer *buffer, 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; @@ -975,13 +954,13 @@ recv_get_config_request(struct datapath *dp, const struct sender *sender, 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); + memcpy(((char *)osc) + sizeof osc->header, + ((char *)&dp->config) + sizeof dp->config.header, + sizeof dp->config - sizeof dp->config.header); return send_openflow_buffer(dp, buffer, sender); } @@ -1043,14 +1022,23 @@ add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm) { int error = -ENOMEM; int n_acts; + int i; struct sw_flow *flow; - /* Check number of actions. */ + /* To prevent loops, make sure there's no action to send to the + * OFP_TABLE virtual port. + */ n_acts = (ntohs(ofm->header.length) - sizeof *ofm) / sizeof *ofm->actions; - if (n_acts > MAX_ACTIONS) { - error = -E2BIG; - goto error; + for (i=0; iactions[i]; + + if (a->type == htons(OFPAT_OUTPUT) + && (a->arg.output.port == htons(OFPP_TABLE) + || a->arg.output.port == htons(OFPP_NONE))) { + /* xxx Send fancy new error message? */ + goto error; + } } /* Allocate memory. */ @@ -1060,8 +1048,8 @@ add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm) /* Fill out flow. */ flow_extract_match(&flow->key, &ofm->match); - flow->group_id = ntohl(ofm->group_id); flow->max_idle = ntohs(ofm->max_idle); + flow->priority = flow->key.wildcards ? ntohs(ofm->priority) : -1; flow->timeout = time(0) + flow->max_idle; /* FIXME */ flow->n_actions = n_acts; flow->created = time(0); /* FIXME */ @@ -1109,41 +1097,375 @@ recv_flow(struct datapath *dp, const struct sender *sender UNUSED, } else if (command == OFPFC_DELETE) { struct sw_flow_key key; flow_extract_match(&key, &ofm->match); - return chain_delete(dp->chain, &key, 0) ? 0 : -ESRCH; + return chain_delete(dp->chain, &key, 0, 0) ? 0 : -ESRCH; } else if (command == OFPFC_DELETE_STRICT) { struct sw_flow_key key; + uint16_t priority; flow_extract_match(&key, &ofm->match); - return chain_delete(dp->chain, &key, 1) ? 0 : -ESRCH; + priority = key.wildcards ? ntohs(ofm->priority) : -1; + return chain_delete(dp->chain, &key, priority, 1) ? 0 : -ESRCH; } else { return -ENODEV; } } +struct flow_stats_state { + int table_idx; + struct sw_table_position position; + struct ofp_flow_stats_request rq; + time_t now; + + struct buffer *buffer; +}; + +#define MAX_FLOW_STATS_BYTES 4096 + +static int flow_stats_init(struct datapath *dp, const void *body, int body_len, + void **state) +{ + const struct ofp_flow_stats_request *fsr = body; + struct flow_stats_state *s = xmalloc(sizeof *s); + s->table_idx = fsr->table_id == 0xff ? 0 : fsr->table_id; + memset(&s->position, 0, sizeof s->position); + s->rq = *fsr; + *state = s; + return 0; +} + +static int flow_stats_dump_callback(struct sw_flow *flow, void *private) +{ + struct flow_stats_state *s = private; + fill_flow_stats(s->buffer, flow, s->table_idx, s->now); + return s->buffer->size >= MAX_FLOW_STATS_BYTES; +} + +static int flow_stats_dump(struct datapath *dp, void *state, + struct buffer *buffer) +{ + struct flow_stats_state *s = state; + struct sw_flow_key match_key; + + flow_extract_match(&match_key, &s->rq.match); + s->buffer = buffer; + s->now = time(0); + while (s->table_idx < dp->chain->n_tables + && (s->rq.table_id == 0xff || s->rq.table_id == s->table_idx)) + { + struct sw_table *table = dp->chain->tables[s->table_idx]; + + if (table->iterate(table, &match_key, &s->position, + flow_stats_dump_callback, s)) + break; + + s->table_idx++; + memset(&s->position, 0, sizeof s->position); + } + return s->buffer->size >= MAX_FLOW_STATS_BYTES; +} + +static void flow_stats_done(void *state) +{ + free(state); +} + +struct aggregate_stats_state { + struct ofp_aggregate_stats_request rq; +}; + +static int aggregate_stats_init(struct datapath *dp, + const void *body, int body_len, + void **state) +{ + const struct ofp_aggregate_stats_request *rq = body; + struct aggregate_stats_state *s = xmalloc(sizeof *s); + s->rq = *rq; + *state = s; + return 0; +} + +static int aggregate_stats_dump_callback(struct sw_flow *flow, void *private) +{ + struct ofp_aggregate_stats_reply *rpy = private; + rpy->packet_count += flow->packet_count; + rpy->byte_count += flow->byte_count; + rpy->flow_count++; + return 0; +} + +static int aggregate_stats_dump(struct datapath *dp, void *state, + struct buffer *buffer) +{ + struct aggregate_stats_state *s = state; + struct ofp_aggregate_stats_request *rq = &s->rq; + struct ofp_aggregate_stats_reply *rpy; + struct sw_table_position position; + struct sw_flow_key match_key; + int table_idx; + + rpy = buffer_put_uninit(buffer, sizeof *rpy); + memset(rpy, 0, sizeof *rpy); + + flow_extract_match(&match_key, &rq->match); + table_idx = rq->table_id == 0xff ? 0 : rq->table_id; + memset(&position, 0, sizeof position); + while (table_idx < dp->chain->n_tables + && (rq->table_id == 0xff || rq->table_id == table_idx)) + { + struct sw_table *table = dp->chain->tables[table_idx]; + int error; + + error = table->iterate(table, &match_key, &position, + aggregate_stats_dump_callback, rpy); + if (error) + return error; + + table_idx++; + memset(&position, 0, sizeof position); + } + + rpy->packet_count = htonll(rpy->packet_count); + rpy->byte_count = htonll(rpy->byte_count); + rpy->flow_count = htonl(rpy->flow_count); + return 0; +} + +static void aggregate_stats_done(void *state) +{ + free(state); +} + +static int table_stats_dump(struct datapath *dp, void *state, + struct buffer *buffer) +{ + int i; + for (i = 0; i < dp->chain->n_tables; i++) { + struct ofp_table_stats *ots = buffer_put_uninit(buffer, sizeof *ots); + struct sw_table_stats stats; + dp->chain->tables[i]->stats(dp->chain->tables[i], &stats); + strncpy(ots->name, stats.name, sizeof ots->name); + ots->table_id = i; + 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 */ + } + return 0; +} + +struct port_stats_state { + int port; +}; + +static int port_stats_init(struct datapath *dp, const void *body, int body_len, + void **state) +{ + struct port_stats_state *s = xmalloc(sizeof *s); + s->port = 0; + *state = s; + return 0; +} + +static int port_stats_dump(struct datapath *dp, void *state, + struct buffer *buffer) +{ + struct port_stats_state *s = state; + int i; + + for (i = s->port; i < OFPP_MAX; i++) { + struct sw_port *p = &dp->ports[i]; + struct ofp_port_stats *ops; + if (!p->netdev) { + continue; + } + ops = buffer_put_uninit(buffer, sizeof *ops); + ops->port_no = htons(port_no(dp, p)); + memset(ops->pad, 0, sizeof ops->pad); + ops->rx_count = htonll(p->rx_count); + ops->tx_count = htonll(p->tx_count); + ops->drop_count = htonll(p->drop_count); + ops++; + } + s->port = i; + return 0; +} + +static void port_stats_done(void *state) +{ + free(state); +} + +struct stats_type { + /* Minimum and maximum acceptable number of bytes in body member of + * struct ofp_stats_request. */ + size_t min_body, max_body; + + /* Prepares to dump some kind of statistics on 'dp'. 'body' and + * 'body_len' are the 'body' member of the struct ofp_stats_request. + * Returns zero if successful, otherwise a negative error code. + * May initialize '*state' to state information. May be null if no + * initialization is required.*/ + int (*init)(struct datapath *dp, const void *body, int body_len, + void **state); + + /* Appends statistics for 'dp' to 'buffer', which initially contains a + * struct ofp_stats_reply. On success, it should return 1 if it should be + * called again later with another buffer, 0 if it is done, or a negative + * errno value on failure. */ + int (*dump)(struct datapath *dp, void *state, struct buffer *buffer); + + /* Cleans any state created by the init or dump functions. May be null + * if no cleanup is required. */ + void (*done)(void *state); +}; + +static const struct stats_type stats[] = { + [OFPST_FLOW] = { + sizeof(struct ofp_flow_stats_request), + sizeof(struct ofp_flow_stats_request), + flow_stats_init, + flow_stats_dump, + flow_stats_done + }, + [OFPST_AGGREGATE] = { + sizeof(struct ofp_aggregate_stats_request), + sizeof(struct ofp_aggregate_stats_request), + aggregate_stats_init, + aggregate_stats_dump, + aggregate_stats_done + }, + [OFPST_TABLE] = { + 0, + 0, + NULL, + table_stats_dump, + NULL + }, + [OFPST_PORT] = { + 0, + 0, + port_stats_init, + port_stats_dump, + port_stats_done + }, +}; + +struct stats_dump_cb { + bool done; + struct ofp_stats_request *rq; + struct sender sender; + const struct stats_type *s; + void *state; +}; + +static int +stats_dump(struct datapath *dp, void *cb_) +{ + struct stats_dump_cb *cb = cb_; + struct ofp_stats_reply *osr; + struct buffer *buffer; + int err; + + if (cb->done) { + return 0; + } + + osr = make_openflow_reply(sizeof *osr, OFPT_STATS_REPLY, &cb->sender, + &buffer); + osr->type = htons(cb->s - stats); + osr->flags = 0; + + err = cb->s->dump(dp, cb->state, buffer); + if (err >= 0) { + int err2; + if (!err) { + cb->done = true; + } else { + /* Buffer might have been reallocated, so find our data again. */ + osr = buffer_at_assert(buffer, 0, sizeof *osr); + osr->flags = ntohs(OFPSF_REPLY_MORE); + } + err2 = send_openflow_buffer(dp, buffer, &cb->sender); + if (err2) { + err = err2; + } + } + + return err; +} + +static void +stats_done(void *cb_) +{ + struct stats_dump_cb *cb = cb_; + if (cb) { + if (cb->s->done) { + cb->s->done(cb->state); + } + free(cb); + } +} + static int -recv_flow_status_request(struct datapath *dp, const struct sender *sender, - const void *msg) +recv_stats_request(struct datapath *dp, const struct sender *sender, + const void *oh) { - const struct ofp_flow_stat_request *fsr = msg; - if (fsr->type == OFPFS_INDIV) { - return dp_send_flow_stats(dp, sender, &fsr->match); - } else { - /* FIXME */ - return -ENOSYS; - } + const struct ofp_stats_request *rq = oh; + size_t rq_len = ntohs(rq->header.length); + struct stats_dump_cb *cb; + int type, body_len; + int err; + + type = ntohs(rq->type); + if (type >= ARRAY_SIZE(stats) || !stats[type].dump) { + VLOG_WARN("received stats request of unknown type %d", type); + return -EINVAL; + } + + cb = xmalloc(sizeof *cb); + cb->done = false; + cb->rq = xmemdup(rq, rq_len); + cb->sender = *sender; + cb->s = &stats[type]; + cb->state = NULL; + + body_len = rq_len - offsetof(struct ofp_stats_request, body); + if (body_len < cb->s->min_body || body_len > cb->s->max_body) { + VLOG_WARN("stats request type %d with bad body length %d", + type, body_len); + err = -EINVAL; + goto error; + } + + if (cb->s->init) { + err = cb->s->init(dp, rq->body, body_len, &cb->state); + if (err) { + VLOG_WARN("failed initialization of stats request type %d: %s", + type, strerror(-err)); + goto error; + } + } + + remote_start_dump(sender->remote, stats_dump, stats_done, cb); + return 0; + +error: + free(cb->rq); + free(cb); + return err; } static int -recv_port_status_request(struct datapath *dp, const struct sender *sender, - const void *msg) +recv_echo_request(struct datapath *dp, const struct sender *sender, + const void *oh) { - return dp_send_port_stats(dp, sender); + return send_openflow_buffer(dp, make_echo_reply(oh), sender); } static int -recv_table_status_request(struct datapath *dp, const struct sender *sender, - const void *msg) +recv_echo_reply(struct datapath *dp UNUSED, const struct sender *sender UNUSED, + const void *oh UNUSED) { - return dp_send_table_stats(dp, sender); + return 0; } /* 'msg', which is 'length' bytes long, was received from the control path. @@ -1182,25 +1504,25 @@ fwd_control_input(struct datapath *dp, const struct sender *sender, sizeof (struct ofp_port_mod), recv_port_mod, }, - [OFPT_FLOW_STAT_REQUEST] = { - sizeof (struct ofp_flow_stat_request), - recv_flow_status_request, - }, - [OFPT_PORT_STAT_REQUEST] = { - sizeof (struct ofp_port_stat_request), - recv_port_status_request, - }, - [OFPT_TABLE_STAT_REQUEST] = { - sizeof (struct ofp_table_stat_request), - recv_table_status_request, - }, + [OFPT_STATS_REQUEST] = { + 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 != 1 || oh->type >= ARRAY_SIZE(packets) + if (oh->version != OFP_VERSION || oh->type >= ARRAY_SIZE(packets) || ntohs(oh->length) > length) return -EINVAL;