-
- for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
- enum ofp_action_type type = ntohs(ia->type);
- const struct ofp_action_dl_addr *oada;
-
- switch (type) {
- case OFPAT_OUTPUT:
- xlate_output_action(ctx, &ia->output);
- break;
-
- case OFPAT_SET_VLAN_VID:
- ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
- ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
- xlate_set_dl_tci(ctx);
- break;
-
- case OFPAT_SET_VLAN_PCP:
- ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
- ctx->flow.vlan_tci |= htons(
- (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
- xlate_set_dl_tci(ctx);
- break;
-
- case OFPAT_STRIP_VLAN:
- ctx->flow.vlan_tci = htons(0);
- xlate_set_dl_tci(ctx);
- break;
-
- case OFPAT_SET_DL_SRC:
- oada = ((struct ofp_action_dl_addr *) ia);
- nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
- oada->dl_addr, ETH_ADDR_LEN);
- memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
- break;
-
- case OFPAT_SET_DL_DST:
- oada = ((struct ofp_action_dl_addr *) ia);
- nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
- oada->dl_addr, ETH_ADDR_LEN);
- memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
- break;
-
- case OFPAT_SET_NW_SRC:
- nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
- ia->nw_addr.nw_addr);
- ctx->flow.nw_src = ia->nw_addr.nw_addr;
- break;
-
- case OFPAT_SET_NW_DST:
- nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
- ia->nw_addr.nw_addr);
- ctx->flow.nw_dst = ia->nw_addr.nw_addr;
- break;
-
- case OFPAT_SET_NW_TOS:
- nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
- ia->nw_tos.nw_tos);
- ctx->flow.nw_tos = ia->nw_tos.nw_tos;
- break;
-
- case OFPAT_SET_TP_SRC:
- nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
- ia->tp_port.tp_port);
- ctx->flow.tp_src = ia->tp_port.tp_port;
- break;
-
- case OFPAT_SET_TP_DST:
- nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
- ia->tp_port.tp_port);
- ctx->flow.tp_dst = ia->tp_port.tp_port;
- break;
-
- case OFPAT_VENDOR:
- xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
- break;
-
- case OFPAT_ENQUEUE:
- xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
- break;
-
- default:
- VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
- break;
- }
- }
-}
-
-static void
-action_xlate_ctx_init(struct action_xlate_ctx *ctx,
- struct ofproto *ofproto, const struct flow *flow,
- const struct ofpbuf *packet)
-{
- ctx->ofproto = ofproto;
- ctx->flow = *flow;
- ctx->packet = packet;
- ctx->resubmit_hook = NULL;
- ctx->check_special = true;
-}
-
-static struct ofpbuf *
-xlate_actions(struct action_xlate_ctx *ctx,
- const union ofp_action *in, size_t n_in)
-{
- COVERAGE_INC(ofproto_ofp2odp);
-
- ctx->odp_actions = ofpbuf_new(512);
- ctx->tags = 0;
- ctx->may_set_up_flow = true;
- ctx->nf_output_iface = NF_OUT_DROP;
- ctx->recurse = 0;
- ctx->last_pop_priority = -1;
-
- if (!ctx->check_special
- || !ctx->ofproto->ofhooks->special_cb
- || ctx->ofproto->ofhooks->special_cb(&ctx->flow, ctx->packet,
- ctx->ofproto->aux)) {
- do_xlate_actions(in, n_in, ctx);
- } else {
- ctx->may_set_up_flow = false;
- }
-
- 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(ctx->ofproto->in_band, &ctx->flow,
- ctx->odp_actions->data, ctx->odp_actions->size)) {
- ctx->may_set_up_flow = false;
- }
-
- return ctx->odp_actions;
-}
-
-/* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow
- * error message code (composed with ofp_mkerr()) for the caller to propagate
- * upward. Otherwise, returns 0.
- *
- * The log message mentions 'msg_type'. */
-static int
-reject_slave_controller(struct ofconn *ofconn, const const char *msg_type)
-{
- if (ofconn->type == OFCONN_PRIMARY && ofconn->role == NX_ROLE_SLAVE) {
- static struct vlog_rate_limit perm_rl = VLOG_RATE_LIMIT_INIT(1, 5);
- VLOG_WARN_RL(&perm_rl, "rejecting %s message from slave controller",
- msg_type);
-
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_EPERM);
- } else {
- return 0;
- }
-}
-
-static int
-handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
-{
- struct ofproto *p = ofconn->ofproto;
- struct ofp_packet_out *opo;
- struct ofpbuf payload, *buffer;
- union ofp_action *ofp_actions;
- struct action_xlate_ctx ctx;
- struct ofpbuf *odp_actions;
- struct ofpbuf request;
- struct flow flow;
- size_t n_ofp_actions;
- uint16_t in_port;
- int error;
-
- COVERAGE_INC(ofproto_packet_out);
-
- error = reject_slave_controller(ofconn, "OFPT_PACKET_OUT");
- if (error) {
- return error;
- }
-
- /* Get ofp_packet_out. */
- ofpbuf_use_const(&request, oh, ntohs(oh->length));
- opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions));
-
- /* Get actions. */
- error = ofputil_pull_actions(&request, ntohs(opo->actions_len),
- &ofp_actions, &n_ofp_actions);
- if (error) {
- return error;
- }
-
- /* Get payload. */
- if (opo->buffer_id != htonl(UINT32_MAX)) {
- error = pktbuf_retrieve(ofconn->pktbuf, ntohl(opo->buffer_id),
- &buffer, &in_port);
- if (error || !buffer) {
- return error;
- }
- payload = *buffer;
- } else {
- payload = request;
- buffer = NULL;
- }
-
- /* Extract flow, check actions. */
- flow_extract(&payload, 0, ofp_port_to_odp_port(ntohs(opo->in_port)),
- &flow);
- error = validate_actions(ofp_actions, n_ofp_actions, &flow, p->max_ports);
- if (error) {
- goto exit;
- }
-
- /* Send. */
- action_xlate_ctx_init(&ctx, p, &flow, &payload);
- odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
- dpif_execute(p->dpif, odp_actions->data, odp_actions->size, &payload);
- ofpbuf_delete(odp_actions);
-
-exit:
- ofpbuf_delete(buffer);
- return 0;
-}
-
-static void
-update_port_config(struct ofproto *p, struct ofport *port,
- uint32_t config, uint32_t mask)
-{
- mask &= config ^ port->opp.config;
- if (mask & OFPPC_PORT_DOWN) {
- if (config & OFPPC_PORT_DOWN) {
- netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
- } else {
- netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
- }
- }
-#define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | \
- OFPPC_NO_FWD | OFPPC_NO_FLOOD)
- if (mask & REVALIDATE_BITS) {
- COVERAGE_INC(ofproto_costly_flags);
- port->opp.config ^= mask & REVALIDATE_BITS;
- p->need_revalidate = true;
- }
-#undef REVALIDATE_BITS
- if (mask & OFPPC_NO_PACKET_IN) {
- port->opp.config ^= OFPPC_NO_PACKET_IN;
- }
-}
-
-static int
-handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
-{
- struct ofproto *p = ofconn->ofproto;
- const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh;
- struct ofport *port;
- int error;
-
- error = reject_slave_controller(ofconn, "OFPT_PORT_MOD");
- if (error) {
- return error;
- }
-
- port = get_port(p, ofp_port_to_odp_port(ntohs(opm->port_no)));
- if (!port) {
- return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
- } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
- return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
- } else {
- update_port_config(p, port, ntohl(opm->config), ntohl(opm->mask));
- if (opm->advertise) {
- netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
- }
- }
- return 0;
-}
-
-static struct ofpbuf *
-make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len)
-{
- struct ofp_stats_reply *osr;
- struct ofpbuf *msg;
-
- msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX));
- osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg);
- osr->type = type;
- osr->flags = htons(0);
- return msg;
-}
-
-static struct ofpbuf *
-start_ofp_stats_reply(const struct ofp_header *request, size_t body_len)
-{
- const struct ofp_stats_request *osr
- = (const struct ofp_stats_request *) request;
- return make_ofp_stats_reply(osr->header.xid, osr->type, body_len);
-}
-
-static void *
-append_ofp_stats_reply(size_t nbytes, struct ofconn *ofconn,
- struct ofpbuf **msgp)
-{
- struct ofpbuf *msg = *msgp;
- assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply));
- if (nbytes + msg->size > UINT16_MAX) {
- struct ofp_stats_reply *reply = msg->data;
- reply->flags = htons(OFPSF_REPLY_MORE);
- *msgp = make_ofp_stats_reply(reply->header.xid, reply->type, nbytes);
- queue_tx(msg, ofconn, ofconn->reply_counter);
- }
- return ofpbuf_put_uninit(*msgp, nbytes);
-}
-
-static struct ofpbuf *
-make_nxstats_reply(ovs_be32 xid, ovs_be32 subtype, size_t body_len)
-{
- struct nicira_stats_msg *nsm;
- struct ofpbuf *msg;
-
- msg = ofpbuf_new(MIN(sizeof *nsm + body_len, UINT16_MAX));
- nsm = put_openflow_xid(sizeof *nsm, OFPT_STATS_REPLY, xid, msg);
- nsm->type = htons(OFPST_VENDOR);
- nsm->flags = htons(0);
- nsm->vendor = htonl(NX_VENDOR_ID);
- nsm->subtype = subtype;
- return msg;
-}
-
-static struct ofpbuf *
-start_nxstats_reply(const struct nicira_stats_msg *request, size_t body_len)
-{
- return make_nxstats_reply(request->header.xid, request->subtype, body_len);
-}
-
-static void
-append_nxstats_reply(size_t nbytes, struct ofconn *ofconn,
- struct ofpbuf **msgp)
-{
- struct ofpbuf *msg = *msgp;
- assert(nbytes <= UINT16_MAX - sizeof(struct nicira_stats_msg));
- if (nbytes + msg->size > UINT16_MAX) {
- struct nicira_stats_msg *reply = msg->data;
- reply->flags = htons(OFPSF_REPLY_MORE);
- *msgp = make_nxstats_reply(reply->header.xid, reply->subtype, nbytes);
- queue_tx(msg, ofconn, ofconn->reply_counter);
- }
- ofpbuf_prealloc_tailroom(*msgp, nbytes);
-}
-
-static int
-handle_desc_stats_request(struct ofconn *ofconn,
- const struct ofp_header *request)
-{
- struct ofproto *p = ofconn->ofproto;
- struct ofp_desc_stats *ods;
- struct ofpbuf *msg;
-
- msg = start_ofp_stats_reply(request, sizeof *ods);
- ods = append_ofp_stats_reply(sizeof *ods, ofconn, &msg);
- memset(ods, 0, sizeof *ods);
- ovs_strlcpy(ods->mfr_desc, p->mfr_desc, sizeof ods->mfr_desc);
- ovs_strlcpy(ods->hw_desc, p->hw_desc, sizeof ods->hw_desc);
- ovs_strlcpy(ods->sw_desc, p->sw_desc, sizeof ods->sw_desc);
- ovs_strlcpy(ods->serial_num, p->serial_desc, sizeof ods->serial_num);
- ovs_strlcpy(ods->dp_desc, p->dp_desc, sizeof ods->dp_desc);
- queue_tx(msg, ofconn, ofconn->reply_counter);
-
- return 0;
-}
-
-static int
-handle_table_stats_request(struct ofconn *ofconn,
- const struct ofp_header *request)
-{
- struct ofproto *p = ofconn->ofproto;
- struct ofp_table_stats *ots;
- struct ofpbuf *msg;
-
- msg = start_ofp_stats_reply(request, sizeof *ots * 2);
-
- /* Classifier table. */
- ots = append_ofp_stats_reply(sizeof *ots, ofconn, &msg);
- memset(ots, 0, sizeof *ots);
- strcpy(ots->name, "classifier");
- ots->wildcards = (ofconn->flow_format == NXFF_OPENFLOW10
- ? htonl(OFPFW_ALL) : htonl(OVSFW_ALL));
- ots->max_entries = htonl(1024 * 1024); /* An arbitrary big number. */
- ots->active_count = htonl(classifier_count(&p->cls));
- put_32aligned_be64(&ots->lookup_count, htonll(0)); /* XXX */
- put_32aligned_be64(&ots->matched_count, htonll(0)); /* XXX */
-
- queue_tx(msg, ofconn, ofconn->reply_counter);
- return 0;
-}
-
-static void
-append_port_stat(struct ofport *port, struct ofconn *ofconn,
- struct ofpbuf **msgp)
-{
- struct netdev_stats stats;
- struct ofp_port_stats *ops;
-
- /* Intentionally ignore return value, since errors will set
- * 'stats' to all-1s, which is correct for OpenFlow, and
- * netdev_get_stats() will log errors. */
- netdev_get_stats(port->netdev, &stats);
-
- ops = append_ofp_stats_reply(sizeof *ops, ofconn, msgp);
- ops->port_no = htons(port->opp.port_no);
- memset(ops->pad, 0, sizeof ops->pad);
- put_32aligned_be64(&ops->rx_packets, htonll(stats.rx_packets));
- put_32aligned_be64(&ops->tx_packets, htonll(stats.tx_packets));
- put_32aligned_be64(&ops->rx_bytes, htonll(stats.rx_bytes));
- put_32aligned_be64(&ops->tx_bytes, htonll(stats.tx_bytes));
- put_32aligned_be64(&ops->rx_dropped, htonll(stats.rx_dropped));
- put_32aligned_be64(&ops->tx_dropped, htonll(stats.tx_dropped));
- put_32aligned_be64(&ops->rx_errors, htonll(stats.rx_errors));
- put_32aligned_be64(&ops->tx_errors, htonll(stats.tx_errors));
- put_32aligned_be64(&ops->rx_frame_err, htonll(stats.rx_frame_errors));
- put_32aligned_be64(&ops->rx_over_err, htonll(stats.rx_over_errors));
- put_32aligned_be64(&ops->rx_crc_err, htonll(stats.rx_crc_errors));
- put_32aligned_be64(&ops->collisions, htonll(stats.collisions));
-}
-
-static int
-handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
-{
- struct ofproto *p = ofconn->ofproto;
- const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
- struct ofp_port_stats *ops;
- struct ofpbuf *msg;
- struct ofport *port;
-
- msg = start_ofp_stats_reply(oh, 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;
-}
-
-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;
- ovs_be64 cookie;
- 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;
-
- rule_get_stats(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,
- rule->flow_cookie, &cookie);
- put_32aligned_be64(&ofs->cookie, cookie);
- calc_flow_duration(rule->created, &ofs->duration_sec, &ofs->duration_nsec);
- 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);
- put_32aligned_be64(&ofs->packet_count, htonll(packet_count));
- put_32aligned_be64(&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_header *oh)
-{
- const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
- struct ofpbuf *reply;
-
- COVERAGE_INC(ofproto_flows_req);
- reply = start_ofp_stats_reply(oh, 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;
- }
-
- rule_get_stats(rule, &packet_count, &byte_count);
-
- act_len = sizeof *rule->actions * rule->n_actions;
-
- append_nxstats_reply(sizeof *nfs + NXM_MAX_LEN + act_len, ofconn, replyp);
- start_len = (*replyp)->size;
- 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, const struct ofp_header *oh)
-{
- struct nx_flow_stats_request *nfsr;
- struct cls_rule target;
- struct ofpbuf *reply;
- struct ofpbuf b;
- int error;
-
- ofpbuf_use_const(&b, oh, ntohs(oh->length));
-
- /* Dissect the message. */
- nfsr = ofpbuf_pull(&b, sizeof *nfsr);
- error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target);
- if (error) {
- return error;
- }
- if (b.size) {
- return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
- }
-
- 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 rule *rule, struct ds *results)
-{
- uint64_t packet_count, byte_count;
- size_t act_len = sizeof *rule->actions * rule->n_actions;
-
- rule_get_stats(rule, &packet_count, &byte_count);
-