2 * Copyright (c) 2008-2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "openvswitch/ofp-group.h"
20 #include "byte-order.h"
23 #include "openvswitch/ofp-actions.h"
24 #include "openvswitch/dynamic-string.h"
25 #include "openvswitch/ofp-msgs.h"
26 #include "openvswitch/ofp-parse.h"
27 #include "openvswitch/ofp-port.h"
28 #include "openvswitch/ofp-print.h"
29 #include "openvswitch/ofp-prop.h"
30 #include "openvswitch/ofpbuf.h"
31 #include "openvswitch/vlog.h"
34 VLOG_DEFINE_THIS_MODULE(ofp_group
);
36 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
38 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
39 * integer or, for reserved group IDs, the standard OpenFlow name for the group
40 * (either "ANY" or "ALL").
42 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
45 ofputil_group_from_string(const char *s
, uint32_t *group_idp
)
47 if (!strcasecmp(s
, "any")) {
48 *group_idp
= OFPG_ANY
;
49 } else if (!strcasecmp(s
, "all")) {
50 *group_idp
= OFPG_ALL
;
51 } else if (!str_to_uint(s
, 10, group_idp
)) {
52 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
53 "32-bit nonnegative integers or the keywords ANY or "
61 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
62 * Most groups' string representation is just the number, but for special
63 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
65 ofputil_format_group(uint32_t group_id
, struct ds
*s
)
67 char name
[MAX_GROUP_NAME_LEN
];
69 ofputil_group_to_string(group_id
, name
, sizeof name
);
74 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
75 * representation of OpenFlow group ID 'group_id'. Most group are represented
76 * as just their number, but special groups, e.g. OFPG_ALL, are represented
77 * by name, e.g. "ALL". */
79 ofputil_group_to_string(uint32_t group_id
,
80 char namebuf
[MAX_GROUP_NAME_LEN
+ 1], size_t bufsize
)
84 ovs_strlcpy(namebuf
, "ALL", bufsize
);
88 ovs_strlcpy(namebuf
, "ANY", bufsize
);
92 snprintf(namebuf
, bufsize
, "%"PRIu32
, group_id
);
97 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
99 ofputil_bucket_list_destroy(struct ovs_list
*buckets
)
101 struct ofputil_bucket
*bucket
;
103 LIST_FOR_EACH_POP (bucket
, list_node
, buckets
) {
104 free(bucket
->ofpacts
);
109 /* Clones 'bucket' and its ofpacts data */
110 static struct ofputil_bucket
*
111 ofputil_bucket_clone_data(const struct ofputil_bucket
*bucket
)
113 struct ofputil_bucket
*new;
115 new = xmemdup(bucket
, sizeof *bucket
);
116 new->ofpacts
= xmemdup(bucket
->ofpacts
, bucket
->ofpacts_len
);
121 /* Clones each of the buckets in the list 'src' appending them
122 * in turn to 'dest' which should be an initialised list.
123 * An exception is that if the pointer value of a bucket in 'src'
124 * matches 'skip' then it is not cloned or appended to 'dest'.
125 * This allows all of 'src' or 'all of 'src' except 'skip' to
126 * be cloned and appended to 'dest'. */
128 ofputil_bucket_clone_list(struct ovs_list
*dest
, const struct ovs_list
*src
,
129 const struct ofputil_bucket
*skip
)
131 struct ofputil_bucket
*bucket
;
133 LIST_FOR_EACH (bucket
, list_node
, src
) {
134 struct ofputil_bucket
*new_bucket
;
136 if (bucket
== skip
) {
140 new_bucket
= ofputil_bucket_clone_data(bucket
);
141 ovs_list_push_back(dest
, &new_bucket
->list_node
);
145 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
146 * Returns the first bucket found or NULL if no buckets are found. */
147 struct ofputil_bucket
*
148 ofputil_bucket_find(const struct ovs_list
*buckets
, uint32_t bucket_id
)
150 struct ofputil_bucket
*bucket
;
152 if (bucket_id
> OFPG15_BUCKET_MAX
) {
156 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
157 if (bucket
->bucket_id
== bucket_id
) {
165 /* Returns true if more than one bucket in the list 'buckets'
166 * have the same bucket id. Returns false otherwise. */
168 ofputil_bucket_check_duplicate_id(const struct ovs_list
*buckets
)
170 struct ofputil_bucket
*i
, *j
;
172 LIST_FOR_EACH (i
, list_node
, buckets
) {
173 LIST_FOR_EACH_REVERSE (j
, list_node
, buckets
) {
177 if (i
->bucket_id
== j
->bucket_id
) {
186 /* Returns the bucket at the front of the list 'buckets'.
187 * Undefined if 'buckets is empty. */
188 struct ofputil_bucket
*
189 ofputil_bucket_list_front(const struct ovs_list
*buckets
)
191 static struct ofputil_bucket
*bucket
;
193 ASSIGN_CONTAINER(bucket
, ovs_list_front(buckets
), list_node
);
198 /* Returns the bucket at the back of the list 'buckets'.
199 * Undefined if 'buckets is empty. */
200 struct ofputil_bucket
*
201 ofputil_bucket_list_back(const struct ovs_list
*buckets
)
203 static struct ofputil_bucket
*bucket
;
205 ASSIGN_CONTAINER(bucket
, ovs_list_back(buckets
), list_node
);
210 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
211 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
214 * Group statistics include packet and byte counts for each group. */
216 ofputil_encode_group_stats_request(enum ofp_version ofp_version
,
219 struct ofpbuf
*msg
= ofpraw_alloc((ofp_version
== OFP10_VERSION
220 ? OFPRAW_NXST_GROUP_REQUEST
221 : OFPRAW_OFPST11_GROUP_REQUEST
),
223 struct ofp11_group_stats_request
*req
= ofpbuf_put_zeros(msg
, sizeof *req
);
224 req
->group_id
= htonl(group_id
);
230 ofputil_uninit_group_desc(struct ofputil_group_desc
*gd
)
232 ofputil_bucket_list_destroy(&gd
->buckets
);
233 ofputil_group_properties_destroy(&gd
->props
);
236 /* Decodes the OpenFlow group description request in 'oh', returning the group
237 * whose description is requested, or OFPG_ALL if stats for all groups was
240 ofputil_decode_group_desc_request(const struct ofp_header
*oh
)
242 struct ofpbuf request
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
243 enum ofpraw raw
= ofpraw_pull_assert(&request
);
244 if (raw
== OFPRAW_OFPST11_GROUP_DESC_REQUEST
) {
246 } else if (raw
== OFPRAW_NXST_GROUP_DESC_REQUEST
||
247 raw
== OFPRAW_OFPST15_GROUP_DESC_REQUEST
) {
248 ovs_be32
*group_id
= ofpbuf_pull(&request
, sizeof *group_id
);
249 return ntohl(*group_id
);
255 /* Returns an OpenFlow group description request for OpenFlow version
256 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
257 * request stats for all groups (OpenFlow 1.4 and earlier always request all
260 * Group descriptions include the bucket and action configuration for each
263 ofputil_encode_group_desc_request(enum ofp_version ofp_version
,
266 struct ofpbuf
*request
;
268 switch (ofp_version
) {
273 request
= ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST
,
278 case OFP16_VERSION
: {
279 struct ofp15_group_desc_request
*req
;
280 request
= ofpraw_alloc((ofp_version
== OFP10_VERSION
281 ? OFPRAW_NXST_GROUP_DESC_REQUEST
282 : OFPRAW_OFPST15_GROUP_DESC_REQUEST
),
284 req
= ofpbuf_put_zeros(request
, sizeof *req
);
285 req
->group_id
= htonl(group_id
);
297 ofputil_group_desc_request_format(struct ds
*string
,
298 const struct ofp_header
*oh
)
300 uint32_t group_id
= ofputil_decode_group_desc_request(oh
);
301 ds_put_cstr(string
, " group_id=");
302 ofputil_format_group(group_id
, string
);
308 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats
*gs
,
309 struct ofp11_bucket_counter bucket_cnts
[])
313 for (i
= 0; i
< gs
->n_buckets
; i
++) {
314 bucket_cnts
[i
].packet_count
= htonll(gs
->bucket_stats
[i
].packet_count
);
315 bucket_cnts
[i
].byte_count
= htonll(gs
->bucket_stats
[i
].byte_count
);
320 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats
*gs
,
321 struct ofp11_group_stats
*gs11
, size_t length
,
322 struct ofp11_bucket_counter bucket_cnts
[])
324 memset(gs11
, 0, sizeof *gs11
);
325 gs11
->length
= htons(length
);
326 gs11
->group_id
= htonl(gs
->group_id
);
327 gs11
->ref_count
= htonl(gs
->ref_count
);
328 gs11
->packet_count
= htonll(gs
->packet_count
);
329 gs11
->byte_count
= htonll(gs
->byte_count
);
330 ofputil_group_bucket_counters_to_ofp11(gs
, bucket_cnts
);
334 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats
*gs
,
335 struct ofp13_group_stats
*gs13
, size_t length
,
336 struct ofp11_bucket_counter bucket_cnts
[])
338 ofputil_group_stats_to_ofp11(gs
, &gs13
->gs
, length
, bucket_cnts
);
339 gs13
->duration_sec
= htonl(gs
->duration_sec
);
340 gs13
->duration_nsec
= htonl(gs
->duration_nsec
);
344 /* Encodes 'gs' properly for the format of the list of group statistics
345 * replies already begun in 'replies' and appends it to the list. 'replies'
346 * must have originally been initialized with ofpmp_init(). */
348 ofputil_append_group_stats(struct ovs_list
*replies
,
349 const struct ofputil_group_stats
*gs
)
351 size_t bucket_counter_size
;
352 struct ofp11_bucket_counter
*bucket_counters
;
355 bucket_counter_size
= gs
->n_buckets
* sizeof(struct ofp11_bucket_counter
);
357 switch (ofpmp_version(replies
)) {
360 struct ofp11_group_stats
*gs11
;
362 length
= sizeof *gs11
+ bucket_counter_size
;
363 gs11
= ofpmp_append(replies
, length
);
364 bucket_counters
= (struct ofp11_bucket_counter
*)(gs11
+ 1);
365 ofputil_group_stats_to_ofp11(gs
, gs11
, length
, bucket_counters
);
373 case OFP16_VERSION
: {
374 struct ofp13_group_stats
*gs13
;
376 length
= sizeof *gs13
+ bucket_counter_size
;
377 gs13
= ofpmp_append(replies
, length
);
378 bucket_counters
= (struct ofp11_bucket_counter
*)(gs13
+ 1);
379 ofputil_group_stats_to_ofp13(gs
, gs13
, length
, bucket_counters
);
388 /* Returns an OpenFlow group features request for OpenFlow version
391 ofputil_encode_group_features_request(enum ofp_version ofp_version
)
393 return ofpraw_alloc((ofp_version
< OFP12_VERSION
394 ? OFPRAW_NXST_GROUP_FEATURES_REQUEST
395 : OFPRAW_OFPST12_GROUP_FEATURES_REQUEST
),
399 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
400 * group features request 'request'. */
402 ofputil_encode_group_features_reply(
403 const struct ofputil_group_features
*features
,
404 const struct ofp_header
*request
)
406 struct ofpbuf
*reply
= ofpraw_alloc_stats_reply(request
, 0);
407 struct ofp12_group_features_stats
*ogf
408 = ofpbuf_put_zeros(reply
, sizeof *ogf
);
409 ogf
->types
= htonl(features
->types
);
410 ogf
->capabilities
= htonl(features
->capabilities
);
411 for (int i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
412 ogf
->max_groups
[i
] = htonl(features
->max_groups
[i
]);
413 ogf
->actions
[i
] = ofpact_bitmap_to_openflow(features
->ofpacts
[i
],
420 /* Decodes group features reply 'oh' into 'features'. */
422 ofputil_decode_group_features_reply(const struct ofp_header
*oh
,
423 struct ofputil_group_features
*features
)
425 const struct ofp12_group_features_stats
*ogf
= ofpmsg_body(oh
);
428 features
->types
= ntohl(ogf
->types
);
429 features
->capabilities
= ntohl(ogf
->capabilities
);
430 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
431 features
->max_groups
[i
] = ntohl(ogf
->max_groups
[i
]);
432 features
->ofpacts
[i
] = ofpact_bitmap_from_openflow(
433 ogf
->actions
[i
], oh
->version
);
438 group_type_to_string(enum ofp11_group_type type
)
441 case OFPGT11_ALL
: return "all";
442 case OFPGT11_SELECT
: return "select";
443 case OFPGT11_INDIRECT
: return "indirect";
444 case OFPGT11_FF
: return "fast failover";
445 default: OVS_NOT_REACHED();
450 ofputil_group_features_format(struct ds
*string
, const struct ofp_header
*oh
)
452 struct ofputil_group_features features
;
455 ofputil_decode_group_features_reply(oh
, &features
);
457 ds_put_format(string
, "\n Group table:\n");
458 ds_put_format(string
, " Types: 0x%"PRIx32
"\n", features
.types
);
459 ds_put_format(string
, " Capabilities: 0x%"PRIx32
"\n",
460 features
.capabilities
);
462 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
463 if (features
.types
& (1u << i
)) {
464 ds_put_format(string
, " %s group:\n", group_type_to_string(i
));
465 ds_put_format(string
, " max_groups=%#"PRIx32
"\n",
466 features
.max_groups
[i
]);
467 ds_put_format(string
, " actions: ");
468 ofpact_bitmap_format(features
.ofpacts
[i
], string
);
469 ds_put_char(string
, '\n');
476 /* Parse a group status request message into a 32 bit OpenFlow 1.1
477 * group ID and stores the latter in '*group_id'.
478 * Returns 0 if successful, otherwise an OFPERR_* number. */
480 ofputil_decode_group_stats_request(const struct ofp_header
*request
,
483 const struct ofp11_group_stats_request
*gsr11
= ofpmsg_body(request
);
484 *group_id
= ntohl(gsr11
->group_id
);
488 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
489 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
490 * caller to eventually free.
492 * Multiple group stats replies can be packed into a single OpenFlow message.
493 * Calling this function multiple times for a single 'msg' iterates through the
494 * replies. The caller must initially leave 'msg''s layer pointers null and
495 * not modify them between calls.
497 * Returns 0 if successful, EOF if no replies were left in this 'msg',
498 * otherwise a positive errno value. */
500 ofputil_decode_group_stats_reply(struct ofpbuf
*msg
,
501 struct ofputil_group_stats
*gs
)
503 struct ofp11_bucket_counter
*obc
;
504 struct ofp11_group_stats
*ogs11
;
511 gs
->bucket_stats
= NULL
;
512 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
513 : ofpraw_pull(&raw
, msg
));
522 if (raw
== OFPRAW_OFPST11_GROUP_REPLY
) {
523 base_len
= sizeof *ogs11
;
524 ogs11
= ofpbuf_try_pull(msg
, sizeof *ogs11
);
525 gs
->duration_sec
= gs
->duration_nsec
= UINT32_MAX
;
526 } else if (raw
== OFPRAW_NXST_GROUP_REPLY
||
527 raw
== OFPRAW_OFPST13_GROUP_REPLY
) {
528 struct ofp13_group_stats
*ogs13
;
530 base_len
= sizeof *ogs13
;
531 ogs13
= ofpbuf_try_pull(msg
, sizeof *ogs13
);
534 gs
->duration_sec
= ntohl(ogs13
->duration_sec
);
535 gs
->duration_nsec
= ntohl(ogs13
->duration_nsec
);
544 VLOG_WARN_RL(&rl
, "%s reply has %"PRIu32
" leftover bytes at end",
545 ofpraw_get_name(raw
), msg
->size
);
546 return OFPERR_OFPBRC_BAD_LEN
;
548 length
= ntohs(ogs11
->length
);
549 if (length
< sizeof base_len
) {
550 VLOG_WARN_RL(&rl
, "%s reply claims invalid length %"PRIuSIZE
,
551 ofpraw_get_name(raw
), length
);
552 return OFPERR_OFPBRC_BAD_LEN
;
555 gs
->group_id
= ntohl(ogs11
->group_id
);
556 gs
->ref_count
= ntohl(ogs11
->ref_count
);
557 gs
->packet_count
= ntohll(ogs11
->packet_count
);
558 gs
->byte_count
= ntohll(ogs11
->byte_count
);
560 gs
->n_buckets
= (length
- base_len
) / sizeof *obc
;
561 obc
= ofpbuf_try_pull(msg
, gs
->n_buckets
* sizeof *obc
);
563 VLOG_WARN_RL(&rl
, "%s reply has %"PRIu32
" leftover bytes at end",
564 ofpraw_get_name(raw
), msg
->size
);
565 return OFPERR_OFPBRC_BAD_LEN
;
568 gs
->bucket_stats
= xmalloc(gs
->n_buckets
* sizeof *gs
->bucket_stats
);
569 for (i
= 0; i
< gs
->n_buckets
; i
++) {
570 gs
->bucket_stats
[i
].packet_count
= ntohll(obc
[i
].packet_count
);
571 gs
->bucket_stats
[i
].byte_count
= ntohll(obc
[i
].byte_count
);
579 ofputil_group_stats_request_format(struct ds
*string
,
580 const struct ofp_header
*oh
)
585 error
= ofputil_decode_group_stats_request(oh
, &group_id
);
590 ds_put_cstr(string
, " group_id=");
591 ofputil_format_group(group_id
, string
);
596 ofputil_group_stats_format(struct ds
*s
, const struct ofp_header
*oh
)
598 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
600 struct ofputil_group_stats gs
;
603 retval
= ofputil_decode_group_stats_reply(&b
, &gs
);
606 ds_put_cstr(s
, " ***parse error***");
612 ds_put_char(s
, '\n');
615 ds_put_format(s
, "group_id=%"PRIu32
",", gs
.group_id
);
617 if (gs
.duration_sec
!= UINT32_MAX
) {
618 ds_put_cstr(s
, "duration=");
619 ofp_print_duration(s
, gs
.duration_sec
, gs
.duration_nsec
);
622 ds_put_format(s
, "ref_count=%"PRIu32
",", gs
.ref_count
);
623 ds_put_format(s
, "packet_count=%"PRIu64
",", gs
.packet_count
);
624 ds_put_format(s
, "byte_count=%"PRIu64
"", gs
.byte_count
);
626 for (uint32_t bucket_i
= 0; bucket_i
< gs
.n_buckets
; bucket_i
++) {
627 if (gs
.bucket_stats
[bucket_i
].packet_count
!= UINT64_MAX
) {
628 ds_put_format(s
, ",bucket%"PRIu32
":", bucket_i
);
629 ds_put_format(s
, "packet_count=%"PRIu64
",", gs
.bucket_stats
[bucket_i
].packet_count
);
630 ds_put_format(s
, "byte_count=%"PRIu64
"", gs
.bucket_stats
[bucket_i
].byte_count
);
634 free(gs
.bucket_stats
);
639 static char * OVS_WARN_UNUSED_RESULT
640 parse_bucket_str(struct ofputil_bucket
*bucket
, char *str_
,
641 const struct ofputil_port_map
*port_map
,
642 const struct ofputil_table_map
*table_map
,
643 uint8_t group_type
, enum ofputil_protocol
*usable_protocols
)
645 char *pos
, *key
, *value
;
646 struct ofpbuf ofpacts
;
650 bucket
->weight
= group_type
== OFPGT11_SELECT
? 1 : 0;
651 bucket
->bucket_id
= OFPG15_BUCKET_ALL
;
652 bucket
->watch_port
= OFPP_ANY
;
653 bucket
->watch_group
= OFPG_ANY
;
659 while (ofputil_parse_key_value(&pos
, &key
, &value
)) {
660 if (!strcasecmp(key
, "weight")) {
661 error
= str_to_u16(value
, "weight", &bucket
->weight
);
662 } else if (!strcasecmp(key
, "watch_port")) {
663 if (!ofputil_port_from_string(value
, port_map
, &bucket
->watch_port
)
664 || (ofp_to_u16(bucket
->watch_port
) >= ofp_to_u16(OFPP_MAX
)
665 && bucket
->watch_port
!= OFPP_ANY
)) {
666 error
= xasprintf("%s: invalid watch_port", value
);
668 } else if (!strcasecmp(key
, "watch_group")) {
669 error
= str_to_u32(value
, &bucket
->watch_group
);
670 if (!error
&& bucket
->watch_group
> OFPG_MAX
) {
671 error
= xasprintf("invalid watch_group id %"PRIu32
,
672 bucket
->watch_group
);
674 } else if (!strcasecmp(key
, "bucket_id")) {
675 error
= str_to_u32(value
, &bucket
->bucket_id
);
676 if (!error
&& bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
677 error
= xasprintf("invalid bucket_id id %"PRIu32
,
680 *usable_protocols
&= OFPUTIL_P_OF10_ANY
| OFPUTIL_P_OF15_UP
;
681 } else if (!strcasecmp(key
, "action") || !strcasecmp(key
, "actions")) {
682 ds_put_format(&actions
, "%s,", value
);
684 ds_put_format(&actions
, "%s(%s),", key
, value
);
688 ds_destroy(&actions
);
693 if (!actions
.length
) {
694 return xstrdup("bucket must specify actions");
696 if (group_type
== OFPGT11_FF
&& !ofputil_bucket_has_liveness(bucket
)) {
697 return xstrdup("fast failover bucket requires watch_port or "
700 ds_chomp(&actions
, ',');
702 ofpbuf_init(&ofpacts
, 0);
703 struct ofpact_parse_params pp
= {
704 .port_map
= port_map
,
705 .table_map
= table_map
,
707 .usable_protocols
= usable_protocols
,
709 error
= ofpacts_parse_actions(ds_cstr(&actions
), &pp
);
710 ds_destroy(&actions
);
712 ofpbuf_uninit(&ofpacts
);
715 bucket
->ofpacts
= ofpacts
.data
;
716 bucket
->ofpacts_len
= ofpacts
.size
;
721 static char * OVS_WARN_UNUSED_RESULT
722 parse_select_group_field(char *s
, const struct ofputil_port_map
*port_map
,
723 struct field_array
*fa
,
724 enum ofputil_protocol
*usable_protocols
)
726 char *name
, *value_str
;
728 while (ofputil_parse_key_value(&s
, &name
, &value_str
)) {
729 const struct mf_field
*mf
= mf_from_name(name
);
733 union mf_value value
;
735 if (bitmap_is_set(fa
->used
.bm
, mf
->id
)) {
736 return xasprintf("%s: duplicate field", name
);
740 error
= mf_parse_value(mf
, value_str
, port_map
, &value
);
745 /* The mask cannot be all-zeros */
746 if (!mf_is_tun_metadata(mf
) &&
747 is_all_zeros(&value
, mf
->n_bytes
)) {
748 return xasprintf("%s: values are wildcards here "
749 "and must not be all-zeros", s
);
752 /* The values parsed are masks for fields used
753 * by the selection method */
754 if (!mf_is_mask_valid(mf
, &value
)) {
755 return xasprintf("%s: invalid mask for field %s",
756 value_str
, mf
->name
);
759 memset(&value
, 0xff, mf
->n_bytes
);
762 field_array_set(mf
->id
, &value
, fa
);
764 if (is_all_ones(&value
, mf
->n_bytes
)) {
765 *usable_protocols
&= mf
->usable_protocols_exact
;
766 } else if (mf
->usable_protocols_bitwise
== mf
->usable_protocols_cidr
767 || ip_is_cidr(value
.be32
)) {
768 *usable_protocols
&= mf
->usable_protocols_cidr
;
770 *usable_protocols
&= mf
->usable_protocols_bitwise
;
773 return xasprintf("%s: unknown field %s", s
, name
);
780 static char * OVS_WARN_UNUSED_RESULT
781 parse_ofp_group_mod_str__(struct ofputil_group_mod
*gm
, int command
,
783 const struct ofputil_port_map
*port_map
,
784 const struct ofputil_table_map
*table_map
,
785 enum ofputil_protocol
*usable_protocols
)
788 F_GROUP_TYPE
= 1 << 0,
790 F_COMMAND_BUCKET_ID
= 1 << 2,
791 F_COMMAND_BUCKET_ID_ALL
= 1 << 3,
793 bool had_type
= false;
794 bool had_command_bucket_id
= false;
795 struct ofputil_bucket
*bucket
;
798 *usable_protocols
= OFPUTIL_P_ANY
;
803 string
+= strspn(string
, " \t\r\n"); /* Skip white space. */
804 len
= strcspn(string
, ", \t\r\n"); /* Get length of the first token. */
806 if (!strncmp(string
, "add", len
)) {
807 command
= OFPGC11_ADD
;
808 } else if (!strncmp(string
, "delete", len
)) {
809 command
= OFPGC11_DELETE
;
810 } else if (!strncmp(string
, "modify", len
)) {
811 command
= OFPGC11_MODIFY
;
812 } else if (!strncmp(string
, "add_or_mod", len
)) {
813 command
= OFPGC11_ADD_OR_MOD
;
814 } else if (!strncmp(string
, "insert_bucket", len
)) {
815 command
= OFPGC15_INSERT_BUCKET
;
816 } else if (!strncmp(string
, "remove_bucket", len
)) {
817 command
= OFPGC15_REMOVE_BUCKET
;
820 command
= OFPGC11_ADD
;
827 fields
= F_GROUP_TYPE
| F_BUCKETS
;
835 fields
= F_GROUP_TYPE
| F_BUCKETS
;
838 case OFPGC11_ADD_OR_MOD
:
839 fields
= F_GROUP_TYPE
| F_BUCKETS
;
842 case OFPGC15_INSERT_BUCKET
:
843 fields
= F_BUCKETS
| F_COMMAND_BUCKET_ID
;
844 *usable_protocols
&= OFPUTIL_P_OF10_ANY
| OFPUTIL_P_OF15_UP
;
847 case OFPGC15_REMOVE_BUCKET
:
848 fields
= F_COMMAND_BUCKET_ID
| F_COMMAND_BUCKET_ID_ALL
;
849 *usable_protocols
&= OFPUTIL_P_OF10_ANY
| OFPUTIL_P_OF15_UP
;
856 memset(gm
, 0, sizeof *gm
);
857 gm
->command
= command
;
858 gm
->group_id
= OFPG_ANY
;
859 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
860 ovs_list_init(&gm
->buckets
);
861 if (command
== OFPGC11_DELETE
&& string
[0] == '\0') {
862 gm
->group_id
= OFPG_ALL
;
866 /* Strip the buckets off the end of 'string', if there are any, saving a
867 * pointer for later. We want to parse the buckets last because the bucket
868 * type influences bucket defaults. */
869 char *bkt_str
= strstr(string
, "bucket=");
871 if (!(fields
& F_BUCKETS
)) {
872 error
= xstrdup("bucket is not needed");
878 /* Parse everything before the buckets. */
881 while (ofputil_parse_key_value(&pos
, &name
, &value
)) {
882 if (!strcmp(name
, "command_bucket_id")) {
883 if (!(fields
& F_COMMAND_BUCKET_ID
)) {
884 error
= xstrdup("command bucket id is not needed");
887 if (!strcmp(value
, "all")) {
888 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
889 } else if (!strcmp(value
, "first")) {
890 gm
->command_bucket_id
= OFPG15_BUCKET_FIRST
;
891 } else if (!strcmp(value
, "last")) {
892 gm
->command_bucket_id
= OFPG15_BUCKET_LAST
;
894 error
= str_to_u32(value
, &gm
->command_bucket_id
);
898 if (gm
->command_bucket_id
> OFPG15_BUCKET_MAX
899 && (gm
->command_bucket_id
!= OFPG15_BUCKET_FIRST
900 && gm
->command_bucket_id
!= OFPG15_BUCKET_LAST
901 && gm
->command_bucket_id
!= OFPG15_BUCKET_ALL
)) {
902 error
= xasprintf("invalid command bucket id %"PRIu32
,
903 gm
->command_bucket_id
);
907 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
908 && !(fields
& F_COMMAND_BUCKET_ID_ALL
)) {
909 error
= xstrdup("command_bucket_id=all is not permitted");
912 had_command_bucket_id
= true;
913 } else if (!strcmp(name
, "group_id")) {
914 if(!strcmp(value
, "all")) {
915 gm
->group_id
= OFPG_ALL
;
917 error
= str_to_u32(value
, &gm
->group_id
);
921 if (gm
->group_id
!= OFPG_ALL
&& gm
->group_id
> OFPG_MAX
) {
922 error
= xasprintf("invalid group id %"PRIu32
,
927 } else if (!strcmp(name
, "type")){
928 if (!(fields
& F_GROUP_TYPE
)) {
929 error
= xstrdup("type is not needed");
932 if (!strcmp(value
, "all")) {
933 gm
->type
= OFPGT11_ALL
;
934 } else if (!strcmp(value
, "select")) {
935 gm
->type
= OFPGT11_SELECT
;
936 } else if (!strcmp(value
, "indirect")) {
937 gm
->type
= OFPGT11_INDIRECT
;
938 } else if (!strcmp(value
, "ff") ||
939 !strcmp(value
, "fast_failover")) {
940 gm
->type
= OFPGT11_FF
;
942 error
= xasprintf("invalid group type %s", value
);
946 } else if (!strcmp(name
, "selection_method")) {
947 if (!(fields
& F_GROUP_TYPE
)) {
948 error
= xstrdup("selection method is not needed");
951 if (strlen(value
) >= NTR_MAX_SELECTION_METHOD_LEN
) {
952 error
= xasprintf("selection method is longer than %u"
954 NTR_MAX_SELECTION_METHOD_LEN
- 1);
957 memset(gm
->props
.selection_method
, '\0',
958 NTR_MAX_SELECTION_METHOD_LEN
);
959 strcpy(gm
->props
.selection_method
, value
);
960 *usable_protocols
&= OFPUTIL_P_OF10_ANY
| OFPUTIL_P_OF15_UP
;
961 } else if (!strcmp(name
, "selection_method_param")) {
962 if (!(fields
& F_GROUP_TYPE
)) {
963 error
= xstrdup("selection method param is not needed");
966 error
= str_to_u64(value
, &gm
->props
.selection_method_param
);
970 *usable_protocols
&= OFPUTIL_P_OF10_ANY
| OFPUTIL_P_OF15_UP
;
971 } else if (!strcmp(name
, "fields")) {
972 if (!(fields
& F_GROUP_TYPE
)) {
973 error
= xstrdup("fields are not needed");
976 error
= parse_select_group_field(value
, port_map
,
982 *usable_protocols
&= OFPUTIL_P_OF10_ANY
| OFPUTIL_P_OF15_UP
;
984 error
= xasprintf("unknown keyword %s", name
);
988 if (gm
->group_id
== OFPG_ANY
) {
989 error
= xstrdup("must specify a group_id");
992 if (fields
& F_GROUP_TYPE
&& !had_type
) {
993 error
= xstrdup("must specify a type");
997 /* Exclude fields for non "hash" selection method. */
998 if (strcmp(gm
->props
.selection_method
, "hash") &&
999 gm
->props
.fields
.values_size
) {
1000 error
= xstrdup("fields may only be specified with "
1001 "\"selection_method=hash\"");
1004 /* Exclude selection_method_param if no selection_method is given. */
1005 if (gm
->props
.selection_method
[0] == 0
1006 && gm
->props
.selection_method_param
!= 0) {
1007 error
= xstrdup("selection_method_param is only allowed with "
1008 "\"selection_method\"");
1011 if (fields
& F_COMMAND_BUCKET_ID
) {
1012 if (!(fields
& F_COMMAND_BUCKET_ID_ALL
|| had_command_bucket_id
)) {
1013 error
= xstrdup("must specify a command bucket id");
1016 } else if (had_command_bucket_id
) {
1017 error
= xstrdup("command bucket id is not needed");
1021 /* Now parse the buckets, if any. */
1025 bkt_str
= strchr(bkt_str
+ 1, '=');
1027 error
= xstrdup("must specify bucket content");
1032 next_bkt_str
= strstr(bkt_str
, "bucket=");
1034 *next_bkt_str
= '\0';
1037 bucket
= xzalloc(sizeof(struct ofputil_bucket
));
1038 error
= parse_bucket_str(bucket
, bkt_str
, port_map
, table_map
,
1039 gm
->type
, usable_protocols
);
1044 ovs_list_push_back(&gm
->buckets
, &bucket
->list_node
);
1046 if (gm
->type
!= OFPGT11_SELECT
&& bucket
->weight
) {
1047 error
= xstrdup("Only select groups can have bucket weights.");
1051 bkt_str
= next_bkt_str
;
1053 if (gm
->type
== OFPGT11_INDIRECT
&& !ovs_list_is_short(&gm
->buckets
)) {
1054 error
= xstrdup("Indirect groups can have at most one bucket.");
1060 ofputil_uninit_group_mod(gm
);
1064 /* If 'command' is given as -2, each line may start with a command name ("add",
1065 * "modify", "add_or_mod", "delete", "insert_bucket", or "remove_bucket"). A
1066 * missing command name is treated as "add".
1068 char * OVS_WARN_UNUSED_RESULT
1069 parse_ofp_group_mod_str(struct ofputil_group_mod
*gm
, int command
,
1071 const struct ofputil_port_map
*port_map
,
1072 const struct ofputil_table_map
*table_map
,
1073 enum ofputil_protocol
*usable_protocols
)
1075 char *string
= xstrdup(str_
);
1076 char *error
= parse_ofp_group_mod_str__(gm
, command
, string
, port_map
,
1077 table_map
, usable_protocols
);
1082 /* If 'command' is given as -2, each line may start with a command name ("add",
1083 * "modify", "add_or_mod", "delete", "insert_bucket", or "remove_bucket"). A
1084 * missing command name is treated as "add".
1086 char * OVS_WARN_UNUSED_RESULT
1087 parse_ofp_group_mod_file(const char *file_name
,
1088 const struct ofputil_port_map
*port_map
,
1089 const struct ofputil_table_map
*table_map
,
1091 struct ofputil_group_mod
**gms
, size_t *n_gms
,
1092 enum ofputil_protocol
*usable_protocols
)
1094 size_t allocated_gms
;
1102 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
1103 if (stream
== NULL
) {
1104 return xasprintf("%s: open failed (%s)",
1105 file_name
, ovs_strerror(errno
));
1108 allocated_gms
= *n_gms
;
1111 *usable_protocols
= OFPUTIL_P_ANY
;
1112 while (!ds_get_preprocessed_line(&s
, stream
, &line_number
)) {
1113 enum ofputil_protocol usable
;
1116 if (*n_gms
>= allocated_gms
) {
1117 struct ofputil_group_mod
*new_gms
;
1120 new_gms
= x2nrealloc(*gms
, &allocated_gms
, sizeof **gms
);
1121 for (i
= 0; i
< *n_gms
; i
++) {
1122 ovs_list_moved(&new_gms
[i
].buckets
, &(*gms
)[i
].buckets
);
1126 error
= parse_ofp_group_mod_str(&(*gms
)[*n_gms
], command
, ds_cstr(&s
),
1127 port_map
, table_map
, &usable
);
1131 for (i
= 0; i
< *n_gms
; i
++) {
1132 ofputil_uninit_group_mod(&(*gms
)[i
]);
1139 if (stream
!= stdin
) {
1143 char *ret
= xasprintf("%s:%d: %s", file_name
, line_number
, error
);
1147 *usable_protocols
&= usable
;
1152 if (stream
!= stdin
) {
1159 ofputil_put_ofp11_bucket(const struct ofputil_bucket
*bucket
,
1160 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
1162 struct ofp11_bucket
*ob
;
1165 start
= openflow
->size
;
1166 ofpbuf_put_zeros(openflow
, sizeof *ob
);
1167 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
1168 openflow
, ofp_version
);
1169 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
1170 ob
->len
= htons(openflow
->size
- start
);
1171 ob
->weight
= htons(bucket
->weight
);
1172 ob
->watch_port
= ofputil_port_to_ofp11(bucket
->watch_port
);
1173 ob
->watch_group
= htonl(bucket
->watch_group
);
1177 ofputil_put_ofp15_bucket(const struct ofputil_bucket
*bucket
,
1178 uint32_t bucket_id
, enum ofp11_group_type group_type
,
1179 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
1181 struct ofp15_bucket
*ob
;
1182 size_t start
, actions_start
, actions_len
;
1184 start
= openflow
->size
;
1185 ofpbuf_put_zeros(openflow
, sizeof *ob
);
1187 actions_start
= openflow
->size
;
1188 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
1189 openflow
, ofp_version
);
1190 actions_len
= openflow
->size
- actions_start
;
1192 if (group_type
== OFPGT11_SELECT
) {
1193 ofpprop_put_u16(openflow
, OFPGBPT15_WEIGHT
, bucket
->weight
);
1195 if (bucket
->watch_port
!= OFPP_ANY
) {
1196 ofpprop_put_be32(openflow
, OFPGBPT15_WATCH_PORT
,
1197 ofputil_port_to_ofp11(bucket
->watch_port
));
1199 if (bucket
->watch_group
!= OFPG_ANY
) {
1200 ofpprop_put_u32(openflow
, OFPGBPT15_WATCH_GROUP
, bucket
->watch_group
);
1203 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
1204 ob
->len
= htons(openflow
->size
- start
);
1205 ob
->action_array_len
= htons(actions_len
);
1206 ob
->bucket_id
= htonl(bucket_id
);
1210 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version
,
1211 const struct ofputil_group_props
*gp
,
1212 struct ofpbuf
*openflow
)
1214 struct ntr_group_prop_selection_method
*prop
;
1217 start
= openflow
->size
;
1218 ofpbuf_put_zeros(openflow
, sizeof *prop
);
1219 oxm_put_field_array(openflow
, &gp
->fields
, ofp_version
);
1220 prop
= ofpbuf_at_assert(openflow
, start
, sizeof *prop
);
1221 prop
->type
= htons(OFPGPT15_EXPERIMENTER
);
1222 prop
->experimenter
= htonl(NTR_VENDOR_ID
);
1223 prop
->exp_type
= htonl(NTRT_SELECTION_METHOD
);
1224 strcpy(prop
->selection_method
, gp
->selection_method
);
1225 prop
->selection_method_param
= htonll(gp
->selection_method_param
);
1226 ofpprop_end(openflow
, start
);
1230 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc
*gds
,
1231 const struct ovs_list
*buckets
,
1232 struct ovs_list
*replies
,
1233 enum ofp_version version
)
1235 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
1236 struct ofp11_group_desc_stats
*ogds
;
1237 struct ofputil_bucket
*bucket
;
1240 start_ogds
= reply
->size
;
1241 ofpbuf_put_zeros(reply
, sizeof *ogds
);
1242 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
1243 ofputil_put_ofp11_bucket(bucket
, reply
, version
);
1245 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
1246 ogds
->length
= htons(reply
->size
- start_ogds
);
1247 ogds
->type
= gds
->type
;
1248 ogds
->group_id
= htonl(gds
->group_id
);
1250 ofpmp_postappend(replies
, start_ogds
);
1254 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc
*gds
,
1255 const struct ovs_list
*buckets
,
1256 struct ovs_list
*replies
,
1257 enum ofp_version version
)
1259 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
1260 struct ofp15_group_desc_stats
*ogds
;
1261 struct ofputil_bucket
*bucket
;
1262 size_t start_ogds
, start_buckets
;
1264 start_ogds
= reply
->size
;
1265 ofpbuf_put_zeros(reply
, sizeof *ogds
);
1266 start_buckets
= reply
->size
;
1267 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
1268 ofputil_put_ofp15_bucket(bucket
, bucket
->bucket_id
,
1269 gds
->type
, reply
, version
);
1271 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
1272 ogds
->type
= gds
->type
;
1273 ogds
->group_id
= htonl(gds
->group_id
);
1274 ogds
->bucket_list_len
= htons(reply
->size
- start_buckets
);
1276 /* Add group properties */
1277 if (gds
->props
.selection_method
[0]) {
1278 ofputil_put_group_prop_ntr_selection_method(version
, &gds
->props
,
1281 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
1282 ogds
->length
= htons(reply
->size
- start_ogds
);
1284 ofpmp_postappend(replies
, start_ogds
);
1287 /* Appends a group stats reply that contains the data in 'gds' to those already
1288 * present in the list of ofpbufs in 'replies'. 'replies' should have been
1289 * initialized with ofpmp_init(). */
1291 ofputil_append_group_desc_reply(const struct ofputil_group_desc
*gds
,
1292 const struct ovs_list
*buckets
,
1293 struct ovs_list
*replies
)
1295 enum ofp_version version
= ofpmp_version(replies
);
1303 ofputil_append_ofp11_group_desc_reply(gds
, buckets
, replies
, version
);
1309 ofputil_append_ofp15_group_desc_reply(gds
, buckets
, replies
, version
);
1318 ofputil_pull_ofp11_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
1319 enum ofp_version version
, struct ovs_list
*buckets
)
1321 struct ofp11_bucket
*ob
;
1322 uint32_t bucket_id
= 0;
1324 ovs_list_init(buckets
);
1325 while (buckets_length
> 0) {
1326 struct ofputil_bucket
*bucket
;
1327 struct ofpbuf ofpacts
;
1331 ob
= (buckets_length
>= sizeof *ob
1332 ? ofpbuf_try_pull(msg
, sizeof *ob
)
1335 VLOG_WARN_RL(&rl
, "buckets end with %"PRIuSIZE
" leftover bytes",
1337 ofputil_bucket_list_destroy(buckets
);
1338 return OFPERR_OFPGMFC_BAD_BUCKET
;
1341 ob_len
= ntohs(ob
->len
);
1342 if (ob_len
< sizeof *ob
) {
1343 VLOG_WARN_RL(&rl
, "OpenFlow message bucket length "
1344 "%"PRIuSIZE
" is not valid", ob_len
);
1345 ofputil_bucket_list_destroy(buckets
);
1346 return OFPERR_OFPGMFC_BAD_BUCKET
;
1347 } else if (ob_len
> buckets_length
) {
1348 VLOG_WARN_RL(&rl
, "OpenFlow message bucket length %"PRIuSIZE
" "
1349 "exceeds remaining buckets data size %"PRIuSIZE
,
1350 ob_len
, buckets_length
);
1351 ofputil_bucket_list_destroy(buckets
);
1352 return OFPERR_OFPGMFC_BAD_BUCKET
;
1354 buckets_length
-= ob_len
;
1356 ofpbuf_init(&ofpacts
, 0);
1357 error
= ofpacts_pull_openflow_actions(msg
, ob_len
- sizeof *ob
,
1358 version
, NULL
, NULL
, &ofpacts
);
1360 ofpbuf_uninit(&ofpacts
);
1361 ofputil_bucket_list_destroy(buckets
);
1365 bucket
= xzalloc(sizeof *bucket
);
1366 bucket
->weight
= ntohs(ob
->weight
);
1367 error
= ofputil_port_from_ofp11(ob
->watch_port
, &bucket
->watch_port
);
1369 ofpbuf_uninit(&ofpacts
);
1370 ofputil_bucket_list_destroy(buckets
);
1372 return OFPERR_OFPGMFC_BAD_WATCH
;
1374 bucket
->watch_group
= ntohl(ob
->watch_group
);
1375 bucket
->bucket_id
= bucket_id
++;
1377 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
1378 bucket
->ofpacts_len
= ofpacts
.size
;
1379 ovs_list_push_back(buckets
, &bucket
->list_node
);
1386 ofputil_pull_ofp15_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
1387 enum ofp_version version
, uint8_t group_type
,
1388 struct ovs_list
*buckets
)
1390 ovs_list_init(buckets
);
1391 while (buckets_length
> 0) {
1392 struct ofputil_bucket
*bucket
= NULL
;
1393 struct ofpbuf ofpacts
;
1394 enum ofperr err
= OFPERR_OFPGMFC_BAD_BUCKET
;
1395 size_t ob_len
, actions_len
, properties_len
;
1396 ovs_be32 watch_port
= ofputil_port_to_ofp11(OFPP_ANY
);
1397 ovs_be32 watch_group
= htonl(OFPG_ANY
);
1398 ovs_be16 weight
= htons(group_type
== OFPGT11_SELECT
? 1 : 0);
1400 ofpbuf_init(&ofpacts
, 0);
1402 struct ofp15_bucket
*ob
= ofpbuf_try_pull(msg
, sizeof *ob
);
1404 VLOG_WARN_RL(&rl
, "buckets end with %"PRIuSIZE
1405 " leftover bytes", buckets_length
);
1409 ob_len
= ntohs(ob
->len
);
1410 actions_len
= ntohs(ob
->action_array_len
);
1412 if (ob_len
< sizeof *ob
) {
1413 VLOG_WARN_RL(&rl
, "OpenFlow message bucket length "
1414 "%"PRIuSIZE
" is not valid", ob_len
);
1416 } else if (ob_len
> buckets_length
) {
1417 VLOG_WARN_RL(&rl
, "OpenFlow message bucket length "
1418 "%"PRIuSIZE
" exceeds remaining buckets data size %"
1419 PRIuSIZE
, ob_len
, buckets_length
);
1421 } else if (actions_len
> ob_len
- sizeof *ob
) {
1422 VLOG_WARN_RL(&rl
, "OpenFlow message bucket actions "
1423 "length %"PRIuSIZE
" exceeds remaining bucket "
1424 "data size %"PRIuSIZE
, actions_len
,
1425 ob_len
- sizeof *ob
);
1428 buckets_length
-= ob_len
;
1430 err
= ofpacts_pull_openflow_actions(msg
, actions_len
, version
,
1431 NULL
, NULL
, &ofpacts
);
1436 properties_len
= ob_len
- sizeof *ob
- actions_len
;
1437 struct ofpbuf properties
= ofpbuf_const_initializer(
1438 ofpbuf_pull(msg
, properties_len
), properties_len
);
1439 while (properties
.size
> 0) {
1440 struct ofpbuf payload
;
1443 err
= ofpprop_pull(&properties
, &payload
, &type
);
1449 case OFPGBPT15_WEIGHT
:
1450 err
= ofpprop_parse_be16(&payload
, &weight
);
1453 case OFPGBPT15_WATCH_PORT
:
1454 err
= ofpprop_parse_be32(&payload
, &watch_port
);
1457 case OFPGBPT15_WATCH_GROUP
:
1458 err
= ofpprop_parse_be32(&payload
, &watch_group
);
1462 err
= OFPPROP_UNKNOWN(false, "group bucket", type
);
1471 bucket
= xzalloc(sizeof *bucket
);
1473 bucket
->weight
= ntohs(weight
);
1474 err
= ofputil_port_from_ofp11(watch_port
, &bucket
->watch_port
);
1476 err
= OFPERR_OFPGMFC_BAD_WATCH
;
1479 bucket
->watch_group
= ntohl(watch_group
);
1480 bucket
->bucket_id
= ntohl(ob
->bucket_id
);
1481 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
1482 VLOG_WARN_RL(&rl
, "bucket id (%u) is out of range",
1484 err
= OFPERR_OFPGMFC_BAD_BUCKET
;
1488 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
1489 bucket
->ofpacts_len
= ofpacts
.size
;
1490 ovs_list_push_back(buckets
, &bucket
->list_node
);
1496 ofpbuf_uninit(&ofpacts
);
1497 ofputil_bucket_list_destroy(buckets
);
1501 if (ofputil_bucket_check_duplicate_id(buckets
)) {
1502 VLOG_WARN_RL(&rl
, "Duplicate bucket id");
1503 ofputil_bucket_list_destroy(buckets
);
1504 return OFPERR_OFPGMFC_BAD_BUCKET
;
1511 ofputil_init_group_properties(struct ofputil_group_props
*gp
)
1513 memset(gp
, 0, sizeof *gp
);
1517 ofputil_group_properties_copy(struct ofputil_group_props
*to
,
1518 const struct ofputil_group_props
*from
)
1521 to
->fields
.values
= xmemdup(from
->fields
.values
, from
->fields
.values_size
);
1525 ofputil_group_properties_destroy(struct ofputil_group_props
*gp
)
1527 free(gp
->fields
.values
);
1531 parse_group_prop_ntr_selection_method(struct ofpbuf
*payload
,
1532 enum ofp11_group_type group_type
,
1533 enum ofp15_group_mod_command group_cmd
,
1534 struct ofputil_group_props
*gp
)
1536 struct ntr_group_prop_selection_method
*prop
= payload
->data
;
1537 size_t fields_len
, method_len
;
1540 switch (group_type
) {
1541 case OFPGT11_SELECT
:
1544 case OFPGT11_INDIRECT
:
1546 OFPPROP_LOG(&rl
, false, "ntr selection method property is "
1547 "only allowed for select groups");
1548 return OFPERR_OFPBPC_BAD_VALUE
;
1553 switch (group_cmd
) {
1555 case OFPGC15_MODIFY
:
1556 case OFPGC15_ADD_OR_MOD
:
1558 case OFPGC15_DELETE
:
1559 case OFPGC15_INSERT_BUCKET
:
1560 case OFPGC15_REMOVE_BUCKET
:
1561 OFPPROP_LOG(&rl
, false, "ntr selection method property is "
1562 "only allowed for add and delete group modifications");
1563 return OFPERR_OFPBPC_BAD_VALUE
;
1568 if (payload
->size
< sizeof *prop
) {
1569 OFPPROP_LOG(&rl
, false, "ntr selection method property "
1570 "length %u is not valid", payload
->size
);
1571 return OFPERR_OFPBPC_BAD_LEN
;
1574 method_len
= strnlen(prop
->selection_method
, NTR_MAX_SELECTION_METHOD_LEN
);
1576 if (method_len
== NTR_MAX_SELECTION_METHOD_LEN
) {
1577 OFPPROP_LOG(&rl
, false,
1578 "ntr selection method is not null terminated");
1579 return OFPERR_OFPBPC_BAD_VALUE
;
1582 if (strcmp("hash", prop
->selection_method
)
1583 && strcmp("dp_hash", prop
->selection_method
)) {
1584 OFPPROP_LOG(&rl
, false,
1585 "ntr selection method '%s' is not supported",
1586 prop
->selection_method
);
1587 return OFPERR_OFPBPC_BAD_VALUE
;
1589 /* 'method_len' is now non-zero. */
1591 strcpy(gp
->selection_method
, prop
->selection_method
);
1592 gp
->selection_method_param
= ntohll(prop
->selection_method_param
);
1594 ofpbuf_pull(payload
, sizeof *prop
);
1596 fields_len
= ntohs(prop
->length
) - sizeof *prop
;
1597 if (fields_len
&& strcmp("hash", gp
->selection_method
)) {
1598 OFPPROP_LOG(&rl
, false, "ntr selection method %s "
1599 "does not support fields", gp
->selection_method
);
1600 return OFPERR_OFPBPC_BAD_VALUE
;
1603 error
= oxm_pull_field_array(payload
->data
, fields_len
,
1606 OFPPROP_LOG(&rl
, false,
1607 "ntr selection method fields are invalid");
1615 parse_ofp15_group_properties(struct ofpbuf
*msg
,
1616 enum ofp11_group_type group_type
,
1617 enum ofp15_group_mod_command group_cmd
,
1618 struct ofputil_group_props
*gp
,
1619 size_t properties_len
)
1621 struct ofpbuf properties
= ofpbuf_const_initializer(
1622 ofpbuf_pull(msg
, properties_len
), properties_len
);
1623 while (properties
.size
> 0) {
1624 struct ofpbuf payload
;
1628 error
= ofpprop_pull(&properties
, &payload
, &type
);
1634 case OFPPROP_EXP(NTR_VENDOR_ID
, NTRT_SELECTION_METHOD
):
1635 case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID
, NTRT_SELECTION_METHOD
):
1636 error
= parse_group_prop_ntr_selection_method(&payload
, group_type
,
1641 error
= OFPPROP_UNKNOWN(false, "group", type
);
1654 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc
*gd
,
1656 enum ofp_version version
)
1658 struct ofp11_group_desc_stats
*ogds
;
1662 ofpraw_pull_assert(msg
);
1669 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
1671 VLOG_WARN_RL(&rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
1672 "leftover bytes at end", msg
->size
);
1673 return OFPERR_OFPBRC_BAD_LEN
;
1675 gd
->type
= ogds
->type
;
1676 gd
->group_id
= ntohl(ogds
->group_id
);
1678 length
= ntohs(ogds
->length
);
1679 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
1680 VLOG_WARN_RL(&rl
, "OFPST11_GROUP_DESC reply claims invalid "
1681 "length %"PRIuSIZE
, length
);
1682 return OFPERR_OFPBRC_BAD_LEN
;
1685 return ofputil_pull_ofp11_buckets(msg
, length
- sizeof *ogds
, version
,
1690 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc
*gd
,
1692 enum ofp_version version
)
1694 struct ofp15_group_desc_stats
*ogds
;
1695 uint16_t length
, bucket_list_len
;
1699 ofpraw_pull_assert(msg
);
1706 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
1708 VLOG_WARN_RL(&rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
1709 "leftover bytes at end", msg
->size
);
1710 return OFPERR_OFPBRC_BAD_LEN
;
1712 gd
->type
= ogds
->type
;
1713 gd
->group_id
= ntohl(ogds
->group_id
);
1715 length
= ntohs(ogds
->length
);
1716 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
1717 VLOG_WARN_RL(&rl
, "OFPST11_GROUP_DESC reply claims invalid "
1718 "length %u", length
);
1719 return OFPERR_OFPBRC_BAD_LEN
;
1722 bucket_list_len
= ntohs(ogds
->bucket_list_len
);
1723 if (length
< bucket_list_len
+ sizeof *ogds
) {
1724 VLOG_WARN_RL(&rl
, "OFPST11_GROUP_DESC reply claims invalid "
1725 "bucket list length %u", bucket_list_len
);
1726 return OFPERR_OFPBRC_BAD_LEN
;
1728 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, version
, gd
->type
,
1734 /* By definition group desc messages don't have a group mod command.
1735 * However, parse_group_prop_ntr_selection_method() checks to make sure
1736 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
1737 * against group mod messages with other commands supplying
1738 * a NTR selection method group experimenter property.
1739 * Such properties are valid for group desc replies so
1740 * claim that the group mod command is OFPGC15_ADD to
1741 * satisfy the check in parse_group_prop_ntr_selection_method() */
1742 error
= parse_ofp15_group_properties(
1743 msg
, gd
->type
, OFPGC15_ADD
, &gd
->props
,
1744 length
- sizeof *ogds
- bucket_list_len
);
1746 ofputil_bucket_list_destroy(&gd
->buckets
);
1751 /* Converts a group description reply in 'msg' into an abstract
1752 * ofputil_group_desc in 'gd'.
1754 * Multiple group description replies can be packed into a single OpenFlow
1755 * message. Calling this function multiple times for a single 'msg' iterates
1756 * through the replies. The caller must initially leave 'msg''s layer pointers
1757 * null and not modify them between calls.
1759 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1760 * otherwise a positive errno value. */
1762 ofputil_decode_group_desc_reply(struct ofputil_group_desc
*gd
,
1763 struct ofpbuf
*msg
, enum ofp_version version
)
1765 ofputil_init_group_properties(&gd
->props
);
1773 return ofputil_decode_ofp11_group_desc_reply(gd
, msg
, version
);
1778 return ofputil_decode_ofp15_group_desc_reply(gd
, msg
, version
);
1786 ofp_print_bucket_id(struct ds
*s
, const char *label
, uint32_t bucket_id
,
1787 enum ofp_version ofp_version
)
1789 if (ofp_version
> OFP10_VERSION
&& ofp_version
< OFP15_VERSION
) {
1793 ds_put_cstr(s
, label
);
1795 switch (bucket_id
) {
1796 case OFPG15_BUCKET_FIRST
:
1797 ds_put_cstr(s
, "first");
1799 case OFPG15_BUCKET_LAST
:
1800 ds_put_cstr(s
, "last");
1802 case OFPG15_BUCKET_ALL
:
1803 ds_put_cstr(s
, "all");
1806 ds_put_format(s
, "%"PRIu32
, bucket_id
);
1810 ds_put_char(s
, ',');
1814 ofp_print_group(struct ds
*s
, uint32_t group_id
, uint8_t type
,
1815 const struct ovs_list
*p_buckets
,
1816 const struct ofputil_group_props
*props
,
1817 enum ofp_version ofp_version
, bool suppress_type
,
1818 const struct ofputil_port_map
*port_map
,
1819 const struct ofputil_table_map
*table_map
)
1821 struct ofputil_bucket
*bucket
;
1823 ds_put_format(s
, "group_id=%"PRIu32
, group_id
);
1825 if (!suppress_type
) {
1826 static const char *type_str
[] = { "all", "select", "indirect",
1828 ds_put_format(s
, ",type=%s", type_str
[type
> 4 ? 4 : type
]);
1831 if (props
->selection_method
[0]) {
1832 ds_put_format(s
, ",selection_method=%s", props
->selection_method
);
1833 if (props
->selection_method_param
) {
1834 ds_put_format(s
, ",selection_method_param=%"PRIu64
,
1835 props
->selection_method_param
);
1838 size_t n
= bitmap_count1(props
->fields
.used
.bm
, MFF_N_IDS
);
1840 ds_put_cstr(s
, ",fields=");
1841 oxm_format_field_array(s
, &props
->fields
);
1843 ds_put_cstr(s
, ",fields(");
1844 oxm_format_field_array(s
, &props
->fields
);
1845 ds_put_char(s
, ')');
1853 ds_put_char(s
, ',');
1855 LIST_FOR_EACH (bucket
, list_node
, p_buckets
) {
1856 ds_put_cstr(s
, "bucket=");
1858 ofp_print_bucket_id(s
, "bucket_id:", bucket
->bucket_id
, ofp_version
);
1859 if (bucket
->weight
!= (type
== OFPGT11_SELECT
? 1 : 0)) {
1860 ds_put_format(s
, "weight:%"PRIu16
",", bucket
->weight
);
1862 if (bucket
->watch_port
!= OFPP_NONE
) {
1863 ds_put_cstr(s
, "watch_port:");
1864 ofputil_format_port(bucket
->watch_port
, port_map
, s
);
1865 ds_put_char(s
, ',');
1867 if (bucket
->watch_group
!= OFPG_ANY
) {
1868 ds_put_format(s
, "watch_group:%"PRIu32
",", bucket
->watch_group
);
1871 ds_put_cstr(s
, "actions=");
1872 struct ofpact_format_params fp
= {
1873 .port_map
= port_map
,
1874 .table_map
= table_map
,
1877 ofpacts_format(bucket
->ofpacts
, bucket
->ofpacts_len
, &fp
);
1878 ds_put_char(s
, ',');
1885 ofputil_group_desc_format(struct ds
*s
, const struct ofp_header
*oh
,
1886 const struct ofputil_port_map
*port_map
,
1887 const struct ofputil_table_map
*table_map
)
1889 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1891 struct ofputil_group_desc gd
;
1894 retval
= ofputil_decode_group_desc_reply(&gd
, &b
, oh
->version
);
1896 return retval
!= EOF
? retval
: 0;
1899 ds_put_char(s
, '\n');
1900 ds_put_char(s
, ' ');
1901 ofp_print_group(s
, gd
.group_id
, gd
.type
, &gd
.buckets
, &gd
.props
,
1902 oh
->version
, false, port_map
, table_map
);
1903 ofputil_uninit_group_desc(&gd
);
1908 ofputil_uninit_group_mod(struct ofputil_group_mod
*gm
)
1910 ofputil_bucket_list_destroy(&gm
->buckets
);
1911 ofputil_group_properties_destroy(&gm
->props
);
1914 static struct ofpbuf
*
1915 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version
,
1916 const struct ofputil_group_mod
*gm
)
1919 struct ofp11_group_mod
*ogm
;
1921 struct ofputil_bucket
*bucket
;
1923 b
= ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD
, ofp_version
, 0);
1924 start_ogm
= b
->size
;
1925 ofpbuf_put_zeros(b
, sizeof *ogm
);
1927 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
1928 ofputil_put_ofp11_bucket(bucket
, b
, ofp_version
);
1930 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
1931 ogm
->command
= htons(gm
->command
);
1932 ogm
->type
= gm
->type
;
1933 ogm
->group_id
= htonl(gm
->group_id
);
1938 static struct ofpbuf
*
1939 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version
,
1940 const struct ofputil_group_mod
*gm
)
1943 struct ofp15_group_mod
*ogm
;
1945 struct ofputil_bucket
*bucket
;
1946 struct id_pool
*bucket_ids
= NULL
;
1948 b
= ofpraw_alloc((ofp_version
== OFP10_VERSION
1949 ? OFPRAW_NXT_GROUP_MOD
1950 : OFPRAW_OFPT15_GROUP_MOD
), ofp_version
, 0);
1951 start_ogm
= b
->size
;
1952 ofpbuf_put_zeros(b
, sizeof *ogm
);
1954 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
1957 /* Generate a bucket id if none was supplied */
1958 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
1960 const struct ofputil_bucket
*bkt
;
1962 bucket_ids
= id_pool_create(0, OFPG15_BUCKET_MAX
+ 1);
1964 /* Mark all bucket_ids that are present in gm
1965 * as used in the pool. */
1966 LIST_FOR_EACH_REVERSE (bkt
, list_node
, &gm
->buckets
) {
1967 if (bkt
== bucket
) {
1970 if (bkt
->bucket_id
<= OFPG15_BUCKET_MAX
) {
1971 id_pool_add(bucket_ids
, bkt
->bucket_id
);
1976 if (!id_pool_alloc_id(bucket_ids
, &bucket_id
)) {
1980 bucket_id
= bucket
->bucket_id
;
1983 ofputil_put_ofp15_bucket(bucket
, bucket_id
, gm
->type
, b
, ofp_version
);
1985 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
1986 ogm
->command
= htons(gm
->command
);
1987 ogm
->type
= gm
->type
;
1988 ogm
->group_id
= htonl(gm
->group_id
);
1989 ogm
->command_bucket_id
= htonl(gm
->command_bucket_id
);
1990 ogm
->bucket_array_len
= htons(b
->size
- start_ogm
- sizeof *ogm
);
1992 /* Add group properties */
1993 if (gm
->props
.selection_method
[0]) {
1994 ofputil_put_group_prop_ntr_selection_method(ofp_version
, &gm
->props
, b
);
1997 id_pool_destroy(bucket_ids
);
2002 bad_group_cmd(enum ofp15_group_mod_command cmd
)
2004 const char *opt_version
;
2005 const char *version
;
2006 const char *cmd_str
;
2010 case OFPGC15_MODIFY
:
2011 case OFPGC15_ADD_OR_MOD
:
2012 case OFPGC15_DELETE
:
2017 case OFPGC15_INSERT_BUCKET
:
2018 case OFPGC15_REMOVE_BUCKET
:
2029 cmd_str
= "add-group";
2032 case OFPGC15_MODIFY
:
2033 case OFPGC15_ADD_OR_MOD
:
2034 cmd_str
= "mod-group";
2037 case OFPGC15_DELETE
:
2038 cmd_str
= "del-group";
2041 case OFPGC15_INSERT_BUCKET
:
2042 cmd_str
= "insert-bucket";
2045 case OFPGC15_REMOVE_BUCKET
:
2046 cmd_str
= "remove-bucket";
2053 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
2054 cmd_str
, version
, opt_version
);
2058 /* Converts abstract group mod 'gm' into a message for OpenFlow version
2059 * 'ofp_version' and returns the message. */
2061 ofputil_encode_group_mod(enum ofp_version ofp_version
,
2062 const struct ofputil_group_mod
*gm
)
2065 switch (ofp_version
) {
2070 if (gm
->command
> OFPGC11_DELETE
&& gm
->command
!= OFPGC11_ADD_OR_MOD
) {
2071 bad_group_cmd(gm
->command
);
2073 return ofputil_encode_ofp11_group_mod(ofp_version
, gm
);
2078 return ofputil_encode_ofp15_group_mod(ofp_version
, gm
);
2086 ofputil_pull_ofp11_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
2087 struct ofputil_group_mod
*gm
)
2089 const struct ofp11_group_mod
*ogm
;
2092 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
2093 gm
->command
= ntohs(ogm
->command
);
2094 gm
->type
= ogm
->type
;
2095 gm
->group_id
= ntohl(ogm
->group_id
);
2096 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
2098 error
= ofputil_pull_ofp11_buckets(msg
, msg
->size
, ofp_version
,
2101 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
2103 && ofp_version
>= OFP13_VERSION
2104 && gm
->command
== OFPGC11_DELETE
2105 && !ovs_list_is_empty(&gm
->buckets
)) {
2106 error
= OFPERR_OFPGMFC_INVALID_GROUP
;
2107 ofputil_bucket_list_destroy(&gm
->buckets
);
2114 ofputil_pull_ofp15_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
2115 struct ofputil_group_mod
*gm
)
2117 const struct ofp15_group_mod
*ogm
;
2118 uint16_t bucket_list_len
;
2119 enum ofperr error
= OFPERR_OFPGMFC_BAD_BUCKET
;
2121 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
2122 gm
->command
= ntohs(ogm
->command
);
2123 gm
->type
= ogm
->type
;
2124 gm
->group_id
= ntohl(ogm
->group_id
);
2126 gm
->command_bucket_id
= ntohl(ogm
->command_bucket_id
);
2127 switch (gm
->command
) {
2128 case OFPGC15_REMOVE_BUCKET
:
2129 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
2133 case OFPGC15_INSERT_BUCKET
:
2134 if (gm
->command_bucket_id
<= OFPG15_BUCKET_MAX
||
2135 gm
->command_bucket_id
== OFPG15_BUCKET_FIRST
2136 || gm
->command_bucket_id
== OFPG15_BUCKET_LAST
) {
2142 case OFPGC11_MODIFY
:
2143 case OFPGC11_ADD_OR_MOD
:
2144 case OFPGC11_DELETE
:
2146 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
2153 "group command bucket id (%u) is out of range",
2154 gm
->command_bucket_id
);
2155 return OFPERR_OFPGMFC_BAD_BUCKET
;
2158 bucket_list_len
= ntohs(ogm
->bucket_array_len
);
2159 if (bucket_list_len
> msg
->size
) {
2160 return OFPERR_OFPBRC_BAD_LEN
;
2162 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, ofp_version
,
2163 gm
->type
, &gm
->buckets
);
2168 error
= parse_ofp15_group_properties(msg
, gm
->type
, gm
->command
,
2169 &gm
->props
, msg
->size
);
2171 ofputil_bucket_list_destroy(&gm
->buckets
);
2177 ofputil_check_group_mod(const struct ofputil_group_mod
*gm
)
2180 case OFPGT11_INDIRECT
:
2181 if (gm
->command
!= OFPGC11_DELETE
2182 && !ovs_list_is_singleton(&gm
->buckets
) ) {
2183 return OFPERR_OFPGMFC_INVALID_GROUP
;
2187 case OFPGT11_SELECT
:
2191 return OFPERR_OFPGMFC_BAD_TYPE
;
2194 switch (gm
->command
) {
2196 case OFPGC11_MODIFY
:
2197 case OFPGC11_ADD_OR_MOD
:
2198 case OFPGC11_DELETE
:
2199 case OFPGC15_INSERT_BUCKET
:
2201 case OFPGC15_REMOVE_BUCKET
:
2202 if (!ovs_list_is_empty(&gm
->buckets
)) {
2203 return OFPERR_OFPGMFC_BAD_BUCKET
;
2207 return OFPERR_OFPGMFC_BAD_COMMAND
;
2210 struct ofputil_bucket
*bucket
;
2211 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
2212 if (bucket
->weight
&& gm
->type
!= OFPGT11_SELECT
) {
2213 return OFPERR_OFPGMFC_INVALID_GROUP
;
2218 case OFPGT11_INDIRECT
:
2219 if (ofputil_bucket_has_liveness(bucket
)) {
2220 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED
;
2223 case OFPGT11_SELECT
:
2226 if (!ofputil_bucket_has_liveness(bucket
)) {
2227 return OFPERR_OFPGMFC_INVALID_GROUP
;
2231 /* Returning BAD TYPE to be consistent
2232 * though gm->type has been checked already. */
2233 return OFPERR_OFPGMFC_BAD_TYPE
;
2240 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
2241 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
2243 ofputil_decode_group_mod(const struct ofp_header
*oh
,
2244 struct ofputil_group_mod
*gm
)
2246 ofputil_init_group_properties(&gm
->props
);
2248 enum ofp_version ofp_version
= oh
->version
;
2249 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
2250 ofpraw_pull_assert(&msg
);
2253 switch (ofp_version
) {
2258 err
= ofputil_pull_ofp11_group_mod(&msg
, ofp_version
, gm
);
2264 err
= ofputil_pull_ofp15_group_mod(&msg
, ofp_version
, gm
);
2274 err
= ofputil_check_group_mod(gm
);
2276 ofputil_uninit_group_mod(gm
);
2282 ofputil_group_mod_format__(struct ds
*s
, enum ofp_version ofp_version
,
2283 const struct ofputil_group_mod
*gm
,
2284 const struct ofputil_port_map
*port_map
,
2285 const struct ofputil_table_map
*table_map
)
2287 bool bucket_command
= false;
2289 ds_put_char(s
, '\n');
2291 ds_put_char(s
, ' ');
2292 switch (gm
->command
) {
2294 ds_put_cstr(s
, "ADD");
2297 case OFPGC11_MODIFY
:
2298 ds_put_cstr(s
, "MOD");
2301 case OFPGC11_ADD_OR_MOD
:
2302 ds_put_cstr(s
, "ADD_OR_MOD");
2305 case OFPGC11_DELETE
:
2306 ds_put_cstr(s
, "DEL");
2309 case OFPGC15_INSERT_BUCKET
:
2310 ds_put_cstr(s
, "INSERT_BUCKET");
2311 bucket_command
= true;
2314 case OFPGC15_REMOVE_BUCKET
:
2315 ds_put_cstr(s
, "REMOVE_BUCKET");
2316 bucket_command
= true;
2320 ds_put_format(s
, "cmd:%"PRIu16
"", gm
->command
);
2322 ds_put_char(s
, ' ');
2324 if (bucket_command
) {
2325 ofp_print_bucket_id(s
, "command_bucket_id:",
2326 gm
->command_bucket_id
, ofp_version
);
2329 ofp_print_group(s
, gm
->group_id
, gm
->type
, &gm
->buckets
, &gm
->props
,
2330 ofp_version
, bucket_command
, port_map
, table_map
);
2334 ofputil_group_mod_format(struct ds
*s
, const struct ofp_header
*oh
,
2335 const struct ofputil_port_map
*port_map
,
2336 const struct ofputil_table_map
*table_map
)
2338 struct ofputil_group_mod gm
;
2341 error
= ofputil_decode_group_mod(oh
, &gm
);
2345 ofputil_group_mod_format__(s
, oh
->version
, &gm
, port_map
, table_map
);
2346 ofputil_uninit_group_mod(&gm
);