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 ofp11_action_nw_ecn nw_ecn;
48 struct ofp11_action_nw_ttl nw_ttl;
49 struct ofp_action_tp_port tp_port;
50 struct ofp_action_dl_addr dl_addr;
51 struct ofp10_action_enqueue enqueue;
52 struct ofp11_action_output ofp11_output;
53 struct ofp11_action_push push;
54 struct ofp11_action_pop_mpls ofp11_pop_mpls;
55 struct ofp11_action_set_queue ofp11_set_queue;
56 struct ofp11_action_mpls_ttl ofp11_mpls_ttl;
57 struct ofp11_action_group group;
58 struct ofp12_action_set_field set_field;
59 struct nx_action_header nxa_header;
60 struct nx_action_resubmit resubmit;
61 struct nx_action_set_tunnel set_tunnel;
62 struct nx_action_set_tunnel64 set_tunnel64;
63 struct nx_action_write_metadata write_metadata;
64 struct nx_action_set_queue set_queue;
65 struct nx_action_reg_move reg_move;
66 struct nx_action_reg_load reg_load;
67 struct nx_action_stack stack;
68 struct nx_action_note note;
69 struct nx_action_multipath multipath;
70 struct nx_action_bundle bundle;
71 struct nx_action_output_reg output_reg;
72 struct nx_action_cnt_ids cnt_ids;
73 struct nx_action_fin_timeout fin_timeout;
74 struct nx_action_controller controller;
75 struct nx_action_push_mpls push_mpls;
76 struct nx_action_mpls_ttl mpls_ttl;
77 struct nx_action_pop_mpls pop_mpls;
78 struct nx_action_sample sample;
79 struct nx_action_learn learn;
83 output_from_openflow10(const struct ofp10_action_output *oao,
86 struct ofpact_output *output;
88 output = ofpact_put_OUTPUT(out);
89 output->port = u16_to_ofp(ntohs(oao->port));
90 output->max_len = ntohs(oao->max_len);
92 return ofputil_check_output_port(output->port, OFPP_MAX);
96 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
99 struct ofpact_enqueue *enqueue;
101 enqueue = ofpact_put_ENQUEUE(out);
102 enqueue->port = u16_to_ofp(ntohs(oae->port));
103 enqueue->queue = ntohl(oae->queue_id);
104 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
105 && enqueue->port != OFPP_IN_PORT
106 && enqueue->port != OFPP_LOCAL) {
107 return OFPERR_OFPBAC_BAD_OUT_PORT;
113 resubmit_from_openflow(const struct nx_action_resubmit *nar,
116 struct ofpact_resubmit *resubmit;
118 resubmit = ofpact_put_RESUBMIT(out);
119 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
120 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
121 resubmit->table_id = 0xff;
125 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
128 struct ofpact_resubmit *resubmit;
130 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
131 return OFPERR_OFPBAC_BAD_ARGUMENT;
134 resubmit = ofpact_put_RESUBMIT(out);
135 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
136 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
137 resubmit->table_id = nar->table;
142 output_reg_from_openflow(const struct nx_action_output_reg *naor,
145 struct ofpact_output_reg *output_reg;
147 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
148 return OFPERR_OFPBAC_BAD_ARGUMENT;
151 output_reg = ofpact_put_OUTPUT_REG(out);
152 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
153 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
154 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
155 output_reg->max_len = ntohs(naor->max_len);
157 return mf_check_src(&output_reg->src, NULL);
161 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
164 struct ofpact_fin_timeout *oft;
166 oft = ofpact_put_FIN_TIMEOUT(out);
167 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
168 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
172 controller_from_openflow(const struct nx_action_controller *nac,
175 struct ofpact_controller *oc;
177 oc = ofpact_put_CONTROLLER(out);
178 oc->max_len = ntohs(nac->max_len);
179 oc->controller_id = ntohs(nac->controller_id);
180 oc->reason = nac->reason;
184 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
187 struct ofpact_metadata *om;
189 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
190 return OFPERR_NXBRC_MUST_BE_ZERO;
193 om = ofpact_put_WRITE_METADATA(out);
194 om->metadata = nawm->metadata;
195 om->mask = nawm->mask;
201 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
203 struct ofpact_note *note;
206 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
207 note = ofpact_put(out, OFPACT_NOTE,
208 offsetof(struct ofpact_note, data) + length);
209 note->length = length;
210 memcpy(note->data, nan->note, length);
214 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
217 struct ofpact_cnt_ids *ids;
218 enum ofperr error = 0;
220 ids = ofpact_put_DEC_TTL(out);
221 ids->ofpact.compat = compat;
222 ids->n_controllers = 1;
223 ofpbuf_put(out, &id, sizeof id);
225 ofpact_update_len(out, &ids->ofpact);
230 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
233 struct ofpact_cnt_ids *ids;
237 ids = ofpact_put_DEC_TTL(out);
238 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
239 ids->n_controllers = ntohs(nac_ids->n_controllers);
240 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
242 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
243 return OFPERR_NXBRC_MUST_BE_ZERO;
246 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
247 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
248 "allocated for controller ids. %zu bytes are required for "
249 "%"PRIu16" controllers.", ids_size,
250 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
251 return OFPERR_OFPBAC_BAD_LEN;
254 for (i = 0; i < ids->n_controllers; i++) {
255 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
256 ofpbuf_put(out, &id, sizeof id);
260 ofpact_update_len(out, &ids->ofpact);
266 sample_from_openflow(const struct nx_action_sample *nas,
269 struct ofpact_sample *sample;
271 sample = ofpact_put_SAMPLE(out);
272 sample->probability = ntohs(nas->probability);
273 sample->collector_set_id = ntohl(nas->collector_set_id);
274 sample->obs_domain_id = ntohl(nas->obs_domain_id);
275 sample->obs_point_id = ntohl(nas->obs_point_id);
277 if (sample->probability == 0) {
278 return OFPERR_OFPBAC_BAD_ARGUMENT;
285 push_mpls_from_openflow(ovs_be16 ethertype, enum ofpact_mpls_position position,
288 struct ofpact_push_mpls *oam;
290 if (!eth_type_mpls(ethertype)) {
291 return OFPERR_OFPBAC_BAD_ARGUMENT;
293 oam = ofpact_put_PUSH_MPLS(out);
294 oam->ethertype = ethertype;
295 oam->position = position;
301 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
303 const struct nx_action_header *nah = &a->nxa_header;
304 uint16_t len = ntohs(a->header.len);
306 if (len < sizeof(struct nx_action_header)) {
307 return OFPERR_OFPBAC_BAD_LEN;
308 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
309 return OFPERR_OFPBAC_BAD_VENDOR;
312 switch (nah->subtype) {
313 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
314 case CONSTANT_HTONS(ENUM): \
316 ? len >= sizeof(struct STRUCT) \
317 : len == sizeof(struct STRUCT)) { \
318 *code = OFPUTIL_##ENUM; \
321 return OFPERR_OFPBAC_BAD_LEN; \
324 #include "ofp-util.def"
326 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
327 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
329 return OFPERR_OFPBAC_BAD_TYPE;
333 /* Parses 'a' to determine its type. On success stores the correct type into
334 * '*code' and returns 0. On failure returns an OFPERR_* error code and
335 * '*code' is indeterminate.
337 * The caller must have already verified that 'a''s length is potentially
338 * correct (that is, a->header.len is nonzero and a multiple of
339 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
341 * This function verifies that 'a''s length is correct for the type of action
342 * that it represents. */
344 decode_openflow10_action(const union ofp_action *a,
345 enum ofputil_action_code *code)
348 case CONSTANT_HTONS(OFPAT10_VENDOR):
349 return decode_nxast_action(a, code);
351 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
352 case CONSTANT_HTONS(ENUM): \
353 if (a->header.len == htons(sizeof(struct STRUCT))) { \
354 *code = OFPUTIL_##ENUM; \
357 return OFPERR_OFPBAC_BAD_LEN; \
360 #include "ofp-util.def"
363 return OFPERR_OFPBAC_BAD_TYPE;
368 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
371 struct ofpact_tunnel *tunnel;
372 enum ofperr error = 0;
375 case OFPUTIL_ACTION_INVALID:
376 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
377 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
378 #include "ofp-util.def"
381 case OFPUTIL_NXAST_RESUBMIT:
382 resubmit_from_openflow(&a->resubmit, out);
385 case OFPUTIL_NXAST_SET_TUNNEL:
386 tunnel = ofpact_put_SET_TUNNEL(out);
387 tunnel->ofpact.compat = code;
388 tunnel->tun_id = ntohl(a->set_tunnel.tun_id);
391 case OFPUTIL_NXAST_WRITE_METADATA:
392 error = metadata_from_nxast(&a->write_metadata, out);
395 case OFPUTIL_NXAST_SET_QUEUE:
396 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(a->set_queue.queue_id);
399 case OFPUTIL_NXAST_POP_QUEUE:
400 ofpact_put_POP_QUEUE(out);
403 case OFPUTIL_NXAST_REG_MOVE:
404 error = nxm_reg_move_from_openflow(&a->reg_move, out);
407 case OFPUTIL_NXAST_REG_LOAD:
408 error = nxm_reg_load_from_openflow(&a->reg_load, out);
411 case OFPUTIL_NXAST_STACK_PUSH:
412 error = nxm_stack_push_from_openflow(&a->stack, out);
415 case OFPUTIL_NXAST_STACK_POP:
416 error = nxm_stack_pop_from_openflow(&a->stack, out);
419 case OFPUTIL_NXAST_NOTE:
420 note_from_openflow(&a->note, out);
423 case OFPUTIL_NXAST_SET_TUNNEL64:
424 tunnel = ofpact_put_SET_TUNNEL(out);
425 tunnel->ofpact.compat = code;
426 tunnel->tun_id = ntohll(a->set_tunnel64.tun_id);
429 case OFPUTIL_NXAST_MULTIPATH:
430 error = multipath_from_openflow(&a->multipath,
431 ofpact_put_MULTIPATH(out));
434 case OFPUTIL_NXAST_BUNDLE:
435 case OFPUTIL_NXAST_BUNDLE_LOAD:
436 error = bundle_from_openflow(&a->bundle, out);
439 case OFPUTIL_NXAST_OUTPUT_REG:
440 error = output_reg_from_openflow(&a->output_reg, out);
443 case OFPUTIL_NXAST_RESUBMIT_TABLE:
444 error = resubmit_table_from_openflow(&a->resubmit, out);
447 case OFPUTIL_NXAST_LEARN:
448 error = learn_from_openflow(&a->learn, out);
451 case OFPUTIL_NXAST_EXIT:
452 ofpact_put_EXIT(out);
455 case OFPUTIL_NXAST_DEC_TTL:
456 error = dec_ttl_from_openflow(out, code);
459 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
460 error = dec_ttl_cnt_ids_from_openflow(&a->cnt_ids, out);
463 case OFPUTIL_NXAST_FIN_TIMEOUT:
464 fin_timeout_from_openflow(&a->fin_timeout, out);
467 case OFPUTIL_NXAST_CONTROLLER:
468 controller_from_openflow(&a->controller, out);
471 case OFPUTIL_NXAST_PUSH_MPLS:
472 error = push_mpls_from_openflow(a->push_mpls.ethertype,
473 OFPACT_MPLS_AFTER_VLAN, out);
476 case OFPUTIL_NXAST_SET_MPLS_TTL:
477 ofpact_put_SET_MPLS_TTL(out)->ttl = a->mpls_ttl.ttl;
480 case OFPUTIL_NXAST_DEC_MPLS_TTL:
481 ofpact_put_DEC_MPLS_TTL(out);
484 case OFPUTIL_NXAST_POP_MPLS:
485 if (eth_type_mpls(a->pop_mpls.ethertype)) {
486 return OFPERR_OFPBAC_BAD_ARGUMENT;
488 ofpact_put_POP_MPLS(out)->ethertype = a->pop_mpls.ethertype;
491 case OFPUTIL_NXAST_SAMPLE:
492 error = sample_from_openflow(&a->sample, out);
500 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
502 enum ofputil_action_code code;
504 struct ofpact_vlan_vid *vlan_vid;
505 struct ofpact_vlan_pcp *vlan_pcp;
507 error = decode_openflow10_action(a, &code);
513 case OFPUTIL_ACTION_INVALID:
514 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
515 #include "ofp-util.def"
518 case OFPUTIL_OFPAT10_OUTPUT:
519 return output_from_openflow10(&a->output10, out);
521 case OFPUTIL_OFPAT10_SET_VLAN_VID:
522 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
523 return OFPERR_OFPBAC_BAD_ARGUMENT;
525 vlan_vid = ofpact_put_SET_VLAN_VID(out);
526 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
527 vlan_vid->push_vlan_if_needed = true;
528 vlan_vid->ofpact.compat = code;
531 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
532 if (a->vlan_pcp.vlan_pcp & ~7) {
533 return OFPERR_OFPBAC_BAD_ARGUMENT;
535 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
536 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
537 vlan_pcp->push_vlan_if_needed = true;
538 vlan_pcp->ofpact.compat = code;
541 case OFPUTIL_OFPAT10_STRIP_VLAN:
542 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
545 case OFPUTIL_OFPAT10_SET_DL_SRC:
546 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
550 case OFPUTIL_OFPAT10_SET_DL_DST:
551 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
555 case OFPUTIL_OFPAT10_SET_NW_SRC:
556 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
559 case OFPUTIL_OFPAT10_SET_NW_DST:
560 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
563 case OFPUTIL_OFPAT10_SET_NW_TOS:
564 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
565 return OFPERR_OFPBAC_BAD_ARGUMENT;
567 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
570 case OFPUTIL_OFPAT10_SET_TP_SRC:
571 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
574 case OFPUTIL_OFPAT10_SET_TP_DST:
575 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
579 case OFPUTIL_OFPAT10_ENQUEUE:
580 error = enqueue_from_openflow10(&a->enqueue, out);
583 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
584 #include "ofp-util.def"
585 return ofpact_from_nxast(a, code, out);
591 static inline union ofp_action *
592 action_next(const union ofp_action *a)
594 return ((union ofp_action *) (void *)
595 ((uint8_t *) a + ntohs(a->header.len)));
599 action_is_valid(const union ofp_action *a, size_t max_actions)
601 uint16_t len = ntohs(a->header.len);
602 return (!(len % OFP_ACTION_ALIGN)
603 && len >= OFP_ACTION_ALIGN
604 && len / OFP_ACTION_ALIGN <= max_actions);
607 /* This macro is careful to check for actions with bad lengths. */
608 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, MAX_ACTIONS) \
609 for ((ITER) = (ACTIONS), (LEFT) = (MAX_ACTIONS); \
610 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
611 ((LEFT) -= ntohs((ITER)->header.len) / OFP_ACTION_ALIGN, \
612 (ITER) = action_next(ITER)))
615 log_bad_action(const union ofp_action *actions, size_t max_actions,
616 const union ofp_action *bad_action, enum ofperr error)
618 if (!VLOG_DROP_WARN(&rl)) {
622 ds_put_hex_dump(&s, actions, max_actions * OFP_ACTION_ALIGN, 0, false);
623 VLOG_WARN("bad action at offset %#tx (%s):\n%s",
624 (char *)bad_action - (char *)actions,
625 ofperr_get_name(error), ds_cstr(&s));
631 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
633 enum ofperr (*ofpact_from_openflow)(
634 const union ofp_action *a, struct ofpbuf *out))
636 const union ofp_action *a;
639 ACTION_FOR_EACH (a, left, in, n_in) {
640 enum ofperr error = ofpact_from_openflow(a, out);
642 log_bad_action(in, n_in, a, error);
647 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
648 log_bad_action(in, n_in, a, error);
657 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
660 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
664 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
665 struct ofpbuf *ofpacts,
666 enum ofperr (*translate)(const union ofp_action *actions,
668 struct ofpbuf *ofpacts))
670 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
671 const union ofp_action *actions;
674 ofpbuf_clear(ofpacts);
676 if (actions_len % OFP_ACTION_ALIGN != 0) {
677 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
678 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
679 return OFPERR_OFPBRC_BAD_LEN;
682 actions = ofpbuf_try_pull(openflow, actions_len);
683 if (actions == NULL) {
684 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
685 "remaining message length (%zu)",
686 actions_len, openflow->size);
687 return OFPERR_OFPBRC_BAD_LEN;
690 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
692 ofpbuf_clear(ofpacts);
696 error = ofpacts_verify(ofpacts->data, ofpacts->size);
698 ofpbuf_clear(ofpacts);
703 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
704 * front of 'openflow' into ofpacts. On success, replaces any existing content
705 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
706 * Returns 0 if successful, otherwise an OpenFlow error.
708 * The parsed actions are valid generically, but they may not be valid in a
709 * specific context. For example, port numbers up to OFPP_MAX are valid
710 * generically, but specific datapaths may only support port numbers in a
711 * smaller range. Use ofpacts_check() to additional check whether actions are
712 * valid in a specific context. */
714 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
715 struct ofpbuf *ofpacts)
717 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
718 ofpacts_from_openflow10);
721 /* OpenFlow 1.1 actions. */
723 /* Parses 'a' to determine its type. On success stores the correct type into
724 * '*code' and returns 0. On failure returns an OFPERR_* error code and
725 * '*code' is indeterminate.
727 * The caller must have already verified that 'a''s length is potentially
728 * correct (that is, a->header.len is nonzero and a multiple of
729 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
731 * This function verifies that 'a''s length is correct for the type of action
732 * that it represents. */
734 decode_openflow11_action(const union ofp_action *a,
735 enum ofputil_action_code *code)
740 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
741 return decode_nxast_action(a, code);
743 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
744 case CONSTANT_HTONS(ENUM): \
745 len = ntohs(a->header.len); \
747 ? len >= sizeof(struct STRUCT) \
748 : len == sizeof(struct STRUCT)) { \
749 *code = OFPUTIL_##ENUM; \
752 return OFPERR_OFPBAC_BAD_LEN; \
755 #include "ofp-util.def"
758 return OFPERR_OFPBAC_BAD_TYPE;
763 output_from_openflow11(const struct ofp11_action_output *oao,
766 struct ofpact_output *output;
769 output = ofpact_put_OUTPUT(out);
770 output->max_len = ntohs(oao->max_len);
772 error = ofputil_port_from_ofp11(oao->port, &output->port);
777 return ofputil_check_output_port(output->port, OFPP_MAX);
781 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
783 enum ofputil_action_code code;
785 struct ofpact_vlan_vid *vlan_vid;
786 struct ofpact_vlan_pcp *vlan_pcp;
788 error = decode_openflow11_action(a, &code);
794 case OFPUTIL_ACTION_INVALID:
795 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
796 #include "ofp-util.def"
799 case OFPUTIL_OFPAT11_OUTPUT:
800 return output_from_openflow11(&a->ofp11_output, out);
802 case OFPUTIL_OFPAT11_SET_VLAN_VID:
803 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
804 return OFPERR_OFPBAC_BAD_ARGUMENT;
806 vlan_vid = ofpact_put_SET_VLAN_VID(out);
807 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
808 vlan_vid->push_vlan_if_needed = false;
809 vlan_vid->ofpact.compat = code;
812 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
813 if (a->vlan_pcp.vlan_pcp & ~7) {
814 return OFPERR_OFPBAC_BAD_ARGUMENT;
816 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
817 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
818 vlan_pcp->push_vlan_if_needed = false;
819 vlan_pcp->ofpact.compat = code;
822 case OFPUTIL_OFPAT11_PUSH_VLAN:
823 if (a->push.ethertype != htons(ETH_TYPE_VLAN_8021Q)) {
824 /* XXX 802.1AD(QinQ) isn't supported at the moment */
825 return OFPERR_OFPBAC_BAD_ARGUMENT;
827 ofpact_put_PUSH_VLAN(out);
830 case OFPUTIL_OFPAT11_POP_VLAN:
831 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
834 case OFPUTIL_OFPAT11_SET_QUEUE:
835 ofpact_put_SET_QUEUE(out)->queue_id =
836 ntohl(a->ofp11_set_queue.queue_id);
839 case OFPUTIL_OFPAT11_SET_DL_SRC:
840 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
844 case OFPUTIL_OFPAT11_SET_DL_DST:
845 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
849 case OFPUTIL_OFPAT11_DEC_NW_TTL:
850 dec_ttl_from_openflow(out, code);
853 case OFPUTIL_OFPAT11_SET_NW_SRC:
854 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
857 case OFPUTIL_OFPAT11_SET_NW_DST:
858 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
861 case OFPUTIL_OFPAT11_SET_NW_TOS:
862 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
863 return OFPERR_OFPBAC_BAD_ARGUMENT;
865 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
868 case OFPUTIL_OFPAT11_SET_NW_ECN:
869 if (a->nw_ecn.nw_ecn & ~IP_ECN_MASK) {
870 return OFPERR_OFPBAC_BAD_ARGUMENT;
872 ofpact_put_SET_IP_ECN(out)->ecn = a->nw_ecn.nw_ecn;
875 case OFPUTIL_OFPAT11_SET_NW_TTL:
876 ofpact_put_SET_IP_TTL(out)->ttl = a->nw_ttl.nw_ttl;
879 case OFPUTIL_OFPAT11_SET_TP_SRC:
880 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
883 case OFPUTIL_OFPAT11_SET_TP_DST:
884 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
887 case OFPUTIL_OFPAT12_SET_FIELD:
888 return nxm_reg_load_from_openflow12_set_field(&a->set_field, out);
890 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
891 ofpact_put_SET_MPLS_TTL(out)->ttl = a->ofp11_mpls_ttl.mpls_ttl;
894 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
895 ofpact_put_DEC_MPLS_TTL(out);
898 case OFPUTIL_OFPAT11_PUSH_MPLS:
899 error = push_mpls_from_openflow(a->push.ethertype,
900 OFPACT_MPLS_AFTER_VLAN, out);
903 case OFPUTIL_OFPAT11_POP_MPLS:
904 if (eth_type_mpls(a->ofp11_pop_mpls.ethertype)) {
905 return OFPERR_OFPBAC_BAD_ARGUMENT;
907 ofpact_put_POP_MPLS(out)->ethertype = a->ofp11_pop_mpls.ethertype;
910 case OFPUTIL_OFPAT11_GROUP:
911 ofpact_put_GROUP(out)->group_id = ntohl(a->group.group_id);
914 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
915 #include "ofp-util.def"
916 return ofpact_from_nxast(a, code, out);
923 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
926 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
929 /* True if an action sets the value of a field
930 * in a way that is compatibile with the action set.
931 * False otherwise. */
933 ofpact_is_set_action(const struct ofpact *a)
936 case OFPACT_REG_LOAD:
937 case OFPACT_SET_ETH_DST:
938 case OFPACT_SET_ETH_SRC:
939 case OFPACT_SET_IP_DSCP:
940 case OFPACT_SET_IP_ECN:
941 case OFPACT_SET_IP_TTL:
942 case OFPACT_SET_IPV4_DST:
943 case OFPACT_SET_IPV4_SRC:
944 case OFPACT_SET_L4_DST_PORT:
945 case OFPACT_SET_L4_SRC_PORT:
946 case OFPACT_SET_MPLS_TTL:
947 case OFPACT_SET_QUEUE:
948 case OFPACT_SET_TUNNEL:
949 case OFPACT_SET_VLAN_PCP:
950 case OFPACT_SET_VLAN_VID:
953 case OFPACT_CLEAR_ACTIONS:
954 case OFPACT_CONTROLLER:
955 case OFPACT_DEC_MPLS_TTL:
959 case OFPACT_FIN_TIMEOUT:
960 case OFPACT_GOTO_TABLE:
964 case OFPACT_MULTIPATH:
967 case OFPACT_OUTPUT_REG:
968 case OFPACT_POP_MPLS:
969 case OFPACT_POP_QUEUE:
970 case OFPACT_PUSH_MPLS:
971 case OFPACT_PUSH_VLAN:
972 case OFPACT_REG_MOVE:
973 case OFPACT_RESUBMIT:
975 case OFPACT_STACK_POP:
976 case OFPACT_STACK_PUSH:
977 case OFPACT_STRIP_VLAN:
978 case OFPACT_WRITE_ACTIONS:
979 case OFPACT_WRITE_METADATA:
986 /* True if an action is allowed in the action set.
987 * False otherwise. */
989 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
992 case OFPACT_DEC_MPLS_TTL:
996 case OFPACT_POP_MPLS:
997 case OFPACT_PUSH_MPLS:
998 case OFPACT_PUSH_VLAN:
999 case OFPACT_REG_LOAD:
1000 case OFPACT_SET_ETH_DST:
1001 case OFPACT_SET_ETH_SRC:
1002 case OFPACT_SET_IP_DSCP:
1003 case OFPACT_SET_IP_ECN:
1004 case OFPACT_SET_IP_TTL:
1005 case OFPACT_SET_IPV4_DST:
1006 case OFPACT_SET_IPV4_SRC:
1007 case OFPACT_SET_L4_DST_PORT:
1008 case OFPACT_SET_L4_SRC_PORT:
1009 case OFPACT_SET_MPLS_TTL:
1010 case OFPACT_SET_QUEUE:
1011 case OFPACT_SET_TUNNEL:
1012 case OFPACT_SET_VLAN_PCP:
1013 case OFPACT_SET_VLAN_VID:
1014 case OFPACT_STRIP_VLAN:
1017 /* In general these actions are excluded because they are not part of
1018 * the OpenFlow specification nor map to actions that are defined in
1019 * the specification. Thus the order in which they should be applied
1020 * in the action set is undefined. */
1022 case OFPACT_CONTROLLER:
1023 case OFPACT_ENQUEUE:
1025 case OFPACT_FIN_TIMEOUT:
1027 case OFPACT_MULTIPATH:
1029 case OFPACT_OUTPUT_REG:
1030 case OFPACT_POP_QUEUE:
1031 case OFPACT_REG_MOVE:
1032 case OFPACT_RESUBMIT:
1034 case OFPACT_STACK_POP:
1035 case OFPACT_STACK_PUSH:
1037 /* The action set may only include actions and thus
1038 * may not include any instructions */
1039 case OFPACT_CLEAR_ACTIONS:
1040 case OFPACT_GOTO_TABLE:
1042 case OFPACT_WRITE_ACTIONS:
1043 case OFPACT_WRITE_METADATA:
1050 /* Append ofpact 'a' onto the tail of 'out' */
1052 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1054 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1057 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1059 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1060 enum ofpact_type filter)
1062 const struct ofpact *target;
1063 const struct ofpact *a;
1066 OFPACT_FOR_EACH (a, in->data, in->size) {
1067 if (a->type == filter) {
1072 ofpact_copy(out, target);
1074 return target != NULL;
1077 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1078 * The order of appended ofpacts is preserved between 'in' and 'out' */
1080 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1081 bool (*filter)(const struct ofpact *))
1083 const struct ofpact *a;
1085 OFPACT_FOR_EACH (a, in->data, in->size) {
1087 ofpact_copy(out, a);
1092 /* Reads 'action_set', which contains ofpacts accumulated by
1093 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1094 * executed directly into 'action_list'. (These names correspond to the
1095 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1097 * In general this involves appending the last instance of each action that is
1098 * adimissible in the action set in the order described in the OpenFlow
1102 * + output action is only appended if no group action was present in 'in'.
1103 * + As a simplification all set actions are copied in the order the are
1104 * provided in 'in' as many set actions applied to a field has the same
1105 * affect as only applying the last action that sets a field and
1106 * duplicates are removed by do_xlate_actions().
1107 * This has an unwanted side-effect of compsoting multiple
1108 * LOAD_REG actions that touch different regions of the same field. */
1110 ofpacts_execute_action_set(struct ofpbuf *action_list,
1111 const struct ofpbuf *action_set)
1113 /* The OpenFlow spec "Action Set" section specifies this order. */
1114 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1115 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1116 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1117 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1118 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1119 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1120 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1121 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1123 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1124 * we should execute only OFPACT_GROUP.
1126 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1127 * all the actions because there's no point in modifying a packet that will
1128 * not be sent anywhere. */
1129 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1130 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1131 ofpbuf_clear(action_list);
1137 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1138 size_t n_in, struct ofpbuf *out)
1142 size_t start = out->size;
1144 error = ofpacts_from_openflow11(in, n_in, out);
1149 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1150 if (!ofpact_is_allowed_in_actions_set(a)) {
1151 VLOG_WARN_RL(&rl, "disallowed action in action set");
1152 return OFPERR_OFPBAC_BAD_TYPE;
1161 ofpact_from_openflow13(const union ofp_action *a, struct ofpbuf *out)
1163 enum ofputil_action_code code;
1166 error = decode_openflow11_action(a, &code);
1171 if (code == OFPUTIL_OFPAT11_PUSH_MPLS) {
1172 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
1173 error = push_mpls_from_openflow(oap->ethertype,
1174 OFPACT_MPLS_BEFORE_VLAN, out);
1176 error = ofpact_from_openflow11(a, out);
1183 ofpacts_from_openflow13(const union ofp_action *in, size_t n_in,
1186 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow13);
1189 /* OpenFlow 1.1 instructions. */
1191 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1192 static inline const struct STRUCT * OVS_UNUSED \
1193 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1195 ovs_assert(inst->type == htons(ENUM)); \
1196 return ALIGNED_CAST(struct STRUCT *, inst); \
1199 static inline void OVS_UNUSED \
1200 instruction_init_##ENUM(struct STRUCT *s) \
1202 memset(s, 0, sizeof *s); \
1203 s->type = htons(ENUM); \
1204 s->len = htons(sizeof *s); \
1207 static inline struct STRUCT * OVS_UNUSED \
1208 instruction_put_##ENUM(struct ofpbuf *buf) \
1210 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1211 instruction_init_##ENUM(s); \
1217 struct instruction_type_info {
1218 enum ovs_instruction_type type;
1222 static const struct instruction_type_info inst_info[] = {
1223 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1229 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1231 return inst_info[type].name;
1235 ovs_instruction_type_from_name(const char *name)
1237 const struct instruction_type_info *p;
1238 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1239 if (!strcasecmp(name, p->name)) {
1246 enum ovs_instruction_type
1247 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1251 return OVSINST_OFPIT13_METER;
1252 case OFPACT_CLEAR_ACTIONS:
1253 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1254 case OFPACT_WRITE_ACTIONS:
1255 return OVSINST_OFPIT11_WRITE_ACTIONS;
1256 case OFPACT_WRITE_METADATA:
1257 return OVSINST_OFPIT11_WRITE_METADATA;
1258 case OFPACT_GOTO_TABLE:
1259 return OVSINST_OFPIT11_GOTO_TABLE;
1262 case OFPACT_CONTROLLER:
1263 case OFPACT_ENQUEUE:
1264 case OFPACT_OUTPUT_REG:
1266 case OFPACT_SET_VLAN_VID:
1267 case OFPACT_SET_VLAN_PCP:
1268 case OFPACT_STRIP_VLAN:
1269 case OFPACT_PUSH_VLAN:
1270 case OFPACT_SET_ETH_SRC:
1271 case OFPACT_SET_ETH_DST:
1272 case OFPACT_SET_IPV4_SRC:
1273 case OFPACT_SET_IPV4_DST:
1274 case OFPACT_SET_IP_DSCP:
1275 case OFPACT_SET_IP_ECN:
1276 case OFPACT_SET_IP_TTL:
1277 case OFPACT_SET_L4_SRC_PORT:
1278 case OFPACT_SET_L4_DST_PORT:
1279 case OFPACT_REG_MOVE:
1280 case OFPACT_REG_LOAD:
1281 case OFPACT_STACK_PUSH:
1282 case OFPACT_STACK_POP:
1283 case OFPACT_DEC_TTL:
1284 case OFPACT_SET_MPLS_TTL:
1285 case OFPACT_DEC_MPLS_TTL:
1286 case OFPACT_PUSH_MPLS:
1287 case OFPACT_POP_MPLS:
1288 case OFPACT_SET_TUNNEL:
1289 case OFPACT_SET_QUEUE:
1290 case OFPACT_POP_QUEUE:
1291 case OFPACT_FIN_TIMEOUT:
1292 case OFPACT_RESUBMIT:
1294 case OFPACT_MULTIPATH:
1299 return OVSINST_OFPIT11_APPLY_ACTIONS;
1303 static inline struct ofp11_instruction *
1304 instruction_next(const struct ofp11_instruction *inst)
1306 return ((struct ofp11_instruction *) (void *)
1307 ((uint8_t *) inst + ntohs(inst->len)));
1311 instruction_is_valid(const struct ofp11_instruction *inst,
1312 size_t n_instructions)
1314 uint16_t len = ntohs(inst->len);
1315 return (!(len % OFP11_INSTRUCTION_ALIGN)
1316 && len >= sizeof *inst
1317 && len / sizeof *inst <= n_instructions);
1320 /* This macro is careful to check for instructions with bad lengths. */
1321 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1322 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1323 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1324 ((LEFT) -= (ntohs((ITER)->len) \
1325 / sizeof(struct ofp11_instruction)), \
1326 (ITER) = instruction_next(ITER)))
1329 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1330 enum ovs_instruction_type *type)
1332 uint16_t len = ntohs(inst->len);
1334 switch (inst->type) {
1335 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1336 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1338 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1339 case CONSTANT_HTONS(ENUM): \
1341 ? len >= sizeof(struct STRUCT) \
1342 : len == sizeof(struct STRUCT)) { \
1343 *type = OVSINST_##ENUM; \
1346 return OFPERR_OFPBIC_BAD_LEN; \
1352 return OFPERR_OFPBIC_UNKNOWN_INST;
1357 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1359 const struct ofp11_instruction *out[])
1361 const struct ofp11_instruction *inst;
1364 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1365 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1366 enum ovs_instruction_type type;
1369 error = decode_openflow11_instruction(inst, &type);
1375 return OFPERR_ONFBIC_DUP_INSTRUCTION;
1381 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1382 (n_insts - left) * sizeof *inst);
1383 return OFPERR_OFPBIC_BAD_LEN;
1389 get_actions_from_instruction(const struct ofp11_instruction *inst,
1390 const union ofp_action **actions,
1391 size_t *max_actions)
1393 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1394 *max_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1397 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
1398 * front of 'openflow' into ofpacts. On success, replaces any existing content
1399 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1400 * Returns 0 if successful, otherwise an OpenFlow error.
1402 * Actions are processed according to their OpenFlow version which
1403 * is provided in the 'version' parameter.
1405 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1406 * instructions, so you should call ofpacts_pull_openflow11_instructions()
1407 * instead of this function.
1409 * The parsed actions are valid generically, but they may not be valid in a
1410 * specific context. For example, port numbers up to OFPP_MAX are valid
1411 * generically, but specific datapaths may only support port numbers in a
1412 * smaller range. Use ofpacts_check() to additional check whether actions are
1413 * valid in a specific context. */
1415 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1416 enum ofp_version version,
1417 unsigned int actions_len,
1418 struct ofpbuf *ofpacts)
1424 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1425 ofpacts_from_openflow11);
1427 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1428 ofpacts_from_openflow13);
1435 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1436 enum ofp_version version,
1437 unsigned int instructions_len,
1438 struct ofpbuf *ofpacts)
1440 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1441 const struct ofp11_instruction *instructions;
1442 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1445 ofpbuf_clear(ofpacts);
1447 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1448 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1450 instructions_len, OFP11_INSTRUCTION_ALIGN);
1451 error = OFPERR_OFPBIC_BAD_LEN;
1455 instructions = ofpbuf_try_pull(openflow, instructions_len);
1456 if (instructions == NULL) {
1457 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1458 "remaining message length (%zu)",
1459 instructions_len, openflow->size);
1460 error = OFPERR_OFPBIC_BAD_LEN;
1464 error = decode_openflow11_instructions(
1465 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1471 if (insts[OVSINST_OFPIT13_METER]) {
1472 const struct ofp13_instruction_meter *oim;
1473 struct ofpact_meter *om;
1475 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1476 insts[OVSINST_OFPIT13_METER]);
1478 om = ofpact_put_METER(ofpacts);
1479 om->meter_id = ntohl(oim->meter_id);
1481 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1482 const union ofp_action *actions;
1485 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1486 &actions, &max_actions);
1491 error = ofpacts_from_openflow11(actions, max_actions, ofpacts);
1494 error = ofpacts_from_openflow13(actions, max_actions, ofpacts);
1503 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1504 instruction_get_OFPIT11_CLEAR_ACTIONS(
1505 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1506 ofpact_put_CLEAR_ACTIONS(ofpacts);
1508 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1509 struct ofpact_nest *on;
1510 const union ofp_action *actions;
1514 ofpact_pad(ofpacts);
1515 start = ofpacts->size;
1516 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1517 offsetof(struct ofpact_nest, actions));
1518 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1519 &actions, &max_actions);
1520 error = ofpacts_from_openflow11_for_action_set(actions, max_actions,
1525 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1526 on->ofpact.len = ofpacts->size - start;
1528 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1529 const struct ofp11_instruction_write_metadata *oiwm;
1530 struct ofpact_metadata *om;
1532 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1533 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1535 om = ofpact_put_WRITE_METADATA(ofpacts);
1536 om->metadata = oiwm->metadata;
1537 om->mask = oiwm->metadata_mask;
1539 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1540 const struct ofp11_instruction_goto_table *oigt;
1541 struct ofpact_goto_table *ogt;
1543 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1544 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1545 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1546 ogt->table_id = oigt->table_id;
1549 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1552 ofpbuf_clear(ofpacts);
1557 /* May modify flow->dl_type and flow->vlan_tci, caller must restore them.
1559 * Modifies some actions, filling in fields that could not be properly set
1560 * without context. */
1562 ofpact_check__(struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
1563 uint8_t table_id, bool enforce_consistency)
1565 const struct ofpact_enqueue *enqueue;
1569 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1572 case OFPACT_CONTROLLER:
1575 case OFPACT_ENQUEUE:
1576 enqueue = ofpact_get_ENQUEUE(a);
1577 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1578 && enqueue->port != OFPP_IN_PORT
1579 && enqueue->port != OFPP_LOCAL) {
1580 return OFPERR_OFPBAC_BAD_OUT_PORT;
1584 case OFPACT_OUTPUT_REG:
1585 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1588 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1590 case OFPACT_SET_VLAN_VID:
1591 /* Remember if we saw a vlan tag in the flow to aid translating to
1592 * OpenFlow 1.1+ if need be. */
1593 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
1594 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1595 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1596 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
1599 /* Temporary mark that we have a vlan tag. */
1600 flow->vlan_tci |= htons(VLAN_CFI);
1603 case OFPACT_SET_VLAN_PCP:
1604 /* Remember if we saw a vlan tag in the flow to aid translating to
1605 * OpenFlow 1.1+ if need be. */
1606 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
1607 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1608 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1609 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
1612 /* Temporary mark that we have a vlan tag. */
1613 flow->vlan_tci |= htons(VLAN_CFI);
1616 case OFPACT_STRIP_VLAN:
1617 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
1620 /* Temporary mark that we have no vlan tag. */
1621 flow->vlan_tci = htons(0);
1624 case OFPACT_PUSH_VLAN:
1625 if (flow->vlan_tci & htons(VLAN_CFI)) {
1626 /* Multiple VLAN headers not supported. */
1627 return OFPERR_OFPBAC_BAD_TAG;
1629 /* Temporary mark that we have a vlan tag. */
1630 flow->vlan_tci |= htons(VLAN_CFI);
1633 case OFPACT_SET_ETH_SRC:
1634 case OFPACT_SET_ETH_DST:
1637 case OFPACT_SET_IPV4_SRC:
1638 case OFPACT_SET_IPV4_DST:
1639 if (flow->dl_type != htons(ETH_TYPE_IP)) {
1644 case OFPACT_SET_IP_DSCP:
1645 case OFPACT_SET_IP_ECN:
1646 case OFPACT_SET_IP_TTL:
1647 case OFPACT_DEC_TTL:
1648 if (!is_ip_any(flow)) {
1653 case OFPACT_SET_L4_SRC_PORT:
1654 case OFPACT_SET_L4_DST_PORT:
1655 if (!is_ip_any(flow) ||
1656 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1657 && flow->nw_proto != IPPROTO_SCTP)) {
1662 case OFPACT_REG_MOVE:
1663 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1665 case OFPACT_REG_LOAD:
1666 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1668 case OFPACT_STACK_PUSH:
1669 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1671 case OFPACT_STACK_POP:
1672 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1674 case OFPACT_SET_MPLS_TTL:
1675 case OFPACT_DEC_MPLS_TTL:
1676 if (!eth_type_mpls(flow->dl_type)) {
1681 case OFPACT_SET_TUNNEL:
1682 case OFPACT_SET_QUEUE:
1683 case OFPACT_POP_QUEUE:
1684 case OFPACT_RESUBMIT:
1687 case OFPACT_FIN_TIMEOUT:
1688 if (flow->nw_proto != IPPROTO_TCP) {
1694 return learn_check(ofpact_get_LEARN(a), flow);
1696 case OFPACT_MULTIPATH:
1697 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1703 case OFPACT_PUSH_MPLS:
1704 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1707 case OFPACT_POP_MPLS:
1708 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1709 if (!eth_type_mpls(flow->dl_type)) {
1717 case OFPACT_CLEAR_ACTIONS:
1720 case OFPACT_WRITE_ACTIONS: {
1721 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
1722 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
1723 flow, max_ports, table_id, false);
1726 case OFPACT_WRITE_METADATA:
1729 case OFPACT_METER: {
1730 uint32_t mid = ofpact_get_METER(a)->meter_id;
1731 if (mid == 0 || mid > OFPM13_MAX) {
1732 return OFPERR_OFPMMFC_INVALID_METER;
1737 case OFPACT_GOTO_TABLE:
1738 if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
1739 return OFPERR_OFPBRC_BAD_TABLE_ID;
1751 if (enforce_consistency) {
1752 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1757 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1758 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1759 * switch with no more than 'max_ports' ports.
1761 * May annotate ofpacts with information gathered from the 'flow'.
1763 * May temporarily modify 'flow', but restores the changes before returning. */
1765 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
1766 struct flow *flow, ofp_port_t max_ports, uint8_t table_id,
1767 bool enforce_consistency)
1770 ovs_be16 dl_type = flow->dl_type;
1771 ovs_be16 vlan_tci = flow->vlan_tci;
1772 enum ofperr error = 0;
1774 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1775 error = ofpact_check__(a, flow, max_ports, table_id,
1776 enforce_consistency);
1781 /* Restore fields that may have been modified. */
1782 flow->dl_type = dl_type;
1783 flow->vlan_tci = vlan_tci;
1787 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1788 * in the appropriate order as defined by the OpenFlow spec. */
1790 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1792 const struct ofpact *a;
1793 enum ovs_instruction_type inst;
1795 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1796 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1797 enum ovs_instruction_type next;
1799 next = ovs_instruction_type_from_ofpact_type(a->type);
1800 if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
1803 const char *name = ovs_instruction_name_from_type(inst);
1804 const char *next_name = ovs_instruction_name_from_type(next);
1807 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1808 "1.1+ compatibility", name);
1810 VLOG_WARN("invalid instruction ordering: %s must appear "
1811 "before %s, for OpenFlow 1.1+ compatibility",
1814 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1823 /* Converting ofpacts to Nicira OpenFlow extensions. */
1826 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1829 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1831 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1832 output_reg->src.n_bits);
1833 naor->src = htonl(output_reg->src.field->nxm_header);
1834 naor->max_len = htons(output_reg->max_len);
1838 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1841 struct nx_action_resubmit *nar;
1843 if (resubmit->table_id == 0xff
1844 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1845 nar = ofputil_put_NXAST_RESUBMIT(out);
1847 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1848 nar->table = resubmit->table_id;
1850 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
1854 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1857 uint64_t tun_id = tunnel->tun_id;
1859 if (tun_id <= UINT32_MAX
1860 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1861 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1863 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1868 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1871 struct nx_action_write_metadata *nawm;
1873 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1874 nawm->metadata = om->metadata;
1875 nawm->mask = om->mask;
1879 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1881 size_t start_ofs = out->size;
1882 struct nx_action_note *nan;
1883 unsigned int remainder;
1886 nan = ofputil_put_NXAST_NOTE(out);
1887 out->size -= sizeof nan->note;
1889 ofpbuf_put(out, note->data, note->length);
1891 len = out->size - start_ofs;
1892 remainder = len % OFP_ACTION_ALIGN;
1894 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1896 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
1897 nan->len = htons(out->size - start_ofs);
1901 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1904 struct nx_action_controller *nac;
1906 nac = ofputil_put_NXAST_CONTROLLER(out);
1907 nac->max_len = htons(oc->max_len);
1908 nac->controller_id = htons(oc->controller_id);
1909 nac->reason = oc->reason;
1913 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1916 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1917 ofputil_put_NXAST_DEC_TTL(out);
1919 struct nx_action_cnt_ids *nac_ids =
1920 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1921 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1925 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1926 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1928 ids = ofpbuf_put_zeros(out, ids_len);
1929 for (i = 0; i < oc_ids->n_controllers; i++) {
1930 ids[i] = htons(oc_ids->cnt_ids[i]);
1936 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1939 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1940 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1941 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1945 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1948 struct nx_action_sample *nas;
1950 nas = ofputil_put_NXAST_SAMPLE(out);
1951 nas->probability = htons(os->probability);
1952 nas->collector_set_id = htonl(os->collector_set_id);
1953 nas->obs_domain_id = htonl(os->obs_domain_id);
1954 nas->obs_point_id = htonl(os->obs_point_id);
1958 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1961 case OFPACT_CONTROLLER:
1962 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1965 case OFPACT_OUTPUT_REG:
1966 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1970 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1973 case OFPACT_REG_MOVE:
1974 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1977 case OFPACT_REG_LOAD:
1978 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1981 case OFPACT_STACK_PUSH:
1982 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1985 case OFPACT_STACK_POP:
1986 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1989 case OFPACT_DEC_TTL:
1990 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1993 case OFPACT_SET_MPLS_TTL:
1994 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1995 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1998 case OFPACT_DEC_MPLS_TTL:
1999 ofputil_put_NXAST_DEC_MPLS_TTL(out);
2002 case OFPACT_SET_TUNNEL:
2003 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
2006 case OFPACT_WRITE_METADATA:
2007 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
2010 case OFPACT_SET_QUEUE:
2011 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
2012 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2015 case OFPACT_POP_QUEUE:
2016 ofputil_put_NXAST_POP_QUEUE(out);
2019 case OFPACT_FIN_TIMEOUT:
2020 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
2023 case OFPACT_RESUBMIT:
2024 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
2028 learn_to_nxast(ofpact_get_LEARN(a), out);
2031 case OFPACT_MULTIPATH:
2032 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
2036 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
2040 ofputil_put_NXAST_EXIT(out);
2043 case OFPACT_PUSH_MPLS:
2044 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
2045 ofpact_get_PUSH_MPLS(a)->ethertype;
2048 case OFPACT_POP_MPLS:
2049 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2050 ofpact_get_POP_MPLS(a)->ethertype;
2054 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2059 case OFPACT_ENQUEUE:
2060 case OFPACT_SET_VLAN_VID:
2061 case OFPACT_SET_VLAN_PCP:
2062 case OFPACT_STRIP_VLAN:
2063 case OFPACT_PUSH_VLAN:
2064 case OFPACT_SET_ETH_SRC:
2065 case OFPACT_SET_ETH_DST:
2066 case OFPACT_SET_IPV4_SRC:
2067 case OFPACT_SET_IPV4_DST:
2068 case OFPACT_SET_IP_DSCP:
2069 case OFPACT_SET_IP_ECN:
2070 case OFPACT_SET_IP_TTL:
2071 case OFPACT_SET_L4_SRC_PORT:
2072 case OFPACT_SET_L4_DST_PORT:
2073 case OFPACT_WRITE_ACTIONS:
2074 case OFPACT_CLEAR_ACTIONS:
2075 case OFPACT_GOTO_TABLE:
2081 /* Converting ofpacts to OpenFlow 1.0. */
2084 ofpact_output_to_openflow10(const struct ofpact_output *output,
2087 struct ofp10_action_output *oao;
2089 oao = ofputil_put_OFPAT10_OUTPUT(out);
2090 oao->port = htons(ofp_to_u16(output->port));
2091 oao->max_len = htons(output->max_len);
2095 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2098 struct ofp10_action_enqueue *oae;
2100 oae = ofputil_put_OFPAT10_ENQUEUE(out);
2101 oae->port = htons(ofp_to_u16(enqueue->port));
2102 oae->queue_id = htonl(enqueue->queue);
2106 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2110 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2113 case OFPACT_ENQUEUE:
2114 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2117 case OFPACT_SET_VLAN_VID:
2118 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2119 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2122 case OFPACT_SET_VLAN_PCP:
2123 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2124 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2127 case OFPACT_STRIP_VLAN:
2128 ofputil_put_OFPAT10_STRIP_VLAN(out);
2131 case OFPACT_SET_ETH_SRC:
2132 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2133 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2136 case OFPACT_SET_ETH_DST:
2137 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2138 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2141 case OFPACT_SET_IPV4_SRC:
2142 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2143 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2146 case OFPACT_SET_IPV4_DST:
2147 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2148 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2151 case OFPACT_SET_IP_DSCP:
2152 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2153 = ofpact_get_SET_IP_DSCP(a)->dscp;
2156 case OFPACT_SET_L4_SRC_PORT:
2157 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2158 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2161 case OFPACT_SET_L4_DST_PORT:
2162 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2163 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2166 case OFPACT_PUSH_VLAN:
2167 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
2168 * follow this action. */
2171 case OFPACT_CLEAR_ACTIONS:
2172 case OFPACT_WRITE_ACTIONS:
2173 case OFPACT_GOTO_TABLE:
2181 case OFPACT_CONTROLLER:
2182 case OFPACT_OUTPUT_REG:
2184 case OFPACT_REG_MOVE:
2185 case OFPACT_REG_LOAD:
2186 case OFPACT_STACK_PUSH:
2187 case OFPACT_STACK_POP:
2188 case OFPACT_DEC_TTL:
2189 case OFPACT_SET_IP_ECN:
2190 case OFPACT_SET_IP_TTL:
2191 case OFPACT_SET_MPLS_TTL:
2192 case OFPACT_DEC_MPLS_TTL:
2193 case OFPACT_SET_TUNNEL:
2194 case OFPACT_WRITE_METADATA:
2195 case OFPACT_SET_QUEUE:
2196 case OFPACT_POP_QUEUE:
2197 case OFPACT_FIN_TIMEOUT:
2198 case OFPACT_RESUBMIT:
2200 case OFPACT_MULTIPATH:
2203 case OFPACT_PUSH_MPLS:
2204 case OFPACT_POP_MPLS:
2206 ofpact_to_nxast(a, out);
2211 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
2212 * actions in 'openflow', appending the actions to any existing data in
2215 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
2216 struct ofpbuf *openflow)
2218 const struct ofpact *a;
2220 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2221 ofpact_to_openflow10(a, openflow);
2225 /* Converting ofpacts to OpenFlow 1.1. */
2228 ofpact_output_to_openflow11(const struct ofpact_output *output,
2231 struct ofp11_action_output *oao;
2233 oao = ofputil_put_OFPAT11_OUTPUT(out);
2234 oao->port = ofputil_port_to_ofp11(output->port);
2235 oao->max_len = htons(output->max_len);
2239 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2242 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2243 && (!dec_ttl->ofpact.compat ||
2244 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2245 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2247 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2252 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2256 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2258 case OFPACT_ENQUEUE:
2262 case OFPACT_SET_VLAN_VID:
2263 /* Push a VLAN tag, if one was not seen at action validation time. */
2264 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan
2265 && ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2266 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2267 = htons(ETH_TYPE_VLAN_8021Q);
2269 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2270 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2273 case OFPACT_SET_VLAN_PCP:
2274 /* Push a VLAN tag, if one was not seen at action validation time. */
2275 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan
2276 && ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2277 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2278 = htons(ETH_TYPE_VLAN_8021Q);
2280 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2281 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2284 case OFPACT_STRIP_VLAN:
2285 ofputil_put_OFPAT11_POP_VLAN(out);
2288 case OFPACT_PUSH_VLAN:
2289 /* XXX ETH_TYPE_VLAN_8021AD case */
2290 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2291 htons(ETH_TYPE_VLAN_8021Q);
2294 case OFPACT_SET_QUEUE:
2295 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2296 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2299 case OFPACT_SET_ETH_SRC:
2300 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2301 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2304 case OFPACT_SET_ETH_DST:
2305 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2306 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2309 case OFPACT_SET_IPV4_SRC:
2310 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2311 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2314 case OFPACT_SET_IPV4_DST:
2315 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2316 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2319 case OFPACT_SET_IP_DSCP:
2320 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2321 = ofpact_get_SET_IP_DSCP(a)->dscp;
2324 case OFPACT_SET_IP_ECN:
2325 ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2326 = ofpact_get_SET_IP_ECN(a)->ecn;
2329 case OFPACT_SET_IP_TTL:
2330 ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2331 = ofpact_get_SET_IP_TTL(a)->ttl;
2334 case OFPACT_SET_L4_SRC_PORT:
2335 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2336 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2339 case OFPACT_SET_L4_DST_PORT:
2340 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2341 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2344 case OFPACT_DEC_TTL:
2345 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2348 case OFPACT_SET_MPLS_TTL:
2349 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2350 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2353 case OFPACT_DEC_MPLS_TTL:
2354 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2357 case OFPACT_WRITE_METADATA:
2358 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2361 case OFPACT_PUSH_MPLS:
2362 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2363 ofpact_get_PUSH_MPLS(a)->ethertype;
2366 case OFPACT_POP_MPLS:
2367 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2368 ofpact_get_POP_MPLS(a)->ethertype;
2372 case OFPACT_CLEAR_ACTIONS:
2373 case OFPACT_WRITE_ACTIONS:
2374 case OFPACT_GOTO_TABLE:
2379 ofputil_put_OFPAT11_GROUP(out)->group_id =
2380 htonl(ofpact_get_GROUP(a)->group_id);
2383 case OFPACT_CONTROLLER:
2384 case OFPACT_OUTPUT_REG:
2386 case OFPACT_REG_MOVE:
2387 case OFPACT_REG_LOAD:
2388 case OFPACT_STACK_PUSH:
2389 case OFPACT_STACK_POP:
2390 case OFPACT_SET_TUNNEL:
2391 case OFPACT_POP_QUEUE:
2392 case OFPACT_FIN_TIMEOUT:
2393 case OFPACT_RESUBMIT:
2395 case OFPACT_MULTIPATH:
2399 ofpact_to_nxast(a, out);
2404 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
2405 * 1.1 actions in 'openflow', appending the actions to any existing data in
2408 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
2409 size_t ofpacts_len, struct ofpbuf *openflow)
2411 const struct ofpact *a;
2412 size_t start_size = openflow->size;
2414 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2415 ofpact_to_openflow11(a, openflow);
2418 return openflow->size - start_size;
2422 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2424 struct ofp11_instruction_actions *oia;
2426 /* Update the instruction's length (or, if it's empty, delete it). */
2427 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2428 if (openflow->size > ofs + sizeof *oia) {
2429 oia->len = htons(openflow->size - ofs);
2431 openflow->size = ofs;
2436 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
2438 struct ofpbuf *openflow)
2440 const struct ofpact *a;
2442 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2443 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2444 case OVSINST_OFPIT11_CLEAR_ACTIONS:
2445 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2448 case OVSINST_OFPIT11_GOTO_TABLE: {
2449 struct ofp11_instruction_goto_table *oigt;
2450 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2451 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2452 memset(oigt->pad, 0, sizeof oigt->pad);
2456 case OVSINST_OFPIT11_WRITE_METADATA: {
2457 const struct ofpact_metadata *om;
2458 struct ofp11_instruction_write_metadata *oiwm;
2460 om = ofpact_get_WRITE_METADATA(a);
2461 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2462 oiwm->metadata = om->metadata;
2463 oiwm->metadata_mask = om->mask;
2467 case OVSINST_OFPIT13_METER: {
2468 const struct ofpact_meter *om;
2469 struct ofp13_instruction_meter *oim;
2471 om = ofpact_get_METER(a);
2472 oim = instruction_put_OFPIT13_METER(openflow);
2473 oim->meter_id = htonl(om->meter_id);
2477 case OVSINST_OFPIT11_APPLY_ACTIONS: {
2478 const size_t ofs = openflow->size;
2479 const size_t ofpacts_len_left =
2480 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2481 const struct ofpact *action;
2482 const struct ofpact *processed = a;
2484 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2485 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
2486 if (ovs_instruction_type_from_ofpact_type(action->type)
2487 != OVSINST_OFPIT11_APPLY_ACTIONS) {
2490 ofpact_to_openflow11(action, openflow);
2493 ofpacts_update_instruction_actions(openflow, ofs);
2498 case OVSINST_OFPIT11_WRITE_ACTIONS: {
2499 const size_t ofs = openflow->size;
2500 const struct ofpact_nest *on;
2502 on = ofpact_get_WRITE_ACTIONS(a);
2503 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2504 ofpacts_put_openflow11_actions(on->actions,
2505 ofpact_nest_get_action_len(on),
2507 ofpacts_update_instruction_actions(openflow, ofs);
2515 /* Returns true if 'action' outputs to 'port', false otherwise. */
2517 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
2519 switch (ofpact->type) {
2521 return ofpact_get_OUTPUT(ofpact)->port == port;
2522 case OFPACT_ENQUEUE:
2523 return ofpact_get_ENQUEUE(ofpact)->port == port;
2524 case OFPACT_CONTROLLER:
2525 return port == OFPP_CONTROLLER;
2527 case OFPACT_OUTPUT_REG:
2529 case OFPACT_SET_VLAN_VID:
2530 case OFPACT_SET_VLAN_PCP:
2531 case OFPACT_STRIP_VLAN:
2532 case OFPACT_PUSH_VLAN:
2533 case OFPACT_SET_ETH_SRC:
2534 case OFPACT_SET_ETH_DST:
2535 case OFPACT_SET_IPV4_SRC:
2536 case OFPACT_SET_IPV4_DST:
2537 case OFPACT_SET_IP_DSCP:
2538 case OFPACT_SET_IP_ECN:
2539 case OFPACT_SET_IP_TTL:
2540 case OFPACT_SET_L4_SRC_PORT:
2541 case OFPACT_SET_L4_DST_PORT:
2542 case OFPACT_REG_MOVE:
2543 case OFPACT_REG_LOAD:
2544 case OFPACT_STACK_PUSH:
2545 case OFPACT_STACK_POP:
2546 case OFPACT_DEC_TTL:
2547 case OFPACT_SET_MPLS_TTL:
2548 case OFPACT_DEC_MPLS_TTL:
2549 case OFPACT_SET_TUNNEL:
2550 case OFPACT_WRITE_METADATA:
2551 case OFPACT_SET_QUEUE:
2552 case OFPACT_POP_QUEUE:
2553 case OFPACT_FIN_TIMEOUT:
2554 case OFPACT_RESUBMIT:
2556 case OFPACT_MULTIPATH:
2559 case OFPACT_PUSH_MPLS:
2560 case OFPACT_POP_MPLS:
2562 case OFPACT_CLEAR_ACTIONS:
2563 case OFPACT_WRITE_ACTIONS:
2564 case OFPACT_GOTO_TABLE:
2572 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2573 * to 'port', false otherwise. */
2575 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
2578 const struct ofpact *a;
2580 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2581 if (ofpact_outputs_to_port(a, port)) {
2589 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2590 * to 'group', false otherwise. */
2592 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
2595 const struct ofpact *a;
2597 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2598 if (a->type == OFPACT_GROUP
2599 && ofpact_get_GROUP(a)->group_id == group_id) {
2608 ofpacts_equal(const struct ofpact *a, size_t a_len,
2609 const struct ofpact *b, size_t b_len)
2611 return a_len == b_len && !memcmp(a, b, a_len);
2614 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2615 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2617 * This function relies on the order of 'ofpacts' being correct (as checked by
2618 * ofpacts_verify()). */
2620 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2622 const struct ofpact *a;
2624 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2625 enum ovs_instruction_type inst;
2627 inst = ovs_instruction_type_from_ofpact_type(a->type);
2628 if (a->type == OFPACT_METER) {
2629 return ofpact_get_METER(a)->meter_id;
2630 } else if (inst > OVSINST_OFPIT13_METER) {
2638 /* Formatting ofpacts. */
2641 print_note(const struct ofpact_note *note, struct ds *string)
2645 ds_put_cstr(string, "note:");
2646 for (i = 0; i < note->length; i++) {
2648 ds_put_char(string, '.');
2650 ds_put_format(string, "%02"PRIx8, note->data[i]);
2655 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2660 ds_put_cstr(s, "dec_ttl");
2661 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2662 ds_put_cstr(s, "(");
2663 for (i = 0; i < ids->n_controllers; i++) {
2665 ds_put_cstr(s, ",");
2667 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2669 ds_put_cstr(s, ")");
2674 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2677 ds_put_cstr(s, "fin_timeout(");
2678 if (fin_timeout->fin_idle_timeout) {
2679 ds_put_format(s, "idle_timeout=%"PRIu16",",
2680 fin_timeout->fin_idle_timeout);
2682 if (fin_timeout->fin_hard_timeout) {
2683 ds_put_format(s, "hard_timeout=%"PRIu16",",
2684 fin_timeout->fin_hard_timeout);
2687 ds_put_char(s, ')');
2691 ofpact_format(const struct ofpact *a, struct ds *s)
2693 const struct ofpact_enqueue *enqueue;
2694 const struct ofpact_resubmit *resubmit;
2695 const struct ofpact_controller *controller;
2696 const struct ofpact_metadata *metadata;
2697 const struct ofpact_tunnel *tunnel;
2698 const struct ofpact_sample *sample;
2703 port = ofpact_get_OUTPUT(a)->port;
2704 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
2705 ds_put_format(s, "output:%"PRIu16, port);
2707 ofputil_format_port(port, s);
2708 if (port == OFPP_CONTROLLER) {
2709 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2714 case OFPACT_CONTROLLER:
2715 controller = ofpact_get_CONTROLLER(a);
2716 if (controller->reason == OFPR_ACTION &&
2717 controller->controller_id == 0) {
2718 ds_put_format(s, "CONTROLLER:%"PRIu16,
2719 ofpact_get_CONTROLLER(a)->max_len);
2721 enum ofp_packet_in_reason reason = controller->reason;
2723 ds_put_cstr(s, "controller(");
2724 if (reason != OFPR_ACTION) {
2725 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2727 ds_put_format(s, "reason=%s,",
2728 ofputil_packet_in_reason_to_string(
2729 reason, reasonbuf, sizeof reasonbuf));
2731 if (controller->max_len != UINT16_MAX) {
2732 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2734 if (controller->controller_id != 0) {
2735 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2738 ds_put_char(s, ')');
2742 case OFPACT_ENQUEUE:
2743 enqueue = ofpact_get_ENQUEUE(a);
2744 ds_put_format(s, "enqueue:");
2745 ofputil_format_port(enqueue->port, s);
2746 ds_put_format(s, "q%"PRIu32, enqueue->queue);
2749 case OFPACT_OUTPUT_REG:
2750 ds_put_cstr(s, "output:");
2751 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2755 bundle_format(ofpact_get_BUNDLE(a), s);
2758 case OFPACT_SET_VLAN_VID:
2759 ds_put_format(s, "%s:%"PRIu16,
2760 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_VID
2763 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2766 case OFPACT_SET_VLAN_PCP:
2767 ds_put_format(s, "%s:%"PRIu8,
2768 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_PCP
2771 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2774 case OFPACT_STRIP_VLAN:
2775 ds_put_cstr(s, a->compat == OFPUTIL_OFPAT11_POP_VLAN
2776 ? "pop_vlan" : "strip_vlan");
2779 case OFPACT_PUSH_VLAN:
2780 /* XXX 802.1AD case*/
2781 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2784 case OFPACT_SET_ETH_SRC:
2785 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2786 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2789 case OFPACT_SET_ETH_DST:
2790 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2791 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2794 case OFPACT_SET_IPV4_SRC:
2795 ds_put_format(s, "mod_nw_src:"IP_FMT,
2796 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2799 case OFPACT_SET_IPV4_DST:
2800 ds_put_format(s, "mod_nw_dst:"IP_FMT,
2801 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2804 case OFPACT_SET_IP_DSCP:
2805 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
2808 case OFPACT_SET_IP_ECN:
2809 ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
2812 case OFPACT_SET_IP_TTL:
2813 ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
2816 case OFPACT_SET_L4_SRC_PORT:
2817 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2820 case OFPACT_SET_L4_DST_PORT:
2821 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2824 case OFPACT_REG_MOVE:
2825 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2828 case OFPACT_REG_LOAD:
2829 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2832 case OFPACT_STACK_PUSH:
2833 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2836 case OFPACT_STACK_POP:
2837 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2840 case OFPACT_DEC_TTL:
2841 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2844 case OFPACT_SET_MPLS_TTL:
2845 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2846 ofpact_get_SET_MPLS_TTL(a)->ttl);
2849 case OFPACT_DEC_MPLS_TTL:
2850 ds_put_cstr(s, "dec_mpls_ttl");
2853 case OFPACT_SET_TUNNEL:
2854 tunnel = ofpact_get_SET_TUNNEL(a);
2855 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2856 (tunnel->tun_id > UINT32_MAX
2857 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2861 case OFPACT_SET_QUEUE:
2862 ds_put_format(s, "set_queue:%"PRIu32,
2863 ofpact_get_SET_QUEUE(a)->queue_id);
2866 case OFPACT_POP_QUEUE:
2867 ds_put_cstr(s, "pop_queue");
2870 case OFPACT_FIN_TIMEOUT:
2871 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2874 case OFPACT_RESUBMIT:
2875 resubmit = ofpact_get_RESUBMIT(a);
2876 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2877 ds_put_cstr(s, "resubmit:");
2878 ofputil_format_port(resubmit->in_port, s);
2880 ds_put_format(s, "resubmit(");
2881 if (resubmit->in_port != OFPP_IN_PORT) {
2882 ofputil_format_port(resubmit->in_port, s);
2884 ds_put_char(s, ',');
2885 if (resubmit->table_id != 255) {
2886 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2888 ds_put_char(s, ')');
2893 learn_format(ofpact_get_LEARN(a), s);
2896 case OFPACT_MULTIPATH:
2897 multipath_format(ofpact_get_MULTIPATH(a), s);
2901 print_note(ofpact_get_NOTE(a), s);
2904 case OFPACT_PUSH_MPLS:
2905 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2906 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2909 case OFPACT_POP_MPLS:
2910 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2911 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2915 ds_put_cstr(s, "exit");
2919 sample = ofpact_get_SAMPLE(a);
2921 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2922 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2923 sample->probability, sample->collector_set_id,
2924 sample->obs_domain_id, sample->obs_point_id);
2927 case OFPACT_WRITE_ACTIONS: {
2928 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2929 ds_put_format(s, "%s(",
2930 ovs_instruction_name_from_type(
2931 OVSINST_OFPIT11_WRITE_ACTIONS));
2932 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
2933 ds_put_char(s, ')');
2937 case OFPACT_CLEAR_ACTIONS:
2938 ds_put_format(s, "%s",
2939 ovs_instruction_name_from_type(
2940 OVSINST_OFPIT11_CLEAR_ACTIONS));
2943 case OFPACT_WRITE_METADATA:
2944 metadata = ofpact_get_WRITE_METADATA(a);
2945 ds_put_format(s, "%s:%#"PRIx64,
2946 ovs_instruction_name_from_type(
2947 OVSINST_OFPIT11_WRITE_METADATA),
2948 ntohll(metadata->metadata));
2949 if (metadata->mask != OVS_BE64_MAX) {
2950 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2954 case OFPACT_GOTO_TABLE:
2955 ds_put_format(s, "%s:%"PRIu8,
2956 ovs_instruction_name_from_type(
2957 OVSINST_OFPIT11_GOTO_TABLE),
2958 ofpact_get_GOTO_TABLE(a)->table_id);
2962 ds_put_format(s, "%s:%"PRIu32,
2963 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
2964 ofpact_get_METER(a)->meter_id);
2968 ds_put_format(s, "group:%"PRIu32,
2969 ofpact_get_GROUP(a)->group_id);
2974 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2975 * 'ofpacts' to 'string'. */
2977 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2981 ds_put_cstr(string, "drop");
2983 const struct ofpact *a;
2985 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2987 ds_put_cstr(string, ",");
2990 /* XXX write-actions */
2991 ofpact_format(a, string);
2996 /* Internal use by helpers. */
2999 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
3001 struct ofpact *ofpact;
3003 ofpact_pad(ofpacts);
3004 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
3005 ofpact_init(ofpact, type, len);
3010 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
3012 memset(ofpact, 0, len);
3013 ofpact->type = type;
3014 ofpact->compat = OFPUTIL_ACTION_INVALID;
3018 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
3019 * starting at 'ofpact'.
3021 * This is the correct way to update a variable-length ofpact's length after
3022 * adding the variable-length part of the payload. (See the large comment
3023 * near the end of ofp-actions.h for more information.) */
3025 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
3027 ovs_assert(ofpact == ofpacts->l2);
3028 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
3031 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
3032 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
3033 * client must call this itself after adding the final ofpact to an array of
3036 * (The consequences of failing to call this function are probably not dire.
3037 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
3038 * not dereference it. That's undefined behavior, technically, but it will not
3039 * cause a real problem on common systems. Still, it seems better to call
3042 ofpact_pad(struct ofpbuf *ofpacts)
3044 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
3046 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
3051 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
3054 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
3055 load->dst.field = mf;
3057 load->dst.n_bits = mf->n_bits;
3058 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
3059 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);