2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
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 "ofp-actions.h"
21 #include "byte-order.h"
23 #include "dynamic-string.h"
25 #include "meta-flow.h"
26 #include "multipath.h"
32 VLOG_DEFINE_THIS_MODULE(ofp_actions);
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
36 /* Converting OpenFlow 1.0 to ofpacts. */
39 output_from_openflow10(const struct ofp10_action_output *oao,
42 struct ofpact_output *output;
44 output = ofpact_put_OUTPUT(out);
45 output->port = ntohs(oao->port);
46 output->max_len = ntohs(oao->max_len);
48 return ofputil_check_output_port(output->port, OFPP_MAX);
52 enqueue_from_openflow10(const struct ofp_action_enqueue *oae,
55 struct ofpact_enqueue *enqueue;
57 enqueue = ofpact_put_ENQUEUE(out);
58 enqueue->port = ntohs(oae->port);
59 enqueue->queue = ntohl(oae->queue_id);
60 if (enqueue->port >= OFPP_MAX && enqueue->port != OFPP_IN_PORT
61 && enqueue->port != OFPP_LOCAL) {
62 return OFPERR_OFPBAC_BAD_OUT_PORT;
68 resubmit_from_openflow(const struct nx_action_resubmit *nar,
71 struct ofpact_resubmit *resubmit;
73 resubmit = ofpact_put_RESUBMIT(out);
74 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
75 resubmit->in_port = ntohs(nar->in_port);
76 resubmit->table_id = 0xff;
80 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
83 struct ofpact_resubmit *resubmit;
85 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
86 return OFPERR_OFPBAC_BAD_ARGUMENT;
89 resubmit = ofpact_put_RESUBMIT(out);
90 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
91 resubmit->in_port = ntohs(nar->in_port);
92 resubmit->table_id = nar->table;
97 output_reg_from_openflow(const struct nx_action_output_reg *naor,
100 struct ofpact_output_reg *output_reg;
102 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
103 return OFPERR_OFPBAC_BAD_ARGUMENT;
106 output_reg = ofpact_put_OUTPUT_REG(out);
107 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
108 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
109 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
110 output_reg->max_len = ntohs(naor->max_len);
112 return mf_check_src(&output_reg->src, NULL);
116 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
119 struct ofpact_fin_timeout *oft;
121 oft = ofpact_put_FIN_TIMEOUT(out);
122 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
123 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
127 controller_from_openflow(const struct nx_action_controller *nac,
130 struct ofpact_controller *oc;
132 oc = ofpact_put_CONTROLLER(out);
133 oc->max_len = ntohs(nac->max_len);
134 oc->controller_id = ntohs(nac->controller_id);
135 oc->reason = nac->reason;
139 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
141 struct ofpact_note *note;
144 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
145 note = ofpact_put(out, OFPACT_NOTE,
146 offsetof(struct ofpact_note, data) + length);
147 note->length = length;
148 memcpy(note->data, nan->note, length);
152 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
154 const struct nx_action_header *nah = (const struct nx_action_header *) a;
155 uint16_t len = ntohs(a->header.len);
157 if (len < sizeof(struct nx_action_header)) {
158 return OFPERR_OFPBAC_BAD_LEN;
159 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
160 return OFPERR_OFPBAC_BAD_VENDOR;
163 switch (nah->subtype) {
164 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
165 case CONSTANT_HTONS(ENUM): \
167 ? len >= sizeof(struct STRUCT) \
168 : len == sizeof(struct STRUCT)) { \
169 *code = OFPUTIL_##ENUM; \
172 return OFPERR_OFPBAC_BAD_LEN; \
175 #include "ofp-util.def"
177 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
178 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
180 return OFPERR_OFPBAC_BAD_TYPE;
184 /* Parses 'a' to determine its type. On success stores the correct type into
185 * '*code' and returns 0. On failure returns an OFPERR_* error code and
186 * '*code' is indeterminate.
188 * The caller must have already verified that 'a''s length is potentially
189 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
190 * ofp_action) and no longer than the amount of space allocated to 'a').
192 * This function verifies that 'a''s length is correct for the type of action
193 * that it represents. */
195 decode_openflow10_action(const union ofp_action *a,
196 enum ofputil_action_code *code)
199 case CONSTANT_HTONS(OFPAT10_VENDOR):
200 return decode_nxast_action(a, code);
202 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
203 case CONSTANT_HTONS(ENUM): \
204 if (a->header.len == htons(sizeof(struct STRUCT))) { \
205 *code = OFPUTIL_##ENUM; \
208 return OFPERR_OFPBAC_BAD_LEN; \
211 #include "ofp-util.def"
214 return OFPERR_OFPBAC_BAD_TYPE;
219 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
222 const struct nx_action_resubmit *nar;
223 const struct nx_action_set_tunnel *nast;
224 const struct nx_action_set_queue *nasq;
225 const struct nx_action_note *nan;
226 const struct nx_action_set_tunnel64 *nast64;
227 struct ofpact_tunnel *tunnel;
228 enum ofperr error = 0;
231 case OFPUTIL_ACTION_INVALID:
232 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
233 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
234 #include "ofp-util.def"
237 case OFPUTIL_NXAST_RESUBMIT:
238 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
241 case OFPUTIL_NXAST_SET_TUNNEL:
242 nast = (const struct nx_action_set_tunnel *) a;
243 tunnel = ofpact_put_SET_TUNNEL(out);
244 tunnel->ofpact.compat = code;
245 tunnel->tun_id = ntohl(nast->tun_id);
248 case OFPUTIL_NXAST_SET_QUEUE:
249 nasq = (const struct nx_action_set_queue *) a;
250 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
253 case OFPUTIL_NXAST_POP_QUEUE:
254 ofpact_put_POP_QUEUE(out);
257 case OFPUTIL_NXAST_REG_MOVE:
258 error = nxm_reg_move_from_openflow(
259 (const struct nx_action_reg_move *) a, out);
262 case OFPUTIL_NXAST_REG_LOAD:
263 error = nxm_reg_load_from_openflow(
264 (const struct nx_action_reg_load *) a, out);
267 case OFPUTIL_NXAST_NOTE:
268 nan = (const struct nx_action_note *) a;
269 note_from_openflow(nan, out);
272 case OFPUTIL_NXAST_SET_TUNNEL64:
273 nast64 = (const struct nx_action_set_tunnel64 *) a;
274 tunnel = ofpact_put_SET_TUNNEL(out);
275 tunnel->ofpact.compat = code;
276 tunnel->tun_id = ntohll(nast64->tun_id);
279 case OFPUTIL_NXAST_MULTIPATH:
280 error = multipath_from_openflow((const struct nx_action_multipath *) a,
281 ofpact_put_MULTIPATH(out));
284 case OFPUTIL_NXAST_AUTOPATH:
285 error = autopath_from_openflow((const struct nx_action_autopath *) a,
286 ofpact_put_AUTOPATH(out));
289 case OFPUTIL_NXAST_BUNDLE:
290 case OFPUTIL_NXAST_BUNDLE_LOAD:
291 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
294 case OFPUTIL_NXAST_OUTPUT_REG:
295 error = output_reg_from_openflow(
296 (const struct nx_action_output_reg *) a, out);
299 case OFPUTIL_NXAST_RESUBMIT_TABLE:
300 nar = (const struct nx_action_resubmit *) a;
301 error = resubmit_table_from_openflow(nar, out);
304 case OFPUTIL_NXAST_LEARN:
305 error = learn_from_openflow((const struct nx_action_learn *) a, out);
308 case OFPUTIL_NXAST_EXIT:
309 ofpact_put_EXIT(out);
312 case OFPUTIL_NXAST_DEC_TTL:
313 ofpact_put_DEC_TTL(out);
316 case OFPUTIL_NXAST_FIN_TIMEOUT:
317 fin_timeout_from_openflow(
318 (const struct nx_action_fin_timeout *) a, out);
321 case OFPUTIL_NXAST_CONTROLLER:
322 controller_from_openflow((const struct nx_action_controller *) a, out);
330 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
332 enum ofputil_action_code code;
335 error = decode_openflow10_action(a, &code);
341 case OFPUTIL_ACTION_INVALID:
342 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
343 #include "ofp-util.def"
346 case OFPUTIL_OFPAT10_OUTPUT:
347 return output_from_openflow10(&a->output10, out);
349 case OFPUTIL_OFPAT10_SET_VLAN_VID:
350 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
351 return OFPERR_OFPBAC_BAD_ARGUMENT;
353 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
356 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
357 if (a->vlan_pcp.vlan_pcp & ~7) {
358 return OFPERR_OFPBAC_BAD_ARGUMENT;
360 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
363 case OFPUTIL_OFPAT10_STRIP_VLAN:
364 ofpact_put_STRIP_VLAN(out);
367 case OFPUTIL_OFPAT10_SET_DL_SRC:
368 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
369 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
372 case OFPUTIL_OFPAT10_SET_DL_DST:
373 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
374 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
377 case OFPUTIL_OFPAT10_SET_NW_SRC:
378 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
381 case OFPUTIL_OFPAT10_SET_NW_DST:
382 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
385 case OFPUTIL_OFPAT10_SET_NW_TOS:
386 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
387 return OFPERR_OFPBAC_BAD_ARGUMENT;
389 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
392 case OFPUTIL_OFPAT10_SET_TP_SRC:
393 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
396 case OFPUTIL_OFPAT10_SET_TP_DST:
397 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
401 case OFPUTIL_OFPAT10_ENQUEUE:
402 error = enqueue_from_openflow10((const struct ofp_action_enqueue *) a,
406 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
407 #include "ofp-util.def"
408 return ofpact_from_nxast(a, code, out);
414 static inline union ofp_action *
415 action_next(const union ofp_action *a)
417 return ((union ofp_action *) (void *)
418 ((uint8_t *) a + ntohs(a->header.len)));
422 action_is_valid(const union ofp_action *a, size_t n_actions)
424 uint16_t len = ntohs(a->header.len);
425 return (!(len % OFP_ACTION_ALIGN)
427 && len / sizeof *a <= n_actions);
430 /* This macro is careful to check for actions with bad lengths. */
431 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
432 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
433 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
434 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
435 (ITER) = action_next(ITER)))
438 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
441 if (!VLOG_DROP_WARN(&rl)) {
445 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
446 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
447 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
453 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
455 enum ofperr (*ofpact_from_openflow)(
456 const union ofp_action *a, struct ofpbuf *out))
458 const union ofp_action *a;
461 ACTION_FOR_EACH (a, left, in, n_in) {
462 enum ofperr error = ofpact_from_openflow(a, out);
464 log_bad_action(in, n_in, a - in, error);
469 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
470 log_bad_action(in, n_in, n_in - left, error);
479 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
482 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
486 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
487 struct ofpbuf *ofpacts,
488 enum ofperr (*translate)(const union ofp_action *actions,
490 struct ofpbuf *ofpacts))
492 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
493 const union ofp_action *actions;
496 ofpbuf_clear(ofpacts);
498 if (actions_len % OFP_ACTION_ALIGN != 0) {
499 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
500 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
501 return OFPERR_OFPBRC_BAD_LEN;
504 actions = ofpbuf_try_pull(openflow, actions_len);
505 if (actions == NULL) {
506 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
507 "remaining message length (%zu)",
508 actions_len, openflow->size);
509 return OFPERR_OFPBRC_BAD_LEN;
512 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
514 ofpbuf_clear(ofpacts);
519 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
520 * front of 'openflow' into ofpacts. On success, replaces any existing content
521 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
522 * Returns 0 if successful, otherwise an OpenFlow error.
524 * The parsed actions are valid generically, but they may not be valid in a
525 * specific context. For example, port numbers up to OFPP_MAX are valid
526 * generically, but specific datapaths may only support port numbers in a
527 * smaller range. Use ofpacts_check() to additional check whether actions are
528 * valid in a specific context. */
530 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
531 struct ofpbuf *ofpacts)
533 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
534 ofpacts_from_openflow10);
537 /* OpenFlow 1.1 actions. */
539 /* Parses 'a' to determine its type. On success stores the correct type into
540 * '*code' and returns 0. On failure returns an OFPERR_* error code and
541 * '*code' is indeterminate.
543 * The caller must have already verified that 'a''s length is potentially
544 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
545 * ofp_action) and no longer than the amount of space allocated to 'a').
547 * This function verifies that 'a''s length is correct for the type of action
548 * that it represents. */
550 decode_openflow11_action(const union ofp_action *a,
551 enum ofputil_action_code *code)
554 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
555 return decode_nxast_action(a, code);
557 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) \
558 case CONSTANT_HTONS(ENUM): \
559 if (a->header.len == htons(sizeof(struct STRUCT))) { \
560 *code = OFPUTIL_##ENUM; \
563 return OFPERR_OFPBAC_BAD_LEN; \
566 #include "ofp-util.def"
569 return OFPERR_OFPBAC_BAD_TYPE;
574 output_from_openflow11(const struct ofp11_action_output *oao,
577 struct ofpact_output *output;
580 output = ofpact_put_OUTPUT(out);
581 output->max_len = ntohs(oao->max_len);
583 error = ofputil_port_from_ofp11(oao->port, &output->port);
588 return ofputil_check_output_port(output->port, OFPP_MAX);
592 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
594 enum ofputil_action_code code;
597 error = decode_openflow11_action(a, &code);
603 case OFPUTIL_ACTION_INVALID:
604 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
605 #include "ofp-util.def"
608 case OFPUTIL_OFPAT11_OUTPUT:
609 return output_from_openflow11((const struct ofp11_action_output *) a,
612 case OFPUTIL_OFPAT11_SET_VLAN_VID:
613 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
614 return OFPERR_OFPBAC_BAD_ARGUMENT;
616 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
619 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
620 if (a->vlan_pcp.vlan_pcp & ~7) {
621 return OFPERR_OFPBAC_BAD_ARGUMENT;
623 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
626 case OFPUTIL_OFPAT11_SET_DL_SRC:
627 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
628 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
631 case OFPUTIL_OFPAT11_SET_DL_DST:
632 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
633 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
636 case OFPUTIL_OFPAT11_SET_NW_SRC:
637 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
640 case OFPUTIL_OFPAT11_SET_NW_DST:
641 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
644 case OFPUTIL_OFPAT11_SET_NW_TOS:
645 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
646 return OFPERR_OFPBAC_BAD_ARGUMENT;
648 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
651 case OFPUTIL_OFPAT11_SET_TP_SRC:
652 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
655 case OFPUTIL_OFPAT11_SET_TP_DST:
656 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
659 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
660 #include "ofp-util.def"
661 return ofpact_from_nxast(a, code, out);
668 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
671 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
674 /* OpenFlow 1.1 instructions. */
676 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
677 static inline const struct STRUCT * \
678 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
680 assert(inst->type == htons(ENUM)); \
681 return (struct STRUCT *)inst; \
685 instruction_init_##ENUM(struct STRUCT *s) \
687 memset(s, 0, sizeof *s); \
688 s->type = htons(ENUM); \
689 s->len = htons(sizeof *s); \
692 static inline struct STRUCT * \
693 instruction_put_##ENUM(struct ofpbuf *buf) \
695 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
696 instruction_init_##ENUM(s); \
702 struct instruction_type_info {
703 enum ovs_instruction_type type;
707 static const struct instruction_type_info inst_info[] = {
708 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
714 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
716 const struct instruction_type_info *p;
717 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
718 if (p->type == type) {
726 ofpact_instruction_type_from_name(const char *name)
728 const struct instruction_type_info *p;
729 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
730 if (!strcasecmp(name, p->name)) {
737 static inline struct ofp11_instruction *
738 instruction_next(const struct ofp11_instruction *inst)
740 return ((struct ofp11_instruction *) (void *)
741 ((uint8_t *) inst + ntohs(inst->len)));
745 instruction_is_valid(const struct ofp11_instruction *inst,
746 size_t n_instructions)
748 uint16_t len = ntohs(inst->len);
749 return (!(len % OFP11_INSTRUCTION_ALIGN)
750 && len >= sizeof *inst
751 && len / sizeof *inst <= n_instructions);
754 /* This macro is careful to check for instructions with bad lengths. */
755 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
756 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
757 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
758 ((LEFT) -= (ntohs((ITER)->len) \
759 / sizeof(struct ofp11_instruction)), \
760 (ITER) = instruction_next(ITER)))
763 decode_openflow11_instruction(const struct ofp11_instruction *inst,
764 enum ovs_instruction_type *type)
766 uint16_t len = ntohs(inst->len);
768 switch (inst->type) {
769 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
770 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
772 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
773 case CONSTANT_HTONS(ENUM): \
775 ? len >= sizeof(struct STRUCT) \
776 : len == sizeof(struct STRUCT)) { \
777 *type = OVSINST_##ENUM; \
780 return OFPERR_OFPBIC_BAD_LEN; \
786 return OFPERR_OFPBIC_UNKNOWN_INST;
791 decode_openflow11_instructions(const struct ofp11_instruction insts[],
793 const struct ofp11_instruction *out[])
795 const struct ofp11_instruction *inst;
798 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
799 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
800 enum ovs_instruction_type type;
803 error = decode_openflow11_instruction(inst, &type);
809 return OFPERR_NXBIC_DUP_TYPE;
815 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
816 (n_insts - left) * sizeof *inst);
817 return OFPERR_OFPBIC_BAD_LEN;
823 get_actions_from_instruction(const struct ofp11_instruction *inst,
824 const union ofp_action **actions,
827 *actions = (const union ofp_action *) (inst + 1);
828 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
831 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
832 * front of 'openflow' into ofpacts. On success, replaces any existing content
833 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
834 * Returns 0 if successful, otherwise an OpenFlow error.
836 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
837 * instructions, so you should call ofpacts_pull_openflow11_instructions()
838 * instead of this function.
840 * The parsed actions are valid generically, but they may not be valid in a
841 * specific context. For example, port numbers up to OFPP_MAX are valid
842 * generically, but specific datapaths may only support port numbers in a
843 * smaller range. Use ofpacts_check() to additional check whether actions are
844 * valid in a specific context. */
846 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
847 unsigned int actions_len,
848 struct ofpbuf *ofpacts)
850 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
851 ofpacts_from_openflow11);
855 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
856 unsigned int instructions_len,
857 struct ofpbuf *ofpacts)
859 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
860 const struct ofp11_instruction *instructions;
861 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
864 ofpbuf_clear(ofpacts);
866 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
867 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
869 instructions_len, OFP11_INSTRUCTION_ALIGN);
870 error = OFPERR_OFPBIC_BAD_LEN;
874 instructions = ofpbuf_try_pull(openflow, instructions_len);
875 if (instructions == NULL) {
876 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
877 "remaining message length (%zu)",
878 instructions_len, openflow->size);
879 error = OFPERR_OFPBIC_BAD_LEN;
883 error = decode_openflow11_instructions(
884 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
890 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
891 const union ofp_action *actions;
894 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
895 &actions, &n_actions);
896 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
902 if (insts[OVSINST_OFPIT11_GOTO_TABLE] ||
903 insts[OVSINST_OFPIT11_WRITE_METADATA] ||
904 insts[OVSINST_OFPIT11_WRITE_ACTIONS] ||
905 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
906 error = OFPERR_OFPBIC_UNSUP_INST;
912 ofpbuf_clear(ofpacts);
918 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
920 const struct ofpact_enqueue *enqueue;
924 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
927 case OFPACT_CONTROLLER:
931 enqueue = ofpact_get_ENQUEUE(a);
932 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
933 && enqueue->port != OFPP_LOCAL) {
934 return OFPERR_OFPBAC_BAD_OUT_PORT;
938 case OFPACT_OUTPUT_REG:
939 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
942 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
944 case OFPACT_SET_VLAN_VID:
945 case OFPACT_SET_VLAN_PCP:
946 case OFPACT_STRIP_VLAN:
947 case OFPACT_SET_ETH_SRC:
948 case OFPACT_SET_ETH_DST:
949 case OFPACT_SET_IPV4_SRC:
950 case OFPACT_SET_IPV4_DST:
951 case OFPACT_SET_IPV4_DSCP:
952 case OFPACT_SET_L4_SRC_PORT:
953 case OFPACT_SET_L4_DST_PORT:
956 case OFPACT_REG_MOVE:
957 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
959 case OFPACT_REG_LOAD:
960 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
963 case OFPACT_SET_TUNNEL:
964 case OFPACT_SET_QUEUE:
965 case OFPACT_POP_QUEUE:
966 case OFPACT_FIN_TIMEOUT:
967 case OFPACT_RESUBMIT:
971 return learn_check(ofpact_get_LEARN(a), flow);
973 case OFPACT_MULTIPATH:
974 return multipath_check(ofpact_get_MULTIPATH(a), flow);
976 case OFPACT_AUTOPATH:
977 return autopath_check(ofpact_get_AUTOPATH(a), flow);
988 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
989 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
990 * switch with no more than 'max_ports' ports. */
992 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
993 const struct flow *flow, int max_ports)
995 const struct ofpact *a;
997 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
998 enum ofperr error = ofpact_check__(a, flow, max_ports);
1007 /* Converting ofpacts to Nicira OpenFlow extensions. */
1010 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1013 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1015 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1016 output_reg->src.n_bits);
1017 naor->src = htonl(output_reg->src.field->nxm_header);
1018 naor->max_len = htons(output_reg->max_len);
1022 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1025 struct nx_action_resubmit *nar;
1027 if (resubmit->table_id == 0xff
1028 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1029 nar = ofputil_put_NXAST_RESUBMIT(out);
1031 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1032 nar->table = resubmit->table_id;
1034 nar->in_port = htons(resubmit->in_port);
1038 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1041 uint64_t tun_id = tunnel->tun_id;
1043 if (tun_id <= UINT32_MAX
1044 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1045 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1047 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1052 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1054 size_t start_ofs = out->size;
1055 struct nx_action_note *nan;
1056 unsigned int remainder;
1059 nan = ofputil_put_NXAST_NOTE(out);
1060 out->size -= sizeof nan->note;
1062 ofpbuf_put(out, note->data, note->length);
1064 len = out->size - start_ofs;
1065 remainder = len % OFP_ACTION_ALIGN;
1067 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1069 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1070 nan->len = htons(out->size - start_ofs);
1074 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1077 struct nx_action_controller *nac;
1079 nac = ofputil_put_NXAST_CONTROLLER(out);
1080 nac->max_len = htons(oc->max_len);
1081 nac->controller_id = htons(oc->controller_id);
1082 nac->reason = oc->reason;
1086 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1089 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1090 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1091 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1095 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1098 case OFPACT_CONTROLLER:
1099 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1102 case OFPACT_OUTPUT_REG:
1103 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1107 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1110 case OFPACT_REG_MOVE:
1111 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1114 case OFPACT_REG_LOAD:
1115 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1118 case OFPACT_DEC_TTL:
1119 ofputil_put_NXAST_DEC_TTL(out);
1122 case OFPACT_SET_TUNNEL:
1123 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1126 case OFPACT_SET_QUEUE:
1127 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1128 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1131 case OFPACT_POP_QUEUE:
1132 ofputil_put_NXAST_POP_QUEUE(out);
1135 case OFPACT_FIN_TIMEOUT:
1136 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1139 case OFPACT_RESUBMIT:
1140 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1144 learn_to_nxast(ofpact_get_LEARN(a), out);
1147 case OFPACT_MULTIPATH:
1148 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1151 case OFPACT_AUTOPATH:
1152 autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1156 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1160 ofputil_put_NXAST_EXIT(out);
1164 case OFPACT_ENQUEUE:
1165 case OFPACT_SET_VLAN_VID:
1166 case OFPACT_SET_VLAN_PCP:
1167 case OFPACT_STRIP_VLAN:
1168 case OFPACT_SET_ETH_SRC:
1169 case OFPACT_SET_ETH_DST:
1170 case OFPACT_SET_IPV4_SRC:
1171 case OFPACT_SET_IPV4_DST:
1172 case OFPACT_SET_IPV4_DSCP:
1173 case OFPACT_SET_L4_SRC_PORT:
1174 case OFPACT_SET_L4_DST_PORT:
1179 /* Converting ofpacts to OpenFlow 1.0. */
1182 ofpact_output_to_openflow10(const struct ofpact_output *output,
1185 struct ofp10_action_output *oao;
1187 oao = ofputil_put_OFPAT10_OUTPUT(out);
1188 oao->port = htons(output->port);
1189 oao->max_len = htons(output->max_len);
1193 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1196 struct ofp_action_enqueue *oae;
1198 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1199 oae->port = htons(enqueue->port);
1200 oae->queue_id = htonl(enqueue->queue);
1204 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1208 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1211 case OFPACT_ENQUEUE:
1212 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1215 case OFPACT_SET_VLAN_VID:
1216 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1217 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1220 case OFPACT_SET_VLAN_PCP:
1221 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1222 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1225 case OFPACT_STRIP_VLAN:
1226 ofputil_put_OFPAT10_STRIP_VLAN(out);
1229 case OFPACT_SET_ETH_SRC:
1230 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1231 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1234 case OFPACT_SET_ETH_DST:
1235 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1236 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1239 case OFPACT_SET_IPV4_SRC:
1240 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1241 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1244 case OFPACT_SET_IPV4_DST:
1245 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1246 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1249 case OFPACT_SET_IPV4_DSCP:
1250 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1251 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1254 case OFPACT_SET_L4_SRC_PORT:
1255 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1256 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1259 case OFPACT_SET_L4_DST_PORT:
1260 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1261 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1264 case OFPACT_CONTROLLER:
1265 case OFPACT_OUTPUT_REG:
1267 case OFPACT_REG_MOVE:
1268 case OFPACT_REG_LOAD:
1269 case OFPACT_DEC_TTL:
1270 case OFPACT_SET_TUNNEL:
1271 case OFPACT_SET_QUEUE:
1272 case OFPACT_POP_QUEUE:
1273 case OFPACT_FIN_TIMEOUT:
1274 case OFPACT_RESUBMIT:
1276 case OFPACT_MULTIPATH:
1277 case OFPACT_AUTOPATH:
1280 ofpact_to_nxast(a, out);
1285 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1286 * actions in 'openflow', appending the actions to any existing data in
1289 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1290 struct ofpbuf *openflow)
1292 const struct ofpact *a;
1294 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1295 ofpact_to_openflow10(a, openflow);
1299 /* Converting ofpacts to OpenFlow 1.1. */
1302 ofpact_output_to_openflow11(const struct ofpact_output *output,
1305 struct ofp11_action_output *oao;
1307 oao = ofputil_put_OFPAT11_OUTPUT(out);
1308 oao->port = ofputil_port_to_ofp11(output->port);
1309 oao->max_len = htons(output->max_len);
1313 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1317 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1319 case OFPACT_ENQUEUE:
1323 case OFPACT_SET_VLAN_VID:
1324 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1325 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1328 case OFPACT_SET_VLAN_PCP:
1329 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1330 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1333 case OFPACT_STRIP_VLAN:
1337 case OFPACT_SET_ETH_SRC:
1338 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1339 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1342 case OFPACT_SET_ETH_DST:
1343 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1344 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1347 case OFPACT_SET_IPV4_SRC:
1348 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1349 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1352 case OFPACT_SET_IPV4_DST:
1353 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1354 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1357 case OFPACT_SET_IPV4_DSCP:
1358 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1359 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1362 case OFPACT_SET_L4_SRC_PORT:
1363 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1364 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1367 case OFPACT_SET_L4_DST_PORT:
1368 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1369 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1372 case OFPACT_CONTROLLER:
1373 case OFPACT_OUTPUT_REG:
1375 case OFPACT_REG_MOVE:
1376 case OFPACT_REG_LOAD:
1377 case OFPACT_DEC_TTL:
1378 case OFPACT_SET_TUNNEL:
1379 case OFPACT_SET_QUEUE:
1380 case OFPACT_POP_QUEUE:
1381 case OFPACT_FIN_TIMEOUT:
1382 case OFPACT_RESUBMIT:
1384 case OFPACT_MULTIPATH:
1385 case OFPACT_AUTOPATH:
1388 ofpact_to_nxast(a, out);
1393 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1394 * 1.1 actions in 'openflow', appending the actions to any existing data in
1397 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1398 size_t ofpacts_len, struct ofpbuf *openflow)
1400 const struct ofpact *a;
1401 size_t start_size = openflow->size;
1403 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1404 ofpact_to_openflow11(a, openflow);
1407 return openflow->size - start_size;
1411 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1413 struct ofpbuf *openflow)
1415 struct ofp11_instruction_actions *oia;
1418 /* Put an OFPIT11_APPLY_ACTIONS instruction and fill it in. */
1419 ofs = openflow->size;
1420 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1421 ofpacts_put_openflow11_actions(ofpacts, ofpacts_len, openflow);
1423 /* Update the instruction's length (or, if it's empty, delete it). */
1424 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1425 if (openflow->size > ofs + sizeof *oia) {
1426 oia->len = htons(openflow->size - ofs);
1428 openflow->size = ofs;
1432 /* Returns true if 'action' outputs to 'port', false otherwise. */
1434 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1436 switch (ofpact->type) {
1438 return ofpact_get_OUTPUT(ofpact)->port == port;
1439 case OFPACT_ENQUEUE:
1440 return ofpact_get_ENQUEUE(ofpact)->port == port;
1441 case OFPACT_CONTROLLER:
1442 return port == OFPP_CONTROLLER;
1444 case OFPACT_OUTPUT_REG:
1446 case OFPACT_SET_VLAN_VID:
1447 case OFPACT_SET_VLAN_PCP:
1448 case OFPACT_STRIP_VLAN:
1449 case OFPACT_SET_ETH_SRC:
1450 case OFPACT_SET_ETH_DST:
1451 case OFPACT_SET_IPV4_SRC:
1452 case OFPACT_SET_IPV4_DST:
1453 case OFPACT_SET_IPV4_DSCP:
1454 case OFPACT_SET_L4_SRC_PORT:
1455 case OFPACT_SET_L4_DST_PORT:
1456 case OFPACT_REG_MOVE:
1457 case OFPACT_REG_LOAD:
1458 case OFPACT_DEC_TTL:
1459 case OFPACT_SET_TUNNEL:
1460 case OFPACT_SET_QUEUE:
1461 case OFPACT_POP_QUEUE:
1462 case OFPACT_FIN_TIMEOUT:
1463 case OFPACT_RESUBMIT:
1465 case OFPACT_MULTIPATH:
1466 case OFPACT_AUTOPATH:
1474 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1475 * to 'port', false otherwise. */
1477 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1480 const struct ofpact *a;
1482 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1483 if (ofpact_outputs_to_port(a, port)) {
1492 ofpacts_equal(const struct ofpact *a, size_t a_len,
1493 const struct ofpact *b, size_t b_len)
1495 return a_len == b_len && !memcmp(a, b, a_len);
1498 /* Formatting ofpacts. */
1501 print_note(const struct ofpact_note *note, struct ds *string)
1505 ds_put_cstr(string, "note:");
1506 for (i = 0; i < note->length; i++) {
1508 ds_put_char(string, '.');
1510 ds_put_format(string, "%02"PRIx8, note->data[i]);
1515 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1518 ds_put_cstr(s, "fin_timeout(");
1519 if (fin_timeout->fin_idle_timeout) {
1520 ds_put_format(s, "idle_timeout=%"PRIu16",",
1521 fin_timeout->fin_idle_timeout);
1523 if (fin_timeout->fin_hard_timeout) {
1524 ds_put_format(s, "hard_timeout=%"PRIu16",",
1525 fin_timeout->fin_hard_timeout);
1528 ds_put_char(s, ')');
1532 ofpact_format(const struct ofpact *a, struct ds *s)
1534 const struct ofpact_enqueue *enqueue;
1535 const struct ofpact_resubmit *resubmit;
1536 const struct ofpact_autopath *autopath;
1537 const struct ofpact_controller *controller;
1538 const struct ofpact_tunnel *tunnel;
1543 port = ofpact_get_OUTPUT(a)->port;
1544 if (port < OFPP_MAX) {
1545 ds_put_format(s, "output:%"PRIu16, port);
1547 ofputil_format_port(port, s);
1548 if (port == OFPP_CONTROLLER) {
1549 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1554 case OFPACT_CONTROLLER:
1555 controller = ofpact_get_CONTROLLER(a);
1556 if (controller->reason == OFPR_ACTION &&
1557 controller->controller_id == 0) {
1558 ds_put_format(s, "CONTROLLER:%"PRIu16,
1559 ofpact_get_CONTROLLER(a)->max_len);
1561 enum ofp_packet_in_reason reason = controller->reason;
1563 ds_put_cstr(s, "controller(");
1564 if (reason != OFPR_ACTION) {
1565 ds_put_format(s, "reason=%s,",
1566 ofputil_packet_in_reason_to_string(reason));
1568 if (controller->max_len != UINT16_MAX) {
1569 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1571 if (controller->controller_id != 0) {
1572 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1575 ds_put_char(s, ')');
1579 case OFPACT_ENQUEUE:
1580 enqueue = ofpact_get_ENQUEUE(a);
1581 ds_put_format(s, "enqueue:");
1582 ofputil_format_port(enqueue->port, s);
1583 ds_put_format(s, "q%"PRIu32, enqueue->queue);
1586 case OFPACT_OUTPUT_REG:
1587 ds_put_cstr(s, "output:");
1588 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1592 bundle_format(ofpact_get_BUNDLE(a), s);
1595 case OFPACT_SET_VLAN_VID:
1596 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1597 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1600 case OFPACT_SET_VLAN_PCP:
1601 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1602 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1605 case OFPACT_STRIP_VLAN:
1606 ds_put_cstr(s, "strip_vlan");
1609 case OFPACT_SET_ETH_SRC:
1610 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1611 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1614 case OFPACT_SET_ETH_DST:
1615 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1616 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1619 case OFPACT_SET_IPV4_SRC:
1620 ds_put_format(s, "mod_nw_src:"IP_FMT,
1621 IP_ARGS(&ofpact_get_SET_IPV4_SRC(a)->ipv4));
1624 case OFPACT_SET_IPV4_DST:
1625 ds_put_format(s, "mod_nw_dst:"IP_FMT,
1626 IP_ARGS(&ofpact_get_SET_IPV4_DST(a)->ipv4));
1629 case OFPACT_SET_IPV4_DSCP:
1630 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1633 case OFPACT_SET_L4_SRC_PORT:
1634 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1637 case OFPACT_SET_L4_DST_PORT:
1638 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1641 case OFPACT_REG_MOVE:
1642 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1645 case OFPACT_REG_LOAD:
1646 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1649 case OFPACT_DEC_TTL:
1650 ds_put_cstr(s, "dec_ttl");
1653 case OFPACT_SET_TUNNEL:
1654 tunnel = ofpact_get_SET_TUNNEL(a);
1655 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1656 (tunnel->tun_id > UINT32_MAX
1657 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1661 case OFPACT_SET_QUEUE:
1662 ds_put_format(s, "set_queue:%"PRIu32,
1663 ofpact_get_SET_QUEUE(a)->queue_id);
1666 case OFPACT_POP_QUEUE:
1667 ds_put_cstr(s, "pop_queue");
1670 case OFPACT_FIN_TIMEOUT:
1671 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
1674 case OFPACT_RESUBMIT:
1675 resubmit = ofpact_get_RESUBMIT(a);
1676 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
1677 ds_put_format(s, "resubmit:%"PRIu16, resubmit->in_port);
1679 ds_put_format(s, "resubmit(");
1680 if (resubmit->in_port != OFPP_IN_PORT) {
1681 ofputil_format_port(resubmit->in_port, s);
1683 ds_put_char(s, ',');
1684 if (resubmit->table_id != 255) {
1685 ds_put_format(s, "%"PRIu8, resubmit->table_id);
1687 ds_put_char(s, ')');
1692 learn_format(ofpact_get_LEARN(a), s);
1695 case OFPACT_MULTIPATH:
1696 multipath_format(ofpact_get_MULTIPATH(a), s);
1699 case OFPACT_AUTOPATH:
1700 autopath = ofpact_get_AUTOPATH(a);
1701 ds_put_format(s, "autopath(%u,", autopath->port);
1702 mf_format_subfield(&autopath->dst, s);
1703 ds_put_char(s, ')');
1707 print_note(ofpact_get_NOTE(a), s);
1711 ds_put_cstr(s, "exit");
1716 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
1717 * 'ofpacts' to 'string'. */
1719 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
1722 ds_put_cstr(string, "actions=");
1724 ds_put_cstr(string, "drop");
1726 const struct ofpact *a;
1728 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1730 ds_put_cstr(string, ",");
1732 ofpact_format(a, string);
1737 /* Internal use by helpers. */
1740 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
1742 struct ofpact *ofpact;
1744 ofpact_pad(ofpacts);
1745 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
1746 ofpact_init(ofpact, type, len);
1751 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
1753 memset(ofpact, 0, len);
1754 ofpact->type = type;
1755 ofpact->compat = OFPUTIL_ACTION_INVALID;
1759 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
1760 * starting at 'ofpact'.
1762 * This is the correct way to update a variable-length ofpact's length after
1763 * adding the variable-length part of the payload. (See the large comment
1764 * near the end of ofp-actions.h for more information.) */
1766 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
1768 assert(ofpact == ofpacts->l2);
1769 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
1772 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
1773 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
1774 * client must call this itself after adding the final ofpact to an array of
1777 * (The consequences of failing to call this function are probably not dire.
1778 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
1779 * not dereference it. That's undefined behavior, technically, but it will not
1780 * cause a real problem on common systems. Still, it seems better to call
1783 ofpact_pad(struct ofpbuf *ofpacts)
1785 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
1787 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);