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-table.h"
21 #include "openvswitch/dynamic-string.h"
22 #include "openvswitch/json.h"
23 #include "openvswitch/ofp-actions.h"
24 #include "openvswitch/ofp-msgs.h"
25 #include "openvswitch/ofp-print.h"
26 #include "openvswitch/ofp-prop.h"
27 #include "openvswitch/ofpbuf.h"
28 #include "openvswitch/vlog.h"
31 VLOG_DEFINE_THIS_MODULE(ofp_table
);
33 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
35 static ovs_be32
ofputil_encode_table_config(enum ofputil_table_miss
,
36 enum ofputil_table_eviction
,
37 enum ofputil_table_vacancy
,
39 static enum ofputil_table_vacancy
ofputil_decode_table_vacancy(
40 ovs_be32 config
, enum ofp_version
);
41 static enum ofputil_table_eviction
ofputil_decode_table_eviction(
42 ovs_be32 config
, enum ofp_version
);
45 ofputil_table_miss_to_string(enum ofputil_table_miss miss
)
48 case OFPUTIL_TABLE_MISS_DEFAULT
: return "default";
49 case OFPUTIL_TABLE_MISS_CONTROLLER
: return "controller";
50 case OFPUTIL_TABLE_MISS_CONTINUE
: return "continue";
51 case OFPUTIL_TABLE_MISS_DROP
: return "drop";
52 default: return "***error***";
57 ofputil_table_eviction_to_string(enum ofputil_table_eviction eviction
)
60 case OFPUTIL_TABLE_EVICTION_DEFAULT
: return "default";
61 case OFPUTIL_TABLE_EVICTION_ON
: return "on";
62 case OFPUTIL_TABLE_EVICTION_OFF
: return "off";
63 default: return "***error***";
68 ofputil_table_vacancy_to_string(enum ofputil_table_vacancy vacancy
)
71 case OFPUTIL_TABLE_VACANCY_DEFAULT
: return "default";
72 case OFPUTIL_TABLE_VACANCY_ON
: return "on";
73 case OFPUTIL_TABLE_VACANCY_OFF
: return "off";
74 default: return "***error***";
79 ofp15_table_features_command_is_valid(enum ofp15_table_features_command cmd
)
82 case OFPTFC15_REPLACE
:
85 case OFPTFC15_DISABLE
:
94 ofp15_table_features_command_to_string(enum ofp15_table_features_command cmd
)
97 case OFPTFC15_REPLACE
: return "replace";
98 case OFPTFC15_MODIFY
: return "modify";
99 case OFPTFC15_ENABLE
: return "enable";
100 case OFPTFC15_DISABLE
: return "disable";
101 default: return "***bad command***";
105 /* ofputil_table_map. */
108 ofputil_table_map_init(struct ofputil_table_map
*map
)
110 namemap_init(&map
->map
);
114 ofputil_table_map_put(struct ofputil_table_map
*map
,
115 uint8_t table_id
, const char *name
)
117 namemap_put(&map
->map
, table_id
, name
);
121 ofputil_table_map_get_name(const struct ofputil_table_map
*map
,
124 struct namemap_node
*node
125 = map
? namemap_find_by_number(&map
->map
, table_id
) : NULL
;
126 return node
&& !node
->duplicate
? node
->name
: NULL
;
130 ofputil_table_map_get_number(const struct ofputil_table_map
*map
,
133 struct namemap_node
*node
134 = map
? namemap_find_by_name(&map
->map
, name
) : NULL
;
135 return node
&& !node
->duplicate
? node
->number
: UINT8_MAX
;
139 ofputil_table_map_destroy(struct ofputil_table_map
*map
)
141 namemap_destroy(&map
->map
);
146 /* Stores the table number represented by 's' into '*tablep'. 's' may be an
147 * integer or, if 'table_map' is nonnull, a name (quoted or unquoted).
149 * Returns true if successful, false if 's' is not a valid OpenFlow table
150 * number or name. The caller should issue an error message in this case,
151 * because this function usually does not. (This gives the caller an
152 * opportunity to look up the table name another way, e.g. by contacting the
153 * switch and listing the names of all its tables). */
155 ofputil_table_from_string(const char *s
,
156 const struct ofputil_table_map
*table_map
,
161 VLOG_WARN("Negative value %s is not a valid table number.", s
);
166 if (str_to_uint(s
, 10, &table
)) {
168 VLOG_WARN("table %u is outside the supported range 0 through 255",
176 table
= ofputil_table_map_get_number(table_map
, s
);
178 size_t length
= strlen(s
);
181 && s
[length
- 1] == '"'
182 && json_string_unescape(s
+ 1, length
- 2, &name
)) {
183 table
= ofputil_table_map_get_number(table_map
, name
);
187 if (table
!= UINT8_MAX
) {
196 /* Appends to 's' a string representation of the OpenFlow table number 'table',
197 * either the table number or a name drawn from 'table_map'. */
199 ofputil_format_table(uint8_t table
, const struct ofputil_table_map
*table_map
,
202 const char *table_name
= ofputil_table_map_get_name(table_map
, table
);
204 namemap_put_name(table_name
, s
);
206 ds_put_format(s
, "%"PRIu8
, table
);
210 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
211 * representation of OpenFlow table number 'table', either the table's number
212 * or a name drawn from 'table_map'. */
214 ofputil_table_to_string(uint8_t table
,
215 const struct ofputil_table_map
*table_map
,
216 char *namebuf
, size_t bufsize
)
218 const char *table_name
= ofputil_table_map_get_name(table_map
, table
);
220 struct ds s
= DS_EMPTY_INITIALIZER
;
221 namemap_put_name(table_name
, &s
);
222 ovs_strlcpy(namebuf
, ds_cstr(&s
), bufsize
);
227 snprintf(namebuf
, bufsize
, "%"PRIu8
, table
);
230 /* Table features. */
233 pull_table_feature_property(struct ofpbuf
*msg
, struct ofpbuf
*payload
,
238 error
= ofpprop_pull(msg
, payload
, typep
);
239 if (payload
&& !error
) {
240 ofpbuf_pull(payload
, (char *)payload
->msg
- (char *)payload
->header
);
246 parse_action_bitmap(struct ofpbuf
*payload
, enum ofp_version ofp_version
,
251 while (payload
->size
> 0) {
255 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &type
);
259 if (type
< CHAR_BIT
* sizeof types
) {
264 *ofpacts
= ofpact_bitmap_from_openflow(htonl(types
), ofp_version
);
269 parse_instruction_ids(struct ofpbuf
*payload
, bool loose
, uint32_t *insts
)
272 while (payload
->size
> 0) {
273 enum ovs_instruction_type inst
;
277 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
278 * It seems clear that they aren't padded to 8 bytes, though, because
279 * both standards say that "non-experimenter instructions are 4 bytes"
280 * and do not mention any padding before the first instruction ID.
281 * (There wouldn't be any point in padding to 8 bytes if the IDs were
282 * aligned on an odd 4-byte boundary.)
284 * Anyway, we just assume they're all glommed together on byte
286 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &ofpit
);
291 error
= ovs_instruction_type_from_inst_type(&inst
, ofpit
);
293 *insts
|= 1u << inst
;
302 parse_table_features_next_table(struct ofpbuf
*payload
,
303 unsigned long int *next_tables
)
307 memset(next_tables
, 0, bitmap_n_bytes(255));
308 for (i
= 0; i
< payload
->size
; i
++) {
309 uint8_t id
= ((const uint8_t *) payload
->data
)[i
];
311 return OFPERR_OFPBPC_BAD_VALUE
;
313 bitmap_set1(next_tables
, id
);
319 parse_oxms(struct ofpbuf
*payload
, bool loose
,
320 struct mf_bitmap
*exactp
, struct mf_bitmap
*maskedp
)
322 struct mf_bitmap exact
= MF_BITMAP_INITIALIZER
;
323 struct mf_bitmap masked
= MF_BITMAP_INITIALIZER
;
325 while (payload
->size
> 0) {
326 const struct mf_field
*field
;
330 error
= nx_pull_header(payload
, NULL
, &field
, &hasmask
);
332 bitmap_set1(hasmask
? masked
.bm
: exact
.bm
, field
->id
);
333 } else if (error
!= OFPERR_OFPBMC_BAD_FIELD
|| !loose
) {
339 } else if (!bitmap_is_all_zeros(exact
.bm
, MFF_N_IDS
)) {
340 return OFPERR_OFPBMC_BAD_MASK
;
344 } else if (!bitmap_is_all_zeros(masked
.bm
, MFF_N_IDS
)) {
345 return OFPERR_OFPBMC_BAD_MASK
;
350 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
351 * ofputil_table_features in 'tf'.
353 * If 'raw_properties' is nonnull, this function ignores properties and values
354 * that it does not understand, as a controller would want to do when
355 * interpreting capabilities provided by a switch. In this mode, on success,
356 * it initializes 'raw_properties' to contain the properties that were parsed;
357 * this allows the caller to later re-serialize the same properties without
360 * If 'raw_properties' is null, this function treats unknown properties and
361 * values as an error, as a switch would want to do when interpreting a
362 * configuration request made by a controller.
364 * A single OpenFlow message can specify features for multiple tables. Calling
365 * this function multiple times for a single 'msg' iterates through the tables
366 * in the message. The caller must initially leave 'msg''s layer pointers null
367 * and not modify them between calls.
369 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
370 * a positive "enum ofperr" value. */
372 ofputil_decode_table_features(struct ofpbuf
*msg
,
373 struct ofputil_table_features
*tf
,
374 struct ofpbuf
*raw_properties
)
376 bool loose
= raw_properties
!= NULL
;
378 memset(tf
, 0, sizeof *tf
);
381 ofpraw_pull_assert(msg
);
388 const struct ofp_header
*oh
= msg
->header
;
389 struct ofp13_table_features
*otf
= msg
->data
;
390 if (msg
->size
< sizeof *otf
) {
391 return OFPERR_OFPBPC_BAD_LEN
;
394 unsigned int len
= ntohs(otf
->length
);
395 if (len
< sizeof *otf
|| len
% 8 || len
> msg
->size
) {
396 return OFPERR_OFPBPC_BAD_LEN
;
399 if (oh
->version
>= OFP15_VERSION
) {
400 if (!ofp15_table_features_command_is_valid(otf
->command
)) {
401 return OFPERR_OFPTFFC_BAD_COMMAND
;
403 tf
->command
= otf
->command
;
405 tf
->command
= OFPTFC15_REPLACE
;
408 tf
->table_id
= otf
->table_id
;
409 if (tf
->table_id
== OFPTT_ALL
) {
410 return OFPERR_OFPTFFC_BAD_TABLE
;
413 ovs_strlcpy_arrays(tf
->name
, otf
->name
);
414 tf
->metadata_match
= otf
->metadata_match
;
415 tf
->metadata_write
= otf
->metadata_write
;
416 tf
->miss_config
= OFPUTIL_TABLE_MISS_DEFAULT
;
417 if (oh
->version
>= OFP14_VERSION
) {
418 uint32_t caps
= ntohl(otf
->capabilities
);
419 tf
->supports_eviction
= (caps
& OFPTC14_EVICTION
) != 0;
420 tf
->supports_vacancy_events
= (caps
& OFPTC14_VACANCY_EVENTS
) != 0;
422 tf
->supports_eviction
= -1;
423 tf
->supports_vacancy_events
= -1;
425 tf
->max_entries
= ntohl(otf
->max_entries
);
427 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
429 ofpbuf_pull(&properties
, sizeof *otf
);
430 tf
->any_properties
= properties
.size
> 0;
431 if (raw_properties
) {
432 *raw_properties
= properties
;
435 while (properties
.size
> 0) {
436 struct ofpbuf payload
;
440 error
= pull_table_feature_property(&properties
, &payload
, &type
);
446 uint32_t bit
= 1u << type
;
448 return OFPERR_OFPTFFC_BAD_FEATURES
;
453 switch ((enum ofp13_table_feature_prop_type
) type
) {
454 case OFPTFPT13_INSTRUCTIONS
:
455 error
= parse_instruction_ids(&payload
, loose
,
456 &tf
->nonmiss
.instructions
);
458 case OFPTFPT13_INSTRUCTIONS_MISS
:
459 error
= parse_instruction_ids(&payload
, loose
,
460 &tf
->miss
.instructions
);
463 case OFPTFPT13_NEXT_TABLES
:
464 error
= parse_table_features_next_table(&payload
,
467 case OFPTFPT13_NEXT_TABLES_MISS
:
468 error
= parse_table_features_next_table(&payload
, tf
->miss
.next
);
471 case OFPTFPT13_WRITE_ACTIONS
:
472 error
= parse_action_bitmap(&payload
, oh
->version
,
473 &tf
->nonmiss
.write
.ofpacts
);
475 case OFPTFPT13_WRITE_ACTIONS_MISS
:
476 error
= parse_action_bitmap(&payload
, oh
->version
,
477 &tf
->miss
.write
.ofpacts
);
480 case OFPTFPT13_APPLY_ACTIONS
:
481 error
= parse_action_bitmap(&payload
, oh
->version
,
482 &tf
->nonmiss
.apply
.ofpacts
);
484 case OFPTFPT13_APPLY_ACTIONS_MISS
:
485 error
= parse_action_bitmap(&payload
, oh
->version
,
486 &tf
->miss
.apply
.ofpacts
);
489 case OFPTFPT13_MATCH
:
490 error
= parse_oxms(&payload
, loose
, &tf
->match
, &tf
->mask
);
492 case OFPTFPT13_WILDCARDS
:
493 error
= parse_oxms(&payload
, loose
, &tf
->wildcard
, NULL
);
496 case OFPTFPT13_WRITE_SETFIELD
:
497 error
= parse_oxms(&payload
, loose
,
498 &tf
->nonmiss
.write
.set_fields
, NULL
);
500 case OFPTFPT13_WRITE_SETFIELD_MISS
:
501 error
= parse_oxms(&payload
, loose
,
502 &tf
->miss
.write
.set_fields
, NULL
);
504 case OFPTFPT13_APPLY_SETFIELD
:
505 error
= parse_oxms(&payload
, loose
,
506 &tf
->nonmiss
.apply
.set_fields
, NULL
);
508 case OFPTFPT13_APPLY_SETFIELD_MISS
:
509 error
= parse_oxms(&payload
, loose
,
510 &tf
->miss
.apply
.set_fields
, NULL
);
513 case OFPTFPT13_EXPERIMENTER
:
514 case OFPTFPT13_EXPERIMENTER_MISS
:
516 error
= OFPPROP_UNKNOWN(loose
, "table features", type
);
524 /* OpenFlow 1.3 and 1.4 always require all of the required properties.
525 * OpenFlow 1.5 requires all of them if any property is present. */
526 if ((seen
& OFPTFPT13_REQUIRED
) != OFPTFPT13_REQUIRED
527 && (tf
->any_properties
|| oh
->version
< OFP15_VERSION
)) {
528 VLOG_WARN_RL(&rl
, "table features message missing required property");
529 return OFPERR_OFPTFFC_BAD_FEATURES
;
532 /* Copy nonmiss to miss when appropriate. */
533 if (tf
->any_properties
) {
534 if (!(seen
& (1u << OFPTFPT13_INSTRUCTIONS_MISS
))) {
535 tf
->miss
.instructions
= tf
->nonmiss
.instructions
;
537 if (!(seen
& (1u << OFPTFPT13_NEXT_TABLES_MISS
))) {
538 memcpy(tf
->miss
.next
, tf
->nonmiss
.next
, sizeof tf
->miss
.next
);
540 if (!(seen
& (1u << OFPTFPT13_WRITE_ACTIONS_MISS
))) {
541 tf
->miss
.write
.ofpacts
= tf
->nonmiss
.write
.ofpacts
;
543 if (!(seen
& (1u << OFPTFPT13_APPLY_ACTIONS_MISS
))) {
544 tf
->miss
.apply
.ofpacts
= tf
->nonmiss
.apply
.ofpacts
;
546 if (!(seen
& (1u << OFPTFPT13_WRITE_SETFIELD_MISS
))) {
547 tf
->miss
.write
.set_fields
= tf
->nonmiss
.write
.set_fields
;
549 if (!(seen
& (1u << OFPTFPT13_APPLY_SETFIELD_MISS
))) {
550 tf
->miss
.apply
.set_fields
= tf
->nonmiss
.apply
.set_fields
;
554 /* Fix inconsistencies:
556 * - Turn on 'match' bits that are set in 'mask', because maskable
557 * fields are matchable.
559 * - Turn on 'wildcard' bits that are set in 'mask', because a field
560 * that is arbitrarily maskable can be wildcarded entirely.
562 * - Turn off 'wildcard' bits that are not in 'match', because a field
563 * must be matchable for it to be meaningfully wildcarded. */
564 bitmap_or(tf
->match
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
565 bitmap_or(tf
->wildcard
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
566 bitmap_and(tf
->wildcard
.bm
, tf
->match
.bm
, MFF_N_IDS
);
571 /* Encodes and returns a request to obtain the table features of a switch.
572 * The message is encoded for OpenFlow version 'ofp_version'. */
574 ofputil_encode_table_features_request(enum ofp_version ofp_version
)
576 struct ofpbuf
*request
= NULL
;
578 switch (ofp_version
) {
582 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
583 "(\'-O OpenFlow13\')");
588 request
= ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST
,
599 put_fields_property(struct ofpbuf
*reply
,
600 const struct mf_bitmap
*fields
,
601 const struct mf_bitmap
*masks
,
602 enum ofp13_table_feature_prop_type property
,
603 enum ofp_version version
)
608 start_ofs
= ofpprop_start(reply
, property
);
609 BITMAP_FOR_EACH_1 (field
, MFF_N_IDS
, fields
->bm
) {
610 nx_put_header(reply
, field
, version
,
611 masks
&& bitmap_is_set(masks
->bm
, field
));
613 ofpprop_end(reply
, start_ofs
);
617 put_table_action_features(struct ofpbuf
*reply
,
618 const struct ofputil_table_action_features
*taf
,
619 enum ofp13_table_feature_prop_type actions_type
,
620 enum ofp13_table_feature_prop_type set_fields_type
,
621 int miss_offset
, enum ofp_version version
)
623 ofpprop_put_bitmap(reply
, actions_type
+ miss_offset
,
624 ntohl(ofpact_bitmap_to_openflow(taf
->ofpacts
,
626 put_fields_property(reply
, &taf
->set_fields
, NULL
,
627 set_fields_type
+ miss_offset
, version
);
631 put_table_instruction_features(
632 struct ofpbuf
*reply
, const struct ofputil_table_instruction_features
*tif
,
633 int miss_offset
, enum ofp_version version
)
638 ofpprop_put_bitmap(reply
, OFPTFPT13_INSTRUCTIONS
+ miss_offset
,
639 ntohl(ovsinst_bitmap_to_openflow(tif
->instructions
,
642 start_ofs
= ofpprop_start(reply
, OFPTFPT13_NEXT_TABLES
+ miss_offset
);
643 BITMAP_FOR_EACH_1 (table_id
, 255, tif
->next
) {
644 ofpbuf_put(reply
, &table_id
, 1);
646 ofpprop_end(reply
, start_ofs
);
648 put_table_action_features(reply
, &tif
->write
,
649 OFPTFPT13_WRITE_ACTIONS
,
650 OFPTFPT13_WRITE_SETFIELD
, miss_offset
, version
);
651 put_table_action_features(reply
, &tif
->apply
,
652 OFPTFPT13_APPLY_ACTIONS
,
653 OFPTFPT13_APPLY_SETFIELD
, miss_offset
, version
);
656 /* Appends a table features record to 'msgs', which must be a
657 * OFPT_TABLE_FEATURES request or reply. If 'raw_properties' is nonnull, then
658 * it uses its contents verbatim as the table features properties, ignoring the
659 * corresponding members of 'tf'. */
661 ofputil_append_table_features(const struct ofputil_table_features
*tf
,
662 const struct ofpbuf
*raw_properties
,
663 struct ovs_list
*msgs
)
665 struct ofpbuf
*msg
= ofpbuf_from_list(ovs_list_back(msgs
));
666 enum ofp_version version
= ofpmp_version(msgs
);
667 size_t start_ofs
= msg
->size
;
668 struct ofp13_table_features
*otf
;
670 otf
= ofpbuf_put_zeros(msg
, sizeof *otf
);
671 otf
->table_id
= tf
->table_id
;
672 otf
->command
= version
>= OFP15_VERSION
? tf
->command
: 0;
673 ovs_strlcpy_arrays(otf
->name
, tf
->name
);
674 otf
->metadata_match
= tf
->metadata_match
;
675 otf
->metadata_write
= tf
->metadata_write
;
676 if (version
>= OFP14_VERSION
) {
677 if (tf
->supports_eviction
) {
678 otf
->capabilities
|= htonl(OFPTC14_EVICTION
);
680 if (tf
->supports_vacancy_events
) {
681 otf
->capabilities
|= htonl(OFPTC14_VACANCY_EVENTS
);
684 otf
->max_entries
= htonl(tf
->max_entries
);
686 if (raw_properties
) {
687 ofpbuf_put(msg
, raw_properties
->data
, raw_properties
->size
);
688 } else if (tf
->any_properties
) {
689 put_table_instruction_features(msg
, &tf
->nonmiss
, 0, version
);
690 put_table_instruction_features(msg
, &tf
->miss
, 1, version
);
692 put_fields_property(msg
, &tf
->match
, &tf
->mask
,
693 OFPTFPT13_MATCH
, version
);
694 put_fields_property(msg
, &tf
->wildcard
, NULL
,
695 OFPTFPT13_WILDCARDS
, version
);
698 otf
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *otf
);
699 otf
->length
= htons(msg
->size
- start_ofs
);
700 ofpmp_postappend(msgs
, start_ofs
);
704 parse_table_desc_vacancy_property(struct ofpbuf
*property
,
705 struct ofputil_table_desc
*td
)
707 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
709 if (property
->size
!= sizeof *otv
) {
710 return OFPERR_OFPBPC_BAD_LEN
;
713 td
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
714 td
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
715 td
->table_vacancy
.vacancy
= otv
->vacancy
;
719 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
720 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
721 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
724 ofputil_decode_table_desc(struct ofpbuf
*msg
,
725 struct ofputil_table_desc
*td
,
726 enum ofp_version version
)
728 memset(td
, 0, sizeof *td
);
731 ofpraw_pull_assert(msg
);
738 struct ofp14_table_desc
*otd
= ofpbuf_try_pull(msg
, sizeof *otd
);
740 VLOG_WARN_RL(&rl
, "OFP14_TABLE_DESC reply has %"PRIu32
" "
741 "leftover bytes at end", msg
->size
);
742 return OFPERR_OFPBRC_BAD_LEN
;
745 td
->table_id
= otd
->table_id
;
746 size_t length
= ntohs(otd
->length
);
747 if (length
< sizeof *otd
|| length
- sizeof *otd
> msg
->size
) {
748 VLOG_WARN_RL(&rl
, "OFP14_TABLE_DESC reply claims invalid "
749 "length %"PRIuSIZE
, length
);
750 return OFPERR_OFPBRC_BAD_LEN
;
752 length
-= sizeof *otd
;
754 td
->eviction
= ofputil_decode_table_eviction(otd
->config
, version
);
755 td
->vacancy
= ofputil_decode_table_vacancy(otd
->config
, version
);
756 td
->eviction_flags
= UINT32_MAX
;
758 struct ofpbuf properties
= ofpbuf_const_initializer(
759 ofpbuf_pull(msg
, length
), length
);
760 while (properties
.size
> 0) {
761 struct ofpbuf payload
;
765 error
= ofpprop_pull(&properties
, &payload
, &type
);
771 case OFPTMPT14_EVICTION
:
772 error
= ofpprop_parse_u32(&payload
, &td
->eviction_flags
);
775 case OFPTMPT14_VACANCY
:
776 error
= parse_table_desc_vacancy_property(&payload
, td
);
780 error
= OFPPROP_UNKNOWN(true, "table_desc", type
);
792 /* Encodes and returns a request to obtain description of tables of a switch.
793 * The message is encoded for OpenFlow version 'ofp_version'. */
795 ofputil_encode_table_desc_request(enum ofp_version ofp_version
)
797 struct ofpbuf
*request
= NULL
;
799 if (ofp_version
>= OFP14_VERSION
) {
800 request
= ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST
,
803 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
804 "(\'-O OpenFlow14\')");
810 /* Function to append Table desc information in a reply list. */
812 ofputil_append_table_desc_reply(const struct ofputil_table_desc
*td
,
813 struct ovs_list
*replies
,
814 enum ofp_version version
)
816 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
818 struct ofp14_table_desc
*otd
;
820 start_otd
= reply
->size
;
821 ofpbuf_put_zeros(reply
, sizeof *otd
);
822 if (td
->eviction_flags
!= UINT32_MAX
) {
823 ofpprop_put_u32(reply
, OFPTMPT14_EVICTION
, td
->eviction_flags
);
825 if (td
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
826 struct ofp14_table_mod_prop_vacancy
*otv
;
828 otv
= ofpprop_put_zeros(reply
, OFPTMPT14_VACANCY
, sizeof *otv
);
829 otv
->vacancy_down
= td
->table_vacancy
.vacancy_down
;
830 otv
->vacancy_up
= td
->table_vacancy
.vacancy_up
;
831 otv
->vacancy
= td
->table_vacancy
.vacancy
;
834 otd
= ofpbuf_at_assert(reply
, start_otd
, sizeof *otd
);
835 otd
->length
= htons(reply
->size
- start_otd
);
836 otd
->table_id
= td
->table_id
;
837 otd
->config
= ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT
,
838 td
->eviction
, td
->vacancy
,
840 ofpmp_postappend(replies
, start_otd
);
844 ofputil_eviction_flag_to_string(uint32_t bit
)
846 enum ofp14_table_mod_prop_eviction_flag eviction_flag
= bit
;
848 switch (eviction_flag
) {
849 case OFPTMPEF14_OTHER
: return "OTHER";
850 case OFPTMPEF14_IMPORTANCE
: return "IMPORTANCE";
851 case OFPTMPEF14_LIFETIME
: return "LIFETIME";
857 /* Appends to 'string' a description of the bitmap of OFPTMPEF14_* values in
858 * 'eviction_flags'. */
860 ofputil_put_eviction_flags(struct ds
*string
, uint32_t eviction_flags
)
862 if (eviction_flags
!= UINT32_MAX
) {
863 ofp_print_bit_names(string
, eviction_flags
,
864 ofputil_eviction_flag_to_string
, '|');
866 ds_put_cstr(string
, "(default)");
871 ofputil_table_desc_format(struct ds
*s
, const struct ofputil_table_desc
*td
,
872 const struct ofputil_table_map
*table_map
)
874 ds_put_format(s
, "\n table ");
875 ofputil_format_table(td
->table_id
, table_map
, s
);
876 ds_put_cstr(s
, ":\n");
877 ds_put_format(s
, " eviction=%s eviction_flags=",
878 ofputil_table_eviction_to_string(td
->eviction
));
879 ofputil_put_eviction_flags(s
, td
->eviction_flags
);
880 ds_put_char(s
, '\n');
881 ds_put_format(s
, " vacancy=%s",
882 ofputil_table_vacancy_to_string(td
->vacancy
));
883 if (td
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
884 ds_put_format(s
, " vacancy_down=%"PRIu8
"%%",
885 td
->table_vacancy
.vacancy_down
);
886 ds_put_format(s
, " vacancy_up=%"PRIu8
"%%",
887 td
->table_vacancy
.vacancy_up
);
888 ds_put_format(s
, " vacancy=%"PRIu8
"%%",
889 td
->table_vacancy
.vacancy
);
891 ds_put_char(s
, '\n');
894 /* This function parses Vacancy property, and decodes the
895 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
896 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
897 * greater than vacancy_up and also when current vacancy has non-zero
898 * value. Returns 0 on success. */
900 parse_table_mod_vacancy_property(struct ofpbuf
*property
,
901 struct ofputil_table_mod
*tm
)
903 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
905 if (property
->size
!= sizeof *otv
) {
906 return OFPERR_OFPBPC_BAD_LEN
;
908 tm
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
909 tm
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
910 if (tm
->table_vacancy
.vacancy_down
> tm
->table_vacancy
.vacancy_up
) {
911 OFPPROP_LOG(&rl
, false,
912 "Value of vacancy_down is greater than vacancy_up");
913 return OFPERR_OFPBPC_BAD_VALUE
;
915 if (tm
->table_vacancy
.vacancy_down
> 100 ||
916 tm
->table_vacancy
.vacancy_up
> 100) {
917 OFPPROP_LOG(&rl
, false, "Vacancy threshold percentage "
918 "should not be greater than 100");
919 return OFPERR_OFPBPC_BAD_VALUE
;
921 tm
->table_vacancy
.vacancy
= otv
->vacancy
;
922 if (tm
->table_vacancy
.vacancy
) {
923 OFPPROP_LOG(&rl
, false,
924 "Vacancy value should be zero for table-mod messages");
925 return OFPERR_OFPBPC_BAD_VALUE
;
930 /* Given 'config', taken from an OpenFlow 'version' message that specifies
931 * table configuration (a table mod, table stats, or table features message),
932 * returns the table vacancy configuration that it specifies.
934 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
935 * so for other 'version' this function always returns
936 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
937 static enum ofputil_table_vacancy
938 ofputil_decode_table_vacancy(ovs_be32 config
, enum ofp_version version
)
940 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_VACANCY_DEFAULT
941 : config
& htonl(OFPTC14_VACANCY_EVENTS
) ? OFPUTIL_TABLE_VACANCY_ON
942 : OFPUTIL_TABLE_VACANCY_OFF
);
945 /* Given 'config', taken from an OpenFlow 'version' message that specifies
946 * table configuration (a table mod, table stats, or table features message),
947 * returns the table eviction configuration that it specifies.
949 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
950 * so for other 'version' values this function always returns
951 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
952 static enum ofputil_table_eviction
953 ofputil_decode_table_eviction(ovs_be32 config
, enum ofp_version version
)
955 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_EVICTION_DEFAULT
956 : config
& htonl(OFPTC14_EVICTION
) ? OFPUTIL_TABLE_EVICTION_ON
957 : OFPUTIL_TABLE_EVICTION_OFF
);
960 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
961 * OpenFlow messages of the given 'version', based on the provided 'miss' and
962 * 'eviction' values. */
964 ofputil_encode_table_config(enum ofputil_table_miss miss
,
965 enum ofputil_table_eviction eviction
,
966 enum ofputil_table_vacancy vacancy
,
967 enum ofp_version version
)
970 /* Search for "OFPTC_* Table Configuration" in the documentation for more
971 * information on the crazy evolution of this field. */
974 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
979 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
981 case OFPUTIL_TABLE_MISS_DEFAULT
:
982 /* Really this shouldn't be used for encoding (the caller should
983 * provide a specific value) but I can't imagine that defaulting to
984 * the fall-through case here will hurt. */
985 case OFPUTIL_TABLE_MISS_CONTROLLER
:
987 return htonl(OFPTC11_TABLE_MISS_CONTROLLER
);
988 case OFPUTIL_TABLE_MISS_CONTINUE
:
989 return htonl(OFPTC11_TABLE_MISS_CONTINUE
);
990 case OFPUTIL_TABLE_MISS_DROP
:
991 return htonl(OFPTC11_TABLE_MISS_DROP
);
996 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
997 * flags, so this is correct. */
1003 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
1004 * OFPTC14_VACANCY_EVENTS. */
1005 if (eviction
== OFPUTIL_TABLE_EVICTION_ON
) {
1006 config
|= OFPTC14_EVICTION
;
1008 if (vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
1009 config
|= OFPTC14_VACANCY_EVENTS
;
1011 return htonl(config
);
1017 /* Given 'config', taken from an OpenFlow 'version' message that specifies
1018 * table configuration (a table mod, table stats, or table features message),
1019 * returns the table miss configuration that it specifies.
1021 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
1022 * other 'version' values this function always returns
1023 * OFPUTIL_TABLE_MISS_DEFAULT. */
1024 static enum ofputil_table_miss
1025 ofputil_decode_table_miss(ovs_be32 config_
, enum ofp_version version
)
1027 uint32_t config
= ntohl(config_
);
1029 if (version
== OFP11_VERSION
|| version
== OFP12_VERSION
) {
1030 switch (config
& OFPTC11_TABLE_MISS_MASK
) {
1031 case OFPTC11_TABLE_MISS_CONTROLLER
:
1032 return OFPUTIL_TABLE_MISS_CONTROLLER
;
1034 case OFPTC11_TABLE_MISS_CONTINUE
:
1035 return OFPUTIL_TABLE_MISS_CONTINUE
;
1037 case OFPTC11_TABLE_MISS_DROP
:
1038 return OFPUTIL_TABLE_MISS_DROP
;
1041 VLOG_WARN_RL(&rl
, "bad table miss config %d", config
);
1042 return OFPUTIL_TABLE_MISS_CONTROLLER
;
1045 return OFPUTIL_TABLE_MISS_DEFAULT
;
1049 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
1050 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
1052 ofputil_decode_table_mod(const struct ofp_header
*oh
,
1053 struct ofputil_table_mod
*pm
)
1055 memset(pm
, 0, sizeof *pm
);
1056 pm
->miss
= OFPUTIL_TABLE_MISS_DEFAULT
;
1057 pm
->eviction
= OFPUTIL_TABLE_EVICTION_DEFAULT
;
1058 pm
->eviction_flags
= UINT32_MAX
;
1059 pm
->vacancy
= OFPUTIL_TABLE_VACANCY_DEFAULT
;
1061 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1062 enum ofpraw raw
= ofpraw_pull_assert(&b
);
1063 if (raw
== OFPRAW_OFPT11_TABLE_MOD
) {
1064 const struct ofp11_table_mod
*otm
= b
.data
;
1066 pm
->table_id
= otm
->table_id
;
1067 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
1068 } else if (raw
== OFPRAW_OFPT14_TABLE_MOD
) {
1069 const struct ofp14_table_mod
*otm
= ofpbuf_pull(&b
, sizeof *otm
);
1071 pm
->table_id
= otm
->table_id
;
1072 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
1073 pm
->eviction
= ofputil_decode_table_eviction(otm
->config
, oh
->version
);
1074 pm
->vacancy
= ofputil_decode_table_vacancy(otm
->config
, oh
->version
);
1075 while (b
.size
> 0) {
1076 struct ofpbuf property
;
1080 error
= ofpprop_pull(&b
, &property
, &type
);
1086 case OFPTMPT14_EVICTION
:
1087 error
= ofpprop_parse_u32(&property
, &pm
->eviction
);
1090 case OFPTMPT14_VACANCY
:
1091 error
= parse_table_mod_vacancy_property(&property
, pm
);
1095 error
= OFPERR_OFPBRC_BAD_TYPE
;
1104 return OFPERR_OFPBRC_BAD_TYPE
;
1110 /* Converts the abstract form of a "table mod" message in '*tm' into an
1111 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
1112 * buffer owned by the caller. */
1114 ofputil_encode_table_mod(const struct ofputil_table_mod
*tm
,
1115 enum ofputil_protocol protocol
)
1117 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
1120 switch (ofp_version
) {
1121 case OFP10_VERSION
: {
1122 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
1123 "(\'-O OpenFlow11\')");
1128 case OFP13_VERSION
: {
1129 struct ofp11_table_mod
*otm
;
1131 b
= ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD
, ofp_version
, 0);
1132 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
1133 otm
->table_id
= tm
->table_id
;
1134 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
1135 tm
->vacancy
, ofp_version
);
1140 case OFP16_VERSION
: {
1141 struct ofp14_table_mod
*otm
;
1143 b
= ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD
, ofp_version
, 0);
1144 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
1145 otm
->table_id
= tm
->table_id
;
1146 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
1147 tm
->vacancy
, ofp_version
);
1149 if (tm
->eviction_flags
!= UINT32_MAX
) {
1150 ofpprop_put_u32(b
, OFPTMPT14_EVICTION
, tm
->eviction_flags
);
1152 if (tm
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
1153 struct ofp14_table_mod_prop_vacancy
*otv
;
1155 otv
= ofpprop_put_zeros(b
, OFPTMPT14_VACANCY
, sizeof *otv
);
1156 otv
->vacancy_down
= tm
->table_vacancy
.vacancy_down
;
1157 otv
->vacancy_up
= tm
->table_vacancy
.vacancy_up
;
1169 ofputil_table_mod_format(struct ds
*s
, const struct ofputil_table_mod
*tm
,
1170 const struct ofputil_table_map
*table_map
)
1172 if (tm
->table_id
== 0xff) {
1173 ds_put_cstr(s
, " table_id: ALL_TABLES");
1175 ds_put_format(s
, " table_id=");
1176 ofputil_format_table(tm
->table_id
, table_map
, s
);
1179 if (tm
->miss
!= OFPUTIL_TABLE_MISS_DEFAULT
) {
1180 ds_put_format(s
, ", flow_miss_config=%s",
1181 ofputil_table_miss_to_string(tm
->miss
));
1183 if (tm
->eviction
!= OFPUTIL_TABLE_EVICTION_DEFAULT
) {
1184 ds_put_format(s
, ", eviction=%s",
1185 ofputil_table_eviction_to_string(tm
->eviction
));
1187 if (tm
->eviction_flags
!= UINT32_MAX
) {
1188 ds_put_cstr(s
, "eviction_flags=");
1189 ofputil_put_eviction_flags(s
, tm
->eviction_flags
);
1191 if (tm
->vacancy
!= OFPUTIL_TABLE_VACANCY_DEFAULT
) {
1192 ds_put_format(s
, ", vacancy=%s",
1193 ofputil_table_vacancy_to_string(tm
->vacancy
));
1194 if (tm
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
1195 ds_put_format(s
, " vacancy:%"PRIu8
""
1196 ",%"PRIu8
"", tm
->table_vacancy
.vacancy_down
,
1197 tm
->table_vacancy
.vacancy_up
);
1202 /* Convert 'setting' (as described for the "mod-table" command
1203 * in ovs-ofctl man page) into 'tm->table_vacancy->vacancy_up' and
1204 * 'tm->table_vacancy->vacancy_down' threshold values.
1205 * For the two threshold values, value of vacancy_up is always greater
1206 * than value of vacancy_down.
1208 * Returns NULL if successful, otherwise a malloc()'d string describing the
1209 * error. The caller is responsible for freeing the returned string. */
1210 static char * OVS_WARN_UNUSED_RESULT
1211 parse_ofp_table_vacancy(struct ofputil_table_mod
*tm
, const char *setting
)
1213 char *save_ptr
= NULL
;
1214 char *vac_up
, *vac_down
;
1215 char *value
= xstrdup(setting
);
1217 int vacancy_up
, vacancy_down
;
1219 strtok_r(value
, ":", &save_ptr
);
1220 vac_down
= strtok_r(NULL
, ",", &save_ptr
);
1222 ret_msg
= xasprintf("Vacancy down value missing");
1225 if (!str_to_int(vac_down
, 0, &vacancy_down
) ||
1226 vacancy_down
< 0 || vacancy_down
> 100) {
1227 ret_msg
= xasprintf("Invalid vacancy down value \"%s\"", vac_down
);
1230 vac_up
= strtok_r(NULL
, ",", &save_ptr
);
1232 ret_msg
= xasprintf("Vacancy up value missing");
1235 if (!str_to_int(vac_up
, 0, &vacancy_up
) ||
1236 vacancy_up
< 0 || vacancy_up
> 100) {
1237 ret_msg
= xasprintf("Invalid vacancy up value \"%s\"", vac_up
);
1240 if (vacancy_down
> vacancy_up
) {
1241 ret_msg
= xasprintf("Invalid vacancy range, vacancy up should be "
1242 "greater than vacancy down (%s)",
1243 ofperr_to_string(OFPERR_OFPBPC_BAD_VALUE
));
1248 tm
->table_vacancy
.vacancy_down
= vacancy_down
;
1249 tm
->table_vacancy
.vacancy_up
= vacancy_up
;
1257 /* Convert 'table_id' and 'setting' (as described for the "mod-table" command
1258 * in the ovs-ofctl man page) into 'tm' for sending a table_mod command to a
1259 * switch. If 'setting' sets the name of the table, puts the new name in
1260 * '*namep' (a suffix of 'setting'), otherwise stores NULL.
1262 * Stores a bitmap of the OpenFlow versions that are usable for 'tm' into
1263 * '*usable_versions'.
1265 * Returns NULL if successful, otherwise a malloc()'d string describing the
1266 * error. The caller is responsible for freeing the returned string. */
1267 char * OVS_WARN_UNUSED_RESULT
1268 parse_ofp_table_mod(struct ofputil_table_mod
*tm
, const char **namep
,
1269 const char *table_id
, const char *setting
,
1270 const struct ofputil_table_map
*table_map
,
1271 uint32_t *usable_versions
)
1273 *usable_versions
= 0;
1275 if (!strcasecmp(table_id
, "all")) {
1276 tm
->table_id
= OFPTT_ALL
;
1277 } else if (!ofputil_table_from_string(table_id
, table_map
,
1279 return xasprintf("unknown table \"%s\"", table_id
);
1282 tm
->miss
= OFPUTIL_TABLE_MISS_DEFAULT
;
1283 tm
->eviction
= OFPUTIL_TABLE_EVICTION_DEFAULT
;
1284 tm
->eviction_flags
= UINT32_MAX
;
1285 tm
->vacancy
= OFPUTIL_TABLE_VACANCY_DEFAULT
;
1286 tm
->table_vacancy
.vacancy_down
= 0;
1287 tm
->table_vacancy
.vacancy_up
= 0;
1288 tm
->table_vacancy
.vacancy
= 0;
1289 /* Only OpenFlow 1.1 and 1.2 can configure table-miss via table_mod.
1290 * Only OpenFlow 1.4+ can configure eviction and vacancy events
1293 if (!strcmp(setting
, "controller")) {
1294 tm
->miss
= OFPUTIL_TABLE_MISS_CONTROLLER
;
1295 *usable_versions
= (1u << OFP11_VERSION
) | (1u << OFP12_VERSION
);
1296 } else if (!strcmp(setting
, "continue")) {
1297 tm
->miss
= OFPUTIL_TABLE_MISS_CONTINUE
;
1298 *usable_versions
= (1u << OFP11_VERSION
) | (1u << OFP12_VERSION
);
1299 } else if (!strcmp(setting
, "drop")) {
1300 tm
->miss
= OFPUTIL_TABLE_MISS_DROP
;
1301 *usable_versions
= (1u << OFP11_VERSION
) | (1u << OFP12_VERSION
);
1302 } else if (!strcmp(setting
, "evict")) {
1303 tm
->eviction
= OFPUTIL_TABLE_EVICTION_ON
;
1304 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1305 } else if (!strcmp(setting
, "noevict")) {
1306 tm
->eviction
= OFPUTIL_TABLE_EVICTION_OFF
;
1307 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1308 } else if (!strncmp(setting
, "vacancy", strcspn(setting
, ":"))) {
1309 tm
->vacancy
= OFPUTIL_TABLE_VACANCY_ON
;
1310 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1311 char *error
= parse_ofp_table_vacancy(tm
, setting
);
1315 } else if (!strcmp(setting
, "novacancy")) {
1316 tm
->vacancy
= OFPUTIL_TABLE_VACANCY_OFF
;
1317 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1318 } else if (tm
->table_id
!= OFPTT_ALL
&& !strncmp(setting
, "name:", 5)) {
1319 *namep
= setting
+ 5;
1320 *usable_versions
= ((1 << OFP13_VERSION
) | (1u << OFP14_VERSION
)
1321 | (1u << OFP15_VERSION
));
1323 return xasprintf("invalid table_mod setting %s", setting
);
1326 if (tm
->table_id
== 0xfe
1327 && tm
->miss
== OFPUTIL_TABLE_MISS_CONTINUE
) {
1328 return xstrdup("last table's flow miss handling can not be continue");
1335 print_table_action_features(struct ds
*s
,
1336 const struct ofputil_table_action_features
*taf
)
1339 ds_put_cstr(s
, " actions: ");
1340 ofpact_bitmap_format(taf
->ofpacts
, s
);
1341 ds_put_char(s
, '\n');
1344 if (!bitmap_is_all_zeros(taf
->set_fields
.bm
, MFF_N_IDS
)) {
1347 ds_put_cstr(s
, " supported on Set-Field:");
1348 BITMAP_FOR_EACH_1 (i
, MFF_N_IDS
, taf
->set_fields
.bm
) {
1349 ds_put_format(s
, " %s", mf_from_id(i
)->name
);
1351 ds_put_char(s
, '\n');
1356 table_action_features_equal(const struct ofputil_table_action_features
*a
,
1357 const struct ofputil_table_action_features
*b
)
1359 return (a
->ofpacts
== b
->ofpacts
1360 && bitmap_equal(a
->set_fields
.bm
, b
->set_fields
.bm
, MFF_N_IDS
));
1364 table_action_features_empty(const struct ofputil_table_action_features
*taf
)
1366 return !taf
->ofpacts
&& bitmap_is_all_zeros(taf
->set_fields
.bm
, MFF_N_IDS
);
1370 print_table_instruction_features(
1372 const struct ofputil_table_instruction_features
*tif
,
1373 const struct ofputil_table_instruction_features
*prev_tif
)
1377 if (!bitmap_is_all_zeros(tif
->next
, 255)) {
1378 ds_put_cstr(s
, " next tables: ");
1379 for (start
= bitmap_scan(tif
->next
, 1, 0, 255); start
< 255;
1380 start
= bitmap_scan(tif
->next
, 1, end
, 255)) {
1381 end
= bitmap_scan(tif
->next
, 0, start
+ 1, 255);
1382 if (end
== start
+ 1) {
1383 ds_put_format(s
, "%d,", start
);
1385 ds_put_format(s
, "%d-%d,", start
, end
- 1);
1389 if (ds_last(s
) == ' ') {
1390 ds_put_cstr(s
, "none");
1392 ds_put_char(s
, '\n');
1395 if (tif
->instructions
) {
1396 if (prev_tif
&& tif
->instructions
== prev_tif
->instructions
) {
1397 ds_put_cstr(s
, " (same instructions)\n");
1399 ds_put_cstr(s
, " instructions: ");
1402 for (i
= 0; i
< 32; i
++) {
1403 if (tif
->instructions
& (1u << i
)) {
1404 const char *name
= ovs_instruction_name_from_type(i
);
1406 ds_put_cstr(s
, name
);
1408 ds_put_format(s
, "%d", i
);
1410 ds_put_char(s
, ',');
1414 ds_put_char(s
, '\n');
1419 && table_action_features_equal(&tif
->write
, &prev_tif
->write
)
1420 && table_action_features_equal(&tif
->apply
, &prev_tif
->apply
)
1421 && !bitmap_is_all_zeros(tif
->write
.set_fields
.bm
, MFF_N_IDS
)) {
1422 ds_put_cstr(s
, " (same actions)\n");
1423 } else if (!table_action_features_equal(&tif
->write
, &tif
->apply
)) {
1424 ds_put_cstr(s
, " Write-Actions features:\n");
1425 print_table_action_features(s
, &tif
->write
);
1426 ds_put_cstr(s
, " Apply-Actions features:\n");
1427 print_table_action_features(s
, &tif
->apply
);
1428 } else if (tif
->write
.ofpacts
1429 || !bitmap_is_all_zeros(tif
->write
.set_fields
.bm
, MFF_N_IDS
)) {
1430 ds_put_cstr(s
, " Write-Actions and Apply-Actions features:\n");
1431 print_table_action_features(s
, &tif
->write
);
1435 /* Compares bitmaps of next tables 'a' and 'b', for tables 'a_table_id' and
1436 * 'b_table_id', respectively. Returns true if the bitmaps are equal.
1438 * The bitmaps are considered equal if b_table_id == a_table_id + 1 and the bit
1439 * for 'b_table_id' is set in 'a' but not in 'b'. This is because OpenFlow
1440 * requires that a table not be able to do a goto_table back to its own table
1441 * or an earlier one. Without considering these equivalent, every table will
1442 * be different from every one in some way, which just isn't useful in printing
1443 * table features. */
1445 table_instruction_features_next_equal(const unsigned long *a
, int a_table_id
,
1446 const unsigned long *b
, int b_table_id
)
1448 if (b_table_id
== a_table_id
+ 1
1449 && bitmap_is_set(a
, b_table_id
)
1450 && !bitmap_is_set(b
, b_table_id
)) {
1451 for (size_t i
= 0; i
< BITMAP_N_LONGS(255); i
++) {
1452 unsigned long diff
= a
[i
] ^ b
[i
];
1453 if (i
== b_table_id
/ BITMAP_ULONG_BITS
) {
1454 diff
&= ~bitmap_bit__(b_table_id
);
1461 } else if (a_table_id
== b_table_id
+ 1) {
1462 return table_instruction_features_next_equal(b
, b_table_id
,
1465 return bitmap_equal(a
, b
, 255);
1470 table_instruction_features_equal(
1471 const struct ofputil_table_instruction_features
*a
, int a_table_id
,
1472 const struct ofputil_table_instruction_features
*b
, int b_table_id
)
1474 return (table_instruction_features_next_equal(a
->next
, a_table_id
,
1475 b
->next
, b_table_id
)
1476 && a
->instructions
== b
->instructions
1477 && table_action_features_equal(&a
->write
, &b
->write
)
1478 && table_action_features_equal(&a
->apply
, &b
->apply
));
1482 table_instruction_features_empty(
1483 const struct ofputil_table_instruction_features
*tif
)
1485 return (bitmap_is_all_zeros(tif
->next
, 255)
1486 && !tif
->instructions
1487 && table_action_features_empty(&tif
->write
)
1488 && table_action_features_empty(&tif
->apply
));
1492 table_features_equal(const struct ofputil_table_features
*a
,
1493 const struct ofputil_table_features
*b
)
1495 return (a
->metadata_match
== b
->metadata_match
1496 && a
->metadata_write
== b
->metadata_write
1497 && a
->miss_config
== b
->miss_config
1498 && a
->supports_eviction
== b
->supports_eviction
1499 && a
->supports_vacancy_events
== b
->supports_vacancy_events
1500 && a
->max_entries
== b
->max_entries
1501 && table_instruction_features_equal(&a
->nonmiss
, a
->table_id
,
1502 &b
->nonmiss
, b
->table_id
)
1503 && table_instruction_features_equal(&a
->miss
, a
->table_id
,
1504 &b
->miss
, b
->table_id
)
1505 && bitmap_equal(a
->match
.bm
, b
->match
.bm
, MFF_N_IDS
));
1509 table_features_empty(const struct ofputil_table_features
*tf
)
1511 return (!tf
->metadata_match
1512 && !tf
->metadata_write
1513 && tf
->miss_config
== OFPUTIL_TABLE_MISS_DEFAULT
1514 && tf
->supports_eviction
< 0
1515 && tf
->supports_vacancy_events
< 0
1517 && table_instruction_features_empty(&tf
->nonmiss
)
1518 && table_instruction_features_empty(&tf
->miss
)
1519 && bitmap_is_all_zeros(tf
->match
.bm
, MFF_N_IDS
));
1523 table_stats_equal(const struct ofputil_table_stats
*a
,
1524 const struct ofputil_table_stats
*b
)
1526 return (a
->active_count
== b
->active_count
1527 && a
->lookup_count
== b
->lookup_count
1528 && a
->matched_count
== b
->matched_count
);
1532 ofputil_table_features_format(
1534 const struct ofputil_table_features
*features
,
1535 const struct ofputil_table_features
*prev_features
,
1536 const struct ofputil_table_stats
*stats
,
1537 const struct ofputil_table_stats
*prev_stats
,
1538 int *first_ditto
, int *last_ditto
)
1540 if (!prev_features
&& features
->command
!= OFPTFC15_REPLACE
) {
1541 ds_put_format(s
, "\n command: %s",
1542 ofp15_table_features_command_to_string(
1543 features
->command
));
1546 int table
= features
->table_id
;
1547 int prev_table
= prev_features
? prev_features
->table_id
: 0;
1549 bool same_stats
= !stats
|| (prev_stats
1550 && table_stats_equal(stats
, prev_stats
));
1551 bool same_features
= prev_features
&& table_features_equal(features
,
1553 if (same_stats
&& same_features
&& !features
->name
[0]) {
1554 if (*first_ditto
< 0) {
1555 *first_ditto
= table
;
1557 *last_ditto
= table
;
1560 ofputil_table_features_format_finish(s
, *first_ditto
, *last_ditto
);
1563 ds_put_format(s
, "\n table %d", table
);
1564 if (features
->name
[0]) {
1565 ds_put_format(s
, " (\"%s\")", features
->name
);
1567 ds_put_char(s
, ':');
1569 if (same_stats
&& same_features
) {
1570 ds_put_cstr(s
, " ditto");
1573 ds_put_char(s
, '\n');
1575 ds_put_format(s
, " active=%"PRIu32
", ", stats
->active_count
);
1576 ds_put_format(s
, "lookup=%"PRIu64
", ", stats
->lookup_count
);
1577 ds_put_format(s
, "matched=%"PRIu64
"\n", stats
->matched_count
);
1579 if (same_features
) {
1580 if (!table_features_empty(features
)) {
1581 ds_put_cstr(s
, " (same features)\n");
1585 if (features
->metadata_match
|| features
->metadata_write
) {
1586 ds_put_format(s
, " metadata: match=%#"PRIx64
" write=%#"PRIx64
"\n",
1587 ntohll(features
->metadata_match
),
1588 ntohll(features
->metadata_write
));
1591 if (features
->miss_config
!= OFPUTIL_TABLE_MISS_DEFAULT
) {
1592 ds_put_format(s
, " config=%s\n",
1593 ofputil_table_miss_to_string(features
->miss_config
));
1596 if (features
->supports_eviction
>= 0) {
1597 ds_put_format(s
, " eviction: %ssupported\n",
1598 features
->supports_eviction
? "" : "not ");
1601 if (features
->supports_vacancy_events
>= 0) {
1602 ds_put_format(s
, " vacancy events: %ssupported\n",
1603 features
->supports_vacancy_events
? "" : "not ");
1607 if (features
->max_entries
) {
1608 ds_put_format(s
, " max_entries=%"PRIu32
"\n", features
->max_entries
);
1611 const struct ofputil_table_instruction_features
*prev_nonmiss
1612 = prev_features
? &prev_features
->nonmiss
: NULL
;
1613 const struct ofputil_table_instruction_features
*prev_miss
1614 = prev_features
? &prev_features
->miss
: NULL
;
1616 && table_instruction_features_equal(&features
->nonmiss
, table
,
1617 prev_nonmiss
, prev_table
)
1618 && table_instruction_features_equal(&features
->miss
, table
,
1619 prev_miss
, prev_table
)) {
1620 if (!table_instruction_features_empty(&features
->nonmiss
)) {
1621 ds_put_cstr(s
, " (same instructions)\n");
1623 } else if (!table_instruction_features_equal(&features
->nonmiss
, table
,
1624 &features
->miss
, table
)) {
1625 ds_put_cstr(s
, " instructions (other than table miss):\n");
1626 print_table_instruction_features(s
, &features
->nonmiss
, prev_nonmiss
);
1627 ds_put_cstr(s
, " instructions (table miss):\n");
1628 print_table_instruction_features(s
, &features
->miss
, prev_miss
);
1629 } else if (!table_instruction_features_empty(&features
->nonmiss
)) {
1630 ds_put_cstr(s
, " instructions (table miss and others):\n");
1631 print_table_instruction_features(s
, &features
->nonmiss
, prev_nonmiss
);
1634 if (!bitmap_is_all_zeros(features
->match
.bm
, MFF_N_IDS
)) {
1636 && bitmap_equal(features
->match
.bm
, prev_features
->match
.bm
,
1638 ds_put_cstr(s
, " (same matching)\n");
1640 ds_put_cstr(s
, " matching:\n");
1643 BITMAP_FOR_EACH_1 (i
, MFF_N_IDS
, features
->match
.bm
) {
1644 const struct mf_field
*f
= mf_from_id(i
);
1645 bool mask
= bitmap_is_set(features
->mask
.bm
, i
);
1646 bool wildcard
= bitmap_is_set(features
->wildcard
.bm
, i
);
1648 ds_put_format(s
, " %s: %s\n",
1650 (mask
? "arbitrary mask"
1651 : wildcard
? "exact match or wildcard"
1652 : "must exact match"));
1659 ofputil_table_features_format_finish(struct ds
*s
,
1660 int first_ditto
, int last_ditto
)
1662 if (first_ditto
< 0) {
1666 ds_put_char(s
, '\n');
1667 if (first_ditto
== last_ditto
) {
1668 ds_put_format(s
, " table %d: ditto\n", first_ditto
);
1670 ds_put_format(s
, " tables %d...%d: ditto\n", first_ditto
, last_ditto
);
1674 /* Returns true if 'super' is a superset of 'sub', false otherwise. */
1676 ofputil_table_action_features_is_superset(
1677 const struct ofputil_table_action_features
*super
,
1678 const struct ofputil_table_action_features
*sub
)
1680 return (uint_is_superset(super
->ofpacts
, sub
->ofpacts
)
1681 && mf_bitmap_is_superset(&super
->set_fields
, &sub
->set_fields
));
1684 /* Returns true if 'super' is a superset of 'sub', false otherwise. */
1686 ofputil_table_instruction_features_is_superset(
1687 const struct ofputil_table_instruction_features
*super
,
1688 const struct ofputil_table_instruction_features
*sub
)
1690 return (bitmap_is_superset(super
->next
, sub
->next
, 255)
1691 && uint_is_superset(super
->instructions
, sub
->instructions
)
1692 && ofputil_table_action_features_is_superset(&super
->write
,
1694 && ofputil_table_action_features_is_superset(&super
->apply
,
1698 /* Returns true if 'super' is a superset of 'sub', false otherwise. */
1700 ofputil_table_features_are_superset(
1701 const struct ofputil_table_features
*super
,
1702 const struct ofputil_table_features
*sub
)
1704 return (be64_is_superset(super
->metadata_match
, sub
->metadata_match
)
1705 && be64_is_superset(super
->metadata_write
, sub
->metadata_write
)
1706 && super
->max_entries
>= sub
->max_entries
1707 && super
->supports_eviction
>= sub
->supports_eviction
1708 && super
->supports_vacancy_events
>= sub
->supports_vacancy_events
1709 && ofputil_table_instruction_features_is_superset(&super
->nonmiss
,
1711 && ofputil_table_instruction_features_is_superset(&super
->miss
,
1713 && mf_bitmap_is_superset(&super
->match
, &sub
->match
)
1714 && mf_bitmap_is_superset(&super
->mask
, &sub
->mask
)
1715 && mf_bitmap_is_superset(&super
->wildcard
, &sub
->wildcard
));
1720 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
1721 * matched and a field that must be wildcarded. This function returns a bitmap
1722 * that contains both kinds of fields. */
1723 static struct mf_bitmap
1724 wild_or_nonmatchable_fields(const struct ofputil_table_features
*features
)
1726 struct mf_bitmap wc
= features
->match
;
1727 bitmap_not(wc
.bm
, MFF_N_IDS
);
1728 bitmap_or(wc
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
1732 struct ofp10_wc_map
{
1733 enum ofp10_flow_wildcards wc10
;
1734 enum mf_field_id mf
;
1737 static const struct ofp10_wc_map ofp10_wc_map
[] = {
1738 { OFPFW10_IN_PORT
, MFF_IN_PORT
},
1739 { OFPFW10_DL_VLAN
, MFF_VLAN_VID
},
1740 { OFPFW10_DL_SRC
, MFF_ETH_SRC
},
1741 { OFPFW10_DL_DST
, MFF_ETH_DST
},
1742 { OFPFW10_DL_TYPE
, MFF_ETH_TYPE
},
1743 { OFPFW10_NW_PROTO
, MFF_IP_PROTO
},
1744 { OFPFW10_TP_SRC
, MFF_TCP_SRC
},
1745 { OFPFW10_TP_DST
, MFF_TCP_DST
},
1746 { OFPFW10_NW_SRC_MASK
, MFF_IPV4_SRC
},
1747 { OFPFW10_NW_DST_MASK
, MFF_IPV4_DST
},
1748 { OFPFW10_DL_VLAN_PCP
, MFF_VLAN_PCP
},
1749 { OFPFW10_NW_TOS
, MFF_IP_DSCP
},
1753 mf_bitmap_to_of10(const struct mf_bitmap
*fields
)
1755 const struct ofp10_wc_map
*p
;
1758 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
1759 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
1766 static struct mf_bitmap
1767 mf_bitmap_from_of10(ovs_be32 wc10_
)
1769 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
1770 const struct ofp10_wc_map
*p
;
1771 uint32_t wc10
= ntohl(wc10_
);
1773 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
1774 if (wc10
& p
->wc10
) {
1775 bitmap_set1(fields
.bm
, p
->mf
);
1782 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats
*stats
,
1783 const struct ofputil_table_features
*features
,
1786 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
1787 struct ofp10_table_stats
*out
;
1789 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
1790 out
->table_id
= features
->table_id
;
1791 ovs_strlcpy_arrays(out
->name
, features
->name
);
1792 out
->wildcards
= mf_bitmap_to_of10(&wc
);
1793 out
->max_entries
= htonl(features
->max_entries
);
1794 out
->active_count
= htonl(stats
->active_count
);
1795 put_32aligned_be64(&out
->lookup_count
, htonll(stats
->lookup_count
));
1796 put_32aligned_be64(&out
->matched_count
, htonll(stats
->matched_count
));
1799 struct ofp11_wc_map
{
1800 enum ofp11_flow_match_fields wc11
;
1801 enum mf_field_id mf
;
1804 static const struct ofp11_wc_map ofp11_wc_map
[] = {
1805 { OFPFMF11_IN_PORT
, MFF_IN_PORT
},
1806 { OFPFMF11_DL_VLAN
, MFF_VLAN_VID
},
1807 { OFPFMF11_DL_VLAN_PCP
, MFF_VLAN_PCP
},
1808 { OFPFMF11_DL_TYPE
, MFF_ETH_TYPE
},
1809 { OFPFMF11_NW_TOS
, MFF_IP_DSCP
},
1810 { OFPFMF11_NW_PROTO
, MFF_IP_PROTO
},
1811 { OFPFMF11_TP_SRC
, MFF_TCP_SRC
},
1812 { OFPFMF11_TP_DST
, MFF_TCP_DST
},
1813 { OFPFMF11_MPLS_LABEL
, MFF_MPLS_LABEL
},
1814 { OFPFMF11_MPLS_TC
, MFF_MPLS_TC
},
1815 /* I don't know what OFPFMF11_TYPE means. */
1816 { OFPFMF11_DL_SRC
, MFF_ETH_SRC
},
1817 { OFPFMF11_DL_DST
, MFF_ETH_DST
},
1818 { OFPFMF11_NW_SRC
, MFF_IPV4_SRC
},
1819 { OFPFMF11_NW_DST
, MFF_IPV4_DST
},
1820 { OFPFMF11_METADATA
, MFF_METADATA
},
1824 mf_bitmap_to_of11(const struct mf_bitmap
*fields
)
1826 const struct ofp11_wc_map
*p
;
1829 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
1830 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
1837 static struct mf_bitmap
1838 mf_bitmap_from_of11(ovs_be32 wc11_
)
1840 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
1841 const struct ofp11_wc_map
*p
;
1842 uint32_t wc11
= ntohl(wc11_
);
1844 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
1845 if (wc11
& p
->wc11
) {
1846 bitmap_set1(fields
.bm
, p
->mf
);
1853 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats
*stats
,
1854 const struct ofputil_table_features
*features
,
1857 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
1858 struct ofp11_table_stats
*out
;
1860 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
1861 out
->table_id
= features
->table_id
;
1862 ovs_strlcpy_arrays(out
->name
, features
->name
);
1863 out
->wildcards
= mf_bitmap_to_of11(&wc
);
1864 out
->match
= mf_bitmap_to_of11(&features
->match
);
1865 out
->instructions
= ovsinst_bitmap_to_openflow(
1866 features
->nonmiss
.instructions
, OFP11_VERSION
);
1867 out
->write_actions
= ofpact_bitmap_to_openflow(
1868 features
->nonmiss
.write
.ofpacts
, OFP11_VERSION
);
1869 out
->apply_actions
= ofpact_bitmap_to_openflow(
1870 features
->nonmiss
.apply
.ofpacts
, OFP11_VERSION
);
1871 out
->config
= htonl(features
->miss_config
);
1872 out
->max_entries
= htonl(features
->max_entries
);
1873 out
->active_count
= htonl(stats
->active_count
);
1874 out
->lookup_count
= htonll(stats
->lookup_count
);
1875 out
->matched_count
= htonll(stats
->matched_count
);
1879 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats
*stats
,
1880 const struct ofputil_table_features
*features
,
1883 struct ofp12_table_stats
*out
;
1885 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
1886 out
->table_id
= features
->table_id
;
1887 ovs_strlcpy_arrays(out
->name
, features
->name
);
1888 out
->match
= oxm_bitmap_from_mf_bitmap(&features
->match
, OFP12_VERSION
);
1889 out
->wildcards
= oxm_bitmap_from_mf_bitmap(&features
->wildcard
,
1891 out
->write_actions
= ofpact_bitmap_to_openflow(
1892 features
->nonmiss
.write
.ofpacts
, OFP12_VERSION
);
1893 out
->apply_actions
= ofpact_bitmap_to_openflow(
1894 features
->nonmiss
.apply
.ofpacts
, OFP12_VERSION
);
1895 out
->write_setfields
= oxm_bitmap_from_mf_bitmap(
1896 &features
->nonmiss
.write
.set_fields
, OFP12_VERSION
);
1897 out
->apply_setfields
= oxm_bitmap_from_mf_bitmap(
1898 &features
->nonmiss
.apply
.set_fields
, OFP12_VERSION
);
1899 out
->metadata_match
= features
->metadata_match
;
1900 out
->metadata_write
= features
->metadata_write
;
1901 out
->instructions
= ovsinst_bitmap_to_openflow(
1902 features
->nonmiss
.instructions
, OFP12_VERSION
);
1903 out
->config
= ofputil_encode_table_config(features
->miss_config
,
1904 OFPUTIL_TABLE_EVICTION_DEFAULT
,
1905 OFPUTIL_TABLE_VACANCY_DEFAULT
,
1907 out
->max_entries
= htonl(features
->max_entries
);
1908 out
->active_count
= htonl(stats
->active_count
);
1909 out
->lookup_count
= htonll(stats
->lookup_count
);
1910 out
->matched_count
= htonll(stats
->matched_count
);
1914 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats
*stats
,
1917 struct ofp13_table_stats
*out
;
1919 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
1920 out
->table_id
= stats
->table_id
;
1921 out
->active_count
= htonl(stats
->active_count
);
1922 out
->lookup_count
= htonll(stats
->lookup_count
);
1923 out
->matched_count
= htonll(stats
->matched_count
);
1927 ofputil_encode_table_stats_reply(const struct ofp_header
*request
)
1929 return ofpraw_alloc_stats_reply(request
, 0);
1933 ofputil_append_table_stats_reply(struct ofpbuf
*reply
,
1934 const struct ofputil_table_stats
*stats
,
1935 const struct ofputil_table_features
*features
)
1937 struct ofp_header
*oh
= reply
->header
;
1939 ovs_assert(stats
->table_id
== features
->table_id
);
1941 switch ((enum ofp_version
) oh
->version
) {
1943 ofputil_put_ofp10_table_stats(stats
, features
, reply
);
1947 ofputil_put_ofp11_table_stats(stats
, features
, reply
);
1951 ofputil_put_ofp12_table_stats(stats
, features
, reply
);
1958 ofputil_put_ofp13_table_stats(stats
, reply
);
1967 ofputil_decode_ofp10_table_stats(struct ofpbuf
*msg
,
1968 struct ofputil_table_stats
*stats
,
1969 struct ofputil_table_features
*features
)
1971 struct ofp10_table_stats
*ots
;
1973 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
1975 return OFPERR_OFPBRC_BAD_LEN
;
1978 features
->table_id
= ots
->table_id
;
1979 ovs_strlcpy_arrays(features
->name
, ots
->name
);
1980 features
->max_entries
= ntohl(ots
->max_entries
);
1981 features
->match
= features
->wildcard
= mf_bitmap_from_of10(ots
->wildcards
);
1983 stats
->table_id
= ots
->table_id
;
1984 stats
->active_count
= ntohl(ots
->active_count
);
1985 stats
->lookup_count
= ntohll(get_32aligned_be64(&ots
->lookup_count
));
1986 stats
->matched_count
= ntohll(get_32aligned_be64(&ots
->matched_count
));
1992 ofputil_decode_ofp11_table_stats(struct ofpbuf
*msg
,
1993 struct ofputil_table_stats
*stats
,
1994 struct ofputil_table_features
*features
)
1996 struct ofp11_table_stats
*ots
;
1998 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
2000 return OFPERR_OFPBRC_BAD_LEN
;
2003 features
->table_id
= ots
->table_id
;
2004 ovs_strlcpy_arrays(features
->name
, ots
->name
);
2005 features
->max_entries
= ntohl(ots
->max_entries
);
2006 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
2007 ots
->instructions
, OFP11_VERSION
);
2008 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
2009 ots
->write_actions
, OFP11_VERSION
);
2010 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
2011 ots
->write_actions
, OFP11_VERSION
);
2012 features
->miss
= features
->nonmiss
;
2013 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
2015 features
->match
= mf_bitmap_from_of11(ots
->match
);
2016 features
->wildcard
= mf_bitmap_from_of11(ots
->wildcards
);
2017 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
2019 stats
->table_id
= ots
->table_id
;
2020 stats
->active_count
= ntohl(ots
->active_count
);
2021 stats
->lookup_count
= ntohll(ots
->lookup_count
);
2022 stats
->matched_count
= ntohll(ots
->matched_count
);
2028 ofputil_decode_ofp12_table_stats(struct ofpbuf
*msg
,
2029 struct ofputil_table_stats
*stats
,
2030 struct ofputil_table_features
*features
)
2032 struct ofp12_table_stats
*ots
;
2034 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
2036 return OFPERR_OFPBRC_BAD_LEN
;
2039 features
->table_id
= ots
->table_id
;
2040 ovs_strlcpy_arrays(features
->name
, ots
->name
);
2041 features
->metadata_match
= ots
->metadata_match
;
2042 features
->metadata_write
= ots
->metadata_write
;
2043 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
2045 features
->max_entries
= ntohl(ots
->max_entries
);
2047 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
2048 ots
->instructions
, OFP12_VERSION
);
2049 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
2050 ots
->write_actions
, OFP12_VERSION
);
2051 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
2052 ots
->apply_actions
, OFP12_VERSION
);
2053 features
->nonmiss
.write
.set_fields
= oxm_bitmap_to_mf_bitmap(
2054 ots
->write_setfields
, OFP12_VERSION
);
2055 features
->nonmiss
.apply
.set_fields
= oxm_bitmap_to_mf_bitmap(
2056 ots
->apply_setfields
, OFP12_VERSION
);
2057 features
->miss
= features
->nonmiss
;
2059 features
->match
= oxm_bitmap_to_mf_bitmap(ots
->match
, OFP12_VERSION
);
2060 features
->wildcard
= oxm_bitmap_to_mf_bitmap(ots
->wildcards
,
2062 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
2064 stats
->table_id
= ots
->table_id
;
2065 stats
->active_count
= ntohl(ots
->active_count
);
2066 stats
->lookup_count
= ntohll(ots
->lookup_count
);
2067 stats
->matched_count
= ntohll(ots
->matched_count
);
2073 ofputil_decode_ofp13_table_stats(struct ofpbuf
*msg
,
2074 struct ofputil_table_stats
*stats
,
2075 struct ofputil_table_features
*features
)
2077 struct ofp13_table_stats
*ots
;
2079 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
2081 return OFPERR_OFPBRC_BAD_LEN
;
2084 features
->table_id
= ots
->table_id
;
2086 stats
->table_id
= ots
->table_id
;
2087 stats
->active_count
= ntohl(ots
->active_count
);
2088 stats
->lookup_count
= ntohll(ots
->lookup_count
);
2089 stats
->matched_count
= ntohll(ots
->matched_count
);
2095 ofputil_decode_table_stats_reply(struct ofpbuf
*msg
,
2096 struct ofputil_table_stats
*stats
,
2097 struct ofputil_table_features
*features
)
2099 const struct ofp_header
*oh
;
2102 ofpraw_pull_assert(msg
);
2110 memset(stats
, 0, sizeof *stats
);
2111 memset(features
, 0, sizeof *features
);
2112 features
->supports_eviction
= -1;
2113 features
->supports_vacancy_events
= -1;
2115 switch ((enum ofp_version
) oh
->version
) {
2117 return ofputil_decode_ofp10_table_stats(msg
, stats
, features
);
2120 return ofputil_decode_ofp11_table_stats(msg
, stats
, features
);
2123 return ofputil_decode_ofp12_table_stats(msg
, stats
, features
);
2129 return ofputil_decode_ofp13_table_stats(msg
, stats
, features
);
2136 /* Returns a string form of 'reason'. The return value is either a statically
2137 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
2138 * 'bufsize' should be at least OFP_ASYNC_CONFIG_REASON_BUFSIZE. */
2140 ofp_table_reason_to_string(enum ofp14_table_reason reason
,
2141 char *reasonbuf
, size_t bufsize
)
2144 case OFPTR_VACANCY_DOWN
:
2145 return "vacancy_down";
2147 case OFPTR_VACANCY_UP
:
2148 return "vacancy_up";
2151 snprintf(reasonbuf
, bufsize
, "%d", (int) reason
);
2157 ofputil_put_ofp14_table_desc(const struct ofputil_table_desc
*td
,
2158 struct ofpbuf
*b
, enum ofp_version version
)
2160 struct ofp14_table_desc
*otd
;
2161 struct ofp14_table_mod_prop_vacancy
*otv
;
2164 start_otd
= b
->size
;
2165 ofpbuf_put_zeros(b
, sizeof *otd
);
2167 ofpprop_put_u32(b
, OFPTMPT14_EVICTION
, td
->eviction_flags
);
2169 otv
= ofpbuf_put_zeros(b
, sizeof *otv
);
2170 otv
->type
= htons(OFPTMPT14_VACANCY
);
2171 otv
->length
= htons(sizeof *otv
);
2172 otv
->vacancy_down
= td
->table_vacancy
.vacancy_down
;
2173 otv
->vacancy_up
= td
->table_vacancy
.vacancy_up
;
2174 otv
->vacancy
= td
->table_vacancy
.vacancy
;
2176 otd
= ofpbuf_at_assert(b
, start_otd
, sizeof *otd
);
2177 otd
->length
= htons(b
->size
- start_otd
);
2178 otd
->table_id
= td
->table_id
;
2179 otd
->config
= ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT
,
2180 td
->eviction
, td
->vacancy
,
2184 /* Converts the abstract form of a "table status" message in '*ts' into an
2185 * OpenFlow message suitable for 'protocol', and returns that encoded form in
2186 * a buffer owned by the caller. */
2188 ofputil_encode_table_status(const struct ofputil_table_status
*ts
,
2189 enum ofputil_protocol protocol
)
2191 enum ofp_version version
;
2194 version
= ofputil_protocol_to_ofp_version(protocol
);
2195 if (version
>= OFP14_VERSION
) {
2197 struct ofp14_table_status
*ots
;
2199 raw
= OFPRAW_OFPT14_TABLE_STATUS
;
2200 b
= ofpraw_alloc_xid(raw
, version
, htonl(0), 0);
2201 ots
= ofpbuf_put_zeros(b
, sizeof *ots
);
2202 ots
->reason
= ts
->reason
;
2203 ofputil_put_ofp14_table_desc(&ts
->desc
, b
, version
);
2204 ofpmsg_update_length(b
);
2211 /* Decodes the OpenFlow "table status" message in '*ots' into an abstract form
2212 * in '*ts'. Returns 0 if successful, otherwise an OFPERR_* value. */
2214 ofputil_decode_table_status(const struct ofp_header
*oh
,
2215 struct ofputil_table_status
*ts
)
2217 const struct ofp14_table_status
*ots
;
2222 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
2223 raw
= ofpraw_pull_assert(&b
);
2224 ots
= ofpbuf_pull(&b
, sizeof *ots
);
2226 if (raw
== OFPRAW_OFPT14_TABLE_STATUS
) {
2227 if (ots
->reason
!= OFPTR_VACANCY_DOWN
2228 && ots
->reason
!= OFPTR_VACANCY_UP
) {
2229 return OFPERR_OFPBPC_BAD_VALUE
;
2231 ts
->reason
= ots
->reason
;
2233 error
= ofputil_decode_table_desc(&b
, &ts
->desc
, oh
->version
);
2236 return OFPERR_OFPBRC_BAD_VERSION
;
2243 ofputil_format_table_status(struct ds
*string
,
2244 const struct ofputil_table_status
*ts
,
2245 const struct ofputil_table_map
*table_map
)
2247 if (ts
->reason
== OFPTR_VACANCY_DOWN
) {
2248 ds_put_format(string
, " reason=VACANCY_DOWN");
2249 } else if (ts
->reason
== OFPTR_VACANCY_UP
) {
2250 ds_put_format(string
, " reason=VACANCY_UP");
2253 ds_put_format(string
, "\ntable_desc:-");
2254 ofputil_table_desc_format(string
, &ts
->desc
, table_map
);