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-flow.h"
20 #include "byte-order.h"
24 #include "openvswitch/ofp-actions.h"
25 #include "openvswitch/ofp-group.h"
26 #include "openvswitch/ofp-match.h"
27 #include "openvswitch/ofp-msgs.h"
28 #include "openvswitch/ofp-parse.h"
29 #include "openvswitch/ofp-port.h"
30 #include "openvswitch/ofp-print.h"
31 #include "openvswitch/ofp-table.h"
32 #include "openvswitch/ofpbuf.h"
33 #include "openvswitch/vlog.h"
36 VLOG_DEFINE_THIS_MODULE(ofp_flow
);
38 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
40 struct ofputil_flow_mod_flag
{
42 enum ofp_version min_version
, max_version
;
43 enum ofputil_flow_mod_flags flag
;
46 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags
[] = {
47 { OFPFF_SEND_FLOW_REM
, OFP10_VERSION
, 0, OFPUTIL_FF_SEND_FLOW_REM
},
48 { OFPFF_CHECK_OVERLAP
, OFP10_VERSION
, 0, OFPUTIL_FF_CHECK_OVERLAP
},
49 { OFPFF10_EMERG
, OFP10_VERSION
, OFP10_VERSION
,
51 { OFPFF12_RESET_COUNTS
, OFP12_VERSION
, 0, OFPUTIL_FF_RESET_COUNTS
},
52 { OFPFF13_NO_PKT_COUNTS
, OFP13_VERSION
, 0, OFPUTIL_FF_NO_PKT_COUNTS
},
53 { OFPFF13_NO_BYT_COUNTS
, OFP13_VERSION
, 0, OFPUTIL_FF_NO_BYT_COUNTS
},
58 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_
,
59 enum ofp_flow_mod_command command
,
60 enum ofp_version version
,
61 enum ofputil_flow_mod_flags
*flagsp
)
63 uint16_t raw_flags
= ntohs(raw_flags_
);
64 const struct ofputil_flow_mod_flag
*f
;
67 for (f
= ofputil_flow_mod_flags
; f
->raw_flag
; f
++) {
68 if (raw_flags
& f
->raw_flag
69 && version
>= f
->min_version
70 && (!f
->max_version
|| version
<= f
->max_version
)) {
71 raw_flags
&= ~f
->raw_flag
;
76 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
79 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
81 if ((version
== OFP10_VERSION
|| version
== OFP11_VERSION
)
82 && command
== OFPFC_ADD
) {
83 *flagsp
|= OFPUTIL_FF_RESET_COUNTS
;
86 return raw_flags
? OFPERR_OFPFMFC_BAD_FLAGS
: 0;
90 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags
,
91 enum ofp_version version
)
93 const struct ofputil_flow_mod_flag
*f
;
97 for (f
= ofputil_flow_mod_flags
; f
->raw_flag
; f
++) {
99 && version
>= f
->min_version
100 && (!f
->max_version
|| version
<= f
->max_version
)) {
101 raw_flags
|= f
->raw_flag
;
105 return htons(raw_flags
);
109 ofputil_flow_mod_flags_format(struct ds
*s
, enum ofputil_flow_mod_flags flags
)
111 if (flags
& OFPUTIL_FF_SEND_FLOW_REM
) {
112 ds_put_cstr(s
, "send_flow_rem ");
114 if (flags
& OFPUTIL_FF_CHECK_OVERLAP
) {
115 ds_put_cstr(s
, "check_overlap ");
117 if (flags
& OFPUTIL_FF_RESET_COUNTS
) {
118 ds_put_cstr(s
, "reset_counts ");
120 if (flags
& OFPUTIL_FF_NO_PKT_COUNTS
) {
121 ds_put_cstr(s
, "no_packet_counts ");
123 if (flags
& OFPUTIL_FF_NO_BYT_COUNTS
) {
124 ds_put_cstr(s
, "no_byte_counts ");
126 if (flags
& OFPUTIL_FF_HIDDEN_FIELDS
) {
127 ds_put_cstr(s
, "allow_hidden_fields ");
129 if (flags
& OFPUTIL_FF_NO_READONLY
) {
130 ds_put_cstr(s
, "no_readonly_table ");
134 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
135 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
138 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
139 * The caller must initialize 'ofpacts' and retains ownership of it.
140 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
142 * On success, the caller must eventually destroy fm->match.
144 * Does not validate the flow_mod actions. The caller should do that, with
145 * ofpacts_check(). */
147 ofputil_decode_flow_mod(struct ofputil_flow_mod
*fm
,
148 const struct ofp_header
*oh
,
149 enum ofputil_protocol protocol
,
150 const struct tun_table
*tun_table
,
151 const struct vl_mff_map
*vl_mff_map
,
152 struct ofpbuf
*ofpacts
,
153 ofp_port_t max_port
, uint8_t max_table
)
158 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
159 enum ofpraw raw
= ofpraw_pull_assert(&b
);
160 if (raw
== OFPRAW_OFPT11_FLOW_MOD
) {
161 /* Standard OpenFlow 1.1+ flow_mod. */
162 const struct ofp11_flow_mod
*ofm
;
164 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
166 error
= ofputil_pull_ofp11_match(&b
, tun_table
, vl_mff_map
, &match
,
172 /* Translate the message. */
173 fm
->priority
= ntohs(ofm
->priority
);
174 if (ofm
->command
== OFPFC_ADD
175 || (oh
->version
== OFP11_VERSION
176 && (ofm
->command
== OFPFC_MODIFY
||
177 ofm
->command
== OFPFC_MODIFY_STRICT
)
178 && ofm
->cookie_mask
== htonll(0))) {
179 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
180 * not match on the cookie is treated as an "add" if there is no
182 fm
->cookie
= htonll(0);
183 fm
->cookie_mask
= htonll(0);
184 fm
->new_cookie
= ofm
->cookie
;
186 fm
->cookie
= ofm
->cookie
;
187 fm
->cookie_mask
= ofm
->cookie_mask
;
188 fm
->new_cookie
= OVS_BE64_MAX
;
190 fm
->modify_cookie
= false;
191 fm
->command
= ofm
->command
;
195 * OF1.1 entirely forbids table_id == OFPTT_ALL.
196 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
197 fm
->table_id
= ofm
->table_id
;
198 if (fm
->table_id
== OFPTT_ALL
199 && (oh
->version
== OFP11_VERSION
200 || (ofm
->command
!= OFPFC_DELETE
&&
201 ofm
->command
!= OFPFC_DELETE_STRICT
))) {
202 return OFPERR_OFPFMFC_BAD_TABLE_ID
;
205 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
206 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
207 if (oh
->version
>= OFP14_VERSION
&& ofm
->command
== OFPFC_ADD
) {
208 fm
->importance
= ntohs(ofm
->importance
);
212 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
213 error
= ofputil_port_from_ofp11(ofm
->out_port
, &fm
->out_port
);
218 fm
->out_group
= (ofm
->command
== OFPFC_DELETE
||
219 ofm
->command
== OFPFC_DELETE_STRICT
220 ? ntohl(ofm
->out_group
)
222 raw_flags
= ofm
->flags
;
226 if (raw
== OFPRAW_OFPT10_FLOW_MOD
) {
227 /* Standard OpenFlow 1.0 flow_mod. */
228 const struct ofp10_flow_mod
*ofm
;
230 /* Get the ofp10_flow_mod. */
231 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
233 /* Translate the rule. */
234 ofputil_match_from_ofp10_match(&ofm
->match
, &match
);
235 ofputil_normalize_match(&match
);
237 /* OpenFlow 1.0 says that exact-match rules have to have the
238 * highest possible priority. */
239 fm
->priority
= (ofm
->match
.wildcards
& htonl(OFPFW10_ALL
)
240 ? ntohs(ofm
->priority
)
243 /* Translate the message. */
244 command
= ntohs(ofm
->command
);
245 fm
->cookie
= htonll(0);
246 fm
->cookie_mask
= htonll(0);
247 fm
->new_cookie
= ofm
->cookie
;
248 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
249 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
251 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
252 fm
->out_port
= u16_to_ofp(ntohs(ofm
->out_port
));
253 fm
->out_group
= OFPG_ANY
;
254 raw_flags
= ofm
->flags
;
255 } else if (raw
== OFPRAW_NXT_FLOW_MOD
) {
256 /* Nicira extended flow_mod. */
257 const struct nx_flow_mod
*nfm
;
259 /* Dissect the message. */
260 nfm
= ofpbuf_pull(&b
, sizeof *nfm
);
261 error
= nx_pull_match(&b
, ntohs(nfm
->match_len
),
262 &match
, &fm
->cookie
, &fm
->cookie_mask
,
263 false, tun_table
, vl_mff_map
);
268 /* Translate the message. */
269 command
= ntohs(nfm
->command
);
270 if ((command
& 0xff) == OFPFC_ADD
&& fm
->cookie_mask
) {
271 /* Flow additions may only set a new cookie, not match an
272 * existing cookie. */
273 return OFPERR_NXBRC_NXM_INVALID
;
275 fm
->priority
= ntohs(nfm
->priority
);
276 fm
->new_cookie
= nfm
->cookie
;
277 fm
->idle_timeout
= ntohs(nfm
->idle_timeout
);
278 fm
->hard_timeout
= ntohs(nfm
->hard_timeout
);
280 fm
->buffer_id
= ntohl(nfm
->buffer_id
);
281 fm
->out_port
= u16_to_ofp(ntohs(nfm
->out_port
));
282 fm
->out_group
= OFPG_ANY
;
283 raw_flags
= nfm
->flags
;
288 fm
->modify_cookie
= fm
->new_cookie
!= OVS_BE64_MAX
;
289 if (protocol
& OFPUTIL_P_TID
) {
290 fm
->command
= command
& 0xff;
291 fm
->table_id
= command
>> 8;
293 if (command
> 0xff) {
294 VLOG_WARN_RL(&rl
, "flow_mod has explicit table_id "
295 "but flow_mod_table_id extension is not enabled");
297 fm
->command
= command
;
302 /* Check for mismatched conntrack original direction tuple address fields
303 * w.r.t. the IP version of the match. */
304 if (((match
.wc
.masks
.ct_nw_src
|| match
.wc
.masks
.ct_nw_dst
)
305 && match
.flow
.dl_type
!= htons(ETH_TYPE_IP
))
306 || ((ipv6_addr_is_set(&match
.wc
.masks
.ct_ipv6_src
)
307 || ipv6_addr_is_set(&match
.wc
.masks
.ct_ipv6_dst
))
308 && match
.flow
.dl_type
!= htons(ETH_TYPE_IPV6
))) {
309 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
312 if (fm
->command
> OFPFC_DELETE_STRICT
) {
313 return OFPERR_OFPFMFC_BAD_COMMAND
;
316 fm
->ofpacts_tlv_bitmap
= 0;
317 error
= ofpacts_pull_openflow_instructions(&b
, b
.size
, oh
->version
,
319 &fm
->ofpacts_tlv_bitmap
,
324 fm
->ofpacts
= ofpacts
->data
;
325 fm
->ofpacts_len
= ofpacts
->size
;
327 error
= ofputil_decode_flow_mod_flags(raw_flags
, fm
->command
,
328 oh
->version
, &fm
->flags
);
333 if (fm
->flags
& OFPUTIL_FF_EMERG
) {
334 /* We do not support the OpenFlow 1.0 emergency flow cache, which
335 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
337 * OpenFlow 1.0 specifies the error code to use when idle_timeout
338 * or hard_timeout is nonzero. Otherwise, there is no good error
339 * code, so just state that the flow table is full. */
340 return (fm
->hard_timeout
|| fm
->idle_timeout
341 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
342 : OFPERR_OFPFMFC_TABLE_FULL
);
345 error
= ofpacts_check_consistency(fm
->ofpacts
, fm
->ofpacts_len
,
347 fm
->table_id
, max_table
, protocol
);
349 minimatch_init(&fm
->match
, &match
);
355 ofputil_tid_command(const struct ofputil_flow_mod
*fm
,
356 enum ofputil_protocol protocol
)
358 return htons(protocol
& OFPUTIL_P_TID
359 ? (fm
->command
& 0xff) | (fm
->table_id
<< 8)
363 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
364 * 'protocol' and returns the message. */
366 ofputil_encode_flow_mod(const struct ofputil_flow_mod
*fm
,
367 enum ofputil_protocol protocol
)
369 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
370 ovs_be16 raw_flags
= ofputil_encode_flow_mod_flags(fm
->flags
, version
);
374 minimatch_expand(&fm
->match
, &match
);
377 case OFPUTIL_P_OF11_STD
:
378 case OFPUTIL_P_OF12_OXM
:
379 case OFPUTIL_P_OF13_OXM
:
380 case OFPUTIL_P_OF14_OXM
:
381 case OFPUTIL_P_OF15_OXM
:
382 case OFPUTIL_P_OF16_OXM
: {
383 struct ofp11_flow_mod
*ofm
;
386 tailroom
= ofputil_match_typical_len(protocol
) + fm
->ofpacts_len
;
387 msg
= ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD
, version
, tailroom
);
388 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
389 if ((protocol
== OFPUTIL_P_OF11_STD
390 && (fm
->command
== OFPFC_MODIFY
||
391 fm
->command
== OFPFC_MODIFY_STRICT
)
392 && fm
->cookie_mask
== htonll(0))
393 || fm
->command
== OFPFC_ADD
) {
394 ofm
->cookie
= fm
->new_cookie
;
396 ofm
->cookie
= fm
->cookie
& fm
->cookie_mask
;
398 ofm
->cookie_mask
= fm
->cookie_mask
;
399 if (fm
->table_id
!= OFPTT_ALL
400 || (protocol
!= OFPUTIL_P_OF11_STD
401 && (fm
->command
== OFPFC_DELETE
||
402 fm
->command
== OFPFC_DELETE_STRICT
))) {
403 ofm
->table_id
= fm
->table_id
;
407 ofm
->command
= fm
->command
;
408 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
409 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
410 ofm
->priority
= htons(fm
->priority
);
411 ofm
->buffer_id
= htonl(fm
->buffer_id
);
412 ofm
->out_port
= ofputil_port_to_ofp11(fm
->out_port
);
413 ofm
->out_group
= htonl(fm
->out_group
);
414 ofm
->flags
= raw_flags
;
415 if (version
>= OFP14_VERSION
&& fm
->command
== OFPFC_ADD
) {
416 ofm
->importance
= htons(fm
->importance
);
420 ofputil_put_ofp11_match(msg
, &match
, protocol
);
421 ofpacts_put_openflow_instructions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
426 case OFPUTIL_P_OF10_STD
:
427 case OFPUTIL_P_OF10_STD_TID
: {
428 struct ofp10_flow_mod
*ofm
;
430 msg
= ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD
, OFP10_VERSION
,
432 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
433 ofputil_match_to_ofp10_match(&match
, &ofm
->match
);
434 ofm
->cookie
= fm
->new_cookie
;
435 ofm
->command
= ofputil_tid_command(fm
, protocol
);
436 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
437 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
438 ofm
->priority
= htons(fm
->priority
);
439 ofm
->buffer_id
= htonl(fm
->buffer_id
);
440 ofm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
441 ofm
->flags
= raw_flags
;
442 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
447 case OFPUTIL_P_OF10_NXM
:
448 case OFPUTIL_P_OF10_NXM_TID
: {
449 struct nx_flow_mod
*nfm
;
452 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MOD
, OFP10_VERSION
,
453 NXM_TYPICAL_LEN
+ fm
->ofpacts_len
);
454 nfm
= ofpbuf_put_zeros(msg
, sizeof *nfm
);
455 nfm
->command
= ofputil_tid_command(fm
, protocol
);
456 nfm
->cookie
= fm
->new_cookie
;
457 match_len
= nx_put_match(msg
, &match
, fm
->cookie
, fm
->cookie_mask
);
459 nfm
->idle_timeout
= htons(fm
->idle_timeout
);
460 nfm
->hard_timeout
= htons(fm
->hard_timeout
);
461 nfm
->priority
= htons(fm
->priority
);
462 nfm
->buffer_id
= htonl(fm
->buffer_id
);
463 nfm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
464 nfm
->flags
= raw_flags
;
465 nfm
->match_len
= htons(match_len
);
466 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
475 ofpmsg_update_length(msg
);
480 ofputil_flow_mod_format(struct ds
*s
, const struct ofp_header
*oh
,
481 const struct ofputil_port_map
*port_map
,
482 const struct ofputil_table_map
*table_map
,
485 struct ofputil_flow_mod fm
;
486 struct ofpbuf ofpacts
;
490 enum ofputil_protocol protocol
;
492 protocol
= ofputil_protocol_from_ofp_version(oh
->version
);
493 protocol
= ofputil_protocol_set_tid(protocol
, true);
495 ofpbuf_init(&ofpacts
, 64);
496 error
= ofputil_decode_flow_mod(&fm
, oh
, protocol
, NULL
, NULL
, &ofpacts
,
499 ofpbuf_uninit(&ofpacts
);
504 switch (fm
.command
) {
506 ds_put_cstr(s
, "ADD");
509 ds_put_cstr(s
, "MOD");
511 case OFPFC_MODIFY_STRICT
:
512 ds_put_cstr(s
, "MOD_STRICT");
515 ds_put_cstr(s
, "DEL");
517 case OFPFC_DELETE_STRICT
:
518 ds_put_cstr(s
, "DEL_STRICT");
521 ds_put_format(s
, "cmd:%d", fm
.command
);
524 || ofputil_table_map_get_name(table_map
, fm
.table_id
)) {
525 ds_put_format(s
, " table:");
526 ofputil_format_table(fm
.table_id
, table_map
, s
);
530 ofpraw_decode(&raw
, oh
);
531 if (verbosity
>= 3 && raw
== OFPRAW_OFPT10_FLOW_MOD
) {
532 const struct ofp10_flow_mod
*ofm
= ofpmsg_body(oh
);
533 ofp10_match_print(s
, &ofm
->match
, port_map
, verbosity
);
535 /* ofp_print_match() doesn't print priority. */
536 need_priority
= true;
537 } else if (verbosity
>= 3 && raw
== OFPRAW_NXT_FLOW_MOD
) {
538 const struct nx_flow_mod
*nfm
= ofpmsg_body(oh
);
539 const void *nxm
= nfm
+ 1;
542 nxm_s
= nx_match_to_string(nxm
, ntohs(nfm
->match_len
));
543 ds_put_cstr(s
, nxm_s
);
546 /* nx_match_to_string() doesn't print priority. */
547 need_priority
= true;
550 minimatch_expand(&fm
.match
, &match
);
551 match_format(&match
, port_map
, s
, fm
.priority
);
553 /* match_format() does print priority. */
554 need_priority
= false;
557 if (ds_last(s
) != ' ') {
560 if (fm
.new_cookie
!= htonll(0) && fm
.new_cookie
!= OVS_BE64_MAX
) {
561 ds_put_format(s
, "cookie:0x%"PRIx64
" ", ntohll(fm
.new_cookie
));
563 if (fm
.cookie_mask
!= htonll(0)) {
564 ds_put_format(s
, "cookie:0x%"PRIx64
"/0x%"PRIx64
" ",
565 ntohll(fm
.cookie
), ntohll(fm
.cookie_mask
));
567 if (fm
.idle_timeout
!= OFP_FLOW_PERMANENT
) {
568 ds_put_format(s
, "idle:%"PRIu16
" ", fm
.idle_timeout
);
570 if (fm
.hard_timeout
!= OFP_FLOW_PERMANENT
) {
571 ds_put_format(s
, "hard:%"PRIu16
" ", fm
.hard_timeout
);
573 if (fm
.importance
!= 0) {
574 ds_put_format(s
, "importance:%"PRIu16
" ", fm
.importance
);
576 if (fm
.priority
!= OFP_DEFAULT_PRIORITY
&& need_priority
) {
577 ds_put_format(s
, "pri:%d ", fm
.priority
);
579 if (fm
.buffer_id
!= UINT32_MAX
) {
580 ds_put_format(s
, "buf:0x%"PRIx32
" ", fm
.buffer_id
);
582 if (fm
.out_port
!= OFPP_ANY
) {
583 ds_put_format(s
, "out_port:");
584 ofputil_format_port(fm
.out_port
, port_map
, s
);
588 if (oh
->version
== OFP10_VERSION
|| oh
->version
== OFP11_VERSION
) {
589 /* Don't print the reset_counts flag for OF1.0 and OF1.1 because those
590 * versions don't really have such a flag and printing one is likely to
592 fm
.flags
&= ~OFPUTIL_FF_RESET_COUNTS
;
594 ofputil_flow_mod_flags_format(s
, fm
.flags
);
596 ds_put_cstr(s
, "actions=");
597 struct ofpact_format_params fp
= {
598 .port_map
= port_map
,
599 .table_map
= table_map
,
602 ofpacts_format(fm
.ofpacts
, fm
.ofpacts_len
, &fp
);
603 ofpbuf_uninit(&ofpacts
);
604 minimatch_destroy(&fm
.match
);
610 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request
*fsr
,
611 const struct ofp10_flow_stats_request
*ofsr
,
614 fsr
->aggregate
= aggregate
;
615 ofputil_match_from_ofp10_match(&ofsr
->match
, &fsr
->match
);
616 fsr
->out_port
= u16_to_ofp(ntohs(ofsr
->out_port
));
617 fsr
->out_group
= OFPG_ANY
;
618 fsr
->table_id
= ofsr
->table_id
;
619 fsr
->cookie
= fsr
->cookie_mask
= htonll(0);
625 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request
*fsr
,
626 struct ofpbuf
*b
, bool aggregate
,
627 const struct tun_table
*tun_table
,
628 const struct vl_mff_map
*vl_mff_map
)
630 const struct ofp11_flow_stats_request
*ofsr
;
633 ofsr
= ofpbuf_pull(b
, sizeof *ofsr
);
634 fsr
->aggregate
= aggregate
;
635 fsr
->table_id
= ofsr
->table_id
;
636 error
= ofputil_port_from_ofp11(ofsr
->out_port
, &fsr
->out_port
);
640 fsr
->out_group
= ntohl(ofsr
->out_group
);
641 fsr
->cookie
= ofsr
->cookie
;
642 fsr
->cookie_mask
= ofsr
->cookie_mask
;
643 error
= ofputil_pull_ofp11_match(b
, tun_table
, vl_mff_map
, &fsr
->match
,
653 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request
*fsr
,
654 struct ofpbuf
*b
, bool aggregate
,
655 const struct tun_table
*tun_table
,
656 const struct vl_mff_map
*vl_mff_map
)
658 const struct nx_flow_stats_request
*nfsr
;
661 nfsr
= ofpbuf_pull(b
, sizeof *nfsr
);
662 error
= nx_pull_match(b
, ntohs(nfsr
->match_len
), &fsr
->match
,
663 &fsr
->cookie
, &fsr
->cookie_mask
, false, tun_table
,
669 return OFPERR_OFPBRC_BAD_LEN
;
672 fsr
->aggregate
= aggregate
;
673 fsr
->out_port
= u16_to_ofp(ntohs(nfsr
->out_port
));
674 fsr
->out_group
= OFPG_ANY
;
675 fsr
->table_id
= nfsr
->table_id
;
680 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
681 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
682 * successful, otherwise an OpenFlow error code.
684 * 'vl_mff_map' is an optional parameter that is used to validate the length
685 * of variable length mf_fields in 'match'. If it is not provided, the
686 * default mf_fields with maximum length will be used. */
688 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request
*fsr
,
689 const struct ofp_header
*oh
,
690 const struct tun_table
*tun_table
,
691 const struct vl_mff_map
*vl_mff_map
)
693 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
694 enum ofpraw raw
= ofpraw_pull_assert(&b
);
696 case OFPRAW_OFPST10_FLOW_REQUEST
:
697 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, false);
699 case OFPRAW_OFPST10_AGGREGATE_REQUEST
:
700 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, true);
702 case OFPRAW_OFPST11_FLOW_REQUEST
:
703 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, false, tun_table
,
706 case OFPRAW_OFPST11_AGGREGATE_REQUEST
:
707 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, true, tun_table
,
710 case OFPRAW_NXST_FLOW_REQUEST
:
711 return ofputil_decode_nxst_flow_request(fsr
, &b
, false, tun_table
,
714 case OFPRAW_NXST_AGGREGATE_REQUEST
:
715 return ofputil_decode_nxst_flow_request(fsr
, &b
, true, tun_table
,
719 /* Hey, the caller lied. */
724 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
725 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
726 * 'protocol', and returns the message. */
728 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request
*fsr
,
729 enum ofputil_protocol protocol
)
735 case OFPUTIL_P_OF11_STD
:
736 case OFPUTIL_P_OF12_OXM
:
737 case OFPUTIL_P_OF13_OXM
:
738 case OFPUTIL_P_OF14_OXM
:
739 case OFPUTIL_P_OF15_OXM
:
740 case OFPUTIL_P_OF16_OXM
: {
741 struct ofp11_flow_stats_request
*ofsr
;
743 raw
= (fsr
->aggregate
744 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
745 : OFPRAW_OFPST11_FLOW_REQUEST
);
746 msg
= ofpraw_alloc(raw
, ofputil_protocol_to_ofp_version(protocol
),
747 ofputil_match_typical_len(protocol
));
748 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
749 ofsr
->table_id
= fsr
->table_id
;
750 ofsr
->out_port
= ofputil_port_to_ofp11(fsr
->out_port
);
751 ofsr
->out_group
= htonl(fsr
->out_group
);
752 ofsr
->cookie
= fsr
->cookie
;
753 ofsr
->cookie_mask
= fsr
->cookie_mask
;
754 ofputil_put_ofp11_match(msg
, &fsr
->match
, protocol
);
758 case OFPUTIL_P_OF10_STD
:
759 case OFPUTIL_P_OF10_STD_TID
: {
760 struct ofp10_flow_stats_request
*ofsr
;
762 raw
= (fsr
->aggregate
763 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
764 : OFPRAW_OFPST10_FLOW_REQUEST
);
765 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, 0);
766 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
767 ofputil_match_to_ofp10_match(&fsr
->match
, &ofsr
->match
);
768 ofsr
->table_id
= fsr
->table_id
;
769 ofsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
773 case OFPUTIL_P_OF10_NXM
:
774 case OFPUTIL_P_OF10_NXM_TID
: {
775 struct nx_flow_stats_request
*nfsr
;
778 raw
= (fsr
->aggregate
779 ? OFPRAW_NXST_AGGREGATE_REQUEST
780 : OFPRAW_NXST_FLOW_REQUEST
);
781 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, NXM_TYPICAL_LEN
);
782 ofpbuf_put_zeros(msg
, sizeof *nfsr
);
783 match_len
= nx_put_match(msg
, &fsr
->match
,
784 fsr
->cookie
, fsr
->cookie_mask
);
787 nfsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
788 nfsr
->match_len
= htons(match_len
);
789 nfsr
->table_id
= fsr
->table_id
;
801 ofputil_flow_stats_request_format(struct ds
*s
,
802 const struct ofputil_flow_stats_request
*fsr
,
803 const struct ofputil_port_map
*port_map
,
804 const struct ofputil_table_map
*table_map
)
806 if (fsr
->table_id
!= 0xff) {
807 ds_put_format(s
, " table=");
808 ofputil_format_table(fsr
->table_id
, table_map
, s
);
811 if (fsr
->out_port
!= OFPP_ANY
) {
812 ds_put_cstr(s
, " out_port=");
813 ofputil_format_port(fsr
->out_port
, port_map
, s
);
817 match_format(&fsr
->match
, port_map
, s
, OFP_DEFAULT_PRIORITY
);
820 char * OVS_WARN_UNUSED_RESULT
821 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request
*fsr
,
822 bool aggregate
, const char *string
,
823 const struct ofputil_port_map
*port_map
,
824 const struct ofputil_table_map
*table_map
,
825 enum ofputil_protocol
*usable_protocols
)
827 struct ofputil_flow_mod fm
;
830 error
= parse_ofp_str(&fm
, -1, string
, port_map
, table_map
,
836 /* Special table ID support not required for stats requests. */
837 if (*usable_protocols
& OFPUTIL_P_OF10_STD_TID
) {
838 *usable_protocols
|= OFPUTIL_P_OF10_STD
;
840 if (*usable_protocols
& OFPUTIL_P_OF10_NXM_TID
) {
841 *usable_protocols
|= OFPUTIL_P_OF10_NXM
;
844 fsr
->aggregate
= aggregate
;
845 fsr
->cookie
= fm
.cookie
;
846 fsr
->cookie_mask
= fm
.cookie_mask
;
847 minimatch_expand(&fm
.match
, &fsr
->match
);
848 fsr
->out_port
= fm
.out_port
;
849 fsr
->out_group
= fm
.out_group
;
850 fsr
->table_id
= fm
.table_id
;
852 minimatch_destroy(&fm
.match
);
857 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
858 * ofputil_flow_stats in 'fs'.
860 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
861 * OpenFlow message. Calling this function multiple times for a single 'msg'
862 * iterates through the replies. The caller must initially leave 'msg''s layer
863 * pointers null and not modify them between calls.
865 * Most switches don't send the values needed to populate fs->idle_age and
866 * fs->hard_age, so those members will usually be set to 0. If the switch from
867 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
868 * 'flow_age_extension' as true so that the contents of 'msg' determine the
869 * 'idle_age' and 'hard_age' members in 'fs'.
871 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
872 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
873 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
875 * Returns 0 if successful, EOF if no replies were left in this 'msg',
876 * otherwise an OFPERR_* value. */
878 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats
*fs
,
880 bool flow_age_extension
,
881 struct ofpbuf
*ofpacts
)
883 const struct ofp_header
*oh
;
884 size_t instructions_len
;
888 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
889 : ofpraw_pull(&raw
, msg
));
897 } else if (raw
== OFPRAW_OFPST11_FLOW_REPLY
898 || raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
899 const struct ofp11_flow_stats
*ofs
;
901 uint16_t padded_match_len
;
903 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
905 VLOG_WARN_RL(&rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
906 "bytes at end", msg
->size
);
907 return OFPERR_OFPBRC_BAD_LEN
;
910 length
= ntohs(ofs
->length
);
911 if (length
< sizeof *ofs
) {
912 VLOG_WARN_RL(&rl
, "OFPST_FLOW reply claims invalid "
913 "length %"PRIuSIZE
, length
);
914 return OFPERR_OFPBRC_BAD_LEN
;
917 error
= ofputil_pull_ofp11_match(msg
, NULL
, NULL
, &fs
->match
,
920 VLOG_WARN_RL(&rl
, "OFPST_FLOW reply bad match");
923 instructions_len
= length
- sizeof *ofs
- padded_match_len
;
925 fs
->priority
= ntohs(ofs
->priority
);
926 fs
->table_id
= ofs
->table_id
;
927 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
928 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
929 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
930 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
931 if (oh
->version
>= OFP14_VERSION
) {
932 fs
->importance
= ntohs(ofs
->importance
);
936 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
937 error
= ofputil_decode_flow_mod_flags(ofs
->flags
, -1, oh
->version
,
947 fs
->cookie
= ofs
->cookie
;
948 fs
->packet_count
= ntohll(ofs
->packet_count
);
949 fs
->byte_count
= ntohll(ofs
->byte_count
);
950 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
951 const struct ofp10_flow_stats
*ofs
;
954 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
956 VLOG_WARN_RL(&rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
957 "bytes at end", msg
->size
);
958 return OFPERR_OFPBRC_BAD_LEN
;
961 length
= ntohs(ofs
->length
);
962 if (length
< sizeof *ofs
) {
963 VLOG_WARN_RL(&rl
, "OFPST_FLOW reply claims invalid "
964 "length %"PRIuSIZE
, length
);
965 return OFPERR_OFPBRC_BAD_LEN
;
967 instructions_len
= length
- sizeof *ofs
;
969 fs
->cookie
= get_32aligned_be64(&ofs
->cookie
);
970 ofputil_match_from_ofp10_match(&ofs
->match
, &fs
->match
);
971 fs
->priority
= ntohs(ofs
->priority
);
972 fs
->table_id
= ofs
->table_id
;
973 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
974 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
975 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
976 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
980 fs
->packet_count
= ntohll(get_32aligned_be64(&ofs
->packet_count
));
981 fs
->byte_count
= ntohll(get_32aligned_be64(&ofs
->byte_count
));
983 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
984 const struct nx_flow_stats
*nfs
;
985 size_t match_len
, length
;
987 nfs
= ofpbuf_try_pull(msg
, sizeof *nfs
);
989 VLOG_WARN_RL(&rl
, "NXST_FLOW reply has %"PRIu32
" leftover "
990 "bytes at end", msg
->size
);
991 return OFPERR_OFPBRC_BAD_LEN
;
994 length
= ntohs(nfs
->length
);
995 match_len
= ntohs(nfs
->match_len
);
996 if (length
< sizeof *nfs
+ ROUND_UP(match_len
, 8)) {
997 VLOG_WARN_RL(&rl
, "NXST_FLOW reply with match_len=%"PRIuSIZE
" "
998 "claims invalid length %"PRIuSIZE
, match_len
, length
);
999 return OFPERR_OFPBRC_BAD_LEN
;
1001 error
= nx_pull_match(msg
, match_len
, &fs
->match
, NULL
, NULL
, false,
1006 instructions_len
= length
- sizeof *nfs
- ROUND_UP(match_len
, 8);
1008 fs
->cookie
= nfs
->cookie
;
1009 fs
->table_id
= nfs
->table_id
;
1010 fs
->duration_sec
= ntohl(nfs
->duration_sec
);
1011 fs
->duration_nsec
= ntohl(nfs
->duration_nsec
);
1012 fs
->priority
= ntohs(nfs
->priority
);
1013 fs
->idle_timeout
= ntohs(nfs
->idle_timeout
);
1014 fs
->hard_timeout
= ntohs(nfs
->hard_timeout
);
1018 if (flow_age_extension
) {
1019 if (nfs
->idle_age
) {
1020 fs
->idle_age
= ntohs(nfs
->idle_age
) - 1;
1022 if (nfs
->hard_age
) {
1023 fs
->hard_age
= ntohs(nfs
->hard_age
) - 1;
1026 fs
->packet_count
= ntohll(nfs
->packet_count
);
1027 fs
->byte_count
= ntohll(nfs
->byte_count
);
1033 error
= ofpacts_pull_openflow_instructions(msg
, instructions_len
,
1034 oh
->version
, NULL
, NULL
,
1037 VLOG_WARN_RL(&rl
, "OFPST_FLOW reply bad instructions");
1040 fs
->ofpacts
= ofpacts
->data
;
1041 fs
->ofpacts_len
= ofpacts
->size
;
1046 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1048 * We use this in situations where OVS internally uses UINT64_MAX to mean
1049 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1051 unknown_to_zero(uint64_t count
)
1053 return count
!= UINT64_MAX
? count
: 0;
1056 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1057 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1058 * have been initialized with ofpmp_init(). */
1060 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats
*fs
,
1061 struct ovs_list
*replies
,
1062 const struct tun_table
*tun_table
)
1064 struct ofputil_flow_stats
*fs_
= CONST_CAST(struct ofputil_flow_stats
*,
1066 const struct tun_table
*orig_tun_table
;
1067 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
1068 size_t start_ofs
= reply
->size
;
1069 enum ofp_version version
= ofpmp_version(replies
);
1070 enum ofpraw raw
= ofpmp_decode_raw(replies
);
1072 orig_tun_table
= fs
->match
.flow
.tunnel
.metadata
.tab
;
1073 fs_
->match
.flow
.tunnel
.metadata
.tab
= tun_table
;
1075 if (raw
== OFPRAW_OFPST11_FLOW_REPLY
|| raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
1076 struct ofp11_flow_stats
*ofs
;
1078 ofpbuf_put_uninit(reply
, sizeof *ofs
);
1079 oxm_put_match(reply
, &fs
->match
, version
);
1080 ofpacts_put_openflow_instructions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
1083 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
1084 ofs
->length
= htons(reply
->size
- start_ofs
);
1085 ofs
->table_id
= fs
->table_id
;
1087 ofs
->duration_sec
= htonl(fs
->duration_sec
);
1088 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
1089 ofs
->priority
= htons(fs
->priority
);
1090 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
1091 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
1092 if (version
>= OFP14_VERSION
) {
1093 ofs
->importance
= htons(fs
->importance
);
1095 ofs
->importance
= 0;
1097 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
1098 ofs
->flags
= ofputil_encode_flow_mod_flags(fs
->flags
, version
);
1102 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
1103 ofs
->cookie
= fs
->cookie
;
1104 ofs
->packet_count
= htonll(unknown_to_zero(fs
->packet_count
));
1105 ofs
->byte_count
= htonll(unknown_to_zero(fs
->byte_count
));
1106 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
1107 struct ofp10_flow_stats
*ofs
;
1109 ofpbuf_put_uninit(reply
, sizeof *ofs
);
1110 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
1112 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
1113 ofs
->length
= htons(reply
->size
- start_ofs
);
1114 ofs
->table_id
= fs
->table_id
;
1116 ofputil_match_to_ofp10_match(&fs
->match
, &ofs
->match
);
1117 ofs
->duration_sec
= htonl(fs
->duration_sec
);
1118 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
1119 ofs
->priority
= htons(fs
->priority
);
1120 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
1121 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
1122 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
1123 put_32aligned_be64(&ofs
->cookie
, fs
->cookie
);
1124 put_32aligned_be64(&ofs
->packet_count
,
1125 htonll(unknown_to_zero(fs
->packet_count
)));
1126 put_32aligned_be64(&ofs
->byte_count
,
1127 htonll(unknown_to_zero(fs
->byte_count
)));
1128 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
1129 struct nx_flow_stats
*nfs
;
1132 ofpbuf_put_uninit(reply
, sizeof *nfs
);
1133 match_len
= nx_put_match(reply
, &fs
->match
, 0, 0);
1134 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
1136 nfs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *nfs
);
1137 nfs
->length
= htons(reply
->size
- start_ofs
);
1138 nfs
->table_id
= fs
->table_id
;
1140 nfs
->duration_sec
= htonl(fs
->duration_sec
);
1141 nfs
->duration_nsec
= htonl(fs
->duration_nsec
);
1142 nfs
->priority
= htons(fs
->priority
);
1143 nfs
->idle_timeout
= htons(fs
->idle_timeout
);
1144 nfs
->hard_timeout
= htons(fs
->hard_timeout
);
1145 nfs
->idle_age
= htons(fs
->idle_age
< 0 ? 0
1146 : fs
->idle_age
< UINT16_MAX
? fs
->idle_age
+ 1
1148 nfs
->hard_age
= htons(fs
->hard_age
< 0 ? 0
1149 : fs
->hard_age
< UINT16_MAX
? fs
->hard_age
+ 1
1151 nfs
->match_len
= htons(match_len
);
1152 nfs
->cookie
= fs
->cookie
;
1153 nfs
->packet_count
= htonll(fs
->packet_count
);
1154 nfs
->byte_count
= htonll(fs
->byte_count
);
1159 ofpmp_postappend(replies
, start_ofs
);
1160 fs_
->match
.flow
.tunnel
.metadata
.tab
= orig_tun_table
;
1163 /* Appends a textual form of 'fs' to 'string', translating port numbers to
1164 * names using 'port_map' (if provided). If 'show_stats' is true, the output
1165 * includes the flow duration, packet and byte counts, and its idle and hard
1166 * ages, otherwise they are omitted. */
1168 ofputil_flow_stats_format(struct ds
*string
,
1169 const struct ofputil_flow_stats
*fs
,
1170 const struct ofputil_port_map
*port_map
,
1171 const struct ofputil_table_map
*table_map
,
1174 if (show_stats
|| fs
->cookie
) {
1175 ds_put_format(string
, "%scookie=%s0x%"PRIx64
", ",
1176 colors
.param
, colors
.end
, ntohll(fs
->cookie
));
1179 ds_put_format(string
, "%sduration=%s", colors
.param
, colors
.end
);
1180 ofp_print_duration(string
, fs
->duration_sec
, fs
->duration_nsec
);
1181 ds_put_cstr(string
, ", ");
1184 if (show_stats
|| fs
->table_id
1185 || ofputil_table_map_get_name(table_map
, fs
->table_id
) != NULL
) {
1186 ds_put_format(string
, "%stable=%s", colors
.special
, colors
.end
);
1187 ofputil_format_table(fs
->table_id
, table_map
, string
);
1188 ds_put_cstr(string
, ", ");
1191 ds_put_format(string
, "%sn_packets=%s%"PRIu64
", ",
1192 colors
.param
, colors
.end
, fs
->packet_count
);
1193 ds_put_format(string
, "%sn_bytes=%s%"PRIu64
", ",
1194 colors
.param
, colors
.end
, fs
->byte_count
);
1196 if (fs
->idle_timeout
!= OFP_FLOW_PERMANENT
) {
1197 ds_put_format(string
, "%sidle_timeout=%s%"PRIu16
", ",
1198 colors
.param
, colors
.end
, fs
->idle_timeout
);
1200 if (fs
->hard_timeout
!= OFP_FLOW_PERMANENT
) {
1201 ds_put_format(string
, "%shard_timeout=%s%"PRIu16
", ",
1202 colors
.param
, colors
.end
, fs
->hard_timeout
);
1205 ofputil_flow_mod_flags_format(string
, fs
->flags
);
1207 if (fs
->importance
!= 0) {
1208 ds_put_format(string
, "%simportance=%s%"PRIu16
", ",
1209 colors
.param
, colors
.end
, fs
->importance
);
1211 if (show_stats
&& fs
->idle_age
>= 0) {
1212 ds_put_format(string
, "%sidle_age=%s%d, ",
1213 colors
.param
, colors
.end
, fs
->idle_age
);
1215 if (show_stats
&& fs
->hard_age
>= 0 && fs
->hard_age
!= fs
->duration_sec
) {
1216 ds_put_format(string
, "%shard_age=%s%d, ",
1217 colors
.param
, colors
.end
, fs
->hard_age
);
1220 /* Print the match, followed by a space (but omit the space if the match
1221 * was an empty string). */
1222 size_t length
= string
->length
;
1223 match_format(&fs
->match
, port_map
, string
, fs
->priority
);
1224 if (string
->length
!= length
) {
1225 ds_put_char(string
, ' ');
1228 ds_put_format(string
, "%sactions=%s", colors
.actions
, colors
.end
);
1229 struct ofpact_format_params fp
= {
1230 .port_map
= port_map
,
1231 .table_map
= table_map
,
1234 ofpacts_format(fs
->ofpacts
, fs
->ofpacts_len
, &fp
);
1237 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1238 * NXST_AGGREGATE reply matching 'request', and returns the message. */
1240 ofputil_encode_aggregate_stats_reply(
1241 const struct ofputil_aggregate_stats
*stats
,
1242 const struct ofp_header
*request
)
1244 struct ofp_aggregate_stats_reply
*asr
;
1245 uint64_t packet_count
;
1246 uint64_t byte_count
;
1250 ofpraw_decode(&raw
, request
);
1251 if (raw
== OFPRAW_OFPST10_AGGREGATE_REQUEST
) {
1252 packet_count
= unknown_to_zero(stats
->packet_count
);
1253 byte_count
= unknown_to_zero(stats
->byte_count
);
1255 packet_count
= stats
->packet_count
;
1256 byte_count
= stats
->byte_count
;
1259 msg
= ofpraw_alloc_stats_reply(request
, 0);
1260 asr
= ofpbuf_put_zeros(msg
, sizeof *asr
);
1261 put_32aligned_be64(&asr
->packet_count
, htonll(packet_count
));
1262 put_32aligned_be64(&asr
->byte_count
, htonll(byte_count
));
1263 asr
->flow_count
= htonl(stats
->flow_count
);
1269 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats
*stats
,
1270 const struct ofp_header
*reply
)
1272 struct ofpbuf msg
= ofpbuf_const_initializer(reply
, ntohs(reply
->length
));
1273 ofpraw_pull_assert(&msg
);
1275 struct ofp_aggregate_stats_reply
*asr
= msg
.msg
;
1276 stats
->packet_count
= ntohll(get_32aligned_be64(&asr
->packet_count
));
1277 stats
->byte_count
= ntohll(get_32aligned_be64(&asr
->byte_count
));
1278 stats
->flow_count
= ntohl(asr
->flow_count
);
1284 ofputil_aggregate_stats_format(struct ds
*s
,
1285 const struct ofputil_aggregate_stats
*as
)
1287 ds_put_format(s
, " packet_count=%"PRIu64
, as
->packet_count
);
1288 ds_put_format(s
, " byte_count=%"PRIu64
, as
->byte_count
);
1289 ds_put_format(s
, " flow_count=%"PRIu32
, as
->flow_count
);
1292 /* Parses 'str_value' as the value of subfield 'name', and updates
1293 * 'match' appropriately. Restricts the set of usable protocols to ones
1294 * supporting the parsed field.
1296 * Returns NULL if successful, otherwise a malloc()'d string describing the
1297 * error. The caller is responsible for freeing the returned string. */
1298 static char * OVS_WARN_UNUSED_RESULT
1299 parse_subfield(const char *name
, const char *str_value
, struct match
*match
,
1300 enum ofputil_protocol
*usable_protocols
)
1302 struct mf_subfield sf
;
1305 error
= mf_parse_subfield(&sf
, name
);
1309 if (parse_int_string(str_value
, (uint8_t *)&val
, sf
.field
->n_bytes
,
1310 &tail
) || *tail
!= 0) {
1311 return xasprintf("%s: cannot parse integer value: %s", name
,
1314 if (!bitwise_is_all_zeros(&val
, sf
.field
->n_bytes
, sf
.n_bits
,
1315 sf
.field
->n_bytes
* 8 - sf
.n_bits
)) {
1319 mf_format(sf
.field
, &val
, NULL
, NULL
, &ds
);
1320 error
= xasprintf("%s: value %s does not fit into %d bits",
1321 name
, ds_cstr(&ds
), sf
.n_bits
);
1326 const struct mf_field
*field
= sf
.field
;
1327 union mf_value value
, mask
;
1328 unsigned int size
= field
->n_bytes
;
1330 mf_get(field
, match
, &value
, &mask
);
1331 bitwise_copy(&val
, size
, 0, &value
, size
, sf
.ofs
, sf
.n_bits
);
1332 bitwise_one ( &mask
, size
, sf
.ofs
, sf
.n_bits
);
1333 *usable_protocols
&= mf_set(field
, &value
, &mask
, match
, &error
);
1335 match_add_ethernet_prereq(match
, sf
.field
);
1340 static char * OVS_WARN_UNUSED_RESULT
1341 parse_ofp_str__(struct ofputil_flow_mod
*fm
, int command
, char *string
,
1342 const struct ofputil_port_map
*port_map
,
1343 const struct ofputil_table_map
*table_map
,
1344 enum ofputil_protocol
*usable_protocols
)
1347 F_OUT_PORT
= 1 << 0,
1349 F_IMPORTANCE
= 1 << 2,
1351 F_PRIORITY
= 1 << 4,
1354 char *act_str
= NULL
;
1357 *usable_protocols
= OFPUTIL_P_ANY
;
1359 if (command
== -2) {
1362 string
+= strspn(string
, " \t\r\n"); /* Skip white space. */
1363 len
= strcspn(string
, ", \t\r\n"); /* Get length of the first token. */
1365 if (!strncmp(string
, "add", len
)) {
1366 command
= OFPFC_ADD
;
1367 } else if (!strncmp(string
, "delete", len
)) {
1368 command
= OFPFC_DELETE
;
1369 } else if (!strncmp(string
, "delete_strict", len
)) {
1370 command
= OFPFC_DELETE_STRICT
;
1371 } else if (!strncmp(string
, "modify", len
)) {
1372 command
= OFPFC_MODIFY
;
1373 } else if (!strncmp(string
, "modify_strict", len
)) {
1374 command
= OFPFC_MODIFY_STRICT
;
1377 command
= OFPFC_ADD
;
1384 fields
= F_OUT_PORT
;
1388 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
| F_IMPORTANCE
;
1392 fields
= F_OUT_PORT
;
1395 case OFPFC_DELETE_STRICT
:
1396 fields
= F_OUT_PORT
| F_PRIORITY
;
1400 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
1403 case OFPFC_MODIFY_STRICT
:
1404 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
1411 *fm
= (struct ofputil_flow_mod
) {
1412 .priority
= OFP_DEFAULT_PRIORITY
,
1415 .buffer_id
= UINT32_MAX
,
1416 .out_port
= OFPP_ANY
,
1417 .out_group
= OFPG_ANY
,
1419 /* For modify, by default, don't update the cookie. */
1420 if (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
) {
1421 fm
->new_cookie
= OVS_BE64_MAX
;
1424 if (fields
& F_ACTIONS
) {
1425 act_str
= ofp_extract_actions(string
);
1427 return xstrdup("must specify an action");
1431 struct match match
= MATCH_CATCHALL_INITIALIZER
;
1432 while (ofputil_parse_key_value(&string
, &name
, &value
)) {
1433 const struct ofp_protocol
*p
;
1434 const struct mf_field
*mf
;
1437 if (ofp_parse_protocol(name
, &p
)) {
1438 match_set_dl_type(&match
, htons(p
->dl_type
));
1440 match_set_nw_proto(&match
, p
->nw_proto
);
1442 match_set_default_packet_type(&match
);
1443 } else if (!strcmp(name
, "eth")) {
1444 match_set_packet_type(&match
, htonl(PT_ETH
));
1445 } else if (fields
& F_FLAGS
&& !strcmp(name
, "send_flow_rem")) {
1446 fm
->flags
|= OFPUTIL_FF_SEND_FLOW_REM
;
1447 } else if (fields
& F_FLAGS
&& !strcmp(name
, "check_overlap")) {
1448 fm
->flags
|= OFPUTIL_FF_CHECK_OVERLAP
;
1449 } else if (fields
& F_FLAGS
&& !strcmp(name
, "reset_counts")) {
1450 fm
->flags
|= OFPUTIL_FF_RESET_COUNTS
;
1451 *usable_protocols
&= OFPUTIL_P_OF12_UP
;
1452 } else if (fields
& F_FLAGS
&& !strcmp(name
, "no_packet_counts")) {
1453 fm
->flags
|= OFPUTIL_FF_NO_PKT_COUNTS
;
1454 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
1455 } else if (fields
& F_FLAGS
&& !strcmp(name
, "no_byte_counts")) {
1456 fm
->flags
|= OFPUTIL_FF_NO_BYT_COUNTS
;
1457 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
1458 } else if (!strcmp(name
, "no_readonly_table")
1459 || !strcmp(name
, "allow_hidden_fields")) {
1460 /* ignore these fields. */
1461 } else if ((mf
= mf_from_name(name
)) != NULL
) {
1462 error
= ofp_parse_field(mf
, value
, port_map
,
1463 &match
, usable_protocols
);
1464 } else if (strchr(name
, '[')) {
1465 error
= parse_subfield(name
, value
, &match
, usable_protocols
);
1468 return xasprintf("field %s missing value", name
);
1471 if (!strcmp(name
, "table")) {
1472 if (!ofputil_table_from_string(value
, table_map
,
1474 return xasprintf("unknown table \"%s\"", value
);
1476 if (fm
->table_id
!= 0xff) {
1477 *usable_protocols
&= OFPUTIL_P_TID
;
1479 } else if (fields
& F_OUT_PORT
&& !strcmp(name
, "out_port")) {
1480 if (!ofputil_port_from_string(value
, port_map
,
1482 error
= xasprintf("%s is not a valid OpenFlow port",
1485 } else if (fields
& F_OUT_PORT
&& !strcmp(name
, "out_group")) {
1486 *usable_protocols
&= OFPUTIL_P_OF11_UP
;
1487 if (!ofputil_group_from_string(value
, &fm
->out_group
)) {
1488 error
= xasprintf("%s is not a valid OpenFlow group",
1491 } else if (fields
& F_PRIORITY
&& !strcmp(name
, "priority")) {
1492 uint16_t priority
= 0;
1494 error
= str_to_u16(value
, name
, &priority
);
1495 fm
->priority
= priority
;
1496 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "idle_timeout")) {
1497 error
= str_to_u16(value
, name
, &fm
->idle_timeout
);
1498 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "hard_timeout")) {
1499 error
= str_to_u16(value
, name
, &fm
->hard_timeout
);
1500 } else if (fields
& F_IMPORTANCE
&& !strcmp(name
, "importance")) {
1501 error
= str_to_u16(value
, name
, &fm
->importance
);
1502 } else if (!strcmp(name
, "cookie")) {
1503 char *mask
= strchr(value
, '/');
1506 /* A mask means we're searching for a cookie. */
1507 if (command
== OFPFC_ADD
) {
1508 return xstrdup("flow additions cannot use "
1512 error
= str_to_be64(value
, &fm
->cookie
);
1516 error
= str_to_be64(mask
+ 1, &fm
->cookie_mask
);
1518 /* Matching of the cookie is only supported through NXM or
1520 if (fm
->cookie_mask
!= htonll(0)) {
1521 *usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
1524 /* No mask means that the cookie is being set. */
1525 if (command
!= OFPFC_ADD
&& command
!= OFPFC_MODIFY
1526 && command
!= OFPFC_MODIFY_STRICT
) {
1527 return xstrdup("cannot set cookie");
1529 error
= str_to_be64(value
, &fm
->new_cookie
);
1530 fm
->modify_cookie
= true;
1532 } else if (!strcmp(name
, "duration")
1533 || !strcmp(name
, "n_packets")
1534 || !strcmp(name
, "n_bytes")
1535 || !strcmp(name
, "idle_age")
1536 || !strcmp(name
, "hard_age")) {
1537 /* Ignore these, so that users can feed the output of
1538 * "ovs-ofctl dump-flows" back into commands that parse
1541 error
= xasprintf("unknown keyword %s", name
);
1549 /* Copy ethertype to flow->dl_type for matches on packet_type
1550 * (OFPHTN_ETHERTYPE, ethertype). */
1551 if (match
.wc
.masks
.packet_type
== OVS_BE32_MAX
&&
1552 pt_ns(match
.flow
.packet_type
) == OFPHTN_ETHERTYPE
) {
1553 match
.flow
.dl_type
= pt_ns_type_be(match
.flow
.packet_type
);
1555 /* Check for usable protocol interdependencies between match fields. */
1556 if (match
.flow
.dl_type
== htons(ETH_TYPE_IPV6
)) {
1557 const struct flow_wildcards
*wc
= &match
.wc
;
1558 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
1560 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
1561 * nw_ttl are covered elsewhere so they don't need to be included in
1564 if (wc
->masks
.nw_proto
|| wc
->masks
.nw_tos
1565 || wc
->masks
.tp_src
|| wc
->masks
.tp_dst
) {
1566 *usable_protocols
&= OFPUTIL_P_NXM_OXM_ANY
;
1569 if (!fm
->cookie_mask
&& fm
->new_cookie
== OVS_BE64_MAX
1570 && (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
)) {
1571 /* On modifies without a mask, we are supposed to add a flow if
1572 * one does not exist. If a cookie wasn't been specified, use a
1573 * default of zero. */
1574 fm
->new_cookie
= htonll(0);
1576 if (fields
& F_ACTIONS
) {
1577 enum ofputil_protocol action_usable_protocols
;
1578 struct ofpbuf ofpacts
;
1581 ofpbuf_init(&ofpacts
, 32);
1582 struct ofpact_parse_params pp
= {
1583 .port_map
= port_map
,
1584 .table_map
= table_map
,
1585 .ofpacts
= &ofpacts
,
1586 .usable_protocols
= &action_usable_protocols
1588 error
= ofpacts_parse_instructions(act_str
, &pp
);
1589 *usable_protocols
&= action_usable_protocols
;
1593 err
= ofpacts_check(ofpacts
.data
, ofpacts
.size
, &match
,
1594 OFPP_MAX
, fm
->table_id
, 255, usable_protocols
);
1595 if (!err
&& !*usable_protocols
) {
1596 err
= OFPERR_OFPBAC_MATCH_INCONSISTENT
;
1599 error
= xasprintf("actions are invalid with specified match "
1600 "(%s)", ofperr_to_string(err
));
1605 ofpbuf_uninit(&ofpacts
);
1609 fm
->ofpacts_len
= ofpacts
.size
;
1610 fm
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
1612 fm
->ofpacts_len
= 0;
1615 minimatch_init(&fm
->match
, &match
);
1620 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1621 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
1622 * Returns the set of usable protocols in '*usable_protocols'.
1624 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
1625 * constant for 'command'. To parse syntax for an OFPST_FLOW or
1626 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
1628 * If 'command' is given as -2, 'str_' may begin with a command name ("add",
1629 * "modify", "delete", "modify_strict", or "delete_strict"). A missing command
1630 * name is treated as "add".
1632 * Returns NULL if successful, otherwise a malloc()'d string describing the
1633 * error. The caller is responsible for freeing the returned string.
1635 * On success, the caller is responsible for freeing fm->ofpacts and
1637 char * OVS_WARN_UNUSED_RESULT
1638 parse_ofp_str(struct ofputil_flow_mod
*fm
, int command
, const char *str_
,
1639 const struct ofputil_port_map
*port_map
,
1640 const struct ofputil_table_map
*table_map
,
1641 enum ofputil_protocol
*usable_protocols
)
1643 char *string
= xstrdup(str_
);
1646 error
= parse_ofp_str__(fm
, command
, string
, port_map
, table_map
,
1650 fm
->ofpacts_len
= 0;
1657 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1658 * (one of OFPFC_*) into 'fm'.
1660 * If 'command' is given as -2, 'string' may begin with a command name ("add",
1661 * "modify", "delete", "modify_strict", or "delete_strict"). A missing command
1662 * name is treated as "add".
1664 * Returns NULL if successful, otherwise a malloc()'d string describing the
1665 * error. The caller is responsible for freeing the returned string. */
1666 char * OVS_WARN_UNUSED_RESULT
1667 parse_ofp_flow_mod_str(struct ofputil_flow_mod
*fm
, const char *string
,
1668 const struct ofputil_port_map
*port_map
,
1669 const struct ofputil_table_map
*table_map
,
1671 enum ofputil_protocol
*usable_protocols
)
1673 char *error
= parse_ofp_str(fm
, command
, string
, port_map
, table_map
,
1677 /* Normalize a copy of the match. This ensures that non-normalized
1678 * flows get logged but doesn't affect what gets sent to the switch, so
1679 * that the switch can do whatever it likes with the flow. */
1681 minimatch_expand(&fm
->match
, &match
);
1682 ofputil_normalize_match(&match
);
1688 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
1689 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1690 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1692 * If 'command' is given as -2, each line may start with a command name
1693 * ("add", "modify", "delete", "modify_strict", or "delete_strict"). A missing
1694 * command name is treated as "add".
1696 * Returns NULL if successful, otherwise a malloc()'d string describing the
1697 * error. The caller is responsible for freeing the returned string. */
1698 char * OVS_WARN_UNUSED_RESULT
1699 parse_ofp_flow_mod_file(const char *file_name
,
1700 const struct ofputil_port_map
*port_map
,
1701 const struct ofputil_table_map
*table_map
,
1703 struct ofputil_flow_mod
**fms
, size_t *n_fms
,
1704 enum ofputil_protocol
*usable_protocols
)
1706 size_t allocated_fms
;
1711 *usable_protocols
= OFPUTIL_P_ANY
;
1716 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
1717 if (stream
== NULL
) {
1718 return xasprintf("%s: open failed (%s)",
1719 file_name
, ovs_strerror(errno
));
1722 allocated_fms
= *n_fms
;
1725 while (!ds_get_preprocessed_line(&s
, stream
, &line_number
)) {
1727 enum ofputil_protocol usable
;
1729 if (*n_fms
>= allocated_fms
) {
1730 *fms
= x2nrealloc(*fms
, &allocated_fms
, sizeof **fms
);
1732 error
= parse_ofp_flow_mod_str(&(*fms
)[*n_fms
], ds_cstr(&s
), port_map
,
1733 table_map
, command
, &usable
);
1738 for (i
= 0; i
< *n_fms
; i
++) {
1739 free(CONST_CAST(struct ofpact
*, (*fms
)[i
].ofpacts
));
1740 minimatch_destroy(&(*fms
)[i
].match
);
1747 if (stream
!= stdin
) {
1751 err_msg
= xasprintf("%s:%d: %s", file_name
, line_number
, error
);
1755 *usable_protocols
&= usable
; /* Each line can narrow the set. */
1760 if (stream
!= stdin
) {
1766 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1767 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1768 * mf_field. Fields must be specified in a natural order for satisfying
1769 * prerequisites. If 'wc' is specified, masks the field in 'wc' for each of the
1770 * field specified in flow. If the map, 'names_portno' is specfied, converts
1771 * the in_port name into port no while setting the 'flow'.
1773 * Returns NULL on success, otherwise a malloc()'d string that explains the
1776 parse_ofp_exact_flow(struct flow
*flow
, struct flow_wildcards
*wc
,
1777 const struct tun_table
*tun_table
, const char *s
,
1778 const struct ofputil_port_map
*port_map
)
1780 char *pos
, *key
, *value_s
;
1784 memset(flow
, 0, sizeof *flow
);
1786 memset(wc
, 0, sizeof *wc
);
1788 flow
->tunnel
.metadata
.tab
= tun_table
;
1790 pos
= copy
= xstrdup(s
);
1791 while (ofputil_parse_key_value(&pos
, &key
, &value_s
)) {
1792 const struct ofp_protocol
*p
;
1793 if (ofp_parse_protocol(key
, &p
)) {
1794 if (flow
->dl_type
) {
1795 error
= xasprintf("%s: Ethernet type set multiple times", s
);
1798 flow
->dl_type
= htons(p
->dl_type
);
1800 wc
->masks
.dl_type
= OVS_BE16_MAX
;
1804 if (flow
->nw_proto
) {
1805 error
= xasprintf("%s: network protocol set "
1806 "multiple times", s
);
1809 flow
->nw_proto
= p
->nw_proto
;
1811 wc
->masks
.nw_proto
= UINT8_MAX
;
1815 const struct mf_field
*mf
;
1816 union mf_value value
;
1819 mf
= mf_from_name(key
);
1821 error
= xasprintf("%s: unknown field %s", s
, key
);
1825 if (!mf_are_prereqs_ok(mf
, flow
, NULL
)) {
1826 error
= xasprintf("%s: prerequisites not met for setting %s",
1831 if (mf_is_set(mf
, flow
)) {
1832 error
= xasprintf("%s: field %s set multiple times", s
, key
);
1836 field_error
= mf_parse_value(mf
, value_s
, port_map
, &value
);
1838 error
= xasprintf("%s: bad value for %s (%s)",
1839 s
, key
, field_error
);
1844 mf_set_flow_value(mf
, &value
, flow
);
1846 mf_mask_field(mf
, wc
);
1851 if (!flow
->in_port
.ofp_port
) {
1852 flow
->in_port
.ofp_port
= OFPP_NONE
;
1859 memset(flow
, 0, sizeof *flow
);
1861 memset(wc
, 0, sizeof *wc
);