X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=ofproto%2Fofproto.c;h=00b573110e99ed71091d36911f87cff3e718d820;hb=6569d3f419c96b18ea81a41ed13f2306119bcc00;hp=d5f258ffe8c879a53c1287b3cf1a2ca495e1b5c5;hpb=bbc32a883f790a331b412eecc6de4b7cdf1e63cb;p=openvswitch diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c index d5f258ff..00b57311 100644 --- a/ofproto/ofproto.c +++ b/ofproto/ofproto.c @@ -90,6 +90,8 @@ COVERAGE_DEFINE(ofproto_update_port); #include "sflow_api.h" +struct rule; + struct ofport { struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */ struct netdev *netdev; @@ -100,11 +102,48 @@ struct ofport { static void ofport_free(struct ofport *); static void hton_ofp_phy_port(struct ofp_phy_port *); -static int xlate_actions(const union ofp_action *in, size_t n_in, - const struct flow *, struct ofproto *, - const struct ofpbuf *packet, - struct odp_actions *out, tag_type *tags, - bool *may_set_up_flow, uint16_t *nf_output_iface); +struct action_xlate_ctx { +/* action_xlate_ctx_init() initializes these members. */ + + /* The ofproto. */ + struct ofproto *ofproto; + + /* Flow to which the OpenFlow actions apply. xlate_actions() will modify + * this flow when actions change header fields. */ + struct flow flow; + + /* The packet corresponding to 'flow', or a null pointer if we are + * revalidating without a packet to refer to. */ + const struct ofpbuf *packet; + + /* If nonnull, called just before executing a resubmit action. + * + * This is normally null so the client has to set it manually after + * calling action_xlate_ctx_init(). */ + void (*resubmit_hook)(struct action_xlate_ctx *, const struct rule *); + +/* xlate_actions() initializes and uses these members. The client might want + * to look at them after it returns. */ + + /* Datapath action set. This is xlate_actions()'s primary output. */ + struct odp_actions out; + + tag_type tags; /* Tags associated with OFPP_NORMAL actions. */ + bool may_set_up_flow; /* True ordinarily; false if the actions must + * be reassessed for every packet. */ + uint16_t nf_output_iface; /* Output interface index for NetFlow. */ + +/* xlate_actions() initializes and uses these members, but the client has no + * reason to look at them. */ + + int recurse; /* Recursion level, via xlate_table_action. */ +}; + +static void action_xlate_ctx_init(struct action_xlate_ctx *, + struct ofproto *, const struct flow *, + const struct ofpbuf *); +static int xlate_actions(struct action_xlate_ctx *ctx, + const union ofp_action *in, size_t n_in); /* An OpenFlow flow. */ struct rule { @@ -359,6 +398,9 @@ struct ofproto { struct mac_learning *ml; }; +/* Map from dpif name to struct ofproto, for use by unixctl commands. */ +static struct shash all_ofprotos = SHASH_INITIALIZER(&all_ofprotos); + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); static const struct ofhooks default_ofhooks; @@ -377,6 +419,8 @@ static void update_port(struct ofproto *, const char *devname); static int init_ports(struct ofproto *); static void reinit_ports(struct ofproto *); +static void ofproto_unixctl_init(void); + int ofproto_create(const char *datapath, const char *datapath_type, const struct ofhooks *ofhooks, void *aux, @@ -389,6 +433,8 @@ ofproto_create(const char *datapath, const char *datapath_type, *ofprotop = NULL; + ofproto_unixctl_init(); + /* Connect to datapath and start listening for messages. */ error = dpif_open(datapath, datapath_type, &dpif); if (error) { @@ -470,6 +516,8 @@ ofproto_create(const char *datapath, const char *datapath_type, p->datapath_id = pick_datapath_id(p); VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id); + shash_add_once(&all_ofprotos, dpif_name(p->dpif), p); + *ofprotop = p; return 0; } @@ -991,6 +1039,8 @@ ofproto_destroy(struct ofproto *p) return; } + shash_find_and_delete(&all_ofprotos, dpif_name(p->dpif)); + /* Destroy fail-open and in-band early, since they touch the classifier. */ fail_open_destroy(p->fail_open); p->fail_open = NULL; @@ -1349,18 +1399,18 @@ ofproto_send_packet(struct ofproto *p, const struct flow *flow, const union ofp_action *actions, size_t n_actions, const struct ofpbuf *packet) { - struct odp_actions odp_actions; + struct action_xlate_ctx ctx; int error; - error = xlate_actions(actions, n_actions, flow, p, packet, &odp_actions, - NULL, NULL, NULL); + action_xlate_ctx_init(&ctx, p, flow, packet); + error = xlate_actions(&ctx, actions, n_actions); if (error) { return error; } /* XXX Should we translate the dpif_execute() errno value into an OpenFlow * error code? */ - dpif_execute(p->dpif, odp_actions.actions, odp_actions.n_actions, packet); + dpif_execute(p->dpif, ctx.out.actions, ctx.out.n_actions, packet); return 0; } @@ -2069,8 +2119,8 @@ static void rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port, struct ofpbuf *packet) { + struct action_xlate_ctx ctx; struct facet *facet; - struct odp_actions a; struct flow flow; size_t size; @@ -2096,14 +2146,15 @@ rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port, /* We can't account anything to a facet. If we were to try, then that * facet would have a non-matching rule, busting our invariants. */ - if (xlate_actions(rule->actions, rule->n_actions, &flow, ofproto, - packet, &a, NULL, 0, NULL)) { + action_xlate_ctx_init(&ctx, ofproto, &flow, packet); + if (xlate_actions(&ctx, rule->actions, rule->n_actions)) { ofpbuf_delete(packet); return; } + size = packet->size; if (execute_odp_actions(ofproto, in_port, - a.actions, a.n_actions, packet)) { + ctx.out.actions, ctx.out.n_actions, packet)) { rule->used = time_msec(); rule->packet_count++; rule->byte_count += size; @@ -2195,19 +2246,21 @@ facet_make_actions(struct ofproto *p, struct facet *facet, const struct ofpbuf *packet) { const struct rule *rule = facet->rule; - struct odp_actions a; + struct action_xlate_ctx ctx; size_t actions_len; - xlate_actions(rule->actions, rule->n_actions, &facet->flow, p, - packet, &a, &facet->tags, &facet->may_install, - &facet->nf_flow.output_iface); + action_xlate_ctx_init(&ctx, p, &facet->flow, packet); + xlate_actions(&ctx, rule->actions, rule->n_actions); + facet->tags = ctx.tags; + facet->may_install = ctx.may_set_up_flow; + facet->nf_flow.output_iface = ctx.nf_output_iface; - actions_len = a.n_actions * sizeof *a.actions; - if (facet->n_actions != a.n_actions - || memcmp(facet->actions, a.actions, actions_len)) { + actions_len = ctx.out.n_actions * sizeof *ctx.out.actions; + if (facet->n_actions != ctx.out.n_actions + || memcmp(facet->actions, ctx.out.actions, actions_len)) { free(facet->actions); - facet->n_actions = a.n_actions; - facet->actions = xmemdup(a.actions, actions_len); + facet->n_actions = ctx.out.n_actions; + facet->actions = xmemdup(ctx.out.actions, actions_len); } } @@ -2375,10 +2428,9 @@ facet_lookup_valid(struct ofproto *ofproto, const struct flow *flow) static bool facet_revalidate(struct ofproto *ofproto, struct facet *facet) { + struct action_xlate_ctx ctx; struct rule *new_rule; - struct odp_actions a; size_t actions_len; - uint16_t new_nf_output_iface; bool actions_changed; COVERAGE_INC(facet_revalidate); @@ -2396,12 +2448,12 @@ facet_revalidate(struct ofproto *ofproto, struct facet *facet) * We are very cautious about actually modifying 'facet' state at this * point, because we might need to, e.g., emit a NetFlow expiration and, if * so, we need to have the old state around to properly compose it. */ - xlate_actions(new_rule->actions, new_rule->n_actions, &facet->flow, - ofproto, NULL, &a, &facet->tags, &facet->may_install, - &new_nf_output_iface); - actions_len = a.n_actions * sizeof *a.actions; - actions_changed = (facet->n_actions != a.n_actions - || memcmp(facet->actions, a.actions, actions_len)); + action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL); + xlate_actions(&ctx, new_rule->actions, new_rule->n_actions); + actions_len = ctx.out.n_actions * sizeof *ctx.out.actions; + actions_changed = (facet->n_actions != ctx.out.n_actions + || memcmp(facet->actions, ctx.out.actions, + actions_len)); /* If the ODP actions changed or the installability changed, then we need * to talk to the datapath. */ @@ -2411,8 +2463,8 @@ facet_revalidate(struct ofproto *ofproto, struct facet *facet) memset(&put.flow.stats, 0, sizeof put.flow.stats); odp_flow_key_from_flow(&put.flow.key, &facet->flow); - put.flow.actions = a.actions; - put.flow.n_actions = a.n_actions; + put.flow.actions = ctx.out.actions; + put.flow.n_actions = ctx.out.n_actions; put.flow.flags = 0; put.flags = ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS; dpif_flow_put(ofproto->dpif, &put); @@ -2428,11 +2480,13 @@ facet_revalidate(struct ofproto *ofproto, struct facet *facet) } /* Update 'facet' now that we've taken care of all the old state. */ - facet->nf_flow.output_iface = new_nf_output_iface; + facet->tags = ctx.tags; + facet->nf_flow.output_iface = ctx.nf_output_iface; + facet->may_install = ctx.may_set_up_flow; if (actions_changed) { free(facet->actions); - facet->n_actions = a.n_actions; - facet->actions = xmemdup(a.actions, actions_len); + facet->n_actions = ctx.out.n_actions; + facet->actions = xmemdup(ctx.out.actions, actions_len); } if (facet->rule != new_rule) { COVERAGE_INC(facet_changed_rule); @@ -2572,23 +2626,6 @@ add_controller_action(struct odp_actions *actions, uint16_t max_len) a->controller.arg = max_len; } -struct action_xlate_ctx { - /* Input. */ - struct flow flow; /* Flow to which these actions correspond. */ - int recurse; /* Recursion level, via xlate_table_action. */ - struct ofproto *ofproto; - const struct ofpbuf *packet; /* The packet corresponding to 'flow', or a - * null pointer if we are revalidating - * without a packet to refer to. */ - - /* Output. */ - struct odp_actions *out; /* Datapath actions. */ - tag_type tags; /* Tags associated with OFPP_NORMAL actions. */ - bool may_set_up_flow; /* True ordinarily; false if the actions must - * be reassessed for every packet. */ - uint16_t nf_output_iface; /* Output interface index for NetFlow. */ -}; - /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a * flow translation. */ #define MAX_RESUBMIT_RECURSION 8 @@ -2614,7 +2651,7 @@ add_output_action(struct action_xlate_ctx *ctx, uint16_t port) */ } - odp_actions_add(ctx->out, ODPAT_OUTPUT)->output.port = port; + odp_actions_add(&ctx->out, ODPAT_OUTPUT)->output.port = port; ctx->nf_output_iface = port; } @@ -2639,6 +2676,10 @@ xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port) rule = rule_lookup(ctx->ofproto, &ctx->flow); ctx->flow.in_port = old_in_port; + if (ctx->resubmit_hook) { + ctx->resubmit_hook(ctx, rule); + } + if (rule) { ctx->recurse++; do_xlate_actions(rule->actions, rule->n_actions, ctx); @@ -2685,7 +2726,7 @@ xlate_output_action__(struct action_xlate_ctx *ctx, break; case OFPP_NORMAL: if (!ctx->ofproto->ofhooks->normal_cb(&ctx->flow, ctx->packet, - ctx->out, &ctx->tags, + &ctx->out, &ctx->tags, &ctx->nf_output_iface, ctx->ofproto->aux)) { COVERAGE_INC(ofproto_uninstallable); @@ -2694,14 +2735,14 @@ xlate_output_action__(struct action_xlate_ctx *ctx, break; case OFPP_FLOOD: flood_packets(ctx->ofproto, ctx->flow.in_port, OFPPC_NO_FLOOD, - &ctx->nf_output_iface, ctx->out); + &ctx->nf_output_iface, &ctx->out); break; case OFPP_ALL: flood_packets(ctx->ofproto, ctx->flow.in_port, 0, - &ctx->nf_output_iface, ctx->out); + &ctx->nf_output_iface, &ctx->out); break; case OFPP_CONTROLLER: - add_controller_action(ctx->out, max_len); + add_controller_action(&ctx->out, max_len); break; case OFPP_LOCAL: add_output_action(ctx, ODPP_LOCAL); @@ -2738,9 +2779,9 @@ xlate_output_action(struct action_xlate_ctx *ctx, static void remove_pop_action(struct action_xlate_ctx *ctx) { - size_t n = ctx->out->n_actions; - if (n > 0 && ctx->out->actions[n - 1].type == ODPAT_POP_PRIORITY) { - ctx->out->n_actions--; + size_t n = ctx->out.n_actions; + if (n > 0 && ctx->out.actions[n - 1].type == ODPAT_POP_PRIORITY) { + ctx->out.n_actions--; } } @@ -2770,10 +2811,10 @@ xlate_enqueue_action(struct action_xlate_ctx *ctx, /* Add ODP actions. */ remove_pop_action(ctx); - odp_actions_add(ctx->out, ODPAT_SET_PRIORITY)->priority.priority + odp_actions_add(&ctx->out, ODPAT_SET_PRIORITY)->priority.priority = priority; add_output_action(ctx, odp_port); - odp_actions_add(ctx->out, ODPAT_POP_PRIORITY); + odp_actions_add(&ctx->out, ODPAT_POP_PRIORITY); /* Update NetFlow output port. */ if (ctx->nf_output_iface == NF_OUT_DROP) { @@ -2799,7 +2840,7 @@ xlate_set_queue_action(struct action_xlate_ctx *ctx, } remove_pop_action(ctx); - odp_actions_add(ctx->out, ODPAT_SET_PRIORITY)->priority.priority + odp_actions_add(&ctx->out, ODPAT_SET_PRIORITY)->priority.priority = priority; } @@ -2808,9 +2849,9 @@ xlate_set_dl_tci(struct action_xlate_ctx *ctx) { ovs_be16 tci = ctx->flow.vlan_tci; if (!(tci & htons(VLAN_CFI))) { - odp_actions_add(ctx->out, ODPAT_STRIP_VLAN); + odp_actions_add(&ctx->out, ODPAT_STRIP_VLAN); } else { - union odp_action *oa = odp_actions_add(ctx->out, ODPAT_SET_DL_TCI); + union odp_action *oa = odp_actions_add(&ctx->out, ODPAT_SET_DL_TCI); oa->dl_tci.tci = tci & ~htons(VLAN_CFI); } } @@ -2836,7 +2877,7 @@ xlate_nicira_action(struct action_xlate_ctx *ctx, const struct nx_action_set_tunnel *nast; const struct nx_action_set_queue *nasq; union odp_action *oa; - int subtype = ntohs(nah->subtype); + enum nx_action_subtype subtype = ntohs(nah->subtype); assert(nah->vendor == htonl(NX_VENDOR_ID)); switch (subtype) { @@ -2847,13 +2888,13 @@ xlate_nicira_action(struct action_xlate_ctx *ctx, case NXAST_SET_TUNNEL: nast = (const struct nx_action_set_tunnel *) nah; - oa = odp_actions_add(ctx->out, ODPAT_SET_TUNNEL); + oa = odp_actions_add(&ctx->out, ODPAT_SET_TUNNEL); ctx->flow.tun_id = oa->tunnel.tun_id = nast->tun_id; break; case NXAST_DROP_SPOOFED_ARP: if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) { - odp_actions_add(ctx->out, ODPAT_DROP_SPOOFED_ARP); + odp_actions_add(&ctx->out, ODPAT_DROP_SPOOFED_ARP); } break; @@ -2863,7 +2904,7 @@ xlate_nicira_action(struct action_xlate_ctx *ctx, break; case NXAST_POP_QUEUE: - odp_actions_add(ctx->out, ODPAT_POP_PRIORITY); + odp_actions_add(&ctx->out, ODPAT_POP_PRIORITY); break; case NXAST_REG_MOVE: @@ -2881,8 +2922,9 @@ xlate_nicira_action(struct action_xlate_ctx *ctx, /* If you add a new action here that modifies flow data, don't forget to * update the flow key in ctx->flow at the same time. */ + case NXAST_SNAT__OBSOLETE: default: - VLOG_DBG_RL(&rl, "unknown Nicira action type %"PRIu16, subtype); + VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype); break; } } @@ -2904,7 +2946,7 @@ do_xlate_actions(const union ofp_action *in, size_t n_in, } for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) { - uint16_t type = ntohs(ia->type); + enum ofp_action_type type = ntohs(ia->type); union odp_action *oa; switch (type) { @@ -2931,7 +2973,7 @@ do_xlate_actions(const union ofp_action *in, size_t n_in, break; case OFPAT_SET_DL_SRC: - oa = odp_actions_add(ctx->out, ODPAT_SET_DL_SRC); + oa = odp_actions_add(&ctx->out, ODPAT_SET_DL_SRC); memcpy(oa->dl_addr.dl_addr, ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN); memcpy(ctx->flow.dl_src, @@ -2939,7 +2981,7 @@ do_xlate_actions(const union ofp_action *in, size_t n_in, break; case OFPAT_SET_DL_DST: - oa = odp_actions_add(ctx->out, ODPAT_SET_DL_DST); + oa = odp_actions_add(&ctx->out, ODPAT_SET_DL_DST); memcpy(oa->dl_addr.dl_addr, ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN); memcpy(ctx->flow.dl_dst, @@ -2947,27 +2989,27 @@ do_xlate_actions(const union ofp_action *in, size_t n_in, break; case OFPAT_SET_NW_SRC: - oa = odp_actions_add(ctx->out, ODPAT_SET_NW_SRC); + oa = odp_actions_add(&ctx->out, ODPAT_SET_NW_SRC); ctx->flow.nw_src = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr; break; case OFPAT_SET_NW_DST: - oa = odp_actions_add(ctx->out, ODPAT_SET_NW_DST); + oa = odp_actions_add(&ctx->out, ODPAT_SET_NW_DST); ctx->flow.nw_dst = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr; break; case OFPAT_SET_NW_TOS: - oa = odp_actions_add(ctx->out, ODPAT_SET_NW_TOS); + oa = odp_actions_add(&ctx->out, ODPAT_SET_NW_TOS); ctx->flow.nw_tos = oa->nw_tos.nw_tos = ia->nw_tos.nw_tos; break; case OFPAT_SET_TP_SRC: - oa = odp_actions_add(ctx->out, ODPAT_SET_TP_SRC); + oa = odp_actions_add(&ctx->out, ODPAT_SET_TP_SRC); ctx->flow.tp_src = oa->tp_port.tp_port = ia->tp_port.tp_port; break; case OFPAT_SET_TP_DST: - oa = odp_actions_add(ctx->out, ODPAT_SET_TP_DST); + oa = odp_actions_add(&ctx->out, ODPAT_SET_TP_DST); ctx->flow.tp_dst = oa->tp_port.tp_port = ia->tp_port.tp_port; break; @@ -2980,52 +3022,45 @@ do_xlate_actions(const union ofp_action *in, size_t n_in, break; default: - VLOG_DBG_RL(&rl, "unknown action type %"PRIu16, type); + VLOG_DBG_RL(&rl, "unknown action type %d", (int) type); break; } } } -static int -xlate_actions(const union ofp_action *in, size_t n_in, - const struct flow *flow, struct ofproto *ofproto, - const struct ofpbuf *packet, - struct odp_actions *out, tag_type *tags, bool *may_set_up_flow, - uint16_t *nf_output_iface) +static void +action_xlate_ctx_init(struct action_xlate_ctx *ctx, + struct ofproto *ofproto, const struct flow *flow, + const struct ofpbuf *packet) { - struct action_xlate_ctx ctx; + ctx->ofproto = ofproto; + ctx->flow = *flow; + ctx->packet = packet; + ctx->resubmit_hook = NULL; +} +static int +xlate_actions(struct action_xlate_ctx *ctx, + const union ofp_action *in, size_t n_in) +{ COVERAGE_INC(ofproto_ofp2odp); - odp_actions_init(out); - ctx.flow = *flow; - ctx.recurse = 0; - ctx.ofproto = ofproto; - ctx.packet = packet; - ctx.out = out; - ctx.tags = 0; - ctx.may_set_up_flow = true; - ctx.nf_output_iface = NF_OUT_DROP; - do_xlate_actions(in, n_in, &ctx); - remove_pop_action(&ctx); + odp_actions_init(&ctx->out); + ctx->tags = 0; + ctx->may_set_up_flow = true; + ctx->nf_output_iface = NF_OUT_DROP; + ctx->recurse = 0; + do_xlate_actions(in, n_in, ctx); + remove_pop_action(ctx); /* Check with in-band control to see if we're allowed to set up this * flow. */ - if (!in_band_rule_check(ofproto->in_band, flow, out)) { - ctx.may_set_up_flow = false; + if (!in_band_rule_check(ctx->ofproto->in_band, &ctx->flow, &ctx->out)) { + ctx->may_set_up_flow = false; } - if (tags) { - *tags = ctx.tags; - } - if (may_set_up_flow) { - *may_set_up_flow = ctx.may_set_up_flow; - } - if (nf_output_iface) { - *nf_output_iface = ctx.nf_output_iface; - } - if (odp_actions_overflow(out)) { + if (odp_actions_overflow(&ctx->out)) { COVERAGE_INC(odp_overflow); - odp_actions_init(out); + odp_actions_init(&ctx->out); return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_TOO_MANY); } return 0; @@ -3057,7 +3092,7 @@ handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh) struct ofp_packet_out *opo; struct ofpbuf payload, *buffer; union ofp_action *ofp_actions; - struct odp_actions odp_actions; + struct action_xlate_ctx ctx; struct ofpbuf request; struct flow flow; size_t n_ofp_actions; @@ -3104,11 +3139,10 @@ handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh) } /* Send. */ - error = xlate_actions(ofp_actions, n_ofp_actions, &flow, p, &payload, - &odp_actions, NULL, NULL, NULL); + action_xlate_ctx_init(&ctx, p, &flow, &payload); + error = xlate_actions(&ctx, ofp_actions, n_ofp_actions); if (!error) { - dpif_execute(p->dpif, odp_actions.actions, odp_actions.n_actions, - &payload); + dpif_execute(p->dpif, ctx.out.actions, ctx.out.n_actions, &payload); } exit: @@ -4878,6 +4912,172 @@ pick_fallback_dpid(void) return eth_addr_to_uint64(ea); } +static void +ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED, + void *aux OVS_UNUSED) +{ + const struct shash_node *node; + struct ds results; + + ds_init(&results); + SHASH_FOR_EACH (node, &all_ofprotos) { + ds_put_format(&results, "%s\n", node->name); + } + unixctl_command_reply(conn, 200, ds_cstr(&results)); + ds_destroy(&results); +} + +struct ofproto_trace { + struct action_xlate_ctx ctx; + struct flow flow; + struct ds *result; +}; + +static void +trace_format_rule(struct ds *result, int level, const struct rule *rule) +{ + ds_put_char_multiple(result, '\t', level); + if (!rule) { + ds_put_cstr(result, "No match\n"); + return; + } + + ds_put_format(result, "Rule: cookie=%#"PRIx64" ", + ntohll(rule->flow_cookie)); + cls_rule_format(&rule->cr, result); + ds_put_char(result, '\n'); + + ds_put_char_multiple(result, '\t', level); + ds_put_cstr(result, "OpenFlow "); + ofp_print_actions(result, (const struct ofp_action_header *) rule->actions, + rule->n_actions * sizeof *rule->actions); + ds_put_char(result, '\n'); +} + +static void +trace_format_flow(struct ds *result, int level, const char *title, + struct ofproto_trace *trace) +{ + ds_put_char_multiple(result, '\t', level); + ds_put_format(result, "%s: ", title); + if (flow_equal(&trace->ctx.flow, &trace->flow)) { + ds_put_cstr(result, "unchanged"); + } else { + flow_format(result, &trace->ctx.flow); + trace->flow = trace->ctx.flow; + } + ds_put_char(result, '\n'); +} + +static void +trace_resubmit(struct action_xlate_ctx *ctx, const struct rule *rule) +{ + struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx); + struct ds *result = trace->result; + + ds_put_char(result, '\n'); + trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace); + trace_format_rule(result, ctx->recurse + 1, rule); +} + +static void +ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_, + void *aux OVS_UNUSED) +{ + char *dpname, *in_port_s, *tun_id_s, *packet_s; + char *args = xstrdup(args_); + char *save_ptr = NULL; + struct ofproto *ofproto; + struct ofpbuf packet; + struct rule *rule; + struct ds result; + struct flow flow; + uint16_t in_port; + ovs_be32 tun_id; + char *s; + + ofpbuf_init(&packet, strlen(args) / 2); + ds_init(&result); + + dpname = strtok_r(args, " ", &save_ptr); + tun_id_s = strtok_r(NULL, " ", &save_ptr); + in_port_s = strtok_r(NULL, " ", &save_ptr); + packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */ + if (!dpname || !in_port_s || !packet_s) { + unixctl_command_reply(conn, 501, "Bad command syntax"); + goto exit; + } + + ofproto = shash_find_data(&all_ofprotos, dpname); + if (!ofproto) { + unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list " + "for help)"); + goto exit; + } + + tun_id = ntohl(strtoul(tun_id_s, NULL, 10)); + in_port = ofp_port_to_odp_port(atoi(in_port_s)); + + packet_s = ofpbuf_put_hex(&packet, packet_s, NULL); + packet_s += strspn(packet_s, " "); + if (*packet_s != '\0') { + unixctl_command_reply(conn, 501, "Trailing garbage in command"); + goto exit; + } + if (packet.size < ETH_HEADER_LEN) { + unixctl_command_reply(conn, 501, "Packet data too short for Ethernet"); + goto exit; + } + + ds_put_cstr(&result, "Packet: "); + s = ofp_packet_to_string(packet.data, packet.size, packet.size); + ds_put_cstr(&result, s); + free(s); + + flow_extract(&packet, tun_id, in_port, &flow); + ds_put_cstr(&result, "Flow: "); + flow_format(&result, &flow); + ds_put_char(&result, '\n'); + + rule = rule_lookup(ofproto, &flow); + trace_format_rule(&result, 0, rule); + if (rule) { + struct ofproto_trace trace; + + trace.result = &result; + trace.flow = flow; + action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet); + trace.ctx.resubmit_hook = trace_resubmit; + xlate_actions(&trace.ctx, rule->actions, rule->n_actions); + + ds_put_char(&result, '\n'); + trace_format_flow(&result, 0, "Final flow", &trace); + ds_put_cstr(&result, "Datapath actions: "); + format_odp_actions(&result, + trace.ctx.out.actions, trace.ctx.out.n_actions); + } + + unixctl_command_reply(conn, 200, ds_cstr(&result)); + +exit: + ds_destroy(&result); + ofpbuf_uninit(&packet); + free(args); +} + +static void +ofproto_unixctl_init(void) +{ + static bool registered; + if (registered) { + return; + } + registered = true; + + unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL); + unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL); +} + static bool default_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet, struct odp_actions *actions, tag_type *tags,