+ ofpmp_init(&replies, request);
+
+ if (group_id == OFPG_ALL) {
+ ovs_rwlock_rdlock(&ofproto->groups_rwlock);
+ HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
+ ovs_rwlock_rdlock(&group->rwlock);
+ append_group_stats(group, &replies);
+ ovs_rwlock_unlock(&group->rwlock);
+ }
+ ovs_rwlock_unlock(&ofproto->groups_rwlock);
+ } else {
+ if (ofproto_group_lookup(ofproto, group_id, &group)) {
+ append_group_stats(group, &replies);
+ ofproto_group_release(group);
+ }
+ }
+
+ ofconn_send_replies(ofconn, &replies);
+
+ return 0;
+}
+
+static enum ofperr
+handle_group_desc_stats_request(struct ofconn *ofconn,
+ const struct ofp_header *request)
+{
+ struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
+ struct list replies;
+ struct ofputil_group_desc gds;
+ struct ofgroup *group;
+
+ ofpmp_init(&replies, request);
+
+ ovs_rwlock_rdlock(&ofproto->groups_rwlock);
+ HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) {
+ gds.group_id = group->group_id;
+ gds.type = group->type;
+ ofputil_append_group_desc_reply(&gds, &group->buckets, &replies);
+ }
+ ovs_rwlock_unlock(&ofproto->groups_rwlock);
+
+ ofconn_send_replies(ofconn, &replies);
+
+ return 0;
+}
+
+static enum ofperr
+handle_group_features_stats_request(struct ofconn *ofconn,
+ const struct ofp_header *request)
+{
+ struct ofproto *p = ofconn_get_ofproto(ofconn);
+ struct ofpbuf *msg;
+
+ msg = ofputil_encode_group_features_reply(&p->ogf, request);
+ if (msg) {
+ ofconn_send_reply(ofconn, msg);
+ }
+
+ return 0;
+}
+
+static enum ofperr
+handle_queue_get_config_request(struct ofconn *ofconn,
+ const struct ofp_header *oh)
+{
+ struct ofproto *p = ofconn_get_ofproto(ofconn);
+ struct netdev_queue_dump queue_dump;
+ struct ofport *ofport;
+ unsigned int queue_id;
+ struct ofpbuf *reply;
+ struct smap details;
+ ofp_port_t request;
+ enum ofperr error;
+
+ error = ofputil_decode_queue_get_config_request(oh, &request);
+ if (error) {
+ return error;
+ }
+
+ ofport = ofproto_get_port(p, request);
+ if (!ofport) {
+ return OFPERR_OFPQOFC_BAD_PORT;
+ }
+
+ reply = ofputil_encode_queue_get_config_reply(oh);
+
+ smap_init(&details);
+ NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump, ofport->netdev) {
+ struct ofputil_queue_config queue;
+
+ /* None of the existing queues have compatible properties, so we
+ * hard-code omitting min_rate and max_rate. */
+ queue.queue_id = queue_id;
+ queue.min_rate = UINT16_MAX;
+ queue.max_rate = UINT16_MAX;
+ ofputil_append_queue_get_config_reply(reply, &queue);
+ }
+ smap_destroy(&details);
+
+ ofconn_send_reply(ofconn, reply);
+
+ return 0;
+}
+
+/* Implements OFPGC11_ADD
+ * 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 the ofproto's flow table. Returns 0 on success, an OpenFlow
+ * error code on failure, or OFPROTO_POSTPONE if the operation cannot be
+ * initiated now but may be retried later.
+ *
+ * Upon successful return, takes ownership of 'fm->ofpacts'. On failure,
+ * ownership remains with the caller.
+ *
+ * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
+ * if any. */
+static enum ofperr
+add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
+{
+ struct ofgroup *ofgroup;
+ enum ofperr error;
+
+ if (gm->group_id > OFPG_MAX) {
+ return OFPERR_OFPGMFC_INVALID_GROUP;
+ }
+ if (gm->type > OFPGT11_FF) {
+ return OFPERR_OFPGMFC_BAD_TYPE;
+ }
+
+ /* Allocate new group and initialize it. */
+ ofgroup = ofproto->ofproto_class->group_alloc();
+ if (!ofgroup) {
+ VLOG_WARN_RL(&rl, "%s: failed to create group", ofproto->name);
+ return OFPERR_OFPGMFC_OUT_OF_GROUPS;
+ }
+
+ ovs_rwlock_init(&ofgroup->rwlock);
+ ofgroup->ofproto = ofproto;
+ ofgroup->group_id = gm->group_id;
+ ofgroup->type = gm->type;
+ ofgroup->created = ofgroup->modified = time_msec();
+
+ list_move(&ofgroup->buckets, &gm->buckets);
+ ofgroup->n_buckets = list_size(&ofgroup->buckets);
+
+ /* Construct called BEFORE any locks are held. */
+ error = ofproto->ofproto_class->group_construct(ofgroup);
+ if (error) {
+ goto free_out;
+ }
+
+ /* We wrlock as late as possible to minimize the time we jam any other
+ * threads: No visible state changes before acquiring the lock. */
+ ovs_rwlock_wrlock(&ofproto->groups_rwlock);
+
+ if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
+ error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
+ goto unlock_out;
+ }
+
+ if (ofproto_group_exists__(ofproto, gm->group_id)) {
+ error = OFPERR_OFPGMFC_GROUP_EXISTS;
+ goto unlock_out;
+ }
+
+ if (!error) {
+ /* Insert new group. */
+ hmap_insert(&ofproto->groups, &ofgroup->hmap_node,
+ hash_int(ofgroup->group_id, 0));
+ ofproto->n_groups[ofgroup->type]++;
+
+ ovs_rwlock_unlock(&ofproto->groups_rwlock);
+ return error;
+ }
+
+ unlock_out:
+ ovs_rwlock_unlock(&ofproto->groups_rwlock);
+ ofproto->ofproto_class->group_destruct(ofgroup);
+ free_out:
+ ofputil_bucket_list_destroy(&ofgroup->buckets);
+ ofproto->ofproto_class->group_dealloc(ofgroup);
+
+ return error;
+}
+
+/* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on
+ * failure.
+ *
+ * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id,
+ * if any. */
+static enum ofperr
+modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm)
+{
+ struct ofgroup *ofgroup;
+ struct ofgroup *victim;
+ enum ofperr error;
+
+ if (gm->group_id > OFPG_MAX) {
+ return OFPERR_OFPGMFC_INVALID_GROUP;
+ }
+
+ if (gm->type > OFPGT11_FF) {
+ return OFPERR_OFPGMFC_BAD_TYPE;
+ }
+
+ victim = ofproto->ofproto_class->group_alloc();
+ if (!victim) {
+ VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name);
+ return OFPERR_OFPGMFC_OUT_OF_GROUPS;
+ }
+
+ if (!ofproto_group_write_lookup(ofproto, gm->group_id, &ofgroup)) {
+ error = OFPERR_OFPGMFC_UNKNOWN_GROUP;
+ goto free_out;
+ }
+ /* Both group's and its container's write locks held now.
+ * Also, n_groups[] is protected by ofproto->groups_rwlock. */
+ if (ofgroup->type != gm->type
+ && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) {
+ error = OFPERR_OFPGMFC_OUT_OF_GROUPS;
+ goto unlock_out;
+ }
+
+ *victim = *ofgroup;
+ list_move(&victim->buckets, &ofgroup->buckets);
+
+ ofgroup->type = gm->type;
+ list_move(&ofgroup->buckets, &gm->buckets);
+ ofgroup->n_buckets = list_size(&ofgroup->buckets);
+
+ error = ofproto->ofproto_class->group_modify(ofgroup, victim);
+ if (!error) {
+ ofputil_bucket_list_destroy(&victim->buckets);
+ ofproto->n_groups[victim->type]--;
+ ofproto->n_groups[ofgroup->type]++;
+ ofgroup->modified = time_msec();
+ } else {
+ ofputil_bucket_list_destroy(&ofgroup->buckets);
+
+ *ofgroup = *victim;
+ list_move(&ofgroup->buckets, &victim->buckets);
+ }
+
+ unlock_out:
+ ovs_rwlock_unlock(&ofgroup->rwlock);
+ ovs_rwlock_unlock(&ofproto->groups_rwlock);
+ free_out:
+ ofproto->ofproto_class->group_dealloc(victim);
+ return error;
+}
+
+static void
+delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup)
+ OVS_RELEASES(ofproto->groups_rwlock)
+{
+ struct match match;
+ struct ofputil_flow_mod fm;
+
+ /* Delete all flow entries containing this group in a group action */
+ match_init_catchall(&match);
+ flow_mod_init(&fm, &match, 0, NULL, 0, OFPFC_DELETE);
+ fm.out_group = ofgroup->group_id;
+ handle_flow_mod__(ofproto, NULL, &fm, NULL);
+
+ /* Must wait until existing readers are done,
+ * while holding the container's write lock at the same time. */
+ ovs_rwlock_wrlock(&ofgroup->rwlock);
+ hmap_remove(&ofproto->groups, &ofgroup->hmap_node);
+ /* No-one can find this group any more. */
+ ofproto->n_groups[ofgroup->type]--;
+ ovs_rwlock_unlock(&ofproto->groups_rwlock);
+
+ ofproto->ofproto_class->group_destruct(ofgroup);
+ ofputil_bucket_list_destroy(&ofgroup->buckets);
+ ovs_rwlock_unlock(&ofgroup->rwlock);
+ ovs_rwlock_destroy(&ofgroup->rwlock);
+ ofproto->ofproto_class->group_dealloc(ofgroup);
+}
+
+/* Implements OFPGC_DELETE. */
+static void
+delete_group(struct ofproto *ofproto, uint32_t group_id)
+{
+ struct ofgroup *ofgroup;
+
+ ovs_rwlock_wrlock(&ofproto->groups_rwlock);
+ if (group_id == OFPG_ALL) {
+ for (;;) {
+ struct hmap_node *node = hmap_first(&ofproto->groups);
+ if (!node) {
+ break;
+ }
+ ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node);
+ delete_group__(ofproto, ofgroup);
+ /* Lock for each node separately, so that we will not jam the
+ * other threads for too long time. */
+ ovs_rwlock_wrlock(&ofproto->groups_rwlock);
+ }
+ } else {
+ HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node,
+ hash_int(group_id, 0), &ofproto->groups) {
+ if (ofgroup->group_id == group_id) {
+ delete_group__(ofproto, ofgroup);
+ return;
+ }
+ }
+ }
+ ovs_rwlock_unlock(&ofproto->groups_rwlock);
+}
+
+static enum ofperr
+handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh)
+{
+ struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
+ struct ofputil_group_mod gm;
+ enum ofperr error;
+
+ error = reject_slave_controller(ofconn);
+ if (error) {
+ return error;
+ }
+
+ error = ofputil_decode_group_mod(oh, &gm);
+ if (error) {
+ return error;
+ }
+
+ switch (gm.command) {
+ case OFPGC11_ADD:
+ return add_group(ofproto, &gm);
+
+ case OFPGC11_MODIFY:
+ return modify_group(ofproto, &gm);
+
+ case OFPGC11_DELETE:
+ delete_group(ofproto, gm.group_id);
+ return 0;
+
+ default:
+ if (gm.command > OFPGC11_DELETE) {
+ VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d",
+ ofproto->name, gm.command);
+ }
+ return OFPERR_OFPGMFC_BAD_COMMAND;
+ }
+}
+
+enum ofproto_table_config
+ofproto_table_get_config(const struct ofproto *ofproto, uint8_t table_id)
+{
+ unsigned int value;
+ atomic_read(&ofproto->tables[table_id].config, &value);
+ return (enum ofproto_table_config)value;
+}
+
+static enum ofperr
+table_mod(struct ofproto *ofproto, const struct ofputil_table_mod *tm)
+{
+ /* Only accept currently supported configurations */
+ if (tm->config & ~OFPTC11_TABLE_MISS_MASK) {
+ return OFPERR_OFPTMFC_BAD_CONFIG;
+ }
+
+ if (tm->table_id == OFPTT_ALL) {
+ int i;
+ for (i = 0; i < ofproto->n_tables; i++) {
+ atomic_store(&ofproto->tables[i].config,
+ (unsigned int)tm->config);
+ }
+ } else if (!check_table_id(ofproto, tm->table_id)) {
+ return OFPERR_OFPTMFC_BAD_TABLE;
+ } else {
+ atomic_store(&ofproto->tables[tm->table_id].config,
+ (unsigned int)tm->config);
+ }
+
+ return 0;
+}
+
+static enum ofperr
+handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh)
+{
+ struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
+ struct ofputil_table_mod tm;
+ enum ofperr error;
+
+ error = reject_slave_controller(ofconn);
+ if (error) {
+ return error;
+ }
+
+ error = ofputil_decode_table_mod(oh, &tm);
+ if (error) {
+ return error;
+ }
+
+ return table_mod(ofproto, &tm);
+}
+
+static enum ofperr
+handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
+ OVS_EXCLUDED(ofproto_mutex)
+{
+ const struct ofp_header *oh = ofpbuf_data(msg);
+ enum ofptype type;
+ enum ofperr error;
+
+ error = ofptype_decode(&type, oh);
+ if (error) {
+ return error;
+ }
+ if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh)
+ && ofpmp_more(oh)) {
+ /* We have no buffer implementation for multipart requests.
+ * Report overflow for requests which consists of multiple
+ * messages. */
+ return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW;
+ }
+