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-protocol.h"
20 #include "openvswitch/dynamic-string.h"
21 #include "openvswitch/ofp-flow.h"
22 #include "openvswitch/ofp-msgs.h"
23 #include "openvswitch/ofpbuf.h"
24 #include "openvswitch/vlog.h"
27 VLOG_DEFINE_THIS_MODULE(ofp_protocol
);
29 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
34 enum ofputil_protocol protocol
;
38 /* Most users really don't care about some of the differences between
39 * protocols. These abbreviations help with that. */
40 static const struct proto_abbrev proto_abbrevs
[] = {
41 { OFPUTIL_P_ANY
, "any" },
42 { OFPUTIL_P_OF10_STD_ANY
, "OpenFlow10" },
43 { OFPUTIL_P_OF10_NXM_ANY
, "NXM" },
44 { OFPUTIL_P_ANY_OXM
, "OXM" },
46 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
48 enum ofputil_protocol ofputil_flow_dump_protocols
[] = {
57 size_t ofputil_n_flow_dump_protocols
= ARRAY_SIZE(ofputil_flow_dump_protocols
);
59 /* Returns the set of ofputil_protocols that are supported with the given
60 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
61 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
62 * if 'version' is not supported or outside the valid range. */
64 ofputil_protocols_from_ofp_version(enum ofp_version version
)
68 return OFPUTIL_P_OF10_STD_ANY
| OFPUTIL_P_OF10_NXM_ANY
;
70 return OFPUTIL_P_OF11_STD
;
72 return OFPUTIL_P_OF12_OXM
;
74 return OFPUTIL_P_OF13_OXM
;
76 return OFPUTIL_P_OF14_OXM
;
78 return OFPUTIL_P_OF15_OXM
;
84 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
85 * connection that has negotiated the given 'version'. 'version' should
86 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
87 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
88 * outside the valid range. */
90 ofputil_protocol_from_ofp_version(enum ofp_version version
)
92 return rightmost_1bit(ofputil_protocols_from_ofp_version(version
));
95 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
96 * etc.) that corresponds to 'protocol'. */
98 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol
)
101 case OFPUTIL_P_OF10_STD
:
102 case OFPUTIL_P_OF10_STD_TID
:
103 case OFPUTIL_P_OF10_NXM
:
104 case OFPUTIL_P_OF10_NXM_TID
:
105 return OFP10_VERSION
;
106 case OFPUTIL_P_OF11_STD
:
107 return OFP11_VERSION
;
108 case OFPUTIL_P_OF12_OXM
:
109 return OFP12_VERSION
;
110 case OFPUTIL_P_OF13_OXM
:
111 return OFP13_VERSION
;
112 case OFPUTIL_P_OF14_OXM
:
113 return OFP14_VERSION
;
114 case OFPUTIL_P_OF15_OXM
:
115 return OFP15_VERSION
;
121 /* Returns a bitmap of OpenFlow versions that are supported by at
122 * least one of the 'protocols'. */
124 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols
)
128 for (; protocols
; protocols
= zero_rightmost_1bit(protocols
)) {
129 enum ofputil_protocol protocol
= rightmost_1bit(protocols
);
131 bitmap
|= 1u << ofputil_protocol_to_ofp_version(protocol
);
137 /* Returns the set of protocols that are supported on top of the
138 * OpenFlow versions included in 'bitmap'. */
139 enum ofputil_protocol
140 ofputil_protocols_from_version_bitmap(uint32_t bitmap
)
142 enum ofputil_protocol protocols
= 0;
144 for (; bitmap
; bitmap
= zero_rightmost_1bit(bitmap
)) {
145 enum ofp_version version
= rightmost_1bit_idx(bitmap
);
147 protocols
|= ofputil_protocols_from_ofp_version(version
);
153 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
156 ofputil_protocol_is_valid(enum ofputil_protocol protocol
)
158 return protocol
& OFPUTIL_P_ANY
&& is_pow2(protocol
);
161 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
162 * extension turned on or off if 'enable' is true or false, respectively.
164 * This extension is only useful for protocols whose "standard" version does
165 * not allow specific tables to be modified. In particular, this is true of
166 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
167 * specifies a table ID and so there is no need for such an extension. When
168 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
169 * extension, this function just returns its 'protocol' argument unchanged
170 * regardless of the value of 'enable'. */
171 enum ofputil_protocol
172 ofputil_protocol_set_tid(enum ofputil_protocol protocol
, bool enable
)
175 case OFPUTIL_P_OF10_STD
:
176 case OFPUTIL_P_OF10_STD_TID
:
177 return enable
? OFPUTIL_P_OF10_STD_TID
: OFPUTIL_P_OF10_STD
;
179 case OFPUTIL_P_OF10_NXM
:
180 case OFPUTIL_P_OF10_NXM_TID
:
181 return enable
? OFPUTIL_P_OF10_NXM_TID
: OFPUTIL_P_OF10_NXM
;
183 case OFPUTIL_P_OF11_STD
:
184 return OFPUTIL_P_OF11_STD
;
186 case OFPUTIL_P_OF12_OXM
:
187 return OFPUTIL_P_OF12_OXM
;
189 case OFPUTIL_P_OF13_OXM
:
190 return OFPUTIL_P_OF13_OXM
;
192 case OFPUTIL_P_OF14_OXM
:
193 return OFPUTIL_P_OF14_OXM
;
195 case OFPUTIL_P_OF15_OXM
:
196 return OFPUTIL_P_OF15_OXM
;
203 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
204 * some extension to a standard protocol version, the return value is the
205 * standard version of that protocol without any extension. If 'protocol' is a
206 * standard protocol version, returns 'protocol' unchanged. */
207 enum ofputil_protocol
208 ofputil_protocol_to_base(enum ofputil_protocol protocol
)
210 return ofputil_protocol_set_tid(protocol
, false);
213 /* Returns 'new_base' with any extensions taken from 'cur'. */
214 enum ofputil_protocol
215 ofputil_protocol_set_base(enum ofputil_protocol cur
,
216 enum ofputil_protocol new_base
)
218 bool tid
= (cur
& OFPUTIL_P_TID
) != 0;
221 case OFPUTIL_P_OF10_STD
:
222 case OFPUTIL_P_OF10_STD_TID
:
223 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD
, tid
);
225 case OFPUTIL_P_OF10_NXM
:
226 case OFPUTIL_P_OF10_NXM_TID
:
227 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM
, tid
);
229 case OFPUTIL_P_OF11_STD
:
230 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD
, tid
);
232 case OFPUTIL_P_OF12_OXM
:
233 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM
, tid
);
235 case OFPUTIL_P_OF13_OXM
:
236 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM
, tid
);
238 case OFPUTIL_P_OF14_OXM
:
239 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM
, tid
);
241 case OFPUTIL_P_OF15_OXM
:
242 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM
, tid
);
249 /* Returns a string form of 'protocol', if a simple form exists (that is, if
250 * 'protocol' is either a single protocol or it is a combination of protocols
251 * that have a single abbreviation). Otherwise, returns NULL. */
253 ofputil_protocol_to_string(enum ofputil_protocol protocol
)
255 const struct proto_abbrev
*p
;
257 /* Use a "switch" statement for single-bit names so that we get a compiler
258 * warning if we forget any. */
260 case OFPUTIL_P_OF10_NXM
:
261 return "NXM-table_id";
263 case OFPUTIL_P_OF10_NXM_TID
:
264 return "NXM+table_id";
266 case OFPUTIL_P_OF10_STD
:
267 return "OpenFlow10-table_id";
269 case OFPUTIL_P_OF10_STD_TID
:
270 return "OpenFlow10+table_id";
272 case OFPUTIL_P_OF11_STD
:
275 case OFPUTIL_P_OF12_OXM
:
276 return "OXM-OpenFlow12";
278 case OFPUTIL_P_OF13_OXM
:
279 return "OXM-OpenFlow13";
281 case OFPUTIL_P_OF14_OXM
:
282 return "OXM-OpenFlow14";
284 case OFPUTIL_P_OF15_OXM
:
285 return "OXM-OpenFlow15";
288 /* Check abbreviations. */
289 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
290 if (protocol
== p
->protocol
) {
298 /* Returns a string that represents 'protocols'. The return value might be a
299 * comma-separated list if 'protocols' doesn't have a simple name. The return
300 * value is "none" if 'protocols' is 0.
302 * The caller must free the returned string (with free()). */
304 ofputil_protocols_to_string(enum ofputil_protocol protocols
)
308 ovs_assert(!(protocols
& ~OFPUTIL_P_ANY
));
309 if (protocols
== 0) {
310 return xstrdup("none");
315 const struct proto_abbrev
*p
;
319 ds_put_char(&s
, ',');
322 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
323 if ((protocols
& p
->protocol
) == p
->protocol
) {
324 ds_put_cstr(&s
, p
->name
);
325 protocols
&= ~p
->protocol
;
330 for (i
= 0; i
< CHAR_BIT
* sizeof(enum ofputil_protocol
); i
++) {
331 enum ofputil_protocol bit
= 1u << i
;
333 if (protocols
& bit
) {
334 ds_put_cstr(&s
, ofputil_protocol_to_string(bit
));
343 return ds_steal_cstr(&s
);
346 static enum ofputil_protocol
347 ofputil_protocol_from_string__(const char *s
, size_t n
)
349 const struct proto_abbrev
*p
;
352 for (i
= 0; i
< CHAR_BIT
* sizeof(enum ofputil_protocol
); i
++) {
353 enum ofputil_protocol bit
= 1u << i
;
354 const char *name
= ofputil_protocol_to_string(bit
);
356 if (name
&& n
== strlen(name
) && !strncasecmp(s
, name
, n
)) {
361 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
362 if (n
== strlen(p
->name
) && !strncasecmp(s
, p
->name
, n
)) {
370 /* Returns the nonempty set of protocols represented by 's', which can be a
371 * single protocol name or abbreviation or a comma-separated list of them.
373 * Aborts the program with an error message if 's' is invalid. */
374 enum ofputil_protocol
375 ofputil_protocols_from_string(const char *s
)
377 const char *orig_s
= s
;
378 enum ofputil_protocol protocols
;
382 enum ofputil_protocol p
;
391 p
= ofputil_protocol_from_string__(s
, n
);
393 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n
, s
);
401 ovs_fatal(0, "%s: no flow protocol specified", orig_s
);
407 ofputil_version_from_string(const char *s
)
409 if (!strcasecmp(s
, "OpenFlow10")) {
410 return OFP10_VERSION
;
412 if (!strcasecmp(s
, "OpenFlow11")) {
413 return OFP11_VERSION
;
415 if (!strcasecmp(s
, "OpenFlow12")) {
416 return OFP12_VERSION
;
418 if (!strcasecmp(s
, "OpenFlow13")) {
419 return OFP13_VERSION
;
421 if (!strcasecmp(s
, "OpenFlow14")) {
422 return OFP14_VERSION
;
424 if (!strcasecmp(s
, "OpenFlow15")) {
425 return OFP15_VERSION
;
431 is_delimiter(unsigned char c
)
433 return isspace(c
) || c
== ',';
437 ofputil_versions_from_string(const char *s
)
447 if (is_delimiter(s
[i
])) {
452 while (s
[i
+ j
] && !is_delimiter(s
[i
+ j
])) {
455 key
= xmemdup0(s
+ i
, j
);
456 version
= ofputil_version_from_string(key
);
458 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key
);
461 bitmap
|= 1u << version
;
469 ofputil_versions_from_strings(char ** const s
, size_t count
)
474 int version
= ofputil_version_from_string(s
[count
]);
476 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s
[count
]);
478 bitmap
|= 1u << version
;
486 ofputil_version_to_string(enum ofp_version ofp_version
)
488 switch (ofp_version
) {
507 ofputil_format_version(struct ds
*msg
, enum ofp_version version
)
509 ds_put_format(msg
, "0x%02x", version
);
513 ofputil_format_version_name(struct ds
*msg
, enum ofp_version version
)
515 ds_put_cstr(msg
, ofputil_version_to_string(version
));
519 ofputil_format_version_bitmap__(struct ds
*msg
, uint32_t bitmap
,
520 void (*format_version
)(struct ds
*msg
,
524 format_version(msg
, raw_ctz(bitmap
));
525 bitmap
= zero_rightmost_1bit(bitmap
);
527 ds_put_cstr(msg
, ", ");
533 ofputil_format_version_bitmap(struct ds
*msg
, uint32_t bitmap
)
535 ofputil_format_version_bitmap__(msg
, bitmap
, ofputil_format_version
);
539 ofputil_format_version_bitmap_names(struct ds
*msg
, uint32_t bitmap
)
541 ofputil_format_version_bitmap__(msg
, bitmap
, ofputil_format_version_name
);
544 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
545 * protocol is 'current', at least partly transitions the protocol to 'want'.
546 * Stores in '*next' the protocol that will be in effect on the OpenFlow
547 * connection if the switch processes the returned message correctly. (If
548 * '*next != want' then the caller will have to iterate.)
550 * If 'current == want', or if it is not possible to transition from 'current'
551 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
552 * protocol versions), returns NULL and stores 'current' in '*next'. */
554 ofputil_encode_set_protocol(enum ofputil_protocol current
,
555 enum ofputil_protocol want
,
556 enum ofputil_protocol
*next
)
558 enum ofp_version cur_version
, want_version
;
559 enum ofputil_protocol cur_base
, want_base
;
560 bool cur_tid
, want_tid
;
562 cur_version
= ofputil_protocol_to_ofp_version(current
);
563 want_version
= ofputil_protocol_to_ofp_version(want
);
564 if (cur_version
!= want_version
) {
569 cur_base
= ofputil_protocol_to_base(current
);
570 want_base
= ofputil_protocol_to_base(want
);
571 if (cur_base
!= want_base
) {
572 *next
= ofputil_protocol_set_base(current
, want_base
);
574 case OFPUTIL_P_OF10_NXM
:
575 case OFPUTIL_P_OF10_STD
:
576 return ofputil_encode_nx_set_flow_format(want_base
);
578 case OFPUTIL_P_OF11_STD
:
579 case OFPUTIL_P_OF12_OXM
:
580 case OFPUTIL_P_OF13_OXM
:
581 case OFPUTIL_P_OF14_OXM
:
582 case OFPUTIL_P_OF15_OXM
:
583 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
584 * verified above that we're not trying to change versions. */
587 case OFPUTIL_P_OF10_STD_TID
:
588 case OFPUTIL_P_OF10_NXM_TID
:
593 cur_tid
= (current
& OFPUTIL_P_TID
) != 0;
594 want_tid
= (want
& OFPUTIL_P_TID
) != 0;
595 if (cur_tid
!= want_tid
) {
596 *next
= ofputil_protocol_set_tid(current
, want_tid
);
597 return ofputil_encode_nx_flow_mod_table_id(want_tid
);
600 ovs_assert(current
== want
);
606 enum nx_flow_format
{
607 NXFF_OPENFLOW10
= 0, /* Standard OpenFlow 1.0 compatible. */
608 NXFF_NXM
= 2 /* Nicira extended match. */
611 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
612 * format to 'protocol'. */
614 ofputil_encode_nx_set_flow_format(enum ofputil_protocol protocol
)
616 struct ofpbuf
*msg
= ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT
,
618 ovs_be32
*nxff
= ofpbuf_put_uninit(msg
, sizeof *nxff
);
619 if (protocol
== OFPUTIL_P_OF10_STD
) {
620 *nxff
= htonl(NXFF_OPENFLOW10
);
621 } else if (protocol
== OFPUTIL_P_OF10_NXM
) {
622 *nxff
= htonl(NXFF_NXM
);
630 /* Returns the protocol specified in the NXT_SET_FLOW_FORMAT message at 'oh'
631 * (either OFPUTIL_P_OF10_STD or OFPUTIL_P_OF10_NXM) or 0 if the message is
633 enum ofputil_protocol
634 ofputil_decode_nx_set_flow_format(const struct ofp_header
*oh
)
636 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
637 ovs_assert(ofpraw_pull_assert(&b
) == OFPRAW_NXT_SET_FLOW_FORMAT
);
639 ovs_be32
*flow_formatp
= ofpbuf_pull(&b
, sizeof *flow_formatp
);
640 uint32_t flow_format
= ntohl(*flow_formatp
);
641 switch (flow_format
) {
642 case NXFF_OPENFLOW10
:
643 return OFPUTIL_P_OF10_STD
;
646 return OFPUTIL_P_OF10_NXM
;
649 VLOG_WARN_RL(&rl
, "NXT_SET_FLOW_FORMAT message specified invalid "
650 "flow format %"PRIu32
, flow_format
);
655 /* These functions work with the Open vSwitch extension feature called
656 * "flow_mod_table_id", which allows a controller to specify the OpenFlow table
657 * to which a flow should be added, instead of having the switch decide which
658 * table is most appropriate as required by OpenFlow 1.0. Because NXM was
659 * designed as an extension to OpenFlow 1.0, the extension applies equally to
660 * ofp10_flow_mod and nx_flow_mod. By default, the extension is disabled.
662 * When this feature is enabled, Open vSwitch treats struct ofp10_flow_mod's
663 * and struct nx_flow_mod's 16-bit 'command' member as two separate fields.
664 * The upper 8 bits are used as the table ID, the lower 8 bits specify the
665 * command as usual. A table ID of 0xff is treated like a wildcarded table ID.
667 * The specific treatment of the table ID depends on the type of flow mod:
669 * - OFPFC_ADD: Given a specific table ID, the flow is always placed in that
670 * table. If an identical flow already exists in that table only, then it
671 * is replaced. If the flow cannot be placed in the specified table,
672 * either because the table is full or because the table cannot support
673 * flows of the given type, the switch replies with an OFPFMFC_TABLE_FULL
674 * error. (A controller can distinguish these cases by comparing the
675 * current and maximum number of entries reported in ofp_table_stats.)
677 * If the table ID is wildcarded, the switch picks an appropriate table
678 * itself. If an identical flow already exist in the selected flow table,
679 * then it is replaced. The choice of table might depend on the flows
680 * that are already in the switch; for example, if one table fills up then
681 * the switch might fall back to another one.
683 * - OFPFC_MODIFY, OFPFC_DELETE: Given a specific table ID, only flows
684 * within that table are matched and modified or deleted. If the table ID
685 * is wildcarded, flows within any table may be matched and modified or
688 * - OFPFC_MODIFY_STRICT, OFPFC_DELETE_STRICT: Given a specific table ID,
689 * only a flow within that table may be matched and modified or deleted.
690 * If the table ID is wildcarded and exactly one flow within any table
691 * matches, then it is modified or deleted; if flows in more than one
692 * table match, then none is modified or deleted.
695 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
696 * extension on or off (according to 'enable'). */
698 ofputil_encode_nx_flow_mod_table_id(bool enable
)
700 struct ofpbuf
*msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID
,
702 uint8_t *p
= ofpbuf_put_zeros(msg
, 8);
707 /* Decodes the NXT_FLOW_MOD_TABLE_ID message at 'oh'. Returns the message's
708 * argument, that is, whether the flow_mod_table_id feature should be
711 ofputil_decode_nx_flow_mod_table_id(const struct ofp_header
*oh
)
713 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
714 ovs_assert(ofpraw_pull_assert(&b
) == OFPRAW_NXT_FLOW_MOD_TABLE_ID
);
715 uint8_t *enable
= ofpbuf_pull(&b
, 8);