2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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 "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 #define OFPAT12_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
287 #include "ofp-util.def"
290 case OFPUTIL_NXAST_RESUBMIT:
291 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
294 case OFPUTIL_NXAST_SET_TUNNEL:
295 nast = (const struct nx_action_set_tunnel *) a;
296 tunnel = ofpact_put_SET_TUNNEL(out);
297 tunnel->ofpact.compat = code;
298 tunnel->tun_id = ntohl(nast->tun_id);
301 case OFPUTIL_NXAST_SET_QUEUE:
302 nasq = (const struct nx_action_set_queue *) a;
303 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
306 case OFPUTIL_NXAST_POP_QUEUE:
307 ofpact_put_POP_QUEUE(out);
310 case OFPUTIL_NXAST_REG_MOVE:
311 error = nxm_reg_move_from_openflow(
312 (const struct nx_action_reg_move *) a, out);
315 case OFPUTIL_NXAST_REG_LOAD:
316 error = nxm_reg_load_from_openflow(
317 (const struct nx_action_reg_load *) a, out);
320 case OFPUTIL_NXAST_NOTE:
321 nan = (const struct nx_action_note *) a;
322 note_from_openflow(nan, out);
325 case OFPUTIL_NXAST_SET_TUNNEL64:
326 nast64 = (const struct nx_action_set_tunnel64 *) a;
327 tunnel = ofpact_put_SET_TUNNEL(out);
328 tunnel->ofpact.compat = code;
329 tunnel->tun_id = ntohll(nast64->tun_id);
332 case OFPUTIL_NXAST_MULTIPATH:
333 error = multipath_from_openflow((const struct nx_action_multipath *) a,
334 ofpact_put_MULTIPATH(out));
337 case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
338 error = autopath_from_openflow((const struct nx_action_autopath *) a,
339 ofpact_put_AUTOPATH(out));
342 case OFPUTIL_NXAST_BUNDLE:
343 case OFPUTIL_NXAST_BUNDLE_LOAD:
344 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
347 case OFPUTIL_NXAST_OUTPUT_REG:
348 error = output_reg_from_openflow(
349 (const struct nx_action_output_reg *) a, out);
352 case OFPUTIL_NXAST_RESUBMIT_TABLE:
353 nar = (const struct nx_action_resubmit *) a;
354 error = resubmit_table_from_openflow(nar, out);
357 case OFPUTIL_NXAST_LEARN:
358 error = learn_from_openflow((const struct nx_action_learn *) a, out);
361 case OFPUTIL_NXAST_EXIT:
362 ofpact_put_EXIT(out);
365 case OFPUTIL_NXAST_DEC_TTL:
366 error = dec_ttl_from_openflow(out);
369 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
370 error = dec_ttl_cnt_ids_from_openflow(
371 (const struct nx_action_cnt_ids *) a, out);
374 case OFPUTIL_NXAST_FIN_TIMEOUT:
375 fin_timeout_from_openflow(
376 (const struct nx_action_fin_timeout *) a, out);
379 case OFPUTIL_NXAST_CONTROLLER:
380 controller_from_openflow((const struct nx_action_controller *) a, out);
388 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
390 enum ofputil_action_code code;
393 error = decode_openflow10_action(a, &code);
399 case OFPUTIL_ACTION_INVALID:
400 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
401 #define OFPAT12_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
402 #include "ofp-util.def"
405 case OFPUTIL_OFPAT10_OUTPUT:
406 return output_from_openflow10(&a->output10, out);
408 case OFPUTIL_OFPAT10_SET_VLAN_VID:
409 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
410 return OFPERR_OFPBAC_BAD_ARGUMENT;
412 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
415 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
416 if (a->vlan_pcp.vlan_pcp & ~7) {
417 return OFPERR_OFPBAC_BAD_ARGUMENT;
419 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
422 case OFPUTIL_OFPAT10_STRIP_VLAN:
423 ofpact_put_STRIP_VLAN(out);
426 case OFPUTIL_OFPAT10_SET_DL_SRC:
427 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
428 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
431 case OFPUTIL_OFPAT10_SET_DL_DST:
432 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
433 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
436 case OFPUTIL_OFPAT10_SET_NW_SRC:
437 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
440 case OFPUTIL_OFPAT10_SET_NW_DST:
441 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
444 case OFPUTIL_OFPAT10_SET_NW_TOS:
445 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
446 return OFPERR_OFPBAC_BAD_ARGUMENT;
448 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
451 case OFPUTIL_OFPAT10_SET_TP_SRC:
452 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
455 case OFPUTIL_OFPAT10_SET_TP_DST:
456 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
460 case OFPUTIL_OFPAT10_ENQUEUE:
461 error = enqueue_from_openflow10((const struct ofp_action_enqueue *) a,
465 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
466 #include "ofp-util.def"
467 return ofpact_from_nxast(a, code, out);
473 static inline union ofp_action *
474 action_next(const union ofp_action *a)
476 return ((union ofp_action *) (void *)
477 ((uint8_t *) a + ntohs(a->header.len)));
481 action_is_valid(const union ofp_action *a, size_t n_actions)
483 uint16_t len = ntohs(a->header.len);
484 return (!(len % OFP_ACTION_ALIGN)
486 && len / sizeof *a <= n_actions);
489 /* This macro is careful to check for actions with bad lengths. */
490 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
491 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
492 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
493 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
494 (ITER) = action_next(ITER)))
497 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
500 if (!VLOG_DROP_WARN(&rl)) {
504 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
505 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
506 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
512 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
514 enum ofperr (*ofpact_from_openflow)(
515 const union ofp_action *a, struct ofpbuf *out))
517 const union ofp_action *a;
520 ACTION_FOR_EACH (a, left, in, n_in) {
521 enum ofperr error = ofpact_from_openflow(a, out);
523 log_bad_action(in, n_in, a - in, error);
528 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
529 log_bad_action(in, n_in, n_in - left, error);
538 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
541 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
545 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
546 struct ofpbuf *ofpacts,
547 enum ofperr (*translate)(const union ofp_action *actions,
549 struct ofpbuf *ofpacts))
551 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
552 const union ofp_action *actions;
555 ofpbuf_clear(ofpacts);
557 if (actions_len % OFP_ACTION_ALIGN != 0) {
558 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
559 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
560 return OFPERR_OFPBRC_BAD_LEN;
563 actions = ofpbuf_try_pull(openflow, actions_len);
564 if (actions == NULL) {
565 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
566 "remaining message length (%zu)",
567 actions_len, openflow->size);
568 return OFPERR_OFPBRC_BAD_LEN;
571 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
573 ofpbuf_clear(ofpacts);
578 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
579 * front of 'openflow' into ofpacts. On success, replaces any existing content
580 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
581 * Returns 0 if successful, otherwise an OpenFlow error.
583 * The parsed actions are valid generically, but they may not be valid in a
584 * specific context. For example, port numbers up to OFPP_MAX are valid
585 * generically, but specific datapaths may only support port numbers in a
586 * smaller range. Use ofpacts_check() to additional check whether actions are
587 * valid in a specific context. */
589 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
590 struct ofpbuf *ofpacts)
592 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
593 ofpacts_from_openflow10);
596 /* OpenFlow 1.1 actions. */
598 /* Parses 'a' to determine its type. On success stores the correct type into
599 * '*code' and returns 0. On failure returns an OFPERR_* error code and
600 * '*code' is indeterminate.
602 * The caller must have already verified that 'a''s length is potentially
603 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
604 * ofp_action) and no longer than the amount of space allocated to 'a').
606 * This function verifies that 'a''s length is correct for the type of action
607 * that it represents. */
609 decode_openflow11_action(const union ofp_action *a,
610 enum ofputil_action_code *code)
613 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
614 return decode_nxast_action(a, code);
616 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) \
617 case CONSTANT_HTONS(ENUM): \
618 if (a->header.len == htons(sizeof(struct STRUCT))) { \
619 *code = OFPUTIL_##ENUM; \
622 return OFPERR_OFPBAC_BAD_LEN; \
625 #include "ofp-util.def"
628 return OFPERR_OFPBAC_BAD_TYPE;
633 output_from_openflow11(const struct ofp11_action_output *oao,
636 struct ofpact_output *output;
639 output = ofpact_put_OUTPUT(out);
640 output->max_len = ntohs(oao->max_len);
642 error = ofputil_port_from_ofp11(oao->port, &output->port);
647 return ofputil_check_output_port(output->port, OFPP_MAX);
651 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
653 enum ofputil_action_code code;
656 error = decode_openflow11_action(a, &code);
662 case OFPUTIL_ACTION_INVALID:
663 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
664 #define OFPAT12_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
665 #include "ofp-util.def"
668 case OFPUTIL_OFPAT11_OUTPUT:
669 return output_from_openflow11((const struct ofp11_action_output *) a,
672 case OFPUTIL_OFPAT11_SET_VLAN_VID:
673 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
674 return OFPERR_OFPBAC_BAD_ARGUMENT;
676 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
679 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
680 if (a->vlan_pcp.vlan_pcp & ~7) {
681 return OFPERR_OFPBAC_BAD_ARGUMENT;
683 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
686 case OFPUTIL_OFPAT11_SET_DL_SRC:
687 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
688 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
691 case OFPUTIL_OFPAT11_SET_DL_DST:
692 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
693 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
696 case OFPUTIL_OFPAT11_SET_NW_SRC:
697 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
700 case OFPUTIL_OFPAT11_SET_NW_DST:
701 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
704 case OFPUTIL_OFPAT11_SET_NW_TOS:
705 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
706 return OFPERR_OFPBAC_BAD_ARGUMENT;
708 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
711 case OFPUTIL_OFPAT11_SET_TP_SRC:
712 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
715 case OFPUTIL_OFPAT11_SET_TP_DST:
716 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
719 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
720 #include "ofp-util.def"
721 return ofpact_from_nxast(a, code, out);
728 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
731 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
734 /* OpenFlow 1.1 instructions. */
736 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
737 static inline const struct STRUCT * \
738 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
740 assert(inst->type == htons(ENUM)); \
741 return (struct STRUCT *)inst; \
745 instruction_init_##ENUM(struct STRUCT *s) \
747 memset(s, 0, sizeof *s); \
748 s->type = htons(ENUM); \
749 s->len = htons(sizeof *s); \
752 static inline struct STRUCT * \
753 instruction_put_##ENUM(struct ofpbuf *buf) \
755 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
756 instruction_init_##ENUM(s); \
762 struct instruction_type_info {
763 enum ovs_instruction_type type;
767 static const struct instruction_type_info inst_info[] = {
768 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
774 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
776 return inst_info[type].name;
780 ofpact_instruction_type_from_name(const char *name)
782 const struct instruction_type_info *p;
783 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
784 if (!strcasecmp(name, p->name)) {
791 static inline struct ofp11_instruction *
792 instruction_next(const struct ofp11_instruction *inst)
794 return ((struct ofp11_instruction *) (void *)
795 ((uint8_t *) inst + ntohs(inst->len)));
799 instruction_is_valid(const struct ofp11_instruction *inst,
800 size_t n_instructions)
802 uint16_t len = ntohs(inst->len);
803 return (!(len % OFP11_INSTRUCTION_ALIGN)
804 && len >= sizeof *inst
805 && len / sizeof *inst <= n_instructions);
808 /* This macro is careful to check for instructions with bad lengths. */
809 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
810 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
811 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
812 ((LEFT) -= (ntohs((ITER)->len) \
813 / sizeof(struct ofp11_instruction)), \
814 (ITER) = instruction_next(ITER)))
817 decode_openflow11_instruction(const struct ofp11_instruction *inst,
818 enum ovs_instruction_type *type)
820 uint16_t len = ntohs(inst->len);
822 switch (inst->type) {
823 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
824 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
826 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
827 case CONSTANT_HTONS(ENUM): \
829 ? len >= sizeof(struct STRUCT) \
830 : len == sizeof(struct STRUCT)) { \
831 *type = OVSINST_##ENUM; \
834 return OFPERR_OFPBIC_BAD_LEN; \
840 return OFPERR_OFPBIC_UNKNOWN_INST;
845 decode_openflow11_instructions(const struct ofp11_instruction insts[],
847 const struct ofp11_instruction *out[])
849 const struct ofp11_instruction *inst;
852 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
853 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
854 enum ovs_instruction_type type;
857 error = decode_openflow11_instruction(inst, &type);
863 return OFPERR_OFPIT_BAD_INSTRUCTION;
869 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
870 (n_insts - left) * sizeof *inst);
871 return OFPERR_OFPBIC_BAD_LEN;
877 get_actions_from_instruction(const struct ofp11_instruction *inst,
878 const union ofp_action **actions,
881 *actions = (const union ofp_action *) (inst + 1);
882 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
885 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
886 * front of 'openflow' into ofpacts. On success, replaces any existing content
887 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
888 * Returns 0 if successful, otherwise an OpenFlow error.
890 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
891 * instructions, so you should call ofpacts_pull_openflow11_instructions()
892 * instead of this function.
894 * The parsed actions are valid generically, but they may not be valid in a
895 * specific context. For example, port numbers up to OFPP_MAX are valid
896 * generically, but specific datapaths may only support port numbers in a
897 * smaller range. Use ofpacts_check() to additional check whether actions are
898 * valid in a specific context. */
900 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
901 unsigned int actions_len,
902 struct ofpbuf *ofpacts)
904 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
905 ofpacts_from_openflow11);
909 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
910 unsigned int instructions_len,
911 struct ofpbuf *ofpacts)
913 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
914 const struct ofp11_instruction *instructions;
915 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
918 ofpbuf_clear(ofpacts);
920 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
921 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
923 instructions_len, OFP11_INSTRUCTION_ALIGN);
924 error = OFPERR_OFPBIC_BAD_LEN;
928 instructions = ofpbuf_try_pull(openflow, instructions_len);
929 if (instructions == NULL) {
930 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
931 "remaining message length (%zu)",
932 instructions_len, openflow->size);
933 error = OFPERR_OFPBIC_BAD_LEN;
937 error = decode_openflow11_instructions(
938 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
944 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
945 const union ofp_action *actions;
948 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
949 &actions, &n_actions);
950 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
956 if (insts[OVSINST_OFPIT11_GOTO_TABLE] ||
957 insts[OVSINST_OFPIT11_WRITE_METADATA] ||
958 insts[OVSINST_OFPIT11_WRITE_ACTIONS] ||
959 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
960 error = OFPERR_OFPBIC_UNSUP_INST;
966 ofpbuf_clear(ofpacts);
972 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
974 const struct ofpact_enqueue *enqueue;
978 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
981 case OFPACT_CONTROLLER:
985 enqueue = ofpact_get_ENQUEUE(a);
986 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
987 && enqueue->port != OFPP_LOCAL) {
988 return OFPERR_OFPBAC_BAD_OUT_PORT;
992 case OFPACT_OUTPUT_REG:
993 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
996 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
998 case OFPACT_SET_VLAN_VID:
999 case OFPACT_SET_VLAN_PCP:
1000 case OFPACT_STRIP_VLAN:
1001 case OFPACT_SET_ETH_SRC:
1002 case OFPACT_SET_ETH_DST:
1003 case OFPACT_SET_IPV4_SRC:
1004 case OFPACT_SET_IPV4_DST:
1005 case OFPACT_SET_IPV4_DSCP:
1006 case OFPACT_SET_L4_SRC_PORT:
1007 case OFPACT_SET_L4_DST_PORT:
1010 case OFPACT_REG_MOVE:
1011 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1013 case OFPACT_REG_LOAD:
1014 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1016 case OFPACT_DEC_TTL:
1017 case OFPACT_SET_TUNNEL:
1018 case OFPACT_SET_QUEUE:
1019 case OFPACT_POP_QUEUE:
1020 case OFPACT_FIN_TIMEOUT:
1021 case OFPACT_RESUBMIT:
1025 return learn_check(ofpact_get_LEARN(a), flow);
1027 case OFPACT_MULTIPATH:
1028 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1030 case OFPACT_AUTOPATH:
1031 return autopath_check(ofpact_get_AUTOPATH(a), flow);
1042 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1043 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1044 * switch with no more than 'max_ports' ports. */
1046 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1047 const struct flow *flow, int max_ports)
1049 const struct ofpact *a;
1051 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1052 enum ofperr error = ofpact_check__(a, flow, max_ports);
1061 /* Converting ofpacts to Nicira OpenFlow extensions. */
1064 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1067 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1069 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1070 output_reg->src.n_bits);
1071 naor->src = htonl(output_reg->src.field->nxm_header);
1072 naor->max_len = htons(output_reg->max_len);
1076 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1079 struct nx_action_resubmit *nar;
1081 if (resubmit->table_id == 0xff
1082 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1083 nar = ofputil_put_NXAST_RESUBMIT(out);
1085 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1086 nar->table = resubmit->table_id;
1088 nar->in_port = htons(resubmit->in_port);
1092 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1095 uint64_t tun_id = tunnel->tun_id;
1097 if (tun_id <= UINT32_MAX
1098 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1099 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1101 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1106 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1108 size_t start_ofs = out->size;
1109 struct nx_action_note *nan;
1110 unsigned int remainder;
1113 nan = ofputil_put_NXAST_NOTE(out);
1114 out->size -= sizeof nan->note;
1116 ofpbuf_put(out, note->data, note->length);
1118 len = out->size - start_ofs;
1119 remainder = len % OFP_ACTION_ALIGN;
1121 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1123 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1124 nan->len = htons(out->size - start_ofs);
1128 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1131 struct nx_action_controller *nac;
1133 nac = ofputil_put_NXAST_CONTROLLER(out);
1134 nac->max_len = htons(oc->max_len);
1135 nac->controller_id = htons(oc->controller_id);
1136 nac->reason = oc->reason;
1140 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1143 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1144 ofputil_put_NXAST_DEC_TTL(out);
1146 struct nx_action_cnt_ids *nac_ids =
1147 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1148 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1152 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1153 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1155 ids = ofpbuf_put_zeros(out, ids_len);
1156 for (i = 0; i < oc_ids->n_controllers; i++) {
1157 ids[i] = htons(oc_ids->cnt_ids[i]);
1163 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1166 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1167 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1168 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1172 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1175 case OFPACT_CONTROLLER:
1176 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1179 case OFPACT_OUTPUT_REG:
1180 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1184 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1187 case OFPACT_REG_MOVE:
1188 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1191 case OFPACT_REG_LOAD:
1192 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1195 case OFPACT_DEC_TTL:
1196 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1199 case OFPACT_SET_TUNNEL:
1200 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1203 case OFPACT_SET_QUEUE:
1204 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1205 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1208 case OFPACT_POP_QUEUE:
1209 ofputil_put_NXAST_POP_QUEUE(out);
1212 case OFPACT_FIN_TIMEOUT:
1213 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1216 case OFPACT_RESUBMIT:
1217 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1221 learn_to_nxast(ofpact_get_LEARN(a), out);
1224 case OFPACT_MULTIPATH:
1225 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1228 case OFPACT_AUTOPATH:
1229 autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1233 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1237 ofputil_put_NXAST_EXIT(out);
1241 case OFPACT_ENQUEUE:
1242 case OFPACT_SET_VLAN_VID:
1243 case OFPACT_SET_VLAN_PCP:
1244 case OFPACT_STRIP_VLAN:
1245 case OFPACT_SET_ETH_SRC:
1246 case OFPACT_SET_ETH_DST:
1247 case OFPACT_SET_IPV4_SRC:
1248 case OFPACT_SET_IPV4_DST:
1249 case OFPACT_SET_IPV4_DSCP:
1250 case OFPACT_SET_L4_SRC_PORT:
1251 case OFPACT_SET_L4_DST_PORT:
1256 /* Converting ofpacts to OpenFlow 1.0. */
1259 ofpact_output_to_openflow10(const struct ofpact_output *output,
1262 struct ofp10_action_output *oao;
1264 oao = ofputil_put_OFPAT10_OUTPUT(out);
1265 oao->port = htons(output->port);
1266 oao->max_len = htons(output->max_len);
1270 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1273 struct ofp_action_enqueue *oae;
1275 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1276 oae->port = htons(enqueue->port);
1277 oae->queue_id = htonl(enqueue->queue);
1281 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1285 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1288 case OFPACT_ENQUEUE:
1289 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1292 case OFPACT_SET_VLAN_VID:
1293 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1294 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1297 case OFPACT_SET_VLAN_PCP:
1298 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1299 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1302 case OFPACT_STRIP_VLAN:
1303 ofputil_put_OFPAT10_STRIP_VLAN(out);
1306 case OFPACT_SET_ETH_SRC:
1307 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1308 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1311 case OFPACT_SET_ETH_DST:
1312 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1313 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1316 case OFPACT_SET_IPV4_SRC:
1317 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1318 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1321 case OFPACT_SET_IPV4_DST:
1322 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1323 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1326 case OFPACT_SET_IPV4_DSCP:
1327 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1328 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1331 case OFPACT_SET_L4_SRC_PORT:
1332 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1333 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1336 case OFPACT_SET_L4_DST_PORT:
1337 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1338 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1341 case OFPACT_CONTROLLER:
1342 case OFPACT_OUTPUT_REG:
1344 case OFPACT_REG_MOVE:
1345 case OFPACT_REG_LOAD:
1346 case OFPACT_DEC_TTL:
1347 case OFPACT_SET_TUNNEL:
1348 case OFPACT_SET_QUEUE:
1349 case OFPACT_POP_QUEUE:
1350 case OFPACT_FIN_TIMEOUT:
1351 case OFPACT_RESUBMIT:
1353 case OFPACT_MULTIPATH:
1354 case OFPACT_AUTOPATH:
1357 ofpact_to_nxast(a, out);
1362 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1363 * actions in 'openflow', appending the actions to any existing data in
1366 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1367 struct ofpbuf *openflow)
1369 const struct ofpact *a;
1371 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1372 ofpact_to_openflow10(a, openflow);
1376 /* Converting ofpacts to OpenFlow 1.1. */
1379 ofpact_output_to_openflow11(const struct ofpact_output *output,
1382 struct ofp11_action_output *oao;
1384 oao = ofputil_put_OFPAT11_OUTPUT(out);
1385 oao->port = ofputil_port_to_ofp11(output->port);
1386 oao->max_len = htons(output->max_len);
1390 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1394 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1396 case OFPACT_ENQUEUE:
1400 case OFPACT_SET_VLAN_VID:
1401 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1402 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1405 case OFPACT_SET_VLAN_PCP:
1406 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1407 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1410 case OFPACT_STRIP_VLAN:
1414 case OFPACT_SET_ETH_SRC:
1415 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1416 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1419 case OFPACT_SET_ETH_DST:
1420 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1421 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1424 case OFPACT_SET_IPV4_SRC:
1425 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1426 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1429 case OFPACT_SET_IPV4_DST:
1430 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1431 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1434 case OFPACT_SET_IPV4_DSCP:
1435 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1436 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1439 case OFPACT_SET_L4_SRC_PORT:
1440 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1441 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1444 case OFPACT_SET_L4_DST_PORT:
1445 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1446 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1449 case OFPACT_CONTROLLER:
1450 case OFPACT_OUTPUT_REG:
1452 case OFPACT_REG_MOVE:
1453 case OFPACT_REG_LOAD:
1454 case OFPACT_DEC_TTL:
1455 case OFPACT_SET_TUNNEL:
1456 case OFPACT_SET_QUEUE:
1457 case OFPACT_POP_QUEUE:
1458 case OFPACT_FIN_TIMEOUT:
1459 case OFPACT_RESUBMIT:
1461 case OFPACT_MULTIPATH:
1462 case OFPACT_AUTOPATH:
1465 ofpact_to_nxast(a, out);
1470 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1471 * 1.1 actions in 'openflow', appending the actions to any existing data in
1474 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1475 size_t ofpacts_len, struct ofpbuf *openflow)
1477 const struct ofpact *a;
1478 size_t start_size = openflow->size;
1480 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1481 ofpact_to_openflow11(a, openflow);
1484 return openflow->size - start_size;
1488 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1490 struct ofpbuf *openflow)
1492 struct ofp11_instruction_actions *oia;
1495 /* Put an OFPIT11_APPLY_ACTIONS instruction and fill it in. */
1496 ofs = openflow->size;
1497 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1498 ofpacts_put_openflow11_actions(ofpacts, ofpacts_len, openflow);
1500 /* Update the instruction's length (or, if it's empty, delete it). */
1501 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1502 if (openflow->size > ofs + sizeof *oia) {
1503 oia->len = htons(openflow->size - ofs);
1505 openflow->size = ofs;
1509 /* Returns true if 'action' outputs to 'port', false otherwise. */
1511 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1513 switch (ofpact->type) {
1515 return ofpact_get_OUTPUT(ofpact)->port == port;
1516 case OFPACT_ENQUEUE:
1517 return ofpact_get_ENQUEUE(ofpact)->port == port;
1518 case OFPACT_CONTROLLER:
1519 return port == OFPP_CONTROLLER;
1521 case OFPACT_OUTPUT_REG:
1523 case OFPACT_SET_VLAN_VID:
1524 case OFPACT_SET_VLAN_PCP:
1525 case OFPACT_STRIP_VLAN:
1526 case OFPACT_SET_ETH_SRC:
1527 case OFPACT_SET_ETH_DST:
1528 case OFPACT_SET_IPV4_SRC:
1529 case OFPACT_SET_IPV4_DST:
1530 case OFPACT_SET_IPV4_DSCP:
1531 case OFPACT_SET_L4_SRC_PORT:
1532 case OFPACT_SET_L4_DST_PORT:
1533 case OFPACT_REG_MOVE:
1534 case OFPACT_REG_LOAD:
1535 case OFPACT_DEC_TTL:
1536 case OFPACT_SET_TUNNEL:
1537 case OFPACT_SET_QUEUE:
1538 case OFPACT_POP_QUEUE:
1539 case OFPACT_FIN_TIMEOUT:
1540 case OFPACT_RESUBMIT:
1542 case OFPACT_MULTIPATH:
1543 case OFPACT_AUTOPATH:
1551 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1552 * to 'port', false otherwise. */
1554 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1557 const struct ofpact *a;
1559 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1560 if (ofpact_outputs_to_port(a, port)) {
1569 ofpacts_equal(const struct ofpact *a, size_t a_len,
1570 const struct ofpact *b, size_t b_len)
1572 return a_len == b_len && !memcmp(a, b, a_len);
1575 /* Formatting ofpacts. */
1578 print_note(const struct ofpact_note *note, struct ds *string)
1582 ds_put_cstr(string, "note:");
1583 for (i = 0; i < note->length; i++) {
1585 ds_put_char(string, '.');
1587 ds_put_format(string, "%02"PRIx8, note->data[i]);
1592 print_dec_ttl(const struct ofpact_cnt_ids *ids,
1597 ds_put_cstr(s, "dec_ttl");
1598 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
1599 ds_put_cstr(s, "(");
1600 for (i = 0; i < ids->n_controllers; i++) {
1602 ds_put_cstr(s, ",");
1604 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
1606 ds_put_cstr(s, ")");
1611 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1614 ds_put_cstr(s, "fin_timeout(");
1615 if (fin_timeout->fin_idle_timeout) {
1616 ds_put_format(s, "idle_timeout=%"PRIu16",",
1617 fin_timeout->fin_idle_timeout);
1619 if (fin_timeout->fin_hard_timeout) {
1620 ds_put_format(s, "hard_timeout=%"PRIu16",",
1621 fin_timeout->fin_hard_timeout);
1624 ds_put_char(s, ')');
1628 ofpact_format(const struct ofpact *a, struct ds *s)
1630 const struct ofpact_enqueue *enqueue;
1631 const struct ofpact_resubmit *resubmit;
1632 const struct ofpact_autopath *autopath;
1633 const struct ofpact_controller *controller;
1634 const struct ofpact_tunnel *tunnel;
1639 port = ofpact_get_OUTPUT(a)->port;
1640 if (port < OFPP_MAX) {
1641 ds_put_format(s, "output:%"PRIu16, port);
1643 ofputil_format_port(port, s);
1644 if (port == OFPP_CONTROLLER) {
1645 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1650 case OFPACT_CONTROLLER:
1651 controller = ofpact_get_CONTROLLER(a);
1652 if (controller->reason == OFPR_ACTION &&
1653 controller->controller_id == 0) {
1654 ds_put_format(s, "CONTROLLER:%"PRIu16,
1655 ofpact_get_CONTROLLER(a)->max_len);
1657 enum ofp_packet_in_reason reason = controller->reason;
1659 ds_put_cstr(s, "controller(");
1660 if (reason != OFPR_ACTION) {
1661 ds_put_format(s, "reason=%s,",
1662 ofputil_packet_in_reason_to_string(reason));
1664 if (controller->max_len != UINT16_MAX) {
1665 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1667 if (controller->controller_id != 0) {
1668 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1671 ds_put_char(s, ')');
1675 case OFPACT_ENQUEUE:
1676 enqueue = ofpact_get_ENQUEUE(a);
1677 ds_put_format(s, "enqueue:");
1678 ofputil_format_port(enqueue->port, s);
1679 ds_put_format(s, "q%"PRIu32, enqueue->queue);
1682 case OFPACT_OUTPUT_REG:
1683 ds_put_cstr(s, "output:");
1684 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1688 bundle_format(ofpact_get_BUNDLE(a), s);
1691 case OFPACT_SET_VLAN_VID:
1692 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1693 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1696 case OFPACT_SET_VLAN_PCP:
1697 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1698 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1701 case OFPACT_STRIP_VLAN:
1702 ds_put_cstr(s, "strip_vlan");
1705 case OFPACT_SET_ETH_SRC:
1706 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1707 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1710 case OFPACT_SET_ETH_DST:
1711 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1712 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1715 case OFPACT_SET_IPV4_SRC:
1716 ds_put_format(s, "mod_nw_src:"IP_FMT,
1717 IP_ARGS(&ofpact_get_SET_IPV4_SRC(a)->ipv4));
1720 case OFPACT_SET_IPV4_DST:
1721 ds_put_format(s, "mod_nw_dst:"IP_FMT,
1722 IP_ARGS(&ofpact_get_SET_IPV4_DST(a)->ipv4));
1725 case OFPACT_SET_IPV4_DSCP:
1726 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1729 case OFPACT_SET_L4_SRC_PORT:
1730 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1733 case OFPACT_SET_L4_DST_PORT:
1734 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1737 case OFPACT_REG_MOVE:
1738 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1741 case OFPACT_REG_LOAD:
1742 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1745 case OFPACT_DEC_TTL:
1746 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
1749 case OFPACT_SET_TUNNEL:
1750 tunnel = ofpact_get_SET_TUNNEL(a);
1751 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1752 (tunnel->tun_id > UINT32_MAX
1753 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1757 case OFPACT_SET_QUEUE:
1758 ds_put_format(s, "set_queue:%"PRIu32,
1759 ofpact_get_SET_QUEUE(a)->queue_id);
1762 case OFPACT_POP_QUEUE:
1763 ds_put_cstr(s, "pop_queue");
1766 case OFPACT_FIN_TIMEOUT:
1767 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
1770 case OFPACT_RESUBMIT:
1771 resubmit = ofpact_get_RESUBMIT(a);
1772 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
1773 ds_put_cstr(s, "resubmit:");
1774 ofputil_format_port(resubmit->in_port, s);
1776 ds_put_format(s, "resubmit(");
1777 if (resubmit->in_port != OFPP_IN_PORT) {
1778 ofputil_format_port(resubmit->in_port, s);
1780 ds_put_char(s, ',');
1781 if (resubmit->table_id != 255) {
1782 ds_put_format(s, "%"PRIu8, resubmit->table_id);
1784 ds_put_char(s, ')');
1789 learn_format(ofpact_get_LEARN(a), s);
1792 case OFPACT_MULTIPATH:
1793 multipath_format(ofpact_get_MULTIPATH(a), s);
1796 case OFPACT_AUTOPATH:
1797 autopath = ofpact_get_AUTOPATH(a);
1798 ds_put_cstr(s, "autopath(");
1799 ofputil_format_port(autopath->port, s);
1800 ds_put_char(s, ',');
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);