2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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"
20 #include "byte-order.h"
22 #include "dynamic-string.h"
24 #include "meta-flow.h"
25 #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. */
40 struct ofp_action_header header;
41 struct ofp_action_vendor_header vendor;
42 struct ofp10_action_output output10;
43 struct ofp_action_vlan_vid vlan_vid;
44 struct ofp_action_vlan_pcp vlan_pcp;
45 struct ofp_action_nw_addr nw_addr;
46 struct ofp_action_nw_tos nw_tos;
47 struct ofp_action_tp_port tp_port;
49 OFP_ASSERT(sizeof(union ofp_action) == 8);
52 output_from_openflow10(const struct ofp10_action_output *oao,
55 struct ofpact_output *output;
57 output = ofpact_put_OUTPUT(out);
58 output->port = u16_to_ofp(ntohs(oao->port));
59 output->max_len = ntohs(oao->max_len);
61 return ofputil_check_output_port(output->port, OFPP_MAX);
65 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
68 struct ofpact_enqueue *enqueue;
70 enqueue = ofpact_put_ENQUEUE(out);
71 enqueue->port = u16_to_ofp(ntohs(oae->port));
72 enqueue->queue = ntohl(oae->queue_id);
73 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
74 && enqueue->port != OFPP_IN_PORT
75 && enqueue->port != OFPP_LOCAL) {
76 return OFPERR_OFPBAC_BAD_OUT_PORT;
82 resubmit_from_openflow(const struct nx_action_resubmit *nar,
85 struct ofpact_resubmit *resubmit;
87 resubmit = ofpact_put_RESUBMIT(out);
88 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
89 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
90 resubmit->table_id = 0xff;
94 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
97 struct ofpact_resubmit *resubmit;
99 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
100 return OFPERR_OFPBAC_BAD_ARGUMENT;
103 resubmit = ofpact_put_RESUBMIT(out);
104 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
105 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
106 resubmit->table_id = nar->table;
111 output_reg_from_openflow(const struct nx_action_output_reg *naor,
114 struct ofpact_output_reg *output_reg;
116 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
117 return OFPERR_OFPBAC_BAD_ARGUMENT;
120 output_reg = ofpact_put_OUTPUT_REG(out);
121 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
122 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
123 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
124 output_reg->max_len = ntohs(naor->max_len);
126 return mf_check_src(&output_reg->src, NULL);
130 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
133 struct ofpact_fin_timeout *oft;
135 oft = ofpact_put_FIN_TIMEOUT(out);
136 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
137 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
141 controller_from_openflow(const struct nx_action_controller *nac,
144 struct ofpact_controller *oc;
146 oc = ofpact_put_CONTROLLER(out);
147 oc->max_len = ntohs(nac->max_len);
148 oc->controller_id = ntohs(nac->controller_id);
149 oc->reason = nac->reason;
153 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
156 struct ofpact_metadata *om;
158 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
159 return OFPERR_NXBRC_MUST_BE_ZERO;
162 om = ofpact_put_WRITE_METADATA(out);
163 om->metadata = nawm->metadata;
164 om->mask = nawm->mask;
170 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
172 struct ofpact_note *note;
175 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
176 note = ofpact_put(out, OFPACT_NOTE,
177 offsetof(struct ofpact_note, data) + length);
178 note->length = length;
179 memcpy(note->data, nan->note, length);
183 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
186 struct ofpact_cnt_ids *ids;
187 enum ofperr error = 0;
189 ids = ofpact_put_DEC_TTL(out);
190 ids->ofpact.compat = compat;
191 ids->n_controllers = 1;
192 ofpbuf_put(out, &id, sizeof id);
194 ofpact_update_len(out, &ids->ofpact);
199 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
202 struct ofpact_cnt_ids *ids;
206 ids = ofpact_put_DEC_TTL(out);
207 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
208 ids->n_controllers = ntohs(nac_ids->n_controllers);
209 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
211 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
212 return OFPERR_NXBRC_MUST_BE_ZERO;
215 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
216 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
217 "allocated for controller ids. %zu bytes are required for "
218 "%"PRIu16" controllers.", ids_size,
219 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
220 return OFPERR_OFPBAC_BAD_LEN;
223 for (i = 0; i < ids->n_controllers; i++) {
224 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
225 ofpbuf_put(out, &id, sizeof id);
229 ofpact_update_len(out, &ids->ofpact);
235 sample_from_openflow(const struct nx_action_sample *nas,
238 struct ofpact_sample *sample;
240 sample = ofpact_put_SAMPLE(out);
241 sample->probability = ntohs(nas->probability);
242 sample->collector_set_id = ntohl(nas->collector_set_id);
243 sample->obs_domain_id = ntohl(nas->obs_domain_id);
244 sample->obs_point_id = ntohl(nas->obs_point_id);
246 if (sample->probability == 0) {
247 return OFPERR_OFPBAC_BAD_ARGUMENT;
254 push_mpls_from_openflow(ovs_be16 ethertype, enum ofpact_mpls_position position,
257 struct ofpact_push_mpls *oam;
259 if (!eth_type_mpls(ethertype)) {
260 return OFPERR_OFPBAC_BAD_ARGUMENT;
262 oam = ofpact_put_PUSH_MPLS(out);
263 oam->ethertype = ethertype;
264 oam->position = position;
270 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
272 const struct nx_action_header *nah = (const struct nx_action_header *) a;
273 uint16_t len = ntohs(a->header.len);
275 if (len < sizeof(struct nx_action_header)) {
276 return OFPERR_OFPBAC_BAD_LEN;
277 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
278 return OFPERR_OFPBAC_BAD_VENDOR;
281 switch (nah->subtype) {
282 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
283 case CONSTANT_HTONS(ENUM): \
285 ? len >= sizeof(struct STRUCT) \
286 : len == sizeof(struct STRUCT)) { \
287 *code = OFPUTIL_##ENUM; \
290 return OFPERR_OFPBAC_BAD_LEN; \
293 #include "ofp-util.def"
295 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
296 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
298 return OFPERR_OFPBAC_BAD_TYPE;
302 /* Parses 'a' to determine its type. On success stores the correct type into
303 * '*code' and returns 0. On failure returns an OFPERR_* error code and
304 * '*code' is indeterminate.
306 * The caller must have already verified that 'a''s length is potentially
307 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
308 * ofp_action) and no longer than the amount of space allocated to 'a').
310 * This function verifies that 'a''s length is correct for the type of action
311 * that it represents. */
313 decode_openflow10_action(const union ofp_action *a,
314 enum ofputil_action_code *code)
317 case CONSTANT_HTONS(OFPAT10_VENDOR):
318 return decode_nxast_action(a, code);
320 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
321 case CONSTANT_HTONS(ENUM): \
322 if (a->header.len == htons(sizeof(struct STRUCT))) { \
323 *code = OFPUTIL_##ENUM; \
326 return OFPERR_OFPBAC_BAD_LEN; \
329 #include "ofp-util.def"
332 return OFPERR_OFPBAC_BAD_TYPE;
337 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
340 const struct nx_action_resubmit *nar;
341 const struct nx_action_set_tunnel *nast;
342 const struct nx_action_set_queue *nasq;
343 const struct nx_action_note *nan;
344 const struct nx_action_set_tunnel64 *nast64;
345 const struct nx_action_write_metadata *nawm;
346 struct ofpact_tunnel *tunnel;
347 enum ofperr error = 0;
350 case OFPUTIL_ACTION_INVALID:
351 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
352 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
353 #include "ofp-util.def"
356 case OFPUTIL_NXAST_RESUBMIT:
357 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
360 case OFPUTIL_NXAST_SET_TUNNEL:
361 nast = (const struct nx_action_set_tunnel *) a;
362 tunnel = ofpact_put_SET_TUNNEL(out);
363 tunnel->ofpact.compat = code;
364 tunnel->tun_id = ntohl(nast->tun_id);
367 case OFPUTIL_NXAST_WRITE_METADATA:
368 nawm = ALIGNED_CAST(const struct nx_action_write_metadata *, a);
369 error = metadata_from_nxast(nawm, out);
372 case OFPUTIL_NXAST_SET_QUEUE:
373 nasq = (const struct nx_action_set_queue *) a;
374 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
377 case OFPUTIL_NXAST_POP_QUEUE:
378 ofpact_put_POP_QUEUE(out);
381 case OFPUTIL_NXAST_REG_MOVE:
382 error = nxm_reg_move_from_openflow(
383 (const struct nx_action_reg_move *) a, out);
386 case OFPUTIL_NXAST_REG_LOAD:
387 error = nxm_reg_load_from_openflow(
388 ALIGNED_CAST(const struct nx_action_reg_load *, a), out);
391 case OFPUTIL_NXAST_STACK_PUSH:
392 error = nxm_stack_push_from_openflow(
393 (const struct nx_action_stack *) a, out);
396 case OFPUTIL_NXAST_STACK_POP:
397 error = nxm_stack_pop_from_openflow(
398 (const struct nx_action_stack *) a, out);
401 case OFPUTIL_NXAST_NOTE:
402 nan = (const struct nx_action_note *) a;
403 note_from_openflow(nan, out);
406 case OFPUTIL_NXAST_SET_TUNNEL64:
407 nast64 = ALIGNED_CAST(const struct nx_action_set_tunnel64 *, a);
408 tunnel = ofpact_put_SET_TUNNEL(out);
409 tunnel->ofpact.compat = code;
410 tunnel->tun_id = ntohll(nast64->tun_id);
413 case OFPUTIL_NXAST_MULTIPATH:
414 error = multipath_from_openflow((const struct nx_action_multipath *) a,
415 ofpact_put_MULTIPATH(out));
418 case OFPUTIL_NXAST_BUNDLE:
419 case OFPUTIL_NXAST_BUNDLE_LOAD:
420 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
423 case OFPUTIL_NXAST_OUTPUT_REG:
424 error = output_reg_from_openflow(
425 (const struct nx_action_output_reg *) a, out);
428 case OFPUTIL_NXAST_RESUBMIT_TABLE:
429 nar = (const struct nx_action_resubmit *) a;
430 error = resubmit_table_from_openflow(nar, out);
433 case OFPUTIL_NXAST_LEARN:
434 error = learn_from_openflow(
435 ALIGNED_CAST(const struct nx_action_learn *, a), out);
438 case OFPUTIL_NXAST_EXIT:
439 ofpact_put_EXIT(out);
442 case OFPUTIL_NXAST_DEC_TTL:
443 error = dec_ttl_from_openflow(out, code);
446 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
447 error = dec_ttl_cnt_ids_from_openflow(
448 (const struct nx_action_cnt_ids *) a, out);
451 case OFPUTIL_NXAST_FIN_TIMEOUT:
452 fin_timeout_from_openflow(
453 (const struct nx_action_fin_timeout *) a, out);
456 case OFPUTIL_NXAST_CONTROLLER:
457 controller_from_openflow((const struct nx_action_controller *) a, out);
460 case OFPUTIL_NXAST_PUSH_MPLS: {
461 struct nx_action_push_mpls *nxapm = (struct nx_action_push_mpls *)a;
462 error = push_mpls_from_openflow(nxapm->ethertype,
463 OFPACT_MPLS_AFTER_VLAN, out);
467 case OFPUTIL_NXAST_SET_MPLS_TTL: {
468 struct nx_action_mpls_ttl *nxamt = (struct nx_action_mpls_ttl *)a;
469 ofpact_put_SET_MPLS_TTL(out)->ttl = nxamt->ttl;
473 case OFPUTIL_NXAST_DEC_MPLS_TTL:
474 ofpact_put_DEC_MPLS_TTL(out);
477 case OFPUTIL_NXAST_POP_MPLS: {
478 struct nx_action_pop_mpls *nxapm = (struct nx_action_pop_mpls *)a;
479 if (eth_type_mpls(nxapm->ethertype)) {
480 return OFPERR_OFPBAC_BAD_ARGUMENT;
482 ofpact_put_POP_MPLS(out)->ethertype = nxapm->ethertype;
486 case OFPUTIL_NXAST_SAMPLE:
487 error = sample_from_openflow(
488 (const struct nx_action_sample *) a, out);
496 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
498 enum ofputil_action_code code;
501 error = decode_openflow10_action(a, &code);
507 case OFPUTIL_ACTION_INVALID:
508 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
509 #include "ofp-util.def"
512 case OFPUTIL_OFPAT10_OUTPUT:
513 return output_from_openflow10(&a->output10, out);
515 case OFPUTIL_OFPAT10_SET_VLAN_VID:
516 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
517 return OFPERR_OFPBAC_BAD_ARGUMENT;
519 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
522 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
523 if (a->vlan_pcp.vlan_pcp & ~7) {
524 return OFPERR_OFPBAC_BAD_ARGUMENT;
526 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
529 case OFPUTIL_OFPAT10_STRIP_VLAN:
530 ofpact_put_STRIP_VLAN(out);
533 case OFPUTIL_OFPAT10_SET_DL_SRC:
534 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
535 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
538 case OFPUTIL_OFPAT10_SET_DL_DST:
539 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
540 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
543 case OFPUTIL_OFPAT10_SET_NW_SRC:
544 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
547 case OFPUTIL_OFPAT10_SET_NW_DST:
548 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
551 case OFPUTIL_OFPAT10_SET_NW_TOS:
552 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
553 return OFPERR_OFPBAC_BAD_ARGUMENT;
555 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
558 case OFPUTIL_OFPAT10_SET_TP_SRC:
559 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
562 case OFPUTIL_OFPAT10_SET_TP_DST:
563 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
567 case OFPUTIL_OFPAT10_ENQUEUE:
568 error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
572 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
573 #include "ofp-util.def"
574 return ofpact_from_nxast(a, code, out);
580 static inline union ofp_action *
581 action_next(const union ofp_action *a)
583 return ((union ofp_action *) (void *)
584 ((uint8_t *) a + ntohs(a->header.len)));
588 action_is_valid(const union ofp_action *a, size_t n_actions)
590 uint16_t len = ntohs(a->header.len);
591 return (!(len % OFP_ACTION_ALIGN)
593 && len / sizeof *a <= n_actions);
596 /* This macro is careful to check for actions with bad lengths. */
597 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
598 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
599 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
600 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
601 (ITER) = action_next(ITER)))
604 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
607 if (!VLOG_DROP_WARN(&rl)) {
611 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
612 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
613 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
619 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
621 enum ofperr (*ofpact_from_openflow)(
622 const union ofp_action *a, struct ofpbuf *out))
624 const union ofp_action *a;
627 ACTION_FOR_EACH (a, left, in, n_in) {
628 enum ofperr error = ofpact_from_openflow(a, out);
630 log_bad_action(in, n_in, a - in, error);
635 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
636 log_bad_action(in, n_in, n_in - left, error);
645 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
648 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
652 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
653 struct ofpbuf *ofpacts,
654 enum ofperr (*translate)(const union ofp_action *actions,
656 struct ofpbuf *ofpacts))
658 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
659 const union ofp_action *actions;
662 ofpbuf_clear(ofpacts);
664 if (actions_len % OFP_ACTION_ALIGN != 0) {
665 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
666 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
667 return OFPERR_OFPBRC_BAD_LEN;
670 actions = ofpbuf_try_pull(openflow, actions_len);
671 if (actions == NULL) {
672 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
673 "remaining message length (%zu)",
674 actions_len, openflow->size);
675 return OFPERR_OFPBRC_BAD_LEN;
678 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
680 ofpbuf_clear(ofpacts);
684 error = ofpacts_verify(ofpacts->data, ofpacts->size);
686 ofpbuf_clear(ofpacts);
691 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
692 * front of 'openflow' into ofpacts. On success, replaces any existing content
693 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
694 * Returns 0 if successful, otherwise an OpenFlow error.
696 * The parsed actions are valid generically, but they may not be valid in a
697 * specific context. For example, port numbers up to OFPP_MAX are valid
698 * generically, but specific datapaths may only support port numbers in a
699 * smaller range. Use ofpacts_check() to additional check whether actions are
700 * valid in a specific context. */
702 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
703 struct ofpbuf *ofpacts)
705 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
706 ofpacts_from_openflow10);
709 /* OpenFlow 1.1 actions. */
711 /* Parses 'a' to determine its type. On success stores the correct type into
712 * '*code' and returns 0. On failure returns an OFPERR_* error code and
713 * '*code' is indeterminate.
715 * The caller must have already verified that 'a''s length is potentially
716 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
717 * ofp_action) and no longer than the amount of space allocated to 'a').
719 * This function verifies that 'a''s length is correct for the type of action
720 * that it represents. */
722 decode_openflow11_action(const union ofp_action *a,
723 enum ofputil_action_code *code)
728 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
729 return decode_nxast_action(a, code);
731 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
732 case CONSTANT_HTONS(ENUM): \
733 len = ntohs(a->header.len); \
735 ? len >= sizeof(struct STRUCT) \
736 : len == sizeof(struct STRUCT)) { \
737 *code = OFPUTIL_##ENUM; \
740 return OFPERR_OFPBAC_BAD_LEN; \
743 #include "ofp-util.def"
746 return OFPERR_OFPBAC_BAD_TYPE;
751 output_from_openflow11(const struct ofp11_action_output *oao,
754 struct ofpact_output *output;
757 output = ofpact_put_OUTPUT(out);
758 output->max_len = ntohs(oao->max_len);
760 error = ofputil_port_from_ofp11(oao->port, &output->port);
765 return ofputil_check_output_port(output->port, OFPP_MAX);
769 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
771 enum ofputil_action_code code;
774 error = decode_openflow11_action(a, &code);
780 case OFPUTIL_ACTION_INVALID:
781 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
782 #include "ofp-util.def"
785 case OFPUTIL_OFPAT11_OUTPUT:
786 return output_from_openflow11((const struct ofp11_action_output *) a,
789 case OFPUTIL_OFPAT11_SET_VLAN_VID:
790 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
791 return OFPERR_OFPBAC_BAD_ARGUMENT;
793 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
796 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
797 if (a->vlan_pcp.vlan_pcp & ~7) {
798 return OFPERR_OFPBAC_BAD_ARGUMENT;
800 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
803 case OFPUTIL_OFPAT11_PUSH_VLAN:
804 if (((const struct ofp11_action_push *)a)->ethertype !=
805 htons(ETH_TYPE_VLAN_8021Q)) {
806 /* XXX 802.1AD(QinQ) isn't supported at the moment */
807 return OFPERR_OFPBAC_BAD_ARGUMENT;
809 ofpact_put_PUSH_VLAN(out);
812 case OFPUTIL_OFPAT11_POP_VLAN:
813 ofpact_put_STRIP_VLAN(out);
816 case OFPUTIL_OFPAT11_SET_QUEUE:
817 ofpact_put_SET_QUEUE(out)->queue_id =
818 ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
821 case OFPUTIL_OFPAT11_SET_DL_SRC:
822 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
823 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
826 case OFPUTIL_OFPAT11_SET_DL_DST:
827 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
828 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
831 case OFPUTIL_OFPAT11_DEC_NW_TTL:
832 dec_ttl_from_openflow(out, code);
835 case OFPUTIL_OFPAT11_SET_NW_SRC:
836 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
839 case OFPUTIL_OFPAT11_SET_NW_DST:
840 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
843 case OFPUTIL_OFPAT11_SET_NW_TOS:
844 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
845 return OFPERR_OFPBAC_BAD_ARGUMENT;
847 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
850 case OFPUTIL_OFPAT11_SET_TP_SRC:
851 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
854 case OFPUTIL_OFPAT11_SET_TP_DST:
855 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
858 case OFPUTIL_OFPAT12_SET_FIELD:
859 return nxm_reg_load_from_openflow12_set_field(
860 (const struct ofp12_action_set_field *)a, out);
862 case OFPUTIL_OFPAT11_SET_MPLS_TTL: {
863 struct ofp11_action_mpls_ttl *oamt = (struct ofp11_action_mpls_ttl *)a;
864 ofpact_put_SET_MPLS_TTL(out)->ttl = oamt->mpls_ttl;
868 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
869 ofpact_put_DEC_MPLS_TTL(out);
872 case OFPUTIL_OFPAT11_PUSH_MPLS: {
873 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
874 error = push_mpls_from_openflow(oap->ethertype,
875 OFPACT_MPLS_AFTER_VLAN, out);
879 case OFPUTIL_OFPAT11_POP_MPLS: {
880 struct ofp11_action_pop_mpls *oapm = (struct ofp11_action_pop_mpls *)a;
881 if (eth_type_mpls(oapm->ethertype)) {
882 return OFPERR_OFPBAC_BAD_ARGUMENT;
884 ofpact_put_POP_MPLS(out)->ethertype = oapm->ethertype;
888 case OFPUTIL_OFPAT11_GROUP: {
889 struct ofp11_action_group *oag = (struct ofp11_action_group *)a;
890 ofpact_put_GROUP(out)->group_id = ntohl(oag->group_id);
894 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
895 #include "ofp-util.def"
896 return ofpact_from_nxast(a, code, out);
903 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
906 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
909 /* True if an action sets the value of a field
910 * in a way that is compatibile with the action set.
911 * False otherwise. */
913 ofpact_is_set_action(const struct ofpact *a)
916 case OFPACT_REG_LOAD:
917 case OFPACT_SET_ETH_DST:
918 case OFPACT_SET_ETH_SRC:
919 case OFPACT_SET_IPV4_DSCP:
920 case OFPACT_SET_IPV4_DST:
921 case OFPACT_SET_IPV4_SRC:
922 case OFPACT_SET_L4_DST_PORT:
923 case OFPACT_SET_L4_SRC_PORT:
924 case OFPACT_SET_MPLS_TTL:
925 case OFPACT_SET_QUEUE:
926 case OFPACT_SET_TUNNEL:
927 case OFPACT_SET_VLAN_PCP:
928 case OFPACT_SET_VLAN_VID:
931 case OFPACT_CLEAR_ACTIONS:
932 case OFPACT_CONTROLLER:
933 case OFPACT_DEC_MPLS_TTL:
937 case OFPACT_FIN_TIMEOUT:
938 case OFPACT_GOTO_TABLE:
942 case OFPACT_MULTIPATH:
945 case OFPACT_OUTPUT_REG:
946 case OFPACT_POP_MPLS:
947 case OFPACT_POP_QUEUE:
948 case OFPACT_PUSH_MPLS:
949 case OFPACT_PUSH_VLAN:
950 case OFPACT_REG_MOVE:
951 case OFPACT_RESUBMIT:
953 case OFPACT_STACK_POP:
954 case OFPACT_STACK_PUSH:
955 case OFPACT_STRIP_VLAN:
956 case OFPACT_WRITE_ACTIONS:
957 case OFPACT_WRITE_METADATA:
964 /* True if an action is allowed in the action set.
965 * False otherwise. */
967 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
970 case OFPACT_DEC_MPLS_TTL:
974 case OFPACT_POP_MPLS:
975 case OFPACT_PUSH_MPLS:
976 case OFPACT_PUSH_VLAN:
977 case OFPACT_REG_LOAD:
978 case OFPACT_SET_ETH_DST:
979 case OFPACT_SET_ETH_SRC:
980 case OFPACT_SET_IPV4_DSCP:
981 case OFPACT_SET_IPV4_DST:
982 case OFPACT_SET_IPV4_SRC:
983 case OFPACT_SET_L4_DST_PORT:
984 case OFPACT_SET_L4_SRC_PORT:
985 case OFPACT_SET_MPLS_TTL:
986 case OFPACT_SET_QUEUE:
987 case OFPACT_SET_TUNNEL:
988 case OFPACT_SET_VLAN_PCP:
989 case OFPACT_SET_VLAN_VID:
990 case OFPACT_STRIP_VLAN:
993 /* In general these actions are excluded because they are not part of
994 * the OpenFlow specification nor map to actions that are defined in
995 * the specification. Thus the order in which they should be applied
996 * in the action set is undefined. */
998 case OFPACT_CONTROLLER:
1001 case OFPACT_FIN_TIMEOUT:
1003 case OFPACT_MULTIPATH:
1005 case OFPACT_OUTPUT_REG:
1006 case OFPACT_POP_QUEUE:
1007 case OFPACT_REG_MOVE:
1008 case OFPACT_RESUBMIT:
1010 case OFPACT_STACK_POP:
1011 case OFPACT_STACK_PUSH:
1013 /* The action set may only include actions and thus
1014 * may not include any instructions */
1015 case OFPACT_CLEAR_ACTIONS:
1016 case OFPACT_GOTO_TABLE:
1018 case OFPACT_WRITE_ACTIONS:
1019 case OFPACT_WRITE_METADATA:
1026 /* Append ofpact 'a' onto the tail of 'out' */
1028 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1030 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1033 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1035 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1036 enum ofpact_type filter)
1038 const struct ofpact *target;
1039 const struct ofpact *a;
1042 OFPACT_FOR_EACH (a, in->data, in->size) {
1043 if (a->type == filter) {
1048 ofpact_copy(out, target);
1050 return target != NULL;
1053 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1054 * The order of appended ofpacts is preserved between 'in' and 'out' */
1056 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1057 bool (*filter)(const struct ofpact *))
1059 const struct ofpact *a;
1061 OFPACT_FOR_EACH (a, in->data, in->size) {
1063 ofpact_copy(out, a);
1068 /* Reads 'action_set', which contains ofpacts accumulated by
1069 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1070 * executed directly into 'action_list'. (These names correspond to the
1071 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1073 * In general this involves appending the last instance of each action that is
1074 * adimissible in the action set in the order described in the OpenFlow
1078 * + output action is only appended if no group action was present in 'in'.
1079 * + As a simplification all set actions are copied in the order the are
1080 * provided in 'in' as many set actions applied to a field has the same
1081 * affect as only applying the last action that sets a field and
1082 * duplicates are removed by do_xlate_actions().
1083 * This has an unwanted side-effect of compsoting multiple
1084 * LOAD_REG actions that touch different regions of the same field. */
1086 ofpacts_execute_action_set(struct ofpbuf *action_list,
1087 const struct ofpbuf *action_set)
1089 /* The OpenFlow spec "Action Set" section specifies this order. */
1090 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1091 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1092 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1093 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1094 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1095 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1096 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1097 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1099 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1100 * we should execute only OFPACT_GROUP.
1102 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1103 * all the actions because there's no point in modifying a packet that will
1104 * not be sent anywhere. */
1105 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1106 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1107 ofpbuf_clear(action_list);
1113 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1114 size_t n_in, struct ofpbuf *out)
1118 size_t start = out->size;
1120 error = ofpacts_from_openflow11(in, n_in, out);
1125 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1126 if (!ofpact_is_allowed_in_actions_set(a)) {
1127 VLOG_WARN_RL(&rl, "disallowed action in action set");
1128 return OFPERR_OFPBAC_BAD_TYPE;
1137 ofpact_from_openflow13(const union ofp_action *a, struct ofpbuf *out)
1139 enum ofputil_action_code code;
1142 error = decode_openflow11_action(a, &code);
1147 if (code == OFPUTIL_OFPAT11_PUSH_MPLS) {
1148 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
1149 error = push_mpls_from_openflow(oap->ethertype,
1150 OFPACT_MPLS_BEFORE_VLAN, out);
1152 error = ofpact_from_openflow11(a, out);
1159 ofpacts_from_openflow13(const union ofp_action *in, size_t n_in,
1162 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow13);
1165 /* OpenFlow 1.1 instructions. */
1167 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1168 static inline const struct STRUCT * OVS_UNUSED \
1169 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1171 ovs_assert(inst->type == htons(ENUM)); \
1172 return ALIGNED_CAST(struct STRUCT *, inst); \
1175 static inline void OVS_UNUSED \
1176 instruction_init_##ENUM(struct STRUCT *s) \
1178 memset(s, 0, sizeof *s); \
1179 s->type = htons(ENUM); \
1180 s->len = htons(sizeof *s); \
1183 static inline struct STRUCT * OVS_UNUSED \
1184 instruction_put_##ENUM(struct ofpbuf *buf) \
1186 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1187 instruction_init_##ENUM(s); \
1193 struct instruction_type_info {
1194 enum ovs_instruction_type type;
1198 static const struct instruction_type_info inst_info[] = {
1199 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1205 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1207 return inst_info[type].name;
1211 ovs_instruction_type_from_name(const char *name)
1213 const struct instruction_type_info *p;
1214 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1215 if (!strcasecmp(name, p->name)) {
1222 enum ovs_instruction_type
1223 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1227 return OVSINST_OFPIT13_METER;
1228 case OFPACT_CLEAR_ACTIONS:
1229 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1230 case OFPACT_WRITE_ACTIONS:
1231 return OVSINST_OFPIT11_WRITE_ACTIONS;
1232 case OFPACT_WRITE_METADATA:
1233 return OVSINST_OFPIT11_WRITE_METADATA;
1234 case OFPACT_GOTO_TABLE:
1235 return OVSINST_OFPIT11_GOTO_TABLE;
1238 case OFPACT_CONTROLLER:
1239 case OFPACT_ENQUEUE:
1240 case OFPACT_OUTPUT_REG:
1242 case OFPACT_SET_VLAN_VID:
1243 case OFPACT_SET_VLAN_PCP:
1244 case OFPACT_STRIP_VLAN:
1245 case OFPACT_PUSH_VLAN:
1246 case OFPACT_SET_ETH_SRC:
1247 case OFPACT_SET_ETH_DST:
1248 case OFPACT_SET_IPV4_SRC:
1249 case OFPACT_SET_IPV4_DST:
1250 case OFPACT_SET_IPV4_DSCP:
1251 case OFPACT_SET_L4_SRC_PORT:
1252 case OFPACT_SET_L4_DST_PORT:
1253 case OFPACT_REG_MOVE:
1254 case OFPACT_REG_LOAD:
1255 case OFPACT_STACK_PUSH:
1256 case OFPACT_STACK_POP:
1257 case OFPACT_DEC_TTL:
1258 case OFPACT_SET_MPLS_TTL:
1259 case OFPACT_DEC_MPLS_TTL:
1260 case OFPACT_PUSH_MPLS:
1261 case OFPACT_POP_MPLS:
1262 case OFPACT_SET_TUNNEL:
1263 case OFPACT_SET_QUEUE:
1264 case OFPACT_POP_QUEUE:
1265 case OFPACT_FIN_TIMEOUT:
1266 case OFPACT_RESUBMIT:
1268 case OFPACT_MULTIPATH:
1273 return OVSINST_OFPIT11_APPLY_ACTIONS;
1277 static inline struct ofp11_instruction *
1278 instruction_next(const struct ofp11_instruction *inst)
1280 return ((struct ofp11_instruction *) (void *)
1281 ((uint8_t *) inst + ntohs(inst->len)));
1285 instruction_is_valid(const struct ofp11_instruction *inst,
1286 size_t n_instructions)
1288 uint16_t len = ntohs(inst->len);
1289 return (!(len % OFP11_INSTRUCTION_ALIGN)
1290 && len >= sizeof *inst
1291 && len / sizeof *inst <= n_instructions);
1294 /* This macro is careful to check for instructions with bad lengths. */
1295 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1296 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1297 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1298 ((LEFT) -= (ntohs((ITER)->len) \
1299 / sizeof(struct ofp11_instruction)), \
1300 (ITER) = instruction_next(ITER)))
1303 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1304 enum ovs_instruction_type *type)
1306 uint16_t len = ntohs(inst->len);
1308 switch (inst->type) {
1309 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1310 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1312 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1313 case CONSTANT_HTONS(ENUM): \
1315 ? len >= sizeof(struct STRUCT) \
1316 : len == sizeof(struct STRUCT)) { \
1317 *type = OVSINST_##ENUM; \
1320 return OFPERR_OFPBIC_BAD_LEN; \
1326 return OFPERR_OFPBIC_UNKNOWN_INST;
1331 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1333 const struct ofp11_instruction *out[])
1335 const struct ofp11_instruction *inst;
1338 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1339 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1340 enum ovs_instruction_type type;
1343 error = decode_openflow11_instruction(inst, &type);
1349 return OFPERR_ONFBIC_DUP_INSTRUCTION;
1355 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1356 (n_insts - left) * sizeof *inst);
1357 return OFPERR_OFPBIC_BAD_LEN;
1363 get_actions_from_instruction(const struct ofp11_instruction *inst,
1364 const union ofp_action **actions,
1367 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1368 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1371 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
1372 * front of 'openflow' into ofpacts. On success, replaces any existing content
1373 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1374 * Returns 0 if successful, otherwise an OpenFlow error.
1376 * Actions are processed according to their OpenFlow version which
1377 * is provided in the 'version' parameter.
1379 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1380 * instructions, so you should call ofpacts_pull_openflow11_instructions()
1381 * instead of this function.
1383 * The parsed actions are valid generically, but they may not be valid in a
1384 * specific context. For example, port numbers up to OFPP_MAX are valid
1385 * generically, but specific datapaths may only support port numbers in a
1386 * smaller range. Use ofpacts_check() to additional check whether actions are
1387 * valid in a specific context. */
1389 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1390 enum ofp_version version,
1391 unsigned int actions_len,
1392 struct ofpbuf *ofpacts)
1398 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1399 ofpacts_from_openflow11);
1401 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1402 ofpacts_from_openflow13);
1409 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1410 enum ofp_version version,
1411 unsigned int instructions_len,
1412 struct ofpbuf *ofpacts)
1414 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1415 const struct ofp11_instruction *instructions;
1416 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1419 ofpbuf_clear(ofpacts);
1421 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1422 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1424 instructions_len, OFP11_INSTRUCTION_ALIGN);
1425 error = OFPERR_OFPBIC_BAD_LEN;
1429 instructions = ofpbuf_try_pull(openflow, instructions_len);
1430 if (instructions == NULL) {
1431 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1432 "remaining message length (%zu)",
1433 instructions_len, openflow->size);
1434 error = OFPERR_OFPBIC_BAD_LEN;
1438 error = decode_openflow11_instructions(
1439 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1445 if (insts[OVSINST_OFPIT13_METER]) {
1446 const struct ofp13_instruction_meter *oim;
1447 struct ofpact_meter *om;
1449 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1450 insts[OVSINST_OFPIT13_METER]);
1452 om = ofpact_put_METER(ofpacts);
1453 om->meter_id = ntohl(oim->meter_id);
1455 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1456 const union ofp_action *actions;
1459 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1460 &actions, &n_actions);
1465 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1468 error = ofpacts_from_openflow13(actions, n_actions, ofpacts);
1477 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1478 instruction_get_OFPIT11_CLEAR_ACTIONS(
1479 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1480 ofpact_put_CLEAR_ACTIONS(ofpacts);
1482 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1483 struct ofpact_nest *on;
1484 const union ofp_action *actions;
1488 ofpact_pad(ofpacts);
1489 start = ofpacts->size;
1490 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1491 offsetof(struct ofpact_nest, actions));
1492 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1493 &actions, &n_actions);
1494 error = ofpacts_from_openflow11_for_action_set(actions, n_actions,
1499 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1500 on->ofpact.len = ofpacts->size - start;
1502 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1503 const struct ofp11_instruction_write_metadata *oiwm;
1504 struct ofpact_metadata *om;
1506 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1507 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1509 om = ofpact_put_WRITE_METADATA(ofpacts);
1510 om->metadata = oiwm->metadata;
1511 om->mask = oiwm->metadata_mask;
1513 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1514 const struct ofp11_instruction_goto_table *oigt;
1515 struct ofpact_goto_table *ogt;
1517 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1518 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1519 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1520 ogt->table_id = oigt->table_id;
1523 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1526 ofpbuf_clear(ofpacts);
1531 /* May modify flow->dl_type, caller must restore it. */
1533 ofpact_check__(const struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
1536 const struct ofpact_enqueue *enqueue;
1540 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1543 case OFPACT_CONTROLLER:
1546 case OFPACT_ENQUEUE:
1547 enqueue = ofpact_get_ENQUEUE(a);
1548 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1549 && enqueue->port != OFPP_IN_PORT
1550 && enqueue->port != OFPP_LOCAL) {
1551 return OFPERR_OFPBAC_BAD_OUT_PORT;
1555 case OFPACT_OUTPUT_REG:
1556 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1559 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1561 case OFPACT_SET_VLAN_VID:
1562 case OFPACT_SET_VLAN_PCP:
1563 case OFPACT_STRIP_VLAN:
1564 case OFPACT_PUSH_VLAN:
1565 case OFPACT_SET_ETH_SRC:
1566 case OFPACT_SET_ETH_DST:
1567 case OFPACT_SET_IPV4_SRC:
1568 case OFPACT_SET_IPV4_DST:
1569 case OFPACT_SET_IPV4_DSCP:
1570 case OFPACT_SET_L4_SRC_PORT:
1571 case OFPACT_SET_L4_DST_PORT:
1574 case OFPACT_REG_MOVE:
1575 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1577 case OFPACT_REG_LOAD:
1578 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1580 case OFPACT_STACK_PUSH:
1581 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1583 case OFPACT_STACK_POP:
1584 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1586 case OFPACT_DEC_TTL:
1587 case OFPACT_SET_MPLS_TTL:
1588 case OFPACT_DEC_MPLS_TTL:
1589 case OFPACT_SET_TUNNEL:
1590 case OFPACT_SET_QUEUE:
1591 case OFPACT_POP_QUEUE:
1592 case OFPACT_FIN_TIMEOUT:
1593 case OFPACT_RESUBMIT:
1597 return learn_check(ofpact_get_LEARN(a), flow);
1599 case OFPACT_MULTIPATH:
1600 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1606 case OFPACT_PUSH_MPLS:
1607 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1610 case OFPACT_POP_MPLS:
1611 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1617 case OFPACT_CLEAR_ACTIONS:
1620 case OFPACT_WRITE_ACTIONS: {
1621 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
1622 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
1623 flow, max_ports, table_id);
1626 case OFPACT_WRITE_METADATA:
1629 case OFPACT_METER: {
1630 uint32_t mid = ofpact_get_METER(a)->meter_id;
1631 if (mid == 0 || mid > OFPM13_MAX) {
1632 return OFPERR_OFPMMFC_INVALID_METER;
1637 case OFPACT_GOTO_TABLE:
1638 if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
1639 return OFPERR_OFPBRC_BAD_TABLE_ID;
1651 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1652 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1653 * switch with no more than 'max_ports' ports.
1655 * May temporarily modify 'flow', but restores the changes before returning. */
1657 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1658 struct flow *flow, ofp_port_t max_ports, uint8_t table_id)
1660 const struct ofpact *a;
1661 ovs_be16 dl_type = flow->dl_type;
1662 enum ofperr error = 0;
1664 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1665 error = ofpact_check__(a, flow, max_ports, table_id);
1670 flow->dl_type = dl_type; /* Restore. */
1674 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1675 * in the appropriate order as defined by the OpenFlow spec. */
1677 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1679 const struct ofpact *a;
1680 enum ovs_instruction_type inst;
1682 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1683 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1684 enum ovs_instruction_type next;
1686 next = ovs_instruction_type_from_ofpact_type(a->type);
1687 if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
1690 const char *name = ovs_instruction_name_from_type(inst);
1691 const char *next_name = ovs_instruction_name_from_type(next);
1694 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1695 "1.1+ compatibility", name);
1697 VLOG_WARN("invalid instruction ordering: %s must appear "
1698 "before %s, for OpenFlow 1.1+ compatibility",
1701 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1710 /* Converting ofpacts to Nicira OpenFlow extensions. */
1713 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1716 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1718 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1719 output_reg->src.n_bits);
1720 naor->src = htonl(output_reg->src.field->nxm_header);
1721 naor->max_len = htons(output_reg->max_len);
1725 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1728 struct nx_action_resubmit *nar;
1730 if (resubmit->table_id == 0xff
1731 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1732 nar = ofputil_put_NXAST_RESUBMIT(out);
1734 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1735 nar->table = resubmit->table_id;
1737 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
1741 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1744 uint64_t tun_id = tunnel->tun_id;
1746 if (tun_id <= UINT32_MAX
1747 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1748 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1750 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1755 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1758 struct nx_action_write_metadata *nawm;
1760 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1761 nawm->metadata = om->metadata;
1762 nawm->mask = om->mask;
1766 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1768 size_t start_ofs = out->size;
1769 struct nx_action_note *nan;
1770 unsigned int remainder;
1773 nan = ofputil_put_NXAST_NOTE(out);
1774 out->size -= sizeof nan->note;
1776 ofpbuf_put(out, note->data, note->length);
1778 len = out->size - start_ofs;
1779 remainder = len % OFP_ACTION_ALIGN;
1781 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1783 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
1784 nan->len = htons(out->size - start_ofs);
1788 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1791 struct nx_action_controller *nac;
1793 nac = ofputil_put_NXAST_CONTROLLER(out);
1794 nac->max_len = htons(oc->max_len);
1795 nac->controller_id = htons(oc->controller_id);
1796 nac->reason = oc->reason;
1800 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1803 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1804 ofputil_put_NXAST_DEC_TTL(out);
1806 struct nx_action_cnt_ids *nac_ids =
1807 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1808 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1812 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1813 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1815 ids = ofpbuf_put_zeros(out, ids_len);
1816 for (i = 0; i < oc_ids->n_controllers; i++) {
1817 ids[i] = htons(oc_ids->cnt_ids[i]);
1823 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1826 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1827 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1828 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1832 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1835 struct nx_action_sample *nas;
1837 nas = ofputil_put_NXAST_SAMPLE(out);
1838 nas->probability = htons(os->probability);
1839 nas->collector_set_id = htonl(os->collector_set_id);
1840 nas->obs_domain_id = htonl(os->obs_domain_id);
1841 nas->obs_point_id = htonl(os->obs_point_id);
1845 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1848 case OFPACT_CONTROLLER:
1849 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1852 case OFPACT_OUTPUT_REG:
1853 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1857 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1860 case OFPACT_REG_MOVE:
1861 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1864 case OFPACT_REG_LOAD:
1865 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1868 case OFPACT_STACK_PUSH:
1869 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1872 case OFPACT_STACK_POP:
1873 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1876 case OFPACT_DEC_TTL:
1877 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1880 case OFPACT_SET_MPLS_TTL:
1881 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1882 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1885 case OFPACT_DEC_MPLS_TTL:
1886 ofputil_put_NXAST_DEC_MPLS_TTL(out);
1889 case OFPACT_SET_TUNNEL:
1890 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1893 case OFPACT_WRITE_METADATA:
1894 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1897 case OFPACT_SET_QUEUE:
1898 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1899 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1902 case OFPACT_POP_QUEUE:
1903 ofputil_put_NXAST_POP_QUEUE(out);
1906 case OFPACT_FIN_TIMEOUT:
1907 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1910 case OFPACT_RESUBMIT:
1911 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1915 learn_to_nxast(ofpact_get_LEARN(a), out);
1918 case OFPACT_MULTIPATH:
1919 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1923 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1927 ofputil_put_NXAST_EXIT(out);
1930 case OFPACT_PUSH_MPLS:
1931 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1932 ofpact_get_PUSH_MPLS(a)->ethertype;
1935 case OFPACT_POP_MPLS:
1936 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1937 ofpact_get_POP_MPLS(a)->ethertype;
1941 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
1946 case OFPACT_ENQUEUE:
1947 case OFPACT_SET_VLAN_VID:
1948 case OFPACT_SET_VLAN_PCP:
1949 case OFPACT_STRIP_VLAN:
1950 case OFPACT_PUSH_VLAN:
1951 case OFPACT_SET_ETH_SRC:
1952 case OFPACT_SET_ETH_DST:
1953 case OFPACT_SET_IPV4_SRC:
1954 case OFPACT_SET_IPV4_DST:
1955 case OFPACT_SET_IPV4_DSCP:
1956 case OFPACT_SET_L4_SRC_PORT:
1957 case OFPACT_SET_L4_DST_PORT:
1958 case OFPACT_WRITE_ACTIONS:
1959 case OFPACT_CLEAR_ACTIONS:
1960 case OFPACT_GOTO_TABLE:
1966 /* Converting ofpacts to OpenFlow 1.0. */
1969 ofpact_output_to_openflow10(const struct ofpact_output *output,
1972 struct ofp10_action_output *oao;
1974 oao = ofputil_put_OFPAT10_OUTPUT(out);
1975 oao->port = htons(ofp_to_u16(output->port));
1976 oao->max_len = htons(output->max_len);
1980 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1983 struct ofp10_action_enqueue *oae;
1985 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1986 oae->port = htons(ofp_to_u16(enqueue->port));
1987 oae->queue_id = htonl(enqueue->queue);
1991 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1995 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1998 case OFPACT_ENQUEUE:
1999 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2002 case OFPACT_SET_VLAN_VID:
2003 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2004 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2007 case OFPACT_SET_VLAN_PCP:
2008 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2009 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2012 case OFPACT_STRIP_VLAN:
2013 ofputil_put_OFPAT10_STRIP_VLAN(out);
2016 case OFPACT_SET_ETH_SRC:
2017 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2018 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2021 case OFPACT_SET_ETH_DST:
2022 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2023 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2026 case OFPACT_SET_IPV4_SRC:
2027 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2028 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2031 case OFPACT_SET_IPV4_DST:
2032 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2033 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2036 case OFPACT_SET_IPV4_DSCP:
2037 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2038 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
2041 case OFPACT_SET_L4_SRC_PORT:
2042 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2043 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2046 case OFPACT_SET_L4_DST_PORT:
2047 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2048 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2051 case OFPACT_PUSH_VLAN:
2052 case OFPACT_CLEAR_ACTIONS:
2053 case OFPACT_WRITE_ACTIONS:
2054 case OFPACT_GOTO_TABLE:
2062 case OFPACT_CONTROLLER:
2063 case OFPACT_OUTPUT_REG:
2065 case OFPACT_REG_MOVE:
2066 case OFPACT_REG_LOAD:
2067 case OFPACT_STACK_PUSH:
2068 case OFPACT_STACK_POP:
2069 case OFPACT_DEC_TTL:
2070 case OFPACT_SET_MPLS_TTL:
2071 case OFPACT_DEC_MPLS_TTL:
2072 case OFPACT_SET_TUNNEL:
2073 case OFPACT_WRITE_METADATA:
2074 case OFPACT_SET_QUEUE:
2075 case OFPACT_POP_QUEUE:
2076 case OFPACT_FIN_TIMEOUT:
2077 case OFPACT_RESUBMIT:
2079 case OFPACT_MULTIPATH:
2082 case OFPACT_PUSH_MPLS:
2083 case OFPACT_POP_MPLS:
2085 ofpact_to_nxast(a, out);
2090 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
2091 * actions in 'openflow', appending the actions to any existing data in
2094 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
2095 struct ofpbuf *openflow)
2097 const struct ofpact *a;
2099 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2100 ofpact_to_openflow10(a, openflow);
2104 /* Converting ofpacts to OpenFlow 1.1. */
2107 ofpact_output_to_openflow11(const struct ofpact_output *output,
2110 struct ofp11_action_output *oao;
2112 oao = ofputil_put_OFPAT11_OUTPUT(out);
2113 oao->port = ofputil_port_to_ofp11(output->port);
2114 oao->max_len = htons(output->max_len);
2118 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2121 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2122 && (!dec_ttl->ofpact.compat ||
2123 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2124 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2126 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2131 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2135 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2137 case OFPACT_ENQUEUE:
2141 case OFPACT_SET_VLAN_VID:
2142 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2143 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2146 case OFPACT_SET_VLAN_PCP:
2147 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2148 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2151 case OFPACT_STRIP_VLAN:
2152 ofputil_put_OFPAT11_POP_VLAN(out);
2155 case OFPACT_PUSH_VLAN:
2156 /* XXX ETH_TYPE_VLAN_8021AD case */
2157 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2158 htons(ETH_TYPE_VLAN_8021Q);
2161 case OFPACT_SET_QUEUE:
2162 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2163 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2166 case OFPACT_SET_ETH_SRC:
2167 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2168 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2171 case OFPACT_SET_ETH_DST:
2172 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2173 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2176 case OFPACT_SET_IPV4_SRC:
2177 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2178 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2181 case OFPACT_SET_IPV4_DST:
2182 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2183 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2186 case OFPACT_SET_IPV4_DSCP:
2187 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2188 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
2191 case OFPACT_SET_L4_SRC_PORT:
2192 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2193 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2196 case OFPACT_SET_L4_DST_PORT:
2197 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2198 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2201 case OFPACT_DEC_TTL:
2202 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2205 case OFPACT_SET_MPLS_TTL:
2206 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2207 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2210 case OFPACT_DEC_MPLS_TTL:
2211 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2214 case OFPACT_WRITE_METADATA:
2215 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2218 case OFPACT_PUSH_MPLS:
2219 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2220 ofpact_get_PUSH_MPLS(a)->ethertype;
2223 case OFPACT_POP_MPLS:
2224 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2225 ofpact_get_POP_MPLS(a)->ethertype;
2229 case OFPACT_CLEAR_ACTIONS:
2230 case OFPACT_WRITE_ACTIONS:
2231 case OFPACT_GOTO_TABLE:
2236 ofputil_put_OFPAT11_GROUP(out)->group_id =
2237 htonl(ofpact_get_GROUP(a)->group_id);
2240 case OFPACT_CONTROLLER:
2241 case OFPACT_OUTPUT_REG:
2243 case OFPACT_REG_MOVE:
2244 case OFPACT_REG_LOAD:
2245 case OFPACT_STACK_PUSH:
2246 case OFPACT_STACK_POP:
2247 case OFPACT_SET_TUNNEL:
2248 case OFPACT_POP_QUEUE:
2249 case OFPACT_FIN_TIMEOUT:
2250 case OFPACT_RESUBMIT:
2252 case OFPACT_MULTIPATH:
2256 ofpact_to_nxast(a, out);
2261 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
2262 * 1.1 actions in 'openflow', appending the actions to any existing data in
2265 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
2266 size_t ofpacts_len, struct ofpbuf *openflow)
2268 const struct ofpact *a;
2269 size_t start_size = openflow->size;
2271 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2272 ofpact_to_openflow11(a, openflow);
2275 return openflow->size - start_size;
2279 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2281 struct ofp11_instruction_actions *oia;
2283 /* Update the instruction's length (or, if it's empty, delete it). */
2284 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2285 if (openflow->size > ofs + sizeof *oia) {
2286 oia->len = htons(openflow->size - ofs);
2288 openflow->size = ofs;
2293 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
2295 struct ofpbuf *openflow)
2297 const struct ofpact *a;
2299 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2300 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2301 case OVSINST_OFPIT11_CLEAR_ACTIONS:
2302 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2305 case OVSINST_OFPIT11_GOTO_TABLE: {
2306 struct ofp11_instruction_goto_table *oigt;
2307 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2308 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2309 memset(oigt->pad, 0, sizeof oigt->pad);
2313 case OVSINST_OFPIT11_WRITE_METADATA: {
2314 const struct ofpact_metadata *om;
2315 struct ofp11_instruction_write_metadata *oiwm;
2317 om = ofpact_get_WRITE_METADATA(a);
2318 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2319 oiwm->metadata = om->metadata;
2320 oiwm->metadata_mask = om->mask;
2324 case OVSINST_OFPIT13_METER: {
2325 const struct ofpact_meter *om;
2326 struct ofp13_instruction_meter *oim;
2328 om = ofpact_get_METER(a);
2329 oim = instruction_put_OFPIT13_METER(openflow);
2330 oim->meter_id = htonl(om->meter_id);
2334 case OVSINST_OFPIT11_APPLY_ACTIONS: {
2335 const size_t ofs = openflow->size;
2336 const size_t ofpacts_len_left =
2337 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2338 const struct ofpact *action;
2339 const struct ofpact *processed = a;
2341 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2342 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
2343 if (ovs_instruction_type_from_ofpact_type(action->type)
2344 != OVSINST_OFPIT11_APPLY_ACTIONS) {
2347 ofpact_to_openflow11(action, openflow);
2350 ofpacts_update_instruction_actions(openflow, ofs);
2355 case OVSINST_OFPIT11_WRITE_ACTIONS: {
2356 const size_t ofs = openflow->size;
2357 const struct ofpact_nest *on;
2359 on = ofpact_get_WRITE_ACTIONS(a);
2360 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2361 ofpacts_put_openflow11_actions(on->actions,
2362 ofpact_nest_get_action_len(on),
2364 ofpacts_update_instruction_actions(openflow, ofs);
2372 /* Returns true if 'action' outputs to 'port', false otherwise. */
2374 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
2376 switch (ofpact->type) {
2378 return ofpact_get_OUTPUT(ofpact)->port == port;
2379 case OFPACT_ENQUEUE:
2380 return ofpact_get_ENQUEUE(ofpact)->port == port;
2381 case OFPACT_CONTROLLER:
2382 return port == OFPP_CONTROLLER;
2384 case OFPACT_OUTPUT_REG:
2386 case OFPACT_SET_VLAN_VID:
2387 case OFPACT_SET_VLAN_PCP:
2388 case OFPACT_STRIP_VLAN:
2389 case OFPACT_PUSH_VLAN:
2390 case OFPACT_SET_ETH_SRC:
2391 case OFPACT_SET_ETH_DST:
2392 case OFPACT_SET_IPV4_SRC:
2393 case OFPACT_SET_IPV4_DST:
2394 case OFPACT_SET_IPV4_DSCP:
2395 case OFPACT_SET_L4_SRC_PORT:
2396 case OFPACT_SET_L4_DST_PORT:
2397 case OFPACT_REG_MOVE:
2398 case OFPACT_REG_LOAD:
2399 case OFPACT_STACK_PUSH:
2400 case OFPACT_STACK_POP:
2401 case OFPACT_DEC_TTL:
2402 case OFPACT_SET_MPLS_TTL:
2403 case OFPACT_DEC_MPLS_TTL:
2404 case OFPACT_SET_TUNNEL:
2405 case OFPACT_WRITE_METADATA:
2406 case OFPACT_SET_QUEUE:
2407 case OFPACT_POP_QUEUE:
2408 case OFPACT_FIN_TIMEOUT:
2409 case OFPACT_RESUBMIT:
2411 case OFPACT_MULTIPATH:
2414 case OFPACT_PUSH_MPLS:
2415 case OFPACT_POP_MPLS:
2417 case OFPACT_CLEAR_ACTIONS:
2418 case OFPACT_WRITE_ACTIONS:
2419 case OFPACT_GOTO_TABLE:
2427 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2428 * to 'port', false otherwise. */
2430 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
2433 const struct ofpact *a;
2435 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2436 if (ofpact_outputs_to_port(a, port)) {
2444 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2445 * to 'group', false otherwise. */
2447 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
2450 const struct ofpact *a;
2452 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2453 if (a->type == OFPACT_GROUP
2454 && ofpact_get_GROUP(a)->group_id == group_id) {
2463 ofpacts_equal(const struct ofpact *a, size_t a_len,
2464 const struct ofpact *b, size_t b_len)
2466 return a_len == b_len && !memcmp(a, b, a_len);
2469 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2470 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2472 * This function relies on the order of 'ofpacts' being correct (as checked by
2473 * ofpacts_verify()). */
2475 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2477 const struct ofpact *a;
2479 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2480 enum ovs_instruction_type inst;
2482 inst = ovs_instruction_type_from_ofpact_type(a->type);
2483 if (a->type == OFPACT_METER) {
2484 return ofpact_get_METER(a)->meter_id;
2485 } else if (inst > OVSINST_OFPIT13_METER) {
2493 /* Formatting ofpacts. */
2496 print_note(const struct ofpact_note *note, struct ds *string)
2500 ds_put_cstr(string, "note:");
2501 for (i = 0; i < note->length; i++) {
2503 ds_put_char(string, '.');
2505 ds_put_format(string, "%02"PRIx8, note->data[i]);
2510 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2515 ds_put_cstr(s, "dec_ttl");
2516 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2517 ds_put_cstr(s, "(");
2518 for (i = 0; i < ids->n_controllers; i++) {
2520 ds_put_cstr(s, ",");
2522 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2524 ds_put_cstr(s, ")");
2529 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2532 ds_put_cstr(s, "fin_timeout(");
2533 if (fin_timeout->fin_idle_timeout) {
2534 ds_put_format(s, "idle_timeout=%"PRIu16",",
2535 fin_timeout->fin_idle_timeout);
2537 if (fin_timeout->fin_hard_timeout) {
2538 ds_put_format(s, "hard_timeout=%"PRIu16",",
2539 fin_timeout->fin_hard_timeout);
2542 ds_put_char(s, ')');
2546 ofpact_format(const struct ofpact *a, struct ds *s)
2548 const struct ofpact_enqueue *enqueue;
2549 const struct ofpact_resubmit *resubmit;
2550 const struct ofpact_controller *controller;
2551 const struct ofpact_metadata *metadata;
2552 const struct ofpact_tunnel *tunnel;
2553 const struct ofpact_sample *sample;
2558 port = ofpact_get_OUTPUT(a)->port;
2559 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
2560 ds_put_format(s, "output:%"PRIu16, port);
2562 ofputil_format_port(port, s);
2563 if (port == OFPP_CONTROLLER) {
2564 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2569 case OFPACT_CONTROLLER:
2570 controller = ofpact_get_CONTROLLER(a);
2571 if (controller->reason == OFPR_ACTION &&
2572 controller->controller_id == 0) {
2573 ds_put_format(s, "CONTROLLER:%"PRIu16,
2574 ofpact_get_CONTROLLER(a)->max_len);
2576 enum ofp_packet_in_reason reason = controller->reason;
2578 ds_put_cstr(s, "controller(");
2579 if (reason != OFPR_ACTION) {
2580 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2582 ds_put_format(s, "reason=%s,",
2583 ofputil_packet_in_reason_to_string(
2584 reason, reasonbuf, sizeof reasonbuf));
2586 if (controller->max_len != UINT16_MAX) {
2587 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2589 if (controller->controller_id != 0) {
2590 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2593 ds_put_char(s, ')');
2597 case OFPACT_ENQUEUE:
2598 enqueue = ofpact_get_ENQUEUE(a);
2599 ds_put_format(s, "enqueue:");
2600 ofputil_format_port(enqueue->port, s);
2601 ds_put_format(s, "q%"PRIu32, enqueue->queue);
2604 case OFPACT_OUTPUT_REG:
2605 ds_put_cstr(s, "output:");
2606 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2610 bundle_format(ofpact_get_BUNDLE(a), s);
2613 case OFPACT_SET_VLAN_VID:
2614 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2615 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2618 case OFPACT_SET_VLAN_PCP:
2619 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2620 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2623 case OFPACT_STRIP_VLAN:
2624 ds_put_cstr(s, "strip_vlan");
2627 case OFPACT_PUSH_VLAN:
2628 /* XXX 802.1AD case*/
2629 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2632 case OFPACT_SET_ETH_SRC:
2633 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2634 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2637 case OFPACT_SET_ETH_DST:
2638 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2639 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2642 case OFPACT_SET_IPV4_SRC:
2643 ds_put_format(s, "mod_nw_src:"IP_FMT,
2644 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2647 case OFPACT_SET_IPV4_DST:
2648 ds_put_format(s, "mod_nw_dst:"IP_FMT,
2649 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2652 case OFPACT_SET_IPV4_DSCP:
2653 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
2656 case OFPACT_SET_L4_SRC_PORT:
2657 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2660 case OFPACT_SET_L4_DST_PORT:
2661 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2664 case OFPACT_REG_MOVE:
2665 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2668 case OFPACT_REG_LOAD:
2669 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2672 case OFPACT_STACK_PUSH:
2673 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2676 case OFPACT_STACK_POP:
2677 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2680 case OFPACT_DEC_TTL:
2681 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2684 case OFPACT_SET_MPLS_TTL:
2685 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2686 ofpact_get_SET_MPLS_TTL(a)->ttl);
2689 case OFPACT_DEC_MPLS_TTL:
2690 ds_put_cstr(s, "dec_mpls_ttl");
2693 case OFPACT_SET_TUNNEL:
2694 tunnel = ofpact_get_SET_TUNNEL(a);
2695 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2696 (tunnel->tun_id > UINT32_MAX
2697 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2701 case OFPACT_SET_QUEUE:
2702 ds_put_format(s, "set_queue:%"PRIu32,
2703 ofpact_get_SET_QUEUE(a)->queue_id);
2706 case OFPACT_POP_QUEUE:
2707 ds_put_cstr(s, "pop_queue");
2710 case OFPACT_FIN_TIMEOUT:
2711 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2714 case OFPACT_RESUBMIT:
2715 resubmit = ofpact_get_RESUBMIT(a);
2716 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2717 ds_put_cstr(s, "resubmit:");
2718 ofputil_format_port(resubmit->in_port, s);
2720 ds_put_format(s, "resubmit(");
2721 if (resubmit->in_port != OFPP_IN_PORT) {
2722 ofputil_format_port(resubmit->in_port, s);
2724 ds_put_char(s, ',');
2725 if (resubmit->table_id != 255) {
2726 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2728 ds_put_char(s, ')');
2733 learn_format(ofpact_get_LEARN(a), s);
2736 case OFPACT_MULTIPATH:
2737 multipath_format(ofpact_get_MULTIPATH(a), s);
2741 print_note(ofpact_get_NOTE(a), s);
2744 case OFPACT_PUSH_MPLS:
2745 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2746 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2749 case OFPACT_POP_MPLS:
2750 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2751 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2755 ds_put_cstr(s, "exit");
2759 sample = ofpact_get_SAMPLE(a);
2761 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2762 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2763 sample->probability, sample->collector_set_id,
2764 sample->obs_domain_id, sample->obs_point_id);
2767 case OFPACT_WRITE_ACTIONS: {
2768 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2769 ds_put_format(s, "%s(",
2770 ovs_instruction_name_from_type(
2771 OVSINST_OFPIT11_WRITE_ACTIONS));
2772 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
2773 ds_put_char(s, ')');
2777 case OFPACT_CLEAR_ACTIONS:
2778 ds_put_format(s, "%s",
2779 ovs_instruction_name_from_type(
2780 OVSINST_OFPIT11_CLEAR_ACTIONS));
2783 case OFPACT_WRITE_METADATA:
2784 metadata = ofpact_get_WRITE_METADATA(a);
2785 ds_put_format(s, "%s:%#"PRIx64,
2786 ovs_instruction_name_from_type(
2787 OVSINST_OFPIT11_WRITE_METADATA),
2788 ntohll(metadata->metadata));
2789 if (metadata->mask != OVS_BE64_MAX) {
2790 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2794 case OFPACT_GOTO_TABLE:
2795 ds_put_format(s, "%s:%"PRIu8,
2796 ovs_instruction_name_from_type(
2797 OVSINST_OFPIT11_GOTO_TABLE),
2798 ofpact_get_GOTO_TABLE(a)->table_id);
2802 ds_put_format(s, "%s:%"PRIu32,
2803 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
2804 ofpact_get_METER(a)->meter_id);
2808 ds_put_format(s, "group:%"PRIu32,
2809 ofpact_get_GROUP(a)->group_id);
2814 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2815 * 'ofpacts' to 'string'. */
2817 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2821 ds_put_cstr(string, "drop");
2823 const struct ofpact *a;
2825 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2827 ds_put_cstr(string, ",");
2830 /* XXX write-actions */
2831 ofpact_format(a, string);
2836 /* Internal use by helpers. */
2839 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2841 struct ofpact *ofpact;
2843 ofpact_pad(ofpacts);
2844 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2845 ofpact_init(ofpact, type, len);
2850 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2852 memset(ofpact, 0, len);
2853 ofpact->type = type;
2854 ofpact->compat = OFPUTIL_ACTION_INVALID;
2858 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2859 * starting at 'ofpact'.
2861 * This is the correct way to update a variable-length ofpact's length after
2862 * adding the variable-length part of the payload. (See the large comment
2863 * near the end of ofp-actions.h for more information.) */
2865 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2867 ovs_assert(ofpact == ofpacts->l2);
2868 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2871 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2872 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2873 * client must call this itself after adding the final ofpact to an array of
2876 * (The consequences of failing to call this function are probably not dire.
2877 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2878 * not dereference it. That's undefined behavior, technically, but it will not
2879 * cause a real problem on common systems. Still, it seems better to call
2882 ofpact_pad(struct ofpbuf *ofpacts)
2884 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2886 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2891 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2894 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2895 load->dst.field = mf;
2897 load->dst.n_bits = mf->n_bits;
2898 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2899 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);