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 dec_ttl_from_openflow(struct ofpbuf *out)
155 struct ofpact_cnt_ids *ids;
156 enum ofperr error = 0;
158 ids = ofpact_put_DEC_TTL(out);
159 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL;
160 ids->n_controllers = 1;
161 ofpbuf_put(out, &id, sizeof id);
163 ofpact_update_len(out, &ids->ofpact);
168 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
171 struct ofpact_cnt_ids *ids;
175 ids = ofpact_put_DEC_TTL(out);
176 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
177 ids->n_controllers = ntohs(nac_ids->n_controllers);
178 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
180 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
181 return OFPERR_NXBRC_MUST_BE_ZERO;
184 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
185 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
186 "allocated for controller ids. %zu bytes are required for "
187 "%"PRIu16" controllers.", ids_size,
188 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
189 return OFPERR_OFPBAC_BAD_LEN;
192 for (i = 0; i < ids->n_controllers; i++) {
193 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
194 ofpbuf_put(out, &id, sizeof id);
198 ofpact_update_len(out, &ids->ofpact);
204 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
206 const struct nx_action_header *nah = (const struct nx_action_header *) a;
207 uint16_t len = ntohs(a->header.len);
209 if (len < sizeof(struct nx_action_header)) {
210 return OFPERR_OFPBAC_BAD_LEN;
211 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
212 return OFPERR_OFPBAC_BAD_VENDOR;
215 switch (nah->subtype) {
216 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
217 case CONSTANT_HTONS(ENUM): \
219 ? len >= sizeof(struct STRUCT) \
220 : len == sizeof(struct STRUCT)) { \
221 *code = OFPUTIL_##ENUM; \
224 return OFPERR_OFPBAC_BAD_LEN; \
227 #include "ofp-util.def"
229 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
230 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
232 return OFPERR_OFPBAC_BAD_TYPE;
236 /* Parses 'a' to determine its type. On success stores the correct type into
237 * '*code' and returns 0. On failure returns an OFPERR_* error code and
238 * '*code' is indeterminate.
240 * The caller must have already verified that 'a''s length is potentially
241 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
242 * ofp_action) and no longer than the amount of space allocated to 'a').
244 * This function verifies that 'a''s length is correct for the type of action
245 * that it represents. */
247 decode_openflow10_action(const union ofp_action *a,
248 enum ofputil_action_code *code)
251 case CONSTANT_HTONS(OFPAT10_VENDOR):
252 return decode_nxast_action(a, code);
254 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
255 case CONSTANT_HTONS(ENUM): \
256 if (a->header.len == htons(sizeof(struct STRUCT))) { \
257 *code = OFPUTIL_##ENUM; \
260 return OFPERR_OFPBAC_BAD_LEN; \
263 #include "ofp-util.def"
266 return OFPERR_OFPBAC_BAD_TYPE;
271 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
274 const struct nx_action_resubmit *nar;
275 const struct nx_action_set_tunnel *nast;
276 const struct nx_action_set_queue *nasq;
277 const struct nx_action_note *nan;
278 const struct nx_action_set_tunnel64 *nast64;
279 struct ofpact_tunnel *tunnel;
280 enum ofperr error = 0;
283 case OFPUTIL_ACTION_INVALID:
284 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
285 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
286 #include "ofp-util.def"
289 case OFPUTIL_NXAST_RESUBMIT:
290 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
293 case OFPUTIL_NXAST_SET_TUNNEL:
294 nast = (const struct nx_action_set_tunnel *) a;
295 tunnel = ofpact_put_SET_TUNNEL(out);
296 tunnel->ofpact.compat = code;
297 tunnel->tun_id = ntohl(nast->tun_id);
300 case OFPUTIL_NXAST_SET_QUEUE:
301 nasq = (const struct nx_action_set_queue *) a;
302 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
305 case OFPUTIL_NXAST_POP_QUEUE:
306 ofpact_put_POP_QUEUE(out);
309 case OFPUTIL_NXAST_REG_MOVE:
310 error = nxm_reg_move_from_openflow(
311 (const struct nx_action_reg_move *) a, out);
314 case OFPUTIL_NXAST_REG_LOAD:
315 error = nxm_reg_load_from_openflow(
316 (const struct nx_action_reg_load *) a, out);
319 case OFPUTIL_NXAST_NOTE:
320 nan = (const struct nx_action_note *) a;
321 note_from_openflow(nan, out);
324 case OFPUTIL_NXAST_SET_TUNNEL64:
325 nast64 = (const struct nx_action_set_tunnel64 *) a;
326 tunnel = ofpact_put_SET_TUNNEL(out);
327 tunnel->ofpact.compat = code;
328 tunnel->tun_id = ntohll(nast64->tun_id);
331 case OFPUTIL_NXAST_MULTIPATH:
332 error = multipath_from_openflow((const struct nx_action_multipath *) a,
333 ofpact_put_MULTIPATH(out));
336 case OFPUTIL_NXAST_AUTOPATH:
337 error = autopath_from_openflow((const struct nx_action_autopath *) a,
338 ofpact_put_AUTOPATH(out));
341 case OFPUTIL_NXAST_BUNDLE:
342 case OFPUTIL_NXAST_BUNDLE_LOAD:
343 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
346 case OFPUTIL_NXAST_OUTPUT_REG:
347 error = output_reg_from_openflow(
348 (const struct nx_action_output_reg *) a, out);
351 case OFPUTIL_NXAST_RESUBMIT_TABLE:
352 nar = (const struct nx_action_resubmit *) a;
353 error = resubmit_table_from_openflow(nar, out);
356 case OFPUTIL_NXAST_LEARN:
357 error = learn_from_openflow((const struct nx_action_learn *) a, out);
360 case OFPUTIL_NXAST_EXIT:
361 ofpact_put_EXIT(out);
364 case OFPUTIL_NXAST_DEC_TTL:
365 error = dec_ttl_from_openflow(out);
368 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
369 error = dec_ttl_cnt_ids_from_openflow(
370 (const struct nx_action_cnt_ids *) a, out);
373 case OFPUTIL_NXAST_FIN_TIMEOUT:
374 fin_timeout_from_openflow(
375 (const struct nx_action_fin_timeout *) a, out);
378 case OFPUTIL_NXAST_CONTROLLER:
379 controller_from_openflow((const struct nx_action_controller *) a, out);
387 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
389 enum ofputil_action_code code;
392 error = decode_openflow10_action(a, &code);
398 case OFPUTIL_ACTION_INVALID:
399 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
400 #include "ofp-util.def"
403 case OFPUTIL_OFPAT10_OUTPUT:
404 return output_from_openflow10(&a->output10, out);
406 case OFPUTIL_OFPAT10_SET_VLAN_VID:
407 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
408 return OFPERR_OFPBAC_BAD_ARGUMENT;
410 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
413 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
414 if (a->vlan_pcp.vlan_pcp & ~7) {
415 return OFPERR_OFPBAC_BAD_ARGUMENT;
417 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
420 case OFPUTIL_OFPAT10_STRIP_VLAN:
421 ofpact_put_STRIP_VLAN(out);
424 case OFPUTIL_OFPAT10_SET_DL_SRC:
425 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
426 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
429 case OFPUTIL_OFPAT10_SET_DL_DST:
430 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
431 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
434 case OFPUTIL_OFPAT10_SET_NW_SRC:
435 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
438 case OFPUTIL_OFPAT10_SET_NW_DST:
439 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
442 case OFPUTIL_OFPAT10_SET_NW_TOS:
443 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
444 return OFPERR_OFPBAC_BAD_ARGUMENT;
446 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
449 case OFPUTIL_OFPAT10_SET_TP_SRC:
450 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
453 case OFPUTIL_OFPAT10_SET_TP_DST:
454 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
458 case OFPUTIL_OFPAT10_ENQUEUE:
459 error = enqueue_from_openflow10((const struct ofp_action_enqueue *) a,
463 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
464 #include "ofp-util.def"
465 return ofpact_from_nxast(a, code, out);
471 static inline union ofp_action *
472 action_next(const union ofp_action *a)
474 return ((union ofp_action *) (void *)
475 ((uint8_t *) a + ntohs(a->header.len)));
479 action_is_valid(const union ofp_action *a, size_t n_actions)
481 uint16_t len = ntohs(a->header.len);
482 return (!(len % OFP_ACTION_ALIGN)
484 && len / sizeof *a <= n_actions);
487 /* This macro is careful to check for actions with bad lengths. */
488 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
489 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
490 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
491 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
492 (ITER) = action_next(ITER)))
495 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
498 if (!VLOG_DROP_WARN(&rl)) {
502 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
503 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
504 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
510 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
512 enum ofperr (*ofpact_from_openflow)(
513 const union ofp_action *a, struct ofpbuf *out))
515 const union ofp_action *a;
518 ACTION_FOR_EACH (a, left, in, n_in) {
519 enum ofperr error = ofpact_from_openflow(a, out);
521 log_bad_action(in, n_in, a - in, error);
526 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
527 log_bad_action(in, n_in, n_in - left, error);
536 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
539 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
543 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
544 struct ofpbuf *ofpacts,
545 enum ofperr (*translate)(const union ofp_action *actions,
547 struct ofpbuf *ofpacts))
549 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
550 const union ofp_action *actions;
553 ofpbuf_clear(ofpacts);
555 if (actions_len % OFP_ACTION_ALIGN != 0) {
556 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
557 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
558 return OFPERR_OFPBRC_BAD_LEN;
561 actions = ofpbuf_try_pull(openflow, actions_len);
562 if (actions == NULL) {
563 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
564 "remaining message length (%zu)",
565 actions_len, openflow->size);
566 return OFPERR_OFPBRC_BAD_LEN;
569 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
571 ofpbuf_clear(ofpacts);
576 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
577 * front of 'openflow' into ofpacts. On success, replaces any existing content
578 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
579 * Returns 0 if successful, otherwise an OpenFlow error.
581 * The parsed actions are valid generically, but they may not be valid in a
582 * specific context. For example, port numbers up to OFPP_MAX are valid
583 * generically, but specific datapaths may only support port numbers in a
584 * smaller range. Use ofpacts_check() to additional check whether actions are
585 * valid in a specific context. */
587 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
588 struct ofpbuf *ofpacts)
590 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
591 ofpacts_from_openflow10);
594 /* OpenFlow 1.1 actions. */
596 /* Parses 'a' to determine its type. On success stores the correct type into
597 * '*code' and returns 0. On failure returns an OFPERR_* error code and
598 * '*code' is indeterminate.
600 * The caller must have already verified that 'a''s length is potentially
601 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
602 * ofp_action) and no longer than the amount of space allocated to 'a').
604 * This function verifies that 'a''s length is correct for the type of action
605 * that it represents. */
607 decode_openflow11_action(const union ofp_action *a,
608 enum ofputil_action_code *code)
611 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
612 return decode_nxast_action(a, code);
614 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) \
615 case CONSTANT_HTONS(ENUM): \
616 if (a->header.len == htons(sizeof(struct STRUCT))) { \
617 *code = OFPUTIL_##ENUM; \
620 return OFPERR_OFPBAC_BAD_LEN; \
623 #include "ofp-util.def"
626 return OFPERR_OFPBAC_BAD_TYPE;
631 output_from_openflow11(const struct ofp11_action_output *oao,
634 struct ofpact_output *output;
637 output = ofpact_put_OUTPUT(out);
638 output->max_len = ntohs(oao->max_len);
640 error = ofputil_port_from_ofp11(oao->port, &output->port);
645 return ofputil_check_output_port(output->port, OFPP_MAX);
649 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
651 enum ofputil_action_code code;
654 error = decode_openflow11_action(a, &code);
660 case OFPUTIL_ACTION_INVALID:
661 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
662 #include "ofp-util.def"
665 case OFPUTIL_OFPAT11_OUTPUT:
666 return output_from_openflow11((const struct ofp11_action_output *) a,
669 case OFPUTIL_OFPAT11_SET_VLAN_VID:
670 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
671 return OFPERR_OFPBAC_BAD_ARGUMENT;
673 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
676 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
677 if (a->vlan_pcp.vlan_pcp & ~7) {
678 return OFPERR_OFPBAC_BAD_ARGUMENT;
680 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
683 case OFPUTIL_OFPAT11_SET_DL_SRC:
684 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
685 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
688 case OFPUTIL_OFPAT11_SET_DL_DST:
689 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
690 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
693 case OFPUTIL_OFPAT11_SET_NW_SRC:
694 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
697 case OFPUTIL_OFPAT11_SET_NW_DST:
698 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
701 case OFPUTIL_OFPAT11_SET_NW_TOS:
702 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
703 return OFPERR_OFPBAC_BAD_ARGUMENT;
705 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
708 case OFPUTIL_OFPAT11_SET_TP_SRC:
709 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
712 case OFPUTIL_OFPAT11_SET_TP_DST:
713 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
716 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
717 #include "ofp-util.def"
718 return ofpact_from_nxast(a, code, out);
725 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
728 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
731 /* OpenFlow 1.1 instructions. */
733 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
734 static inline const struct STRUCT * \
735 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
737 assert(inst->type == htons(ENUM)); \
738 return (struct STRUCT *)inst; \
742 instruction_init_##ENUM(struct STRUCT *s) \
744 memset(s, 0, sizeof *s); \
745 s->type = htons(ENUM); \
746 s->len = htons(sizeof *s); \
749 static inline struct STRUCT * \
750 instruction_put_##ENUM(struct ofpbuf *buf) \
752 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
753 instruction_init_##ENUM(s); \
759 struct instruction_type_info {
760 enum ovs_instruction_type type;
764 static const struct instruction_type_info inst_info[] = {
765 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
771 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
773 const struct instruction_type_info *p;
774 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
775 if (p->type == type) {
783 ofpact_instruction_type_from_name(const char *name)
785 const struct instruction_type_info *p;
786 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
787 if (!strcasecmp(name, p->name)) {
794 static inline struct ofp11_instruction *
795 instruction_next(const struct ofp11_instruction *inst)
797 return ((struct ofp11_instruction *) (void *)
798 ((uint8_t *) inst + ntohs(inst->len)));
802 instruction_is_valid(const struct ofp11_instruction *inst,
803 size_t n_instructions)
805 uint16_t len = ntohs(inst->len);
806 return (!(len % OFP11_INSTRUCTION_ALIGN)
807 && len >= sizeof *inst
808 && len / sizeof *inst <= n_instructions);
811 /* This macro is careful to check for instructions with bad lengths. */
812 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
813 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
814 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
815 ((LEFT) -= (ntohs((ITER)->len) \
816 / sizeof(struct ofp11_instruction)), \
817 (ITER) = instruction_next(ITER)))
820 decode_openflow11_instruction(const struct ofp11_instruction *inst,
821 enum ovs_instruction_type *type)
823 uint16_t len = ntohs(inst->len);
825 switch (inst->type) {
826 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
827 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
829 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
830 case CONSTANT_HTONS(ENUM): \
832 ? len >= sizeof(struct STRUCT) \
833 : len == sizeof(struct STRUCT)) { \
834 *type = OVSINST_##ENUM; \
837 return OFPERR_OFPBIC_BAD_LEN; \
843 return OFPERR_OFPBIC_UNKNOWN_INST;
848 decode_openflow11_instructions(const struct ofp11_instruction insts[],
850 const struct ofp11_instruction *out[])
852 const struct ofp11_instruction *inst;
855 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
856 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
857 enum ovs_instruction_type type;
860 error = decode_openflow11_instruction(inst, &type);
866 return OFPERR_NXBIC_DUP_TYPE;
872 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
873 (n_insts - left) * sizeof *inst);
874 return OFPERR_OFPBIC_BAD_LEN;
880 get_actions_from_instruction(const struct ofp11_instruction *inst,
881 const union ofp_action **actions,
884 *actions = (const union ofp_action *) (inst + 1);
885 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
888 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
889 * front of 'openflow' into ofpacts. On success, replaces any existing content
890 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
891 * Returns 0 if successful, otherwise an OpenFlow error.
893 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
894 * instructions, so you should call ofpacts_pull_openflow11_instructions()
895 * instead of this function.
897 * The parsed actions are valid generically, but they may not be valid in a
898 * specific context. For example, port numbers up to OFPP_MAX are valid
899 * generically, but specific datapaths may only support port numbers in a
900 * smaller range. Use ofpacts_check() to additional check whether actions are
901 * valid in a specific context. */
903 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
904 unsigned int actions_len,
905 struct ofpbuf *ofpacts)
907 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
908 ofpacts_from_openflow11);
912 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
913 unsigned int instructions_len,
914 struct ofpbuf *ofpacts)
916 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
917 const struct ofp11_instruction *instructions;
918 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
921 ofpbuf_clear(ofpacts);
923 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
924 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
926 instructions_len, OFP11_INSTRUCTION_ALIGN);
927 error = OFPERR_OFPBIC_BAD_LEN;
931 instructions = ofpbuf_try_pull(openflow, instructions_len);
932 if (instructions == NULL) {
933 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
934 "remaining message length (%zu)",
935 instructions_len, openflow->size);
936 error = OFPERR_OFPBIC_BAD_LEN;
940 error = decode_openflow11_instructions(
941 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
947 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
948 const union ofp_action *actions;
951 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
952 &actions, &n_actions);
953 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
959 if (insts[OVSINST_OFPIT11_GOTO_TABLE] ||
960 insts[OVSINST_OFPIT11_WRITE_METADATA] ||
961 insts[OVSINST_OFPIT11_WRITE_ACTIONS] ||
962 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
963 error = OFPERR_OFPBIC_UNSUP_INST;
969 ofpbuf_clear(ofpacts);
975 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
977 const struct ofpact_enqueue *enqueue;
981 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
984 case OFPACT_CONTROLLER:
988 enqueue = ofpact_get_ENQUEUE(a);
989 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
990 && enqueue->port != OFPP_LOCAL) {
991 return OFPERR_OFPBAC_BAD_OUT_PORT;
995 case OFPACT_OUTPUT_REG:
996 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
999 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1001 case OFPACT_SET_VLAN_VID:
1002 case OFPACT_SET_VLAN_PCP:
1003 case OFPACT_STRIP_VLAN:
1004 case OFPACT_SET_ETH_SRC:
1005 case OFPACT_SET_ETH_DST:
1006 case OFPACT_SET_IPV4_SRC:
1007 case OFPACT_SET_IPV4_DST:
1008 case OFPACT_SET_IPV4_DSCP:
1009 case OFPACT_SET_L4_SRC_PORT:
1010 case OFPACT_SET_L4_DST_PORT:
1013 case OFPACT_REG_MOVE:
1014 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1016 case OFPACT_REG_LOAD:
1017 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1019 case OFPACT_DEC_TTL:
1020 case OFPACT_SET_TUNNEL:
1021 case OFPACT_SET_QUEUE:
1022 case OFPACT_POP_QUEUE:
1023 case OFPACT_FIN_TIMEOUT:
1024 case OFPACT_RESUBMIT:
1028 return learn_check(ofpact_get_LEARN(a), flow);
1030 case OFPACT_MULTIPATH:
1031 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1033 case OFPACT_AUTOPATH:
1034 return autopath_check(ofpact_get_AUTOPATH(a), flow);
1045 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1046 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1047 * switch with no more than 'max_ports' ports. */
1049 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1050 const struct flow *flow, int max_ports)
1052 const struct ofpact *a;
1054 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1055 enum ofperr error = ofpact_check__(a, flow, max_ports);
1064 /* Converting ofpacts to Nicira OpenFlow extensions. */
1067 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1070 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1072 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1073 output_reg->src.n_bits);
1074 naor->src = htonl(output_reg->src.field->nxm_header);
1075 naor->max_len = htons(output_reg->max_len);
1079 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1082 struct nx_action_resubmit *nar;
1084 if (resubmit->table_id == 0xff
1085 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1086 nar = ofputil_put_NXAST_RESUBMIT(out);
1088 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1089 nar->table = resubmit->table_id;
1091 nar->in_port = htons(resubmit->in_port);
1095 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1098 uint64_t tun_id = tunnel->tun_id;
1100 if (tun_id <= UINT32_MAX
1101 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1102 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1104 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1109 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1111 size_t start_ofs = out->size;
1112 struct nx_action_note *nan;
1113 unsigned int remainder;
1116 nan = ofputil_put_NXAST_NOTE(out);
1117 out->size -= sizeof nan->note;
1119 ofpbuf_put(out, note->data, note->length);
1121 len = out->size - start_ofs;
1122 remainder = len % OFP_ACTION_ALIGN;
1124 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1126 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1127 nan->len = htons(out->size - start_ofs);
1131 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1134 struct nx_action_controller *nac;
1136 nac = ofputil_put_NXAST_CONTROLLER(out);
1137 nac->max_len = htons(oc->max_len);
1138 nac->controller_id = htons(oc->controller_id);
1139 nac->reason = oc->reason;
1143 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1146 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1147 ofputil_put_NXAST_DEC_TTL(out);
1149 struct nx_action_cnt_ids *nac_ids =
1150 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1151 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1155 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1156 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1158 ids = ofpbuf_put_zeros(out, ids_len);
1159 for (i = 0; i < oc_ids->n_controllers; i++) {
1160 ids[i] = htons(oc_ids->cnt_ids[i]);
1166 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1169 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1170 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1171 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1175 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1178 case OFPACT_CONTROLLER:
1179 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1182 case OFPACT_OUTPUT_REG:
1183 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1187 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1190 case OFPACT_REG_MOVE:
1191 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1194 case OFPACT_REG_LOAD:
1195 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1198 case OFPACT_DEC_TTL:
1199 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1202 case OFPACT_SET_TUNNEL:
1203 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1206 case OFPACT_SET_QUEUE:
1207 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1208 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1211 case OFPACT_POP_QUEUE:
1212 ofputil_put_NXAST_POP_QUEUE(out);
1215 case OFPACT_FIN_TIMEOUT:
1216 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1219 case OFPACT_RESUBMIT:
1220 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1224 learn_to_nxast(ofpact_get_LEARN(a), out);
1227 case OFPACT_MULTIPATH:
1228 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1231 case OFPACT_AUTOPATH:
1232 autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1236 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1240 ofputil_put_NXAST_EXIT(out);
1244 case OFPACT_ENQUEUE:
1245 case OFPACT_SET_VLAN_VID:
1246 case OFPACT_SET_VLAN_PCP:
1247 case OFPACT_STRIP_VLAN:
1248 case OFPACT_SET_ETH_SRC:
1249 case OFPACT_SET_ETH_DST:
1250 case OFPACT_SET_IPV4_SRC:
1251 case OFPACT_SET_IPV4_DST:
1252 case OFPACT_SET_IPV4_DSCP:
1253 case OFPACT_SET_L4_SRC_PORT:
1254 case OFPACT_SET_L4_DST_PORT:
1259 /* Converting ofpacts to OpenFlow 1.0. */
1262 ofpact_output_to_openflow10(const struct ofpact_output *output,
1265 struct ofp10_action_output *oao;
1267 oao = ofputil_put_OFPAT10_OUTPUT(out);
1268 oao->port = htons(output->port);
1269 oao->max_len = htons(output->max_len);
1273 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1276 struct ofp_action_enqueue *oae;
1278 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1279 oae->port = htons(enqueue->port);
1280 oae->queue_id = htonl(enqueue->queue);
1284 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1288 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1291 case OFPACT_ENQUEUE:
1292 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1295 case OFPACT_SET_VLAN_VID:
1296 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1297 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1300 case OFPACT_SET_VLAN_PCP:
1301 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1302 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1305 case OFPACT_STRIP_VLAN:
1306 ofputil_put_OFPAT10_STRIP_VLAN(out);
1309 case OFPACT_SET_ETH_SRC:
1310 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1311 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1314 case OFPACT_SET_ETH_DST:
1315 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1316 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1319 case OFPACT_SET_IPV4_SRC:
1320 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1321 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1324 case OFPACT_SET_IPV4_DST:
1325 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1326 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1329 case OFPACT_SET_IPV4_DSCP:
1330 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1331 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1334 case OFPACT_SET_L4_SRC_PORT:
1335 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1336 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1339 case OFPACT_SET_L4_DST_PORT:
1340 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1341 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1344 case OFPACT_CONTROLLER:
1345 case OFPACT_OUTPUT_REG:
1347 case OFPACT_REG_MOVE:
1348 case OFPACT_REG_LOAD:
1349 case OFPACT_DEC_TTL:
1350 case OFPACT_SET_TUNNEL:
1351 case OFPACT_SET_QUEUE:
1352 case OFPACT_POP_QUEUE:
1353 case OFPACT_FIN_TIMEOUT:
1354 case OFPACT_RESUBMIT:
1356 case OFPACT_MULTIPATH:
1357 case OFPACT_AUTOPATH:
1360 ofpact_to_nxast(a, out);
1365 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1366 * actions in 'openflow', appending the actions to any existing data in
1369 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1370 struct ofpbuf *openflow)
1372 const struct ofpact *a;
1374 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1375 ofpact_to_openflow10(a, openflow);
1379 /* Converting ofpacts to OpenFlow 1.1. */
1382 ofpact_output_to_openflow11(const struct ofpact_output *output,
1385 struct ofp11_action_output *oao;
1387 oao = ofputil_put_OFPAT11_OUTPUT(out);
1388 oao->port = ofputil_port_to_ofp11(output->port);
1389 oao->max_len = htons(output->max_len);
1393 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1397 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1399 case OFPACT_ENQUEUE:
1403 case OFPACT_SET_VLAN_VID:
1404 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1405 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1408 case OFPACT_SET_VLAN_PCP:
1409 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1410 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1413 case OFPACT_STRIP_VLAN:
1417 case OFPACT_SET_ETH_SRC:
1418 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1419 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1422 case OFPACT_SET_ETH_DST:
1423 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1424 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1427 case OFPACT_SET_IPV4_SRC:
1428 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1429 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1432 case OFPACT_SET_IPV4_DST:
1433 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1434 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1437 case OFPACT_SET_IPV4_DSCP:
1438 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1439 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1442 case OFPACT_SET_L4_SRC_PORT:
1443 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1444 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1447 case OFPACT_SET_L4_DST_PORT:
1448 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1449 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1452 case OFPACT_CONTROLLER:
1453 case OFPACT_OUTPUT_REG:
1455 case OFPACT_REG_MOVE:
1456 case OFPACT_REG_LOAD:
1457 case OFPACT_DEC_TTL:
1458 case OFPACT_SET_TUNNEL:
1459 case OFPACT_SET_QUEUE:
1460 case OFPACT_POP_QUEUE:
1461 case OFPACT_FIN_TIMEOUT:
1462 case OFPACT_RESUBMIT:
1464 case OFPACT_MULTIPATH:
1465 case OFPACT_AUTOPATH:
1468 ofpact_to_nxast(a, out);
1473 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1474 * 1.1 actions in 'openflow', appending the actions to any existing data in
1477 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1478 size_t ofpacts_len, struct ofpbuf *openflow)
1480 const struct ofpact *a;
1481 size_t start_size = openflow->size;
1483 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1484 ofpact_to_openflow11(a, openflow);
1487 return openflow->size - start_size;
1491 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1493 struct ofpbuf *openflow)
1495 struct ofp11_instruction_actions *oia;
1498 /* Put an OFPIT11_APPLY_ACTIONS instruction and fill it in. */
1499 ofs = openflow->size;
1500 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1501 ofpacts_put_openflow11_actions(ofpacts, ofpacts_len, openflow);
1503 /* Update the instruction's length (or, if it's empty, delete it). */
1504 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1505 if (openflow->size > ofs + sizeof *oia) {
1506 oia->len = htons(openflow->size - ofs);
1508 openflow->size = ofs;
1512 /* Returns true if 'action' outputs to 'port', false otherwise. */
1514 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1516 switch (ofpact->type) {
1518 return ofpact_get_OUTPUT(ofpact)->port == port;
1519 case OFPACT_ENQUEUE:
1520 return ofpact_get_ENQUEUE(ofpact)->port == port;
1521 case OFPACT_CONTROLLER:
1522 return port == OFPP_CONTROLLER;
1524 case OFPACT_OUTPUT_REG:
1526 case OFPACT_SET_VLAN_VID:
1527 case OFPACT_SET_VLAN_PCP:
1528 case OFPACT_STRIP_VLAN:
1529 case OFPACT_SET_ETH_SRC:
1530 case OFPACT_SET_ETH_DST:
1531 case OFPACT_SET_IPV4_SRC:
1532 case OFPACT_SET_IPV4_DST:
1533 case OFPACT_SET_IPV4_DSCP:
1534 case OFPACT_SET_L4_SRC_PORT:
1535 case OFPACT_SET_L4_DST_PORT:
1536 case OFPACT_REG_MOVE:
1537 case OFPACT_REG_LOAD:
1538 case OFPACT_DEC_TTL:
1539 case OFPACT_SET_TUNNEL:
1540 case OFPACT_SET_QUEUE:
1541 case OFPACT_POP_QUEUE:
1542 case OFPACT_FIN_TIMEOUT:
1543 case OFPACT_RESUBMIT:
1545 case OFPACT_MULTIPATH:
1546 case OFPACT_AUTOPATH:
1554 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1555 * to 'port', false otherwise. */
1557 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1560 const struct ofpact *a;
1562 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1563 if (ofpact_outputs_to_port(a, port)) {
1572 ofpacts_equal(const struct ofpact *a, size_t a_len,
1573 const struct ofpact *b, size_t b_len)
1575 return a_len == b_len && !memcmp(a, b, a_len);
1578 /* Formatting ofpacts. */
1581 print_note(const struct ofpact_note *note, struct ds *string)
1585 ds_put_cstr(string, "note:");
1586 for (i = 0; i < note->length; i++) {
1588 ds_put_char(string, '.');
1590 ds_put_format(string, "%02"PRIx8, note->data[i]);
1595 print_dec_ttl(const struct ofpact_cnt_ids *ids,
1600 ds_put_cstr(s, "dec_ttl");
1601 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
1602 ds_put_cstr(s, "(");
1603 for (i = 0; i < ids->n_controllers; i++) {
1605 ds_put_cstr(s, ",");
1607 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
1609 ds_put_cstr(s, ")");
1614 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1617 ds_put_cstr(s, "fin_timeout(");
1618 if (fin_timeout->fin_idle_timeout) {
1619 ds_put_format(s, "idle_timeout=%"PRIu16",",
1620 fin_timeout->fin_idle_timeout);
1622 if (fin_timeout->fin_hard_timeout) {
1623 ds_put_format(s, "hard_timeout=%"PRIu16",",
1624 fin_timeout->fin_hard_timeout);
1627 ds_put_char(s, ')');
1631 ofpact_format(const struct ofpact *a, struct ds *s)
1633 const struct ofpact_enqueue *enqueue;
1634 const struct ofpact_resubmit *resubmit;
1635 const struct ofpact_autopath *autopath;
1636 const struct ofpact_controller *controller;
1637 const struct ofpact_tunnel *tunnel;
1642 port = ofpact_get_OUTPUT(a)->port;
1643 if (port < OFPP_MAX) {
1644 ds_put_format(s, "output:%"PRIu16, port);
1646 ofputil_format_port(port, s);
1647 if (port == OFPP_CONTROLLER) {
1648 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1653 case OFPACT_CONTROLLER:
1654 controller = ofpact_get_CONTROLLER(a);
1655 if (controller->reason == OFPR_ACTION &&
1656 controller->controller_id == 0) {
1657 ds_put_format(s, "CONTROLLER:%"PRIu16,
1658 ofpact_get_CONTROLLER(a)->max_len);
1660 enum ofp_packet_in_reason reason = controller->reason;
1662 ds_put_cstr(s, "controller(");
1663 if (reason != OFPR_ACTION) {
1664 ds_put_format(s, "reason=%s,",
1665 ofputil_packet_in_reason_to_string(reason));
1667 if (controller->max_len != UINT16_MAX) {
1668 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1670 if (controller->controller_id != 0) {
1671 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1674 ds_put_char(s, ')');
1678 case OFPACT_ENQUEUE:
1679 enqueue = ofpact_get_ENQUEUE(a);
1680 ds_put_format(s, "enqueue:");
1681 ofputil_format_port(enqueue->port, s);
1682 ds_put_format(s, "q%"PRIu32, enqueue->queue);
1685 case OFPACT_OUTPUT_REG:
1686 ds_put_cstr(s, "output:");
1687 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1691 bundle_format(ofpact_get_BUNDLE(a), s);
1694 case OFPACT_SET_VLAN_VID:
1695 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1696 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1699 case OFPACT_SET_VLAN_PCP:
1700 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1701 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1704 case OFPACT_STRIP_VLAN:
1705 ds_put_cstr(s, "strip_vlan");
1708 case OFPACT_SET_ETH_SRC:
1709 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1710 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1713 case OFPACT_SET_ETH_DST:
1714 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1715 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1718 case OFPACT_SET_IPV4_SRC:
1719 ds_put_format(s, "mod_nw_src:"IP_FMT,
1720 IP_ARGS(&ofpact_get_SET_IPV4_SRC(a)->ipv4));
1723 case OFPACT_SET_IPV4_DST:
1724 ds_put_format(s, "mod_nw_dst:"IP_FMT,
1725 IP_ARGS(&ofpact_get_SET_IPV4_DST(a)->ipv4));
1728 case OFPACT_SET_IPV4_DSCP:
1729 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1732 case OFPACT_SET_L4_SRC_PORT:
1733 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1736 case OFPACT_SET_L4_DST_PORT:
1737 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1740 case OFPACT_REG_MOVE:
1741 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1744 case OFPACT_REG_LOAD:
1745 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1748 case OFPACT_DEC_TTL:
1749 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
1752 case OFPACT_SET_TUNNEL:
1753 tunnel = ofpact_get_SET_TUNNEL(a);
1754 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1755 (tunnel->tun_id > UINT32_MAX
1756 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1760 case OFPACT_SET_QUEUE:
1761 ds_put_format(s, "set_queue:%"PRIu32,
1762 ofpact_get_SET_QUEUE(a)->queue_id);
1765 case OFPACT_POP_QUEUE:
1766 ds_put_cstr(s, "pop_queue");
1769 case OFPACT_FIN_TIMEOUT:
1770 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
1773 case OFPACT_RESUBMIT:
1774 resubmit = ofpact_get_RESUBMIT(a);
1775 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
1776 ds_put_format(s, "resubmit:%"PRIu16, resubmit->in_port);
1778 ds_put_format(s, "resubmit(");
1779 if (resubmit->in_port != OFPP_IN_PORT) {
1780 ofputil_format_port(resubmit->in_port, s);
1782 ds_put_char(s, ',');
1783 if (resubmit->table_id != 255) {
1784 ds_put_format(s, "%"PRIu8, resubmit->table_id);
1786 ds_put_char(s, ')');
1791 learn_format(ofpact_get_LEARN(a), s);
1794 case OFPACT_MULTIPATH:
1795 multipath_format(ofpact_get_MULTIPATH(a), s);
1798 case OFPACT_AUTOPATH:
1799 autopath = ofpact_get_AUTOPATH(a);
1800 ds_put_format(s, "autopath(%u,", autopath->port);
1801 mf_format_subfield(&autopath->dst, s);
1802 ds_put_char(s, ')');
1806 print_note(ofpact_get_NOTE(a), s);
1810 ds_put_cstr(s, "exit");
1815 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
1816 * 'ofpacts' to 'string'. */
1818 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
1821 ds_put_cstr(string, "actions=");
1823 ds_put_cstr(string, "drop");
1825 const struct ofpact *a;
1827 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1829 ds_put_cstr(string, ",");
1831 ofpact_format(a, string);
1836 /* Internal use by helpers. */
1839 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
1841 struct ofpact *ofpact;
1843 ofpact_pad(ofpacts);
1844 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
1845 ofpact_init(ofpact, type, len);
1850 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
1852 memset(ofpact, 0, len);
1853 ofpact->type = type;
1854 ofpact->compat = OFPUTIL_ACTION_INVALID;
1858 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
1859 * starting at 'ofpact'.
1861 * This is the correct way to update a variable-length ofpact's length after
1862 * adding the variable-length part of the payload. (See the large comment
1863 * near the end of ofp-actions.h for more information.) */
1865 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
1867 assert(ofpact == ofpacts->l2);
1868 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
1871 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
1872 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
1873 * client must call this itself after adding the final ofpact to an array of
1876 * (The consequences of failing to call this function are probably not dire.
1877 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
1878 * not dereference it. That's undefined behavior, technically, but it will not
1879 * cause a real problem on common systems. Still, it seems better to call
1882 ofpact_pad(struct ofpbuf *ofpacts)
1884 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
1886 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);