-handle_port_stats_request(struct ofconn *ofconn, struct ofp_stats_request *osr,
- size_t arg_size)
-{
- struct ofproto *p = ofconn->ofproto;
- struct ofp_port_stats_request *psr;
- struct ofp_port_stats *ops;
- struct ofpbuf *msg;
- struct ofport *port;
-
- if (arg_size != sizeof *psr) {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
- psr = (struct ofp_port_stats_request *) osr->body;
-
- msg = start_ofp_stats_reply(osr, sizeof *ops * 16);
- if (psr->port_no != htons(OFPP_NONE)) {
- port = get_port(p, ofp_port_to_odp_port(ntohs(psr->port_no)));
- if (port) {
- append_port_stat(port, ofconn, &msg);
- }
- } else {
- HMAP_FOR_EACH (port, hmap_node, &p->ports) {
- append_port_stat(port, ofconn, &msg);
- }
- }
-
- queue_tx(msg, ofconn, ofconn->reply_counter);
- return 0;
-}
-
-/* Obtains statistic counters for 'rule' within 'p' and stores them into
- * '*packet_countp' and '*byte_countp'. The returned statistics include
- * statistics for all of 'rule''s facets. */
-static void
-query_stats(struct ofproto *p, struct rule *rule,
- uint64_t *packet_countp, uint64_t *byte_countp)
-{
- uint64_t packet_count, byte_count;
- struct facet *facet;
- struct odp_flow *odp_flows;
- size_t n_odp_flows;
-
- /* Start from historical data for 'rule' itself that are no longer tracked
- * by the datapath. This counts, for example, facets that have expired. */
- packet_count = rule->packet_count;
- byte_count = rule->byte_count;
-
- /* Prepare to ask the datapath for statistics on all of the rule's facets.
- *
- * Also, add any statistics that are not tracked by the datapath for each
- * facet. This includes, for example, statistics for packets that were
- * executed "by hand" by ofproto via dpif_execute() but must be accounted
- * to a rule. */
- odp_flows = xzalloc(list_size(&rule->facets) * sizeof *odp_flows);
- n_odp_flows = 0;
- LIST_FOR_EACH (facet, list_node, &rule->facets) {
- struct odp_flow *odp_flow = &odp_flows[n_odp_flows++];
- odp_flow_key_from_flow(&odp_flow->key, &facet->flow);
- packet_count += facet->packet_count;
- byte_count += facet->byte_count;
- }
-
- /* Fetch up-to-date statistics from the datapath and add them in. */
- if (!dpif_flow_get_multiple(p->dpif, odp_flows, n_odp_flows)) {
- size_t i;
-
- for (i = 0; i < n_odp_flows; i++) {
- struct odp_flow *odp_flow = &odp_flows[i];
- packet_count += odp_flow->stats.n_packets;
- byte_count += odp_flow->stats.n_bytes;
- }
- }
- free(odp_flows);
-
- /* Return the stats to the caller. */
- *packet_countp = packet_count;
- *byte_countp = byte_count;
-}
-
-static void
-calc_flow_duration(long long int start, ovs_be32 *sec, ovs_be32 *nsec)
-{
- long long int msecs = time_msec() - start;
- *sec = htonl(msecs / 1000);
- *nsec = htonl((msecs % 1000) * (1000 * 1000));
-}
-
-static void
-put_ofp_flow_stats(struct ofconn *ofconn, struct rule *rule,
- ovs_be16 out_port, struct ofpbuf **replyp)
-{
- struct ofp_flow_stats *ofs;
- uint64_t packet_count, byte_count;
- size_t act_len, len;
-
- if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
- return;
- }
-
- act_len = sizeof *rule->actions * rule->n_actions;
- len = offsetof(struct ofp_flow_stats, actions) + act_len;
-
- query_stats(ofconn->ofproto, rule, &packet_count, &byte_count);
-
- ofs = append_ofp_stats_reply(len, ofconn, replyp);
- ofs->length = htons(len);
- ofs->table_id = 0;
- ofs->pad = 0;
- ofputil_cls_rule_to_match(&rule->cr, ofconn->flow_format, &ofs->match);
- calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
- ofs->cookie = rule->flow_cookie;
- ofs->priority = htons(rule->cr.priority);
- ofs->idle_timeout = htons(rule->idle_timeout);
- ofs->hard_timeout = htons(rule->hard_timeout);
- memset(ofs->pad2, 0, sizeof ofs->pad2);
- ofs->packet_count = htonll(packet_count);
- ofs->byte_count = htonll(byte_count);
- if (rule->n_actions > 0) {
- memcpy(ofs->actions, rule->actions, act_len);
- }
-}
-
-static bool
-is_valid_table(uint8_t table_id)
-{
- return table_id == 0 || table_id == 0xff;
-}
-
-static int
-handle_flow_stats_request(struct ofconn *ofconn,
- const struct ofp_stats_request *osr, size_t arg_size)
-{
- struct ofp_flow_stats_request *fsr;
- struct ofpbuf *reply;
-
- if (arg_size != sizeof *fsr) {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
- fsr = (struct ofp_flow_stats_request *) osr->body;
-
- COVERAGE_INC(ofproto_flows_req);
- reply = start_ofp_stats_reply(osr, 1024);
- if (is_valid_table(fsr->table_id)) {
- struct cls_cursor cursor;
- struct cls_rule target;
- struct rule *rule;
-
- ofputil_cls_rule_from_match(&fsr->match, 0, NXFF_OPENFLOW10, 0,
- &target);
- cls_cursor_init(&cursor, &ofconn->ofproto->cls, &target);
- CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
- put_ofp_flow_stats(ofconn, rule, fsr->out_port, &reply);
- }
- }
- queue_tx(reply, ofconn, ofconn->reply_counter);
-
- return 0;
-}
-
-static void
-put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule,
- ovs_be16 out_port, struct ofpbuf **replyp)
-{
- struct nx_flow_stats *nfs;
- uint64_t packet_count, byte_count;
- size_t act_len, start_len;
- struct ofpbuf *reply;
-
- if (rule_is_hidden(rule) || !rule_has_out_port(rule, out_port)) {
- return;
- }
-
- query_stats(ofconn->ofproto, rule, &packet_count, &byte_count);
-
- act_len = sizeof *rule->actions * rule->n_actions;
-
- start_len = (*replyp)->size;
- append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp);
- reply = *replyp;
-
- nfs = ofpbuf_put_uninit(reply, sizeof *nfs);
- nfs->table_id = 0;
- nfs->pad = 0;
- calc_flow_duration(rule->created, &nfs->duration_sec, &nfs->duration_nsec);
- nfs->cookie = rule->flow_cookie;
- nfs->priority = htons(rule->cr.priority);
- nfs->idle_timeout = htons(rule->idle_timeout);
- nfs->hard_timeout = htons(rule->hard_timeout);
- nfs->match_len = htons(nx_put_match(reply, &rule->cr));
- memset(nfs->pad2, 0, sizeof nfs->pad2);
- nfs->packet_count = htonll(packet_count);
- nfs->byte_count = htonll(byte_count);
- if (rule->n_actions > 0) {
- ofpbuf_put(reply, rule->actions, act_len);
- }
- nfs->length = htons(reply->size - start_len);
-}
-
-static int
-handle_nxst_flow(struct ofconn *ofconn, struct ofpbuf *b)
-{
- struct nx_flow_stats_request *nfsr;
- struct cls_rule target;
- struct ofpbuf *reply;
- int error;
-
- /* Dissect the message. */
- nfsr = ofpbuf_try_pull(b, sizeof *nfsr);
- if (!nfsr) {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
- error = nx_pull_match(b, ntohs(nfsr->match_len), 0, &target);
- if (error) {
- return error;
- }
-
- COVERAGE_INC(ofproto_flows_req);
- reply = start_nxstats_reply(&nfsr->nsm, 1024);
- if (is_valid_table(nfsr->table_id)) {
- struct cls_cursor cursor;
- struct rule *rule;
-
- cls_cursor_init(&cursor, &ofconn->ofproto->cls, &target);
- CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
- put_nx_flow_stats(ofconn, rule, nfsr->out_port, &reply);
- }
- }
- queue_tx(reply, ofconn, ofconn->reply_counter);
-
- return 0;
-}
-
-static void
-flow_stats_ds(struct ofproto *ofproto, struct rule *rule, struct ds *results)
-{
- struct ofp_match match;
- uint64_t packet_count, byte_count;
- size_t act_len = sizeof *rule->actions * rule->n_actions;
-
- query_stats(ofproto, rule, &packet_count, &byte_count);
- ofputil_cls_rule_to_match(&rule->cr, NXFF_OPENFLOW10, &match);
-
- ds_put_format(results, "duration=%llds, ",
- (time_msec() - rule->created) / 1000);
- ds_put_format(results, "priority=%u, ", rule->cr.priority);
- ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
- ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
- ofp_print_match(results, &match, true);
- if (act_len > 0) {
- ofp_print_actions(results, &rule->actions->header, act_len);
- } else {
- ds_put_cstr(results, "drop");
- }
- ds_put_cstr(results, "\n");
-}
-
-/* Adds a pretty-printed description of all flows to 'results', including
- * those marked hidden by secchan (e.g., by in-band control). */
-void
-ofproto_get_all_flows(struct ofproto *p, struct ds *results)
-{
- struct cls_cursor cursor;
- struct rule *rule;
-
- cls_cursor_init(&cursor, &p->cls, NULL);
- CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
- flow_stats_ds(p, rule, results);
- }
-}
-
-static void
-query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target,
- ovs_be16 out_port, uint8_t table_id,
- struct ofp_aggregate_stats_reply *oasr)
-{
- uint64_t total_packets = 0;
- uint64_t total_bytes = 0;
- int n_flows = 0;
-
- COVERAGE_INC(ofproto_agg_request);
-
- if (is_valid_table(table_id)) {
- struct cls_cursor cursor;
- struct rule *rule;
-
- cls_cursor_init(&cursor, &ofproto->cls, target);
- CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
- if (!rule_is_hidden(rule) && rule_has_out_port(rule, out_port)) {
- uint64_t packet_count;
- uint64_t byte_count;
-
- query_stats(ofproto, rule, &packet_count, &byte_count);
-
- total_packets += packet_count;
- total_bytes += byte_count;
- n_flows++;
- }
- }
- }
-
- oasr->flow_count = htonl(n_flows);
- oasr->packet_count = htonll(total_packets);
- oasr->byte_count = htonll(total_bytes);
- memset(oasr->pad, 0, sizeof oasr->pad);
-}
-
-static int
-handle_aggregate_stats_request(struct ofconn *ofconn,
- const struct ofp_stats_request *osr,
- size_t arg_size)
-{
- struct ofp_aggregate_stats_request *request;
- struct ofp_aggregate_stats_reply *reply;
- struct cls_rule target;
- struct ofpbuf *msg;
-
- if (arg_size != sizeof *request) {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
- request = (struct ofp_aggregate_stats_request *) osr->body;
-
- ofputil_cls_rule_from_match(&request->match, 0, NXFF_OPENFLOW10, 0,
- &target);
-
- msg = start_ofp_stats_reply(osr, sizeof *reply);
- reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg);
- query_aggregate_stats(ofconn->ofproto, &target, request->out_port,
- request->table_id, reply);
- queue_tx(msg, ofconn, ofconn->reply_counter);
- return 0;
-}
-
-static int
-handle_nxst_aggregate(struct ofconn *ofconn, struct ofpbuf *b)
-{
- struct nx_aggregate_stats_request *request;
- struct ofp_aggregate_stats_reply *reply;
- struct cls_rule target;
- struct ofpbuf *buf;
- int error;
-
- /* Dissect the message. */
- request = ofpbuf_try_pull(b, sizeof *request);
- if (!request) {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
- error = nx_pull_match(b, ntohs(request->match_len), 0, &target);
- if (error) {
- return error;
- }
-
- /* Reply. */
- COVERAGE_INC(ofproto_flows_req);
- buf = start_nxstats_reply(&request->nsm, sizeof *reply);
- reply = ofpbuf_put_uninit(buf, sizeof *reply);
- query_aggregate_stats(ofconn->ofproto, &target, request->out_port,
- request->table_id, reply);
- queue_tx(buf, ofconn, ofconn->reply_counter);
-
- return 0;
-}
-
-struct queue_stats_cbdata {
- struct ofconn *ofconn;
- struct ofport *ofport;
- struct ofpbuf *msg;
-};
-
-static void
-put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id,
- const struct netdev_queue_stats *stats)
-{
- struct ofp_queue_stats *reply;
-
- reply = append_ofp_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg);
- reply->port_no = htons(cbdata->ofport->opp.port_no);
- memset(reply->pad, 0, sizeof reply->pad);
- reply->queue_id = htonl(queue_id);
- reply->tx_bytes = htonll(stats->tx_bytes);
- reply->tx_packets = htonll(stats->tx_packets);
- reply->tx_errors = htonll(stats->tx_errors);
-}
-
-static void
-handle_queue_stats_dump_cb(uint32_t queue_id,
- struct netdev_queue_stats *stats,
- void *cbdata_)
-{
- struct queue_stats_cbdata *cbdata = cbdata_;
-
- put_queue_stats(cbdata, queue_id, stats);
-}
-
-static void
-handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
- struct queue_stats_cbdata *cbdata)
-{
- cbdata->ofport = port;
- if (queue_id == OFPQ_ALL) {
- netdev_dump_queue_stats(port->netdev,
- handle_queue_stats_dump_cb, cbdata);
- } else {
- struct netdev_queue_stats stats;
-
- if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
- put_queue_stats(cbdata, queue_id, &stats);
- }
- }
-}
-
-static int
-handle_queue_stats_request(struct ofconn *ofconn,
- const struct ofp_stats_request *osr,
- size_t arg_size)
-{
- struct ofproto *ofproto = ofconn->ofproto;
- struct ofp_queue_stats_request *qsr;
- struct queue_stats_cbdata cbdata;
- struct ofport *port;
- unsigned int port_no;
- uint32_t queue_id;
-
- if (arg_size != sizeof *qsr) {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
- qsr = (struct ofp_queue_stats_request *) osr->body;
-
- COVERAGE_INC(ofproto_queue_req);
-
- cbdata.ofconn = ofconn;
- cbdata.msg = start_ofp_stats_reply(osr, 128);
-
- port_no = ntohs(qsr->port_no);
- queue_id = ntohl(qsr->queue_id);
- if (port_no == OFPP_ALL) {
- HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
- handle_queue_stats_for_port(port, queue_id, &cbdata);
- }
- } else if (port_no < ofproto->max_ports) {
- port = get_port(ofproto, ofp_port_to_odp_port(port_no));
- if (port) {
- handle_queue_stats_for_port(port, queue_id, &cbdata);
- }
- } else {
- ofpbuf_delete(cbdata.msg);
- return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
- }
- queue_tx(cbdata.msg, ofconn, ofconn->reply_counter);
-
- return 0;
-}
-
-static int
-handle_vendor_stats_request(struct ofconn *ofconn,
- struct ofp_stats_request *osr, size_t arg_size)
-{
- struct nicira_stats_msg *nsm;
- struct ofpbuf b;
- ovs_be32 vendor;
-
- if (arg_size < 4) {
- VLOG_WARN_RL(&rl, "truncated vendor stats request body");
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
-
- memcpy(&vendor, osr->body, sizeof vendor);
- if (vendor != htonl(NX_VENDOR_ID)) {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
- }
-
- if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) {
- VLOG_WARN_RL(&rl, "truncated Nicira stats request");
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
-
- nsm = (struct nicira_stats_msg *) osr;
- b.data = nsm;
- b.size = ntohs(nsm->header.length);
- switch (ntohl(nsm->subtype)) {
- case NXST_FLOW:
- return handle_nxst_flow(ofconn, &b);
-
- case NXST_AGGREGATE:
- return handle_nxst_aggregate(ofconn, &b);
-
- default:
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE);
- }
-}
-
-static int
-handle_stats_request(struct ofconn *ofconn, struct ofp_header *oh)
-{
- struct ofp_stats_request *osr;
- size_t arg_size;
- int error;
-
- error = check_ofp_message_array(oh, OFPT_STATS_REQUEST, sizeof *osr,
- 1, &arg_size);
- if (error) {
- return error;
- }
- osr = (struct ofp_stats_request *) oh;
-
- switch (ntohs(osr->type)) {
- case OFPST_DESC:
- return handle_desc_stats_request(ofconn, osr);
-
- case OFPST_FLOW:
- return handle_flow_stats_request(ofconn, osr, arg_size);
-
- case OFPST_AGGREGATE:
- return handle_aggregate_stats_request(ofconn, osr, arg_size);
-
- case OFPST_TABLE:
- return handle_table_stats_request(ofconn, osr);
-
- case OFPST_PORT:
- return handle_port_stats_request(ofconn, osr, arg_size);
-
- case OFPST_QUEUE:
- return handle_queue_stats_request(ofconn, osr, arg_size);
-
- case OFPST_VENDOR:
- return handle_vendor_stats_request(ofconn, osr, arg_size);
-
- default:
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
- }
-}
-
-static long long int
-msec_from_nsec(uint64_t sec, uint32_t nsec)
-{
- return !sec ? 0 : sec * 1000 + nsec / 1000000;
-}
-
-static void
-facet_update_time(struct ofproto *ofproto, struct facet *facet,
- const struct odp_flow_stats *stats)
-{
- long long int used = msec_from_nsec(stats->used_sec, stats->used_nsec);
- if (used > facet->used) {
- facet->used = used;
- if (used > facet->rule->used) {
- facet->rule->used = used;
- }
- netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
- }
-}
-
-/* Folds the statistics from 'stats' into the counters in 'facet'.
- *
- * Because of the meaning of a facet's counters, it only makes sense to do this
- * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
- * packet that was sent by hand or if it represents statistics that have been
- * cleared out of the datapath. */
-static void
-facet_update_stats(struct ofproto *ofproto, struct facet *facet,
- const struct odp_flow_stats *stats)
-{
- if (stats->n_packets) {
- facet_update_time(ofproto, facet, stats);
- facet->packet_count += stats->n_packets;
- facet->byte_count += stats->n_bytes;
- netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
- }
-}
-
-struct flow_mod {
- struct cls_rule cr;
- ovs_be64 cookie;
- uint16_t command;
- uint16_t idle_timeout;
- uint16_t hard_timeout;
- uint32_t buffer_id;
- uint16_t out_port;
- uint16_t flags;
- union ofp_action *actions;
- size_t n_actions;
-};
-
-/* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
- * in which no matching flow already exists in the flow table.
- *
- * Adds the flow specified by 'ofm', which is followed by 'n_actions'
- * ofp_actions, to ofconn->ofproto's flow table. Returns 0 on success or an
- * OpenFlow error code as encoded by ofp_mkerr() on failure.
- *
- * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
- * if any. */
-static int
-add_flow(struct ofconn *ofconn, struct flow_mod *fm)
-{
- struct ofproto *p = ofconn->ofproto;
- struct ofpbuf *packet;
- struct rule *rule;
- uint16_t in_port;
- int error;
-
- if (fm->flags & OFPFF_CHECK_OVERLAP
- && classifier_rule_overlaps(&p->cls, &fm->cr)) {
- return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
- }
-
- error = 0;
- if (fm->buffer_id != UINT32_MAX) {
- error = pktbuf_retrieve(ofconn->pktbuf, fm->buffer_id,
- &packet, &in_port);
- } else {
- packet = NULL;
- in_port = UINT16_MAX;
- }
-
- rule = rule_create(&fm->cr, fm->actions, fm->n_actions,
- fm->idle_timeout, fm->hard_timeout, fm->cookie,
- fm->flags & OFPFF_SEND_FLOW_REM);
- rule_insert(p, rule);
- if (packet) {
- rule_execute(p, rule, in_port, packet);
- }
- return error;
-}
-
-static struct rule *
-find_flow_strict(struct ofproto *p, const struct flow_mod *fm)
-{
- return rule_from_cls_rule(classifier_find_rule_exactly(&p->cls, &fm->cr));
-}
-
-static int
-send_buffered_packet(struct ofconn *ofconn,
- struct rule *rule, uint32_t buffer_id)
-{
- struct ofpbuf *packet;
- uint16_t in_port;
- int error;
-
- if (buffer_id == UINT32_MAX) {
- return 0;
- }
-
- error = pktbuf_retrieve(ofconn->pktbuf, buffer_id, &packet, &in_port);
- if (error) {
- return error;
- }
-
- rule_execute(ofconn->ofproto, rule, in_port, packet);
-
- return 0;
-}
-\f
-/* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
-
-struct modify_flows_cbdata {
- struct ofproto *ofproto;
- const struct flow_mod *fm;
- struct rule *match;
-};
-
-static int modify_flow(struct ofproto *, const struct flow_mod *,
- struct rule *);
-
-/* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code as
- * encoded by ofp_mkerr() on failure.
- *
- * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
- * if any. */
-static int
-modify_flows_loose(struct ofconn *ofconn, struct flow_mod *fm)
-{
- struct ofproto *p = ofconn->ofproto;
- struct rule *match = NULL;
- struct cls_cursor cursor;
- struct rule *rule;
-
- cls_cursor_init(&cursor, &p->cls, &fm->cr);
- CLS_CURSOR_FOR_EACH (rule, cr, &cursor) {
- if (!rule_is_hidden(rule)) {
- match = rule;
- modify_flow(p, fm, rule);
- }
- }
-
- if (match) {
- /* This credits the packet to whichever flow happened to match last.
- * That's weird. Maybe we should do a lookup for the flow that
- * actually matches the packet? Who knows. */
- send_buffered_packet(ofconn, match, fm->buffer_id);
- return 0;
- } else {
- return add_flow(ofconn, fm);
- }
-}
-
-/* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error
- * code as encoded by ofp_mkerr() on failure.
- *
- * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
- * if any. */
-static int
-modify_flow_strict(struct ofconn *ofconn, struct flow_mod *fm)
-{
- struct ofproto *p = ofconn->ofproto;
- struct rule *rule = find_flow_strict(p, fm);
- if (rule && !rule_is_hidden(rule)) {
- modify_flow(p, fm, rule);
- return send_buffered_packet(ofconn, rule, fm->buffer_id);
- } else {
- return add_flow(ofconn, fm);
- }
-}
-
-/* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has
- * been identified as a flow in 'p''s flow table to be modified, by changing
- * the rule's actions to match those in 'ofm' (which is followed by 'n_actions'
- * ofp_action[] structures). */
-static int
-modify_flow(struct ofproto *p, const struct flow_mod *fm, struct rule *rule)
-{
- size_t actions_len = fm->n_actions * sizeof *rule->actions;
-
- rule->flow_cookie = fm->cookie;
-
- /* If the actions are the same, do nothing. */
- if (fm->n_actions == rule->n_actions
- && (!fm->n_actions
- || !memcmp(fm->actions, rule->actions, actions_len))) {
- return 0;
- }
-
- /* Replace actions. */
- free(rule->actions);
- rule->actions = fm->n_actions ? xmemdup(fm->actions, actions_len) : NULL;
- rule->n_actions = fm->n_actions;
-
- p->need_revalidate = true;
-
- return 0;
-}
-\f
-/* OFPFC_DELETE implementation. */
-
-static void delete_flow(struct ofproto *, struct rule *, ovs_be16 out_port);
-
-/* Implements OFPFC_DELETE. */
-static void
-delete_flows_loose(struct ofproto *p, const struct flow_mod *fm)
-{
- struct rule *rule, *next_rule;
- struct cls_cursor cursor;
-
- cls_cursor_init(&cursor, &p->cls, &fm->cr);
- CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) {
- delete_flow(p, rule, htons(fm->out_port));
- }
-}
-
-/* Implements OFPFC_DELETE_STRICT. */
-static void
-delete_flow_strict(struct ofproto *p, struct flow_mod *fm)
-{
- struct rule *rule = find_flow_strict(p, fm);
- if (rule) {
- delete_flow(p, rule, htons(fm->out_port));
- }
-}
-
-/* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has
- * been identified as a flow to delete from 'p''s flow table, by deleting the
- * flow and sending out a OFPT_FLOW_REMOVED message to any interested
- * controller.
- *
- * Will not delete 'rule' if it is hidden. Will delete 'rule' only if
- * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the
- * specified 'out_port'. */
-static void
-delete_flow(struct ofproto *p, struct rule *rule, ovs_be16 out_port)
-{
- if (rule_is_hidden(rule)) {
- return;
- }
-
- if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) {
- return;
- }
-
- rule_send_removed(p, rule, OFPRR_DELETE);
- rule_remove(p, rule);
-}
-\f
-static int
-flow_mod_core(struct ofconn *ofconn, struct flow_mod *fm)
-{
- struct ofproto *p = ofconn->ofproto;
- int error;
-
- error = reject_slave_controller(ofconn, "flow_mod");
- if (error) {
- return error;
- }
-
- error = validate_actions(fm->actions, fm->n_actions,
- &fm->cr.flow, p->max_ports);
- if (error) {
- return error;
- }
-
- /* We do not support the emergency flow cache. It will hopefully
- * get dropped from OpenFlow in the near future. */
- if (fm->flags & OFPFF_EMERG) {
- /* There isn't a good fit for an error code, so just state that the
- * flow table is full. */
- return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
- }
-
- switch (fm->command) {
- case OFPFC_ADD:
- return add_flow(ofconn, fm);
-
- case OFPFC_MODIFY:
- return modify_flows_loose(ofconn, fm);
-
- case OFPFC_MODIFY_STRICT:
- return modify_flow_strict(ofconn, fm);
-
- case OFPFC_DELETE:
- delete_flows_loose(p, fm);
- return 0;
-
- case OFPFC_DELETE_STRICT:
- delete_flow_strict(p, fm);
- return 0;
-
- default:
- return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
- }
-}
-
-static int
-handle_ofpt_flow_mod(struct ofconn *ofconn, struct ofp_header *oh)
-{
- struct ofp_match orig_match;
- struct ofp_flow_mod *ofm;
- struct flow_mod fm;
- struct ofpbuf b;
- int error;
-
- b.data = oh;
- b.size = ntohs(oh->length);
-
- /* Dissect the message. */
- ofm = ofpbuf_try_pull(&b, sizeof *ofm);
- if (!ofm) {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
- error = ofputil_pull_actions(&b, b.size, &fm.actions, &fm.n_actions);
- if (error) {
- return error;
- }
-
- /* Normalize ofm->match. If normalization actually changes anything, then
- * log the differences. */
- ofm->match.pad1[0] = ofm->match.pad2[0] = 0;
- orig_match = ofm->match;
- normalize_match(&ofm->match);
- if (memcmp(&ofm->match, &orig_match, sizeof orig_match)) {
- static struct vlog_rate_limit normal_rl = VLOG_RATE_LIMIT_INIT(1, 1);
- if (!VLOG_DROP_INFO(&normal_rl)) {
- char *old = ofp_match_to_literal_string(&orig_match);
- char *new = ofp_match_to_literal_string(&ofm->match);
- VLOG_INFO("%s: normalization changed ofp_match, details:",
- rconn_get_name(ofconn->rconn));
- VLOG_INFO(" pre: %s", old);
- VLOG_INFO("post: %s", new);
- free(old);
- free(new);
- }
- }
-
- /* Translate the message. */
- ofputil_cls_rule_from_match(&ofm->match, ntohs(ofm->priority),
- ofconn->flow_format, ofm->cookie, &fm.cr);
- fm.cookie = ofm->cookie;
- fm.command = ntohs(ofm->command);
- fm.idle_timeout = ntohs(ofm->idle_timeout);
- fm.hard_timeout = ntohs(ofm->hard_timeout);
- fm.buffer_id = ntohl(ofm->buffer_id);
- fm.out_port = ntohs(ofm->out_port);
- fm.flags = ntohs(ofm->flags);
-
- /* Execute the command. */
- return flow_mod_core(ofconn, &fm);
-}
-
-static int
-handle_nxt_flow_mod(struct ofconn *ofconn, struct ofp_header *oh)
-{
- struct nx_flow_mod *nfm;
- struct flow_mod fm;
- struct ofpbuf b;
- int error;
-
- b.data = oh;
- b.size = ntohs(oh->length);
-
- /* Dissect the message. */
- nfm = ofpbuf_try_pull(&b, sizeof *nfm);
- if (!nfm) {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
- error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
- &fm.cr);
- if (error) {
- return error;
- }
- error = ofputil_pull_actions(&b, b.size, &fm.actions, &fm.n_actions);
- if (error) {
- return error;
- }
-
- /* Translate the message. */
- fm.cookie = nfm->cookie;
- fm.command = ntohs(nfm->command);
- fm.idle_timeout = ntohs(nfm->idle_timeout);
- fm.hard_timeout = ntohs(nfm->hard_timeout);
- fm.buffer_id = ntohl(nfm->buffer_id);
- fm.out_port = ntohs(nfm->out_port);
- fm.flags = ntohs(nfm->flags);
-
- /* Execute the command. */
- return flow_mod_core(ofconn, &fm);
-}
-
-static int
-handle_tun_id_from_cookie(struct ofconn *ofconn, struct nxt_tun_id_cookie *msg)
-{
- int error;
-
- error = check_ofp_message(&msg->header, OFPT_VENDOR, sizeof *msg);
- if (error) {
- return error;
- }
-
- ofconn->flow_format = msg->set ? NXFF_TUN_ID_FROM_COOKIE : NXFF_OPENFLOW10;
- return 0;
-}
-
-static int
-handle_role_request(struct ofconn *ofconn, struct nicira_header *msg)
-{
- struct nx_role_request *nrr;
- struct nx_role_request *reply;
- struct ofpbuf *buf;
- uint32_t role;
-
- if (ntohs(msg->header.length) != sizeof *nrr) {
- VLOG_WARN_RL(&rl, "received role request of length %u (expected %zu)",
- ntohs(msg->header.length), sizeof *nrr);
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
- nrr = (struct nx_role_request *) msg;
-
- if (ofconn->type != OFCONN_PRIMARY) {
- VLOG_WARN_RL(&rl, "ignoring role request on non-controller "
- "connection");
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
- }
-
- role = ntohl(nrr->role);
- if (role != NX_ROLE_OTHER && role != NX_ROLE_MASTER
- && role != NX_ROLE_SLAVE) {
- VLOG_WARN_RL(&rl, "received request for unknown role %"PRIu32, role);
-
- /* There's no good error code for this. */
- return ofp_mkerr(OFPET_BAD_REQUEST, -1);
- }
-
- if (role == NX_ROLE_MASTER) {
- struct ofconn *other;
-
- HMAP_FOR_EACH (other, hmap_node, &ofconn->ofproto->controllers) {
- if (other->role == NX_ROLE_MASTER) {
- other->role = NX_ROLE_SLAVE;
- }
- }
- }
- ofconn->role = role;
-
- reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, msg->header.xid,
- &buf);
- reply->role = htonl(role);
- queue_tx(buf, ofconn, ofconn->reply_counter);
-
- return 0;
-}
-
-static int
-handle_nxt_set_flow_format(struct ofconn *ofconn,
- struct nxt_set_flow_format *msg)
-{
- uint32_t format;
- int error;
-
- error = check_ofp_message(&msg->header, OFPT_VENDOR, sizeof *msg);
- if (error) {
- return error;
- }
-
- format = ntohl(msg->format);
- if (format == NXFF_OPENFLOW10
- || format == NXFF_TUN_ID_FROM_COOKIE
- || format == NXFF_NXM) {
- ofconn->flow_format = format;
- return 0;
- } else {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
- }
-}
-
-static int
-handle_vendor(struct ofconn *ofconn, void *msg)
-{
- struct ofproto *p = ofconn->ofproto;
- struct ofp_vendor_header *ovh = msg;
- struct nicira_header *nh;
-
- if (ntohs(ovh->header.length) < sizeof(struct ofp_vendor_header)) {
- VLOG_WARN_RL(&rl, "received vendor message of length %u "
- "(expected at least %zu)",
- ntohs(ovh->header.length), sizeof(struct ofp_vendor_header));
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
- if (ovh->vendor != htonl(NX_VENDOR_ID)) {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
- }
- if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
- VLOG_WARN_RL(&rl, "received Nicira vendor message of length %u "
- "(expected at least %zu)",
- ntohs(ovh->header.length), sizeof(struct nicira_header));
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
-
- nh = msg;
- switch (ntohl(nh->subtype)) {
- case NXT_STATUS_REQUEST:
- return switch_status_handle_request(p->switch_status, ofconn->rconn,
- msg);
-
- case NXT_TUN_ID_FROM_COOKIE:
- return handle_tun_id_from_cookie(ofconn, msg);
-
- case NXT_ROLE_REQUEST:
- return handle_role_request(ofconn, msg);
-
- case NXT_SET_FLOW_FORMAT:
- return handle_nxt_set_flow_format(ofconn, msg);
-
- case NXT_FLOW_MOD:
- return handle_nxt_flow_mod(ofconn, &ovh->header);
- }
-
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE);
-}
-
-static int
-handle_barrier_request(struct ofconn *ofconn, struct ofp_header *oh)
-{
- struct ofp_header *ob;
- struct ofpbuf *buf;
-
- /* Currently, everything executes synchronously, so we can just
- * immediately send the barrier reply. */
- ob = make_openflow_xid(sizeof *ob, OFPT_BARRIER_REPLY, oh->xid, &buf);
- queue_tx(buf, ofconn, ofconn->reply_counter);
- return 0;
-}
-
-static void
-handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
-{
- struct ofp_header *oh = ofp_msg->data;
- int error;
-
- COVERAGE_INC(ofproto_recv_openflow);
- switch (oh->type) {
- case OFPT_ECHO_REQUEST:
- error = handle_echo_request(ofconn, oh);
- break;
-
- case OFPT_ECHO_REPLY:
- error = 0;
- break;
-
- case OFPT_FEATURES_REQUEST:
- error = handle_features_request(ofconn, oh);
- break;
-
- case OFPT_GET_CONFIG_REQUEST:
- error = handle_get_config_request(ofconn, oh);
- break;
-
- case OFPT_SET_CONFIG:
- error = handle_set_config(ofconn, ofp_msg->data);
- break;
-
- case OFPT_PACKET_OUT:
- error = handle_packet_out(ofconn, ofp_msg->data);
- break;
-
- case OFPT_PORT_MOD:
- error = handle_port_mod(ofconn, oh);
- break;
-
- case OFPT_FLOW_MOD:
- error = handle_ofpt_flow_mod(ofconn, ofp_msg->data);
- break;
-
- case OFPT_STATS_REQUEST:
- error = handle_stats_request(ofconn, oh);
- break;
-
- case OFPT_VENDOR:
- error = handle_vendor(ofconn, ofp_msg->data);
- break;
-
- case OFPT_BARRIER_REQUEST:
- error = handle_barrier_request(ofconn, oh);
- break;
-
- default:
- if (VLOG_IS_WARN_ENABLED()) {
- char *s = ofp_to_string(oh, ntohs(oh->length), 2);
- VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
- free(s);
- }
- error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
- break;
- }
-
- if (error) {
- send_error_oh(ofconn, ofp_msg->data, error);
- }
-}
-\f
-static void
-handle_odp_miss_msg(struct ofproto *p, struct ofpbuf *packet)
-{
- struct odp_msg *msg = packet->data;
- struct ofpbuf payload;
- struct facet *facet;
- struct flow flow;
-
- payload.data = msg + 1;
- payload.size = msg->length - sizeof *msg;
- flow_extract(&payload, msg->arg, msg->port, &flow);
-
- packet->l2 = payload.l2;
- packet->l3 = payload.l3;
- packet->l4 = payload.l4;
- packet->l7 = payload.l7;