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 set_field_from_openflow(const struct ofp12_action_set_field *oasf,
764 struct ofpbuf *ofpacts)
766 uint16_t oasf_len = ntohs(oasf->len);
767 uint32_t oxm_header = ntohl(oasf->dst);
768 uint8_t oxm_length = NXM_LENGTH(oxm_header);
769 struct ofpact_set_field *sf;
770 const struct mf_field *mf;
772 /* ofp12_action_set_field is padded to 64 bits by zero */
773 if (oasf_len != ROUND_UP(sizeof *oasf + oxm_length, 8)) {
774 return OFPERR_OFPBAC_BAD_SET_LEN;
776 if (!is_all_zeros((const uint8_t *)oasf + sizeof *oasf + oxm_length,
777 oasf_len - oxm_length - sizeof *oasf)) {
778 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
781 if (NXM_HASMASK(oxm_header)) {
782 return OFPERR_OFPBAC_BAD_SET_TYPE;
784 mf = mf_from_nxm_header(oxm_header);
786 return OFPERR_OFPBAC_BAD_SET_TYPE;
788 ovs_assert(mf->n_bytes == oxm_length);
789 /* oxm_length is now validated to be compatible with mf_value. */
791 VLOG_WARN_RL(&rl, "destination field %s is not writable", mf->name);
792 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
794 sf = ofpact_put_SET_FIELD(ofpacts);
796 memcpy(&sf->value, oasf + 1, mf->n_bytes);
798 /* The value must be valid for match and must have the OFPVID_PRESENT bit
799 * on for OXM_OF_VLAN_VID. */
800 if (!mf_is_value_valid(mf, &sf->value)
801 || (mf->id == MFF_VLAN_VID
802 && !(sf->value.be16 & htons(OFPVID12_PRESENT)))) {
803 struct ds ds = DS_EMPTY_INITIALIZER;
804 mf_format(mf, &sf->value, NULL, &ds);
805 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
806 mf->name, ds_cstr(&ds));
809 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
815 set_field_to_openflow12(const struct ofpact_set_field *sf,
816 struct ofpbuf *openflow)
818 uint16_t padded_value_len = ROUND_UP(sf->field->n_bytes, 8);
819 struct ofp12_action_set_field *oasf;
822 oasf = ofputil_put_OFPAT12_SET_FIELD(openflow);
823 oasf->dst = htonl(sf->field->oxm_header);
824 oasf->len = htons(sizeof *oasf + padded_value_len);
826 value = ofpbuf_put_zeros(openflow, padded_value_len);
827 memcpy(value, &sf->value, sf->field->n_bytes);
830 /* Convert 'sf' to one or two REG_LOADs. */
832 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
834 const struct mf_field *mf = sf->field;
835 struct nx_action_reg_load *narl;
837 if (mf->n_bits > 64) {
838 ovs_assert(mf->n_bytes == 16); /* IPv6 addr. */
839 /* Split into 64bit chunks */
840 /* Lower bits first. */
841 narl = ofputil_put_NXAST_REG_LOAD(openflow);
842 narl->ofs_nbits = nxm_encode_ofs_nbits(0, 64);
843 narl->dst = htonl(mf->nxm_header);
844 memcpy(&narl->value, &sf->value.ipv6.s6_addr[8], sizeof narl->value);
845 /* Higher bits next. */
846 narl = ofputil_put_NXAST_REG_LOAD(openflow);
847 narl->ofs_nbits = nxm_encode_ofs_nbits(64, mf->n_bits - 64);
848 narl->dst = htonl(mf->nxm_header);
849 memcpy(&narl->value, &sf->value.ipv6.s6_addr[0], sizeof narl->value);
851 narl = ofputil_put_NXAST_REG_LOAD(openflow);
852 narl->ofs_nbits = nxm_encode_ofs_nbits(0, mf->n_bits);
853 narl->dst = htonl(mf->nxm_header);
854 memset(&narl->value, 0, 8 - mf->n_bytes);
855 memcpy((char*)&narl->value + (8 - mf->n_bytes),
856 &sf->value, mf->n_bytes);
860 /* Convert 'sf' to standard OpenFlow 1.1 actions, if we can, falling back
861 * to Nicira extensions if we must.
863 * We check only meta-flow types that can appear within set field actions and
864 * that have a mapping to compatible action types. These struct mf_field
865 * definitions have a defined OXM or NXM header value and specify the field as
868 set_field_to_openflow11(const struct ofpact_set_field *sf,
869 struct ofpbuf *openflow)
871 switch ((int) sf->field->id) {
873 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
875 * If CFI=1, Add or modify VLAN VID & PCP.
876 * OpenFlow 1.1 set actions only apply if the packet
877 * already has VLAN tags. To be sure that is the case
878 * we have to push a VLAN header. As we do not support
879 * multiple layers of VLANs, this is a no-op, if a VLAN
880 * header already exists. This may backfire, however,
881 * when we start supporting multiple layers of VLANs.
882 * If CFI=0, strip VLAN header, if any.
884 if (sf->value.be16 & htons(VLAN_CFI)) {
885 /* Push a VLAN tag, if one was not seen at action validation
887 if (!sf->flow_has_vlan) {
888 ofputil_put_OFPAT11_PUSH_VLAN(openflow)->ethertype
889 = htons(ETH_TYPE_VLAN_8021Q);
891 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
892 = sf->value.be16 & htons(VLAN_VID_MASK);
893 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp
894 = vlan_tci_to_pcp(sf->value.be16);
896 /* If the flow did not match on vlan, we have no way of
897 * knowing if the vlan tag exists, so we must POP just to be
899 ofputil_put_OFPAT11_POP_VLAN(openflow);
904 /* OXM VLAN_PCP to OpenFlow 1.1.
905 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
906 * tag. Clear the OFPVID_PRESENT bit.
908 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
909 = sf->value.be16 & htons(VLAN_VID_MASK);
913 /* OXM VLAN_PCP to OpenFlow 1.1.
914 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
915 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
919 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(openflow)->dl_addr,
920 sf->value.mac, ETH_ADDR_LEN);
924 memcpy(ofputil_put_OFPAT11_SET_DL_DST(openflow)->dl_addr,
925 sf->value.mac, ETH_ADDR_LEN);
929 ofputil_put_OFPAT11_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
933 ofputil_put_OFPAT11_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
937 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
940 case MFF_IP_DSCP_SHIFTED:
941 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
945 ofputil_put_OFPAT11_SET_NW_ECN(openflow)->nw_ecn = sf->value.u8;
949 ofputil_put_OFPAT11_SET_NW_TTL(openflow)->nw_ttl = sf->value.u8;
955 ofputil_put_OFPAT11_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
961 ofputil_put_OFPAT11_SET_TP_DST(openflow)->tp_port = sf->value.be16;
964 case MFF_MPLS_TC: /* XXX */
965 case MFF_MPLS_LABEL: /* XXX */
967 set_field_to_nxast(sf, openflow);
972 /* Convert 'sf' to standard OpenFlow 1.0 actions, if we can, falling back
973 * to Nicira extensions if we must.
975 * We check only meta-flow types that can appear within set field actions and
976 * that have a mapping to compatible action types. These struct mf_field
977 * definitions have a defined OXM or NXM header value and specify the field as
980 set_field_to_openflow10(const struct ofpact_set_field *sf,
981 struct ofpbuf *openflow)
983 switch ((int) sf->field->id) {
985 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
987 * If CFI=1, Add or modify VLAN VID & PCP.
988 * If CFI=0, strip VLAN header, if any.
990 if (sf->value.be16 & htons(VLAN_CFI)) {
991 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
992 = sf->value.be16 & htons(VLAN_VID_MASK);
993 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp
994 = vlan_tci_to_pcp(sf->value.be16);
996 ofputil_put_OFPAT10_STRIP_VLAN(openflow);
1001 /* OXM VLAN_VID to OpenFlow 1.0.
1002 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
1003 * tag. Clear the OFPVID_PRESENT bit.
1005 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1006 = sf->value.be16 & htons(VLAN_VID_MASK);
1010 /* OXM VLAN_PCP to OpenFlow 1.0.
1011 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
1012 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
1016 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(openflow)->dl_addr,
1017 sf->value.mac, ETH_ADDR_LEN);
1021 memcpy(ofputil_put_OFPAT10_SET_DL_DST(openflow)->dl_addr,
1022 sf->value.mac, ETH_ADDR_LEN);
1026 ofputil_put_OFPAT10_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
1030 ofputil_put_OFPAT10_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
1034 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
1037 case MFF_IP_DSCP_SHIFTED:
1038 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
1043 ofputil_put_OFPAT10_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
1048 ofputil_put_OFPAT10_SET_TP_DST(openflow)->tp_port = sf->value.be16;
1052 set_field_to_nxast(sf, openflow);
1058 set_field_to_openflow(const struct ofpact_set_field *sf,
1059 struct ofpbuf *openflow)
1061 struct ofp_header *oh = (struct ofp_header *)openflow->l2;
1063 if (oh->version >= OFP12_VERSION) {
1064 set_field_to_openflow12(sf, openflow);
1065 } else if (oh->version == OFP11_VERSION) {
1066 set_field_to_openflow11(sf, openflow);
1067 } else if (oh->version == OFP10_VERSION) {
1068 set_field_to_openflow10(sf, openflow);
1075 output_from_openflow11(const struct ofp11_action_output *oao,
1078 struct ofpact_output *output;
1081 output = ofpact_put_OUTPUT(out);
1082 output->max_len = ntohs(oao->max_len);
1084 error = ofputil_port_from_ofp11(oao->port, &output->port);
1089 return ofputil_check_output_port(output->port, OFPP_MAX);
1093 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
1095 enum ofputil_action_code code;
1097 struct ofpact_vlan_vid *vlan_vid;
1098 struct ofpact_vlan_pcp *vlan_pcp;
1100 error = decode_openflow11_action(a, &code);
1106 case OFPUTIL_ACTION_INVALID:
1107 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
1108 #include "ofp-util.def"
1111 case OFPUTIL_OFPAT11_OUTPUT:
1112 return output_from_openflow11(&a->ofp11_output, out);
1114 case OFPUTIL_OFPAT11_SET_VLAN_VID:
1115 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1116 return OFPERR_OFPBAC_BAD_ARGUMENT;
1118 vlan_vid = ofpact_put_SET_VLAN_VID(out);
1119 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
1120 vlan_vid->push_vlan_if_needed = false;
1121 vlan_vid->ofpact.compat = code;
1124 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1125 if (a->vlan_pcp.vlan_pcp & ~7) {
1126 return OFPERR_OFPBAC_BAD_ARGUMENT;
1128 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1129 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
1130 vlan_pcp->push_vlan_if_needed = false;
1131 vlan_pcp->ofpact.compat = code;
1134 case OFPUTIL_OFPAT11_PUSH_VLAN:
1135 if (a->push.ethertype != htons(ETH_TYPE_VLAN_8021Q)) {
1136 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1137 return OFPERR_OFPBAC_BAD_ARGUMENT;
1139 ofpact_put_PUSH_VLAN(out);
1142 case OFPUTIL_OFPAT11_POP_VLAN:
1143 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
1146 case OFPUTIL_OFPAT11_SET_QUEUE:
1147 ofpact_put_SET_QUEUE(out)->queue_id =
1148 ntohl(a->ofp11_set_queue.queue_id);
1151 case OFPUTIL_OFPAT11_SET_DL_SRC:
1152 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
1156 case OFPUTIL_OFPAT11_SET_DL_DST:
1157 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
1161 case OFPUTIL_OFPAT11_DEC_NW_TTL:
1162 dec_ttl_from_openflow(out, code);
1165 case OFPUTIL_OFPAT11_SET_NW_SRC:
1166 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
1169 case OFPUTIL_OFPAT11_SET_NW_DST:
1170 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
1173 case OFPUTIL_OFPAT11_SET_NW_TOS:
1174 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
1175 return OFPERR_OFPBAC_BAD_ARGUMENT;
1177 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
1180 case OFPUTIL_OFPAT11_SET_NW_ECN:
1181 if (a->nw_ecn.nw_ecn & ~IP_ECN_MASK) {
1182 return OFPERR_OFPBAC_BAD_ARGUMENT;
1184 ofpact_put_SET_IP_ECN(out)->ecn = a->nw_ecn.nw_ecn;
1187 case OFPUTIL_OFPAT11_SET_NW_TTL:
1188 ofpact_put_SET_IP_TTL(out)->ttl = a->nw_ttl.nw_ttl;
1191 case OFPUTIL_OFPAT11_SET_TP_SRC:
1192 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
1195 case OFPUTIL_OFPAT11_SET_TP_DST:
1196 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
1199 case OFPUTIL_OFPAT12_SET_FIELD:
1200 return set_field_from_openflow(&a->set_field, out);
1202 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
1203 ofpact_put_SET_MPLS_TTL(out)->ttl = a->ofp11_mpls_ttl.mpls_ttl;
1206 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
1207 ofpact_put_DEC_MPLS_TTL(out);
1210 case OFPUTIL_OFPAT11_PUSH_MPLS:
1211 error = push_mpls_from_openflow(a->push.ethertype,
1212 OFPACT_MPLS_AFTER_VLAN, out);
1215 case OFPUTIL_OFPAT11_POP_MPLS:
1216 if (eth_type_mpls(a->ofp11_pop_mpls.ethertype)) {
1217 return OFPERR_OFPBAC_BAD_ARGUMENT;
1219 ofpact_put_POP_MPLS(out)->ethertype = a->ofp11_pop_mpls.ethertype;
1222 case OFPUTIL_OFPAT11_GROUP:
1223 ofpact_put_GROUP(out)->group_id = ntohl(a->group.group_id);
1226 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
1227 #include "ofp-util.def"
1228 return ofpact_from_nxast(a, code, out);
1235 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
1238 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
1241 /* True if an action sets the value of a field
1242 * in a way that is compatibile with the action set.
1243 * False otherwise. */
1245 ofpact_is_set_action(const struct ofpact *a)
1248 case OFPACT_SET_FIELD:
1249 case OFPACT_REG_LOAD:
1250 case OFPACT_SET_ETH_DST:
1251 case OFPACT_SET_ETH_SRC:
1252 case OFPACT_SET_IP_DSCP:
1253 case OFPACT_SET_IP_ECN:
1254 case OFPACT_SET_IP_TTL:
1255 case OFPACT_SET_IPV4_DST:
1256 case OFPACT_SET_IPV4_SRC:
1257 case OFPACT_SET_L4_DST_PORT:
1258 case OFPACT_SET_L4_SRC_PORT:
1259 case OFPACT_SET_MPLS_TTL:
1260 case OFPACT_SET_QUEUE:
1261 case OFPACT_SET_TUNNEL:
1262 case OFPACT_SET_VLAN_PCP:
1263 case OFPACT_SET_VLAN_VID:
1266 case OFPACT_CLEAR_ACTIONS:
1267 case OFPACT_CONTROLLER:
1268 case OFPACT_DEC_MPLS_TTL:
1269 case OFPACT_DEC_TTL:
1270 case OFPACT_ENQUEUE:
1272 case OFPACT_FIN_TIMEOUT:
1273 case OFPACT_GOTO_TABLE:
1277 case OFPACT_MULTIPATH:
1280 case OFPACT_OUTPUT_REG:
1281 case OFPACT_POP_MPLS:
1282 case OFPACT_POP_QUEUE:
1283 case OFPACT_PUSH_MPLS:
1284 case OFPACT_PUSH_VLAN:
1285 case OFPACT_REG_MOVE:
1286 case OFPACT_RESUBMIT:
1288 case OFPACT_STACK_POP:
1289 case OFPACT_STACK_PUSH:
1290 case OFPACT_STRIP_VLAN:
1291 case OFPACT_WRITE_ACTIONS:
1292 case OFPACT_WRITE_METADATA:
1299 /* True if an action is allowed in the action set.
1300 * False otherwise. */
1302 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
1305 case OFPACT_DEC_MPLS_TTL:
1306 case OFPACT_DEC_TTL:
1309 case OFPACT_POP_MPLS:
1310 case OFPACT_PUSH_MPLS:
1311 case OFPACT_PUSH_VLAN:
1312 case OFPACT_REG_LOAD:
1313 case OFPACT_SET_FIELD:
1314 case OFPACT_SET_ETH_DST:
1315 case OFPACT_SET_ETH_SRC:
1316 case OFPACT_SET_IP_DSCP:
1317 case OFPACT_SET_IP_ECN:
1318 case OFPACT_SET_IP_TTL:
1319 case OFPACT_SET_IPV4_DST:
1320 case OFPACT_SET_IPV4_SRC:
1321 case OFPACT_SET_L4_DST_PORT:
1322 case OFPACT_SET_L4_SRC_PORT:
1323 case OFPACT_SET_MPLS_TTL:
1324 case OFPACT_SET_QUEUE:
1325 case OFPACT_SET_TUNNEL:
1326 case OFPACT_SET_VLAN_PCP:
1327 case OFPACT_SET_VLAN_VID:
1328 case OFPACT_STRIP_VLAN:
1331 /* In general these actions are excluded because they are not part of
1332 * the OpenFlow specification nor map to actions that are defined in
1333 * the specification. Thus the order in which they should be applied
1334 * in the action set is undefined. */
1336 case OFPACT_CONTROLLER:
1337 case OFPACT_ENQUEUE:
1339 case OFPACT_FIN_TIMEOUT:
1341 case OFPACT_MULTIPATH:
1343 case OFPACT_OUTPUT_REG:
1344 case OFPACT_POP_QUEUE:
1345 case OFPACT_REG_MOVE:
1346 case OFPACT_RESUBMIT:
1348 case OFPACT_STACK_POP:
1349 case OFPACT_STACK_PUSH:
1351 /* The action set may only include actions and thus
1352 * may not include any instructions */
1353 case OFPACT_CLEAR_ACTIONS:
1354 case OFPACT_GOTO_TABLE:
1356 case OFPACT_WRITE_ACTIONS:
1357 case OFPACT_WRITE_METADATA:
1364 /* Append ofpact 'a' onto the tail of 'out' */
1366 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1368 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1371 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1373 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1374 enum ofpact_type filter)
1376 const struct ofpact *target;
1377 const struct ofpact *a;
1380 OFPACT_FOR_EACH (a, in->data, in->size) {
1381 if (a->type == filter) {
1386 ofpact_copy(out, target);
1388 return target != NULL;
1391 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1392 * The order of appended ofpacts is preserved between 'in' and 'out' */
1394 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1395 bool (*filter)(const struct ofpact *))
1397 const struct ofpact *a;
1399 OFPACT_FOR_EACH (a, in->data, in->size) {
1401 ofpact_copy(out, a);
1406 /* Reads 'action_set', which contains ofpacts accumulated by
1407 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1408 * executed directly into 'action_list'. (These names correspond to the
1409 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1411 * In general this involves appending the last instance of each action that is
1412 * adimissible in the action set in the order described in the OpenFlow
1416 * + output action is only appended if no group action was present in 'in'.
1417 * + As a simplification all set actions are copied in the order the are
1418 * provided in 'in' as many set actions applied to a field has the same
1419 * affect as only applying the last action that sets a field and
1420 * duplicates are removed by do_xlate_actions().
1421 * This has an unwanted side-effect of compsoting multiple
1422 * LOAD_REG actions that touch different regions of the same field. */
1424 ofpacts_execute_action_set(struct ofpbuf *action_list,
1425 const struct ofpbuf *action_set)
1427 /* The OpenFlow spec "Action Set" section specifies this order. */
1428 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1429 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1430 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1431 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1432 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1433 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1434 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1435 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1437 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1438 * we should execute only OFPACT_GROUP.
1440 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1441 * all the actions because there's no point in modifying a packet that will
1442 * not be sent anywhere. */
1443 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1444 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1445 ofpbuf_clear(action_list);
1451 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1452 size_t n_in, struct ofpbuf *out)
1456 size_t start = out->size;
1458 error = ofpacts_from_openflow11(in, n_in, out);
1463 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1464 if (!ofpact_is_allowed_in_actions_set(a)) {
1465 VLOG_WARN_RL(&rl, "disallowed action in action set");
1466 return OFPERR_OFPBAC_BAD_TYPE;
1475 ofpact_from_openflow13(const union ofp_action *a, struct ofpbuf *out)
1477 enum ofputil_action_code code;
1480 error = decode_openflow11_action(a, &code);
1485 if (code == OFPUTIL_OFPAT11_PUSH_MPLS) {
1486 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
1487 error = push_mpls_from_openflow(oap->ethertype,
1488 OFPACT_MPLS_BEFORE_VLAN, out);
1490 error = ofpact_from_openflow11(a, out);
1497 ofpacts_from_openflow13(const union ofp_action *in, size_t n_in,
1500 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow13);
1503 /* OpenFlow 1.1 instructions. */
1505 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1506 static inline const struct STRUCT * OVS_UNUSED \
1507 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1509 ovs_assert(inst->type == htons(ENUM)); \
1510 return ALIGNED_CAST(struct STRUCT *, inst); \
1513 static inline void OVS_UNUSED \
1514 instruction_init_##ENUM(struct STRUCT *s) \
1516 memset(s, 0, sizeof *s); \
1517 s->type = htons(ENUM); \
1518 s->len = htons(sizeof *s); \
1521 static inline struct STRUCT * OVS_UNUSED \
1522 instruction_put_##ENUM(struct ofpbuf *buf) \
1524 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1525 instruction_init_##ENUM(s); \
1531 struct instruction_type_info {
1532 enum ovs_instruction_type type;
1536 static const struct instruction_type_info inst_info[] = {
1537 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1543 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1545 return inst_info[type].name;
1549 ovs_instruction_type_from_name(const char *name)
1551 const struct instruction_type_info *p;
1552 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1553 if (!strcasecmp(name, p->name)) {
1560 enum ovs_instruction_type
1561 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1565 return OVSINST_OFPIT13_METER;
1566 case OFPACT_CLEAR_ACTIONS:
1567 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1568 case OFPACT_WRITE_ACTIONS:
1569 return OVSINST_OFPIT11_WRITE_ACTIONS;
1570 case OFPACT_WRITE_METADATA:
1571 return OVSINST_OFPIT11_WRITE_METADATA;
1572 case OFPACT_GOTO_TABLE:
1573 return OVSINST_OFPIT11_GOTO_TABLE;
1576 case OFPACT_CONTROLLER:
1577 case OFPACT_ENQUEUE:
1578 case OFPACT_OUTPUT_REG:
1580 case OFPACT_SET_VLAN_VID:
1581 case OFPACT_SET_VLAN_PCP:
1582 case OFPACT_STRIP_VLAN:
1583 case OFPACT_PUSH_VLAN:
1584 case OFPACT_SET_ETH_SRC:
1585 case OFPACT_SET_ETH_DST:
1586 case OFPACT_SET_IPV4_SRC:
1587 case OFPACT_SET_IPV4_DST:
1588 case OFPACT_SET_IP_DSCP:
1589 case OFPACT_SET_IP_ECN:
1590 case OFPACT_SET_IP_TTL:
1591 case OFPACT_SET_L4_SRC_PORT:
1592 case OFPACT_SET_L4_DST_PORT:
1593 case OFPACT_REG_MOVE:
1594 case OFPACT_REG_LOAD:
1595 case OFPACT_SET_FIELD:
1596 case OFPACT_STACK_PUSH:
1597 case OFPACT_STACK_POP:
1598 case OFPACT_DEC_TTL:
1599 case OFPACT_SET_MPLS_TTL:
1600 case OFPACT_DEC_MPLS_TTL:
1601 case OFPACT_PUSH_MPLS:
1602 case OFPACT_POP_MPLS:
1603 case OFPACT_SET_TUNNEL:
1604 case OFPACT_SET_QUEUE:
1605 case OFPACT_POP_QUEUE:
1606 case OFPACT_FIN_TIMEOUT:
1607 case OFPACT_RESUBMIT:
1609 case OFPACT_MULTIPATH:
1614 return OVSINST_OFPIT11_APPLY_ACTIONS;
1618 static inline struct ofp11_instruction *
1619 instruction_next(const struct ofp11_instruction *inst)
1621 return ((struct ofp11_instruction *) (void *)
1622 ((uint8_t *) inst + ntohs(inst->len)));
1626 instruction_is_valid(const struct ofp11_instruction *inst,
1627 size_t n_instructions)
1629 uint16_t len = ntohs(inst->len);
1630 return (!(len % OFP11_INSTRUCTION_ALIGN)
1631 && len >= sizeof *inst
1632 && len / sizeof *inst <= n_instructions);
1635 /* This macro is careful to check for instructions with bad lengths. */
1636 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1637 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1638 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1639 ((LEFT) -= (ntohs((ITER)->len) \
1640 / sizeof(struct ofp11_instruction)), \
1641 (ITER) = instruction_next(ITER)))
1644 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1645 enum ovs_instruction_type *type)
1647 uint16_t len = ntohs(inst->len);
1649 switch (inst->type) {
1650 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1651 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1653 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1654 case CONSTANT_HTONS(ENUM): \
1656 ? len >= sizeof(struct STRUCT) \
1657 : len == sizeof(struct STRUCT)) { \
1658 *type = OVSINST_##ENUM; \
1661 return OFPERR_OFPBIC_BAD_LEN; \
1667 return OFPERR_OFPBIC_UNKNOWN_INST;
1672 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1674 const struct ofp11_instruction *out[])
1676 const struct ofp11_instruction *inst;
1679 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1680 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1681 enum ovs_instruction_type type;
1684 error = decode_openflow11_instruction(inst, &type);
1690 return OFPERR_ONFBIC_DUP_INSTRUCTION;
1696 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1697 (n_insts - left) * sizeof *inst);
1698 return OFPERR_OFPBIC_BAD_LEN;
1704 get_actions_from_instruction(const struct ofp11_instruction *inst,
1705 const union ofp_action **actions,
1706 size_t *max_actions)
1708 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1709 *max_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1712 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
1713 * front of 'openflow' into ofpacts. On success, replaces any existing content
1714 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1715 * Returns 0 if successful, otherwise an OpenFlow error.
1717 * Actions are processed according to their OpenFlow version which
1718 * is provided in the 'version' parameter.
1720 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1721 * instructions, so you should call ofpacts_pull_openflow11_instructions()
1722 * instead of this function.
1724 * The parsed actions are valid generically, but they may not be valid in a
1725 * specific context. For example, port numbers up to OFPP_MAX are valid
1726 * generically, but specific datapaths may only support port numbers in a
1727 * smaller range. Use ofpacts_check() to additional check whether actions are
1728 * valid in a specific context. */
1730 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1731 enum ofp_version version,
1732 unsigned int actions_len,
1733 struct ofpbuf *ofpacts)
1739 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1740 ofpacts_from_openflow11);
1742 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1743 ofpacts_from_openflow13);
1750 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1751 enum ofp_version version,
1752 unsigned int instructions_len,
1753 struct ofpbuf *ofpacts)
1755 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1756 const struct ofp11_instruction *instructions;
1757 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1760 ofpbuf_clear(ofpacts);
1762 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1763 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1765 instructions_len, OFP11_INSTRUCTION_ALIGN);
1766 error = OFPERR_OFPBIC_BAD_LEN;
1770 instructions = ofpbuf_try_pull(openflow, instructions_len);
1771 if (instructions == NULL) {
1772 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1773 "remaining message length (%zu)",
1774 instructions_len, openflow->size);
1775 error = OFPERR_OFPBIC_BAD_LEN;
1779 error = decode_openflow11_instructions(
1780 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1786 if (insts[OVSINST_OFPIT13_METER]) {
1787 const struct ofp13_instruction_meter *oim;
1788 struct ofpact_meter *om;
1790 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1791 insts[OVSINST_OFPIT13_METER]);
1793 om = ofpact_put_METER(ofpacts);
1794 om->meter_id = ntohl(oim->meter_id);
1796 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1797 const union ofp_action *actions;
1800 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1801 &actions, &max_actions);
1806 error = ofpacts_from_openflow11(actions, max_actions, ofpacts);
1809 error = ofpacts_from_openflow13(actions, max_actions, ofpacts);
1818 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1819 instruction_get_OFPIT11_CLEAR_ACTIONS(
1820 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1821 ofpact_put_CLEAR_ACTIONS(ofpacts);
1823 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1824 struct ofpact_nest *on;
1825 const union ofp_action *actions;
1829 ofpact_pad(ofpacts);
1830 start = ofpacts->size;
1831 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1832 offsetof(struct ofpact_nest, actions));
1833 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1834 &actions, &max_actions);
1835 error = ofpacts_from_openflow11_for_action_set(actions, max_actions,
1840 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1841 on->ofpact.len = ofpacts->size - start;
1843 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1844 const struct ofp11_instruction_write_metadata *oiwm;
1845 struct ofpact_metadata *om;
1847 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1848 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1850 om = ofpact_put_WRITE_METADATA(ofpacts);
1851 om->metadata = oiwm->metadata;
1852 om->mask = oiwm->metadata_mask;
1854 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1855 const struct ofp11_instruction_goto_table *oigt;
1856 struct ofpact_goto_table *ogt;
1858 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1859 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1860 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1861 ogt->table_id = oigt->table_id;
1864 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1867 ofpbuf_clear(ofpacts);
1872 /* May modify flow->dl_type and flow->vlan_tci, caller must restore them.
1874 * Modifies some actions, filling in fields that could not be properly set
1875 * without context. */
1877 ofpact_check__(struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
1878 uint8_t table_id, bool enforce_consistency)
1880 const struct ofpact_enqueue *enqueue;
1881 const struct mf_field *mf;
1885 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1888 case OFPACT_CONTROLLER:
1891 case OFPACT_ENQUEUE:
1892 enqueue = ofpact_get_ENQUEUE(a);
1893 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1894 && enqueue->port != OFPP_IN_PORT
1895 && enqueue->port != OFPP_LOCAL) {
1896 return OFPERR_OFPBAC_BAD_OUT_PORT;
1900 case OFPACT_OUTPUT_REG:
1901 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1904 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1906 case OFPACT_SET_VLAN_VID:
1907 /* Remember if we saw a vlan tag in the flow to aid translating to
1908 * OpenFlow 1.1+ if need be. */
1909 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
1910 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1911 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1912 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
1915 /* Temporary mark that we have a vlan tag. */
1916 flow->vlan_tci |= htons(VLAN_CFI);
1919 case OFPACT_SET_VLAN_PCP:
1920 /* Remember if we saw a vlan tag in the flow to aid translating to
1921 * OpenFlow 1.1+ if need be. */
1922 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
1923 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1924 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1925 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
1928 /* Temporary mark that we have a vlan tag. */
1929 flow->vlan_tci |= htons(VLAN_CFI);
1932 case OFPACT_STRIP_VLAN:
1933 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
1936 /* Temporary mark that we have no vlan tag. */
1937 flow->vlan_tci = htons(0);
1940 case OFPACT_PUSH_VLAN:
1941 if (flow->vlan_tci & htons(VLAN_CFI)) {
1942 /* Multiple VLAN headers not supported. */
1943 return OFPERR_OFPBAC_BAD_TAG;
1945 /* Temporary mark that we have a vlan tag. */
1946 flow->vlan_tci |= htons(VLAN_CFI);
1949 case OFPACT_SET_ETH_SRC:
1950 case OFPACT_SET_ETH_DST:
1953 case OFPACT_SET_IPV4_SRC:
1954 case OFPACT_SET_IPV4_DST:
1955 if (flow->dl_type != htons(ETH_TYPE_IP)) {
1960 case OFPACT_SET_IP_DSCP:
1961 case OFPACT_SET_IP_ECN:
1962 case OFPACT_SET_IP_TTL:
1963 case OFPACT_DEC_TTL:
1964 if (!is_ip_any(flow)) {
1969 case OFPACT_SET_L4_SRC_PORT:
1970 case OFPACT_SET_L4_DST_PORT:
1971 if (!is_ip_any(flow) ||
1972 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1973 && flow->nw_proto != IPPROTO_SCTP)) {
1978 case OFPACT_REG_MOVE:
1979 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1981 case OFPACT_REG_LOAD:
1982 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1984 case OFPACT_SET_FIELD:
1985 mf = ofpact_get_SET_FIELD(a)->field;
1986 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
1987 if (!mf_are_prereqs_ok(mf, flow) ||
1988 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
1989 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
1991 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1993 /* Remember if we saw a vlan tag in the flow to aid translating to
1994 * OpenFlow 1.1 if need be. */
1995 ofpact_get_SET_FIELD(a)->flow_has_vlan =
1996 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1997 if (mf->id == MFF_VLAN_TCI) {
1998 /* The set field may add or remove the vlan tag,
1999 * Mark the status temporarily. */
2000 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
2004 case OFPACT_STACK_PUSH:
2005 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
2007 case OFPACT_STACK_POP:
2008 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
2010 case OFPACT_SET_MPLS_TTL:
2011 case OFPACT_DEC_MPLS_TTL:
2012 if (!eth_type_mpls(flow->dl_type)) {
2017 case OFPACT_SET_TUNNEL:
2018 case OFPACT_SET_QUEUE:
2019 case OFPACT_POP_QUEUE:
2020 case OFPACT_RESUBMIT:
2023 case OFPACT_FIN_TIMEOUT:
2024 if (flow->nw_proto != IPPROTO_TCP) {
2030 return learn_check(ofpact_get_LEARN(a), flow);
2032 case OFPACT_MULTIPATH:
2033 return multipath_check(ofpact_get_MULTIPATH(a), flow);
2039 case OFPACT_PUSH_MPLS:
2040 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
2043 case OFPACT_POP_MPLS:
2044 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
2045 if (!eth_type_mpls(flow->dl_type)) {
2053 case OFPACT_CLEAR_ACTIONS:
2056 case OFPACT_WRITE_ACTIONS: {
2057 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2058 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
2059 flow, max_ports, table_id, false);
2062 case OFPACT_WRITE_METADATA:
2065 case OFPACT_METER: {
2066 uint32_t mid = ofpact_get_METER(a)->meter_id;
2067 if (mid == 0 || mid > OFPM13_MAX) {
2068 return OFPERR_OFPMMFC_INVALID_METER;
2073 case OFPACT_GOTO_TABLE:
2074 if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
2075 return OFPERR_OFPBRC_BAD_TABLE_ID;
2087 if (enforce_consistency) {
2088 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2093 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2094 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
2095 * switch with no more than 'max_ports' ports.
2097 * May annotate ofpacts with information gathered from the 'flow'.
2099 * May temporarily modify 'flow', but restores the changes before returning. */
2101 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
2102 struct flow *flow, ofp_port_t max_ports, uint8_t table_id,
2103 bool enforce_consistency)
2106 ovs_be16 dl_type = flow->dl_type;
2107 ovs_be16 vlan_tci = flow->vlan_tci;
2108 enum ofperr error = 0;
2110 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2111 error = ofpact_check__(a, flow, max_ports, table_id,
2112 enforce_consistency);
2117 /* Restore fields that may have been modified. */
2118 flow->dl_type = dl_type;
2119 flow->vlan_tci = vlan_tci;
2123 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2124 * in the appropriate order as defined by the OpenFlow spec. */
2126 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
2128 const struct ofpact *a;
2129 enum ovs_instruction_type inst;
2131 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
2132 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2133 enum ovs_instruction_type next;
2135 next = ovs_instruction_type_from_ofpact_type(a->type);
2136 if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
2139 const char *name = ovs_instruction_name_from_type(inst);
2140 const char *next_name = ovs_instruction_name_from_type(next);
2143 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
2144 "1.1+ compatibility", name);
2146 VLOG_WARN("invalid instruction ordering: %s must appear "
2147 "before %s, for OpenFlow 1.1+ compatibility",
2150 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
2159 /* Converting ofpacts to Nicira OpenFlow extensions. */
2162 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
2165 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
2167 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
2168 output_reg->src.n_bits);
2169 naor->src = htonl(output_reg->src.field->nxm_header);
2170 naor->max_len = htons(output_reg->max_len);
2174 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
2177 struct nx_action_resubmit *nar;
2179 if (resubmit->table_id == 0xff
2180 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
2181 nar = ofputil_put_NXAST_RESUBMIT(out);
2183 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
2184 nar->table = resubmit->table_id;
2186 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
2190 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
2193 uint64_t tun_id = tunnel->tun_id;
2195 if (tun_id <= UINT32_MAX
2196 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
2197 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
2199 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
2204 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
2207 struct nx_action_write_metadata *nawm;
2209 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
2210 nawm->metadata = om->metadata;
2211 nawm->mask = om->mask;
2215 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
2217 size_t start_ofs = out->size;
2218 struct nx_action_note *nan;
2219 unsigned int remainder;
2222 nan = ofputil_put_NXAST_NOTE(out);
2223 out->size -= sizeof nan->note;
2225 ofpbuf_put(out, note->data, note->length);
2227 len = out->size - start_ofs;
2228 remainder = len % OFP_ACTION_ALIGN;
2230 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
2232 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
2233 nan->len = htons(out->size - start_ofs);
2237 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
2240 struct nx_action_controller *nac;
2242 nac = ofputil_put_NXAST_CONTROLLER(out);
2243 nac->max_len = htons(oc->max_len);
2244 nac->controller_id = htons(oc->controller_id);
2245 nac->reason = oc->reason;
2249 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
2252 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
2253 ofputil_put_NXAST_DEC_TTL(out);
2255 struct nx_action_cnt_ids *nac_ids =
2256 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
2257 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
2261 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2262 nac_ids->n_controllers = htons(oc_ids->n_controllers);
2264 ids = ofpbuf_put_zeros(out, ids_len);
2265 for (i = 0; i < oc_ids->n_controllers; i++) {
2266 ids[i] = htons(oc_ids->cnt_ids[i]);
2272 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
2275 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
2276 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
2277 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
2281 ofpact_sample_to_nxast(const struct ofpact_sample *os,
2284 struct nx_action_sample *nas;
2286 nas = ofputil_put_NXAST_SAMPLE(out);
2287 nas->probability = htons(os->probability);
2288 nas->collector_set_id = htonl(os->collector_set_id);
2289 nas->obs_domain_id = htonl(os->obs_domain_id);
2290 nas->obs_point_id = htonl(os->obs_point_id);
2294 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
2297 case OFPACT_CONTROLLER:
2298 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
2301 case OFPACT_OUTPUT_REG:
2302 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
2306 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
2309 case OFPACT_REG_MOVE:
2310 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
2313 case OFPACT_REG_LOAD:
2314 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
2317 case OFPACT_SET_FIELD:
2318 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2321 case OFPACT_STACK_PUSH:
2322 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
2325 case OFPACT_STACK_POP:
2326 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
2329 case OFPACT_DEC_TTL:
2330 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
2333 case OFPACT_SET_MPLS_TTL:
2334 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
2335 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2338 case OFPACT_DEC_MPLS_TTL:
2339 ofputil_put_NXAST_DEC_MPLS_TTL(out);
2342 case OFPACT_SET_TUNNEL:
2343 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
2346 case OFPACT_WRITE_METADATA:
2347 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
2350 case OFPACT_SET_QUEUE:
2351 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
2352 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2355 case OFPACT_POP_QUEUE:
2356 ofputil_put_NXAST_POP_QUEUE(out);
2359 case OFPACT_FIN_TIMEOUT:
2360 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
2363 case OFPACT_RESUBMIT:
2364 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
2368 learn_to_nxast(ofpact_get_LEARN(a), out);
2371 case OFPACT_MULTIPATH:
2372 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
2376 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
2380 ofputil_put_NXAST_EXIT(out);
2383 case OFPACT_PUSH_MPLS:
2384 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
2385 ofpact_get_PUSH_MPLS(a)->ethertype;
2388 case OFPACT_POP_MPLS:
2389 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2390 ofpact_get_POP_MPLS(a)->ethertype;
2394 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2399 case OFPACT_ENQUEUE:
2400 case OFPACT_SET_VLAN_VID:
2401 case OFPACT_SET_VLAN_PCP:
2402 case OFPACT_STRIP_VLAN:
2403 case OFPACT_PUSH_VLAN:
2404 case OFPACT_SET_ETH_SRC:
2405 case OFPACT_SET_ETH_DST:
2406 case OFPACT_SET_IPV4_SRC:
2407 case OFPACT_SET_IPV4_DST:
2408 case OFPACT_SET_IP_DSCP:
2409 case OFPACT_SET_IP_ECN:
2410 case OFPACT_SET_IP_TTL:
2411 case OFPACT_SET_L4_SRC_PORT:
2412 case OFPACT_SET_L4_DST_PORT:
2413 case OFPACT_WRITE_ACTIONS:
2414 case OFPACT_CLEAR_ACTIONS:
2415 case OFPACT_GOTO_TABLE:
2421 /* Converting ofpacts to OpenFlow 1.0. */
2424 ofpact_output_to_openflow10(const struct ofpact_output *output,
2427 struct ofp10_action_output *oao;
2429 oao = ofputil_put_OFPAT10_OUTPUT(out);
2430 oao->port = htons(ofp_to_u16(output->port));
2431 oao->max_len = htons(output->max_len);
2435 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2438 struct ofp10_action_enqueue *oae;
2440 oae = ofputil_put_OFPAT10_ENQUEUE(out);
2441 oae->port = htons(ofp_to_u16(enqueue->port));
2442 oae->queue_id = htonl(enqueue->queue);
2446 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2450 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2453 case OFPACT_ENQUEUE:
2454 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2457 case OFPACT_SET_VLAN_VID:
2458 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2459 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2462 case OFPACT_SET_VLAN_PCP:
2463 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2464 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2467 case OFPACT_STRIP_VLAN:
2468 ofputil_put_OFPAT10_STRIP_VLAN(out);
2471 case OFPACT_SET_ETH_SRC:
2472 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2473 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2476 case OFPACT_SET_ETH_DST:
2477 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2478 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2481 case OFPACT_SET_IPV4_SRC:
2482 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2483 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2486 case OFPACT_SET_IPV4_DST:
2487 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2488 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2491 case OFPACT_SET_IP_DSCP:
2492 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2493 = ofpact_get_SET_IP_DSCP(a)->dscp;
2496 case OFPACT_SET_L4_SRC_PORT:
2497 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2498 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2501 case OFPACT_SET_L4_DST_PORT:
2502 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2503 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2506 case OFPACT_PUSH_VLAN:
2507 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
2508 * follow this action. */
2511 case OFPACT_CLEAR_ACTIONS:
2512 case OFPACT_WRITE_ACTIONS:
2513 case OFPACT_GOTO_TABLE:
2521 case OFPACT_CONTROLLER:
2522 case OFPACT_OUTPUT_REG:
2524 case OFPACT_REG_MOVE:
2525 case OFPACT_REG_LOAD:
2526 case OFPACT_SET_FIELD:
2527 case OFPACT_STACK_PUSH:
2528 case OFPACT_STACK_POP:
2529 case OFPACT_DEC_TTL:
2530 case OFPACT_SET_IP_ECN:
2531 case OFPACT_SET_IP_TTL:
2532 case OFPACT_SET_MPLS_TTL:
2533 case OFPACT_DEC_MPLS_TTL:
2534 case OFPACT_SET_TUNNEL:
2535 case OFPACT_WRITE_METADATA:
2536 case OFPACT_SET_QUEUE:
2537 case OFPACT_POP_QUEUE:
2538 case OFPACT_FIN_TIMEOUT:
2539 case OFPACT_RESUBMIT:
2541 case OFPACT_MULTIPATH:
2544 case OFPACT_PUSH_MPLS:
2545 case OFPACT_POP_MPLS:
2547 ofpact_to_nxast(a, out);
2552 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
2553 * actions in 'openflow', appending the actions to any existing data in
2556 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
2557 struct ofpbuf *openflow)
2559 const struct ofpact *a;
2561 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2562 ofpact_to_openflow10(a, openflow);
2566 /* Converting ofpacts to OpenFlow 1.1. */
2569 ofpact_output_to_openflow11(const struct ofpact_output *output,
2572 struct ofp11_action_output *oao;
2574 oao = ofputil_put_OFPAT11_OUTPUT(out);
2575 oao->port = ofputil_port_to_ofp11(output->port);
2576 oao->max_len = htons(output->max_len);
2580 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2583 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2584 && (!dec_ttl->ofpact.compat ||
2585 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2586 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2588 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2593 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2597 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2599 case OFPACT_ENQUEUE:
2603 case OFPACT_SET_VLAN_VID:
2604 /* Push a VLAN tag, if one was not seen at action validation time. */
2605 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan
2606 && ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2607 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2608 = htons(ETH_TYPE_VLAN_8021Q);
2610 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2611 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2614 case OFPACT_SET_VLAN_PCP:
2615 /* Push a VLAN tag, if one was not seen at action validation time. */
2616 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan
2617 && ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2618 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2619 = htons(ETH_TYPE_VLAN_8021Q);
2621 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2622 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2625 case OFPACT_STRIP_VLAN:
2626 ofputil_put_OFPAT11_POP_VLAN(out);
2629 case OFPACT_PUSH_VLAN:
2630 /* XXX ETH_TYPE_VLAN_8021AD case */
2631 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2632 htons(ETH_TYPE_VLAN_8021Q);
2635 case OFPACT_SET_QUEUE:
2636 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2637 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2640 case OFPACT_SET_ETH_SRC:
2641 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2642 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2645 case OFPACT_SET_ETH_DST:
2646 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2647 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2650 case OFPACT_SET_IPV4_SRC:
2651 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2652 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2655 case OFPACT_SET_IPV4_DST:
2656 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2657 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2660 case OFPACT_SET_IP_DSCP:
2661 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2662 = ofpact_get_SET_IP_DSCP(a)->dscp;
2665 case OFPACT_SET_IP_ECN:
2666 ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2667 = ofpact_get_SET_IP_ECN(a)->ecn;
2670 case OFPACT_SET_IP_TTL:
2671 ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2672 = ofpact_get_SET_IP_TTL(a)->ttl;
2675 case OFPACT_SET_L4_SRC_PORT:
2676 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2677 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2680 case OFPACT_SET_L4_DST_PORT:
2681 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2682 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2685 case OFPACT_DEC_TTL:
2686 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2689 case OFPACT_SET_MPLS_TTL:
2690 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2691 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2694 case OFPACT_DEC_MPLS_TTL:
2695 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2698 case OFPACT_WRITE_METADATA:
2699 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2702 case OFPACT_PUSH_MPLS:
2703 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2704 ofpact_get_PUSH_MPLS(a)->ethertype;
2707 case OFPACT_POP_MPLS:
2708 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2709 ofpact_get_POP_MPLS(a)->ethertype;
2713 case OFPACT_CLEAR_ACTIONS:
2714 case OFPACT_WRITE_ACTIONS:
2715 case OFPACT_GOTO_TABLE:
2720 ofputil_put_OFPAT11_GROUP(out)->group_id =
2721 htonl(ofpact_get_GROUP(a)->group_id);
2724 case OFPACT_CONTROLLER:
2725 case OFPACT_OUTPUT_REG:
2727 case OFPACT_REG_MOVE:
2728 case OFPACT_REG_LOAD:
2729 case OFPACT_SET_FIELD:
2730 case OFPACT_STACK_PUSH:
2731 case OFPACT_STACK_POP:
2732 case OFPACT_SET_TUNNEL:
2733 case OFPACT_POP_QUEUE:
2734 case OFPACT_FIN_TIMEOUT:
2735 case OFPACT_RESUBMIT:
2737 case OFPACT_MULTIPATH:
2741 ofpact_to_nxast(a, out);
2746 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
2747 * 1.1 actions in 'openflow', appending the actions to any existing data in
2750 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
2751 size_t ofpacts_len, struct ofpbuf *openflow)
2753 const struct ofpact *a;
2754 size_t start_size = openflow->size;
2756 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2757 ofpact_to_openflow11(a, openflow);
2760 return openflow->size - start_size;
2764 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2766 struct ofp11_instruction_actions *oia;
2768 /* Update the instruction's length (or, if it's empty, delete it). */
2769 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2770 if (openflow->size > ofs + sizeof *oia) {
2771 oia->len = htons(openflow->size - ofs);
2773 openflow->size = ofs;
2778 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
2780 struct ofpbuf *openflow)
2782 const struct ofpact *a;
2784 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2785 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2786 case OVSINST_OFPIT11_CLEAR_ACTIONS:
2787 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2790 case OVSINST_OFPIT11_GOTO_TABLE: {
2791 struct ofp11_instruction_goto_table *oigt;
2792 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2793 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2794 memset(oigt->pad, 0, sizeof oigt->pad);
2798 case OVSINST_OFPIT11_WRITE_METADATA: {
2799 const struct ofpact_metadata *om;
2800 struct ofp11_instruction_write_metadata *oiwm;
2802 om = ofpact_get_WRITE_METADATA(a);
2803 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2804 oiwm->metadata = om->metadata;
2805 oiwm->metadata_mask = om->mask;
2809 case OVSINST_OFPIT13_METER: {
2810 const struct ofpact_meter *om;
2811 struct ofp13_instruction_meter *oim;
2813 om = ofpact_get_METER(a);
2814 oim = instruction_put_OFPIT13_METER(openflow);
2815 oim->meter_id = htonl(om->meter_id);
2819 case OVSINST_OFPIT11_APPLY_ACTIONS: {
2820 const size_t ofs = openflow->size;
2821 const size_t ofpacts_len_left =
2822 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2823 const struct ofpact *action;
2824 const struct ofpact *processed = a;
2826 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2827 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
2828 if (ovs_instruction_type_from_ofpact_type(action->type)
2829 != OVSINST_OFPIT11_APPLY_ACTIONS) {
2832 ofpact_to_openflow11(action, openflow);
2835 ofpacts_update_instruction_actions(openflow, ofs);
2840 case OVSINST_OFPIT11_WRITE_ACTIONS: {
2841 const size_t ofs = openflow->size;
2842 const struct ofpact_nest *on;
2844 on = ofpact_get_WRITE_ACTIONS(a);
2845 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2846 ofpacts_put_openflow11_actions(on->actions,
2847 ofpact_nest_get_action_len(on),
2849 ofpacts_update_instruction_actions(openflow, ofs);
2857 /* Returns true if 'action' outputs to 'port', false otherwise. */
2859 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
2861 switch (ofpact->type) {
2863 return ofpact_get_OUTPUT(ofpact)->port == port;
2864 case OFPACT_ENQUEUE:
2865 return ofpact_get_ENQUEUE(ofpact)->port == port;
2866 case OFPACT_CONTROLLER:
2867 return port == OFPP_CONTROLLER;
2869 case OFPACT_OUTPUT_REG:
2871 case OFPACT_SET_VLAN_VID:
2872 case OFPACT_SET_VLAN_PCP:
2873 case OFPACT_STRIP_VLAN:
2874 case OFPACT_PUSH_VLAN:
2875 case OFPACT_SET_ETH_SRC:
2876 case OFPACT_SET_ETH_DST:
2877 case OFPACT_SET_IPV4_SRC:
2878 case OFPACT_SET_IPV4_DST:
2879 case OFPACT_SET_IP_DSCP:
2880 case OFPACT_SET_IP_ECN:
2881 case OFPACT_SET_IP_TTL:
2882 case OFPACT_SET_L4_SRC_PORT:
2883 case OFPACT_SET_L4_DST_PORT:
2884 case OFPACT_REG_MOVE:
2885 case OFPACT_REG_LOAD:
2886 case OFPACT_SET_FIELD:
2887 case OFPACT_STACK_PUSH:
2888 case OFPACT_STACK_POP:
2889 case OFPACT_DEC_TTL:
2890 case OFPACT_SET_MPLS_TTL:
2891 case OFPACT_DEC_MPLS_TTL:
2892 case OFPACT_SET_TUNNEL:
2893 case OFPACT_WRITE_METADATA:
2894 case OFPACT_SET_QUEUE:
2895 case OFPACT_POP_QUEUE:
2896 case OFPACT_FIN_TIMEOUT:
2897 case OFPACT_RESUBMIT:
2899 case OFPACT_MULTIPATH:
2902 case OFPACT_PUSH_MPLS:
2903 case OFPACT_POP_MPLS:
2905 case OFPACT_CLEAR_ACTIONS:
2906 case OFPACT_WRITE_ACTIONS:
2907 case OFPACT_GOTO_TABLE:
2915 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2916 * to 'port', false otherwise. */
2918 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
2921 const struct ofpact *a;
2923 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2924 if (ofpact_outputs_to_port(a, port)) {
2932 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2933 * to 'group', false otherwise. */
2935 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
2938 const struct ofpact *a;
2940 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2941 if (a->type == OFPACT_GROUP
2942 && ofpact_get_GROUP(a)->group_id == group_id) {
2951 ofpacts_equal(const struct ofpact *a, size_t a_len,
2952 const struct ofpact *b, size_t b_len)
2954 return a_len == b_len && !memcmp(a, b, a_len);
2957 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2958 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2960 * This function relies on the order of 'ofpacts' being correct (as checked by
2961 * ofpacts_verify()). */
2963 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2965 const struct ofpact *a;
2967 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2968 enum ovs_instruction_type inst;
2970 inst = ovs_instruction_type_from_ofpact_type(a->type);
2971 if (a->type == OFPACT_METER) {
2972 return ofpact_get_METER(a)->meter_id;
2973 } else if (inst > OVSINST_OFPIT13_METER) {
2981 /* Formatting ofpacts. */
2984 print_note(const struct ofpact_note *note, struct ds *string)
2988 ds_put_cstr(string, "note:");
2989 for (i = 0; i < note->length; i++) {
2991 ds_put_char(string, '.');
2993 ds_put_format(string, "%02"PRIx8, note->data[i]);
2998 print_dec_ttl(const struct ofpact_cnt_ids *ids,
3003 ds_put_cstr(s, "dec_ttl");
3004 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
3005 ds_put_cstr(s, "(");
3006 for (i = 0; i < ids->n_controllers; i++) {
3008 ds_put_cstr(s, ",");
3010 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
3012 ds_put_cstr(s, ")");
3017 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
3020 ds_put_cstr(s, "fin_timeout(");
3021 if (fin_timeout->fin_idle_timeout) {
3022 ds_put_format(s, "idle_timeout=%"PRIu16",",
3023 fin_timeout->fin_idle_timeout);
3025 if (fin_timeout->fin_hard_timeout) {
3026 ds_put_format(s, "hard_timeout=%"PRIu16",",
3027 fin_timeout->fin_hard_timeout);
3030 ds_put_char(s, ')');
3034 ofpact_format(const struct ofpact *a, struct ds *s)
3036 const struct ofpact_enqueue *enqueue;
3037 const struct ofpact_resubmit *resubmit;
3038 const struct ofpact_controller *controller;
3039 const struct ofpact_metadata *metadata;
3040 const struct ofpact_tunnel *tunnel;
3041 const struct ofpact_sample *sample;
3042 const struct ofpact_set_field *set_field;
3043 const struct mf_field *mf;
3048 port = ofpact_get_OUTPUT(a)->port;
3049 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
3050 ds_put_format(s, "output:%"PRIu16, port);
3052 ofputil_format_port(port, s);
3053 if (port == OFPP_CONTROLLER) {
3054 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
3059 case OFPACT_CONTROLLER:
3060 controller = ofpact_get_CONTROLLER(a);
3061 if (controller->reason == OFPR_ACTION &&
3062 controller->controller_id == 0) {
3063 ds_put_format(s, "CONTROLLER:%"PRIu16,
3064 ofpact_get_CONTROLLER(a)->max_len);
3066 enum ofp_packet_in_reason reason = controller->reason;
3068 ds_put_cstr(s, "controller(");
3069 if (reason != OFPR_ACTION) {
3070 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3072 ds_put_format(s, "reason=%s,",
3073 ofputil_packet_in_reason_to_string(
3074 reason, reasonbuf, sizeof reasonbuf));
3076 if (controller->max_len != UINT16_MAX) {
3077 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
3079 if (controller->controller_id != 0) {
3080 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
3083 ds_put_char(s, ')');
3087 case OFPACT_ENQUEUE:
3088 enqueue = ofpact_get_ENQUEUE(a);
3089 ds_put_format(s, "enqueue:");
3090 ofputil_format_port(enqueue->port, s);
3091 ds_put_format(s, "q%"PRIu32, enqueue->queue);
3094 case OFPACT_OUTPUT_REG:
3095 ds_put_cstr(s, "output:");
3096 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
3100 bundle_format(ofpact_get_BUNDLE(a), s);
3103 case OFPACT_SET_VLAN_VID:
3104 ds_put_format(s, "%s:%"PRIu16,
3105 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_VID
3108 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
3111 case OFPACT_SET_VLAN_PCP:
3112 ds_put_format(s, "%s:%"PRIu8,
3113 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_PCP
3116 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
3119 case OFPACT_STRIP_VLAN:
3120 ds_put_cstr(s, a->compat == OFPUTIL_OFPAT11_POP_VLAN
3121 ? "pop_vlan" : "strip_vlan");
3124 case OFPACT_PUSH_VLAN:
3125 /* XXX 802.1AD case*/
3126 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
3129 case OFPACT_SET_ETH_SRC:
3130 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
3131 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
3134 case OFPACT_SET_ETH_DST:
3135 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
3136 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
3139 case OFPACT_SET_IPV4_SRC:
3140 ds_put_format(s, "mod_nw_src:"IP_FMT,
3141 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
3144 case OFPACT_SET_IPV4_DST:
3145 ds_put_format(s, "mod_nw_dst:"IP_FMT,
3146 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
3149 case OFPACT_SET_IP_DSCP:
3150 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
3153 case OFPACT_SET_IP_ECN:
3154 ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
3157 case OFPACT_SET_IP_TTL:
3158 ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
3161 case OFPACT_SET_L4_SRC_PORT:
3162 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
3165 case OFPACT_SET_L4_DST_PORT:
3166 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
3169 case OFPACT_REG_MOVE:
3170 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
3173 case OFPACT_REG_LOAD:
3174 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
3177 case OFPACT_SET_FIELD:
3178 set_field = ofpact_get_SET_FIELD(a);
3179 mf = set_field->field;
3180 ds_put_format(s, "set_field:");
3181 mf_format(mf, &set_field->value, NULL, s);
3182 ds_put_format(s, "->%s", mf->name);
3185 case OFPACT_STACK_PUSH:
3186 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
3189 case OFPACT_STACK_POP:
3190 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
3193 case OFPACT_DEC_TTL:
3194 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
3197 case OFPACT_SET_MPLS_TTL:
3198 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3199 ofpact_get_SET_MPLS_TTL(a)->ttl);
3202 case OFPACT_DEC_MPLS_TTL:
3203 ds_put_cstr(s, "dec_mpls_ttl");
3206 case OFPACT_SET_TUNNEL:
3207 tunnel = ofpact_get_SET_TUNNEL(a);
3208 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3209 (tunnel->tun_id > UINT32_MAX
3210 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
3214 case OFPACT_SET_QUEUE:
3215 ds_put_format(s, "set_queue:%"PRIu32,
3216 ofpact_get_SET_QUEUE(a)->queue_id);
3219 case OFPACT_POP_QUEUE:
3220 ds_put_cstr(s, "pop_queue");
3223 case OFPACT_FIN_TIMEOUT:
3224 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
3227 case OFPACT_RESUBMIT:
3228 resubmit = ofpact_get_RESUBMIT(a);
3229 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
3230 ds_put_cstr(s, "resubmit:");
3231 ofputil_format_port(resubmit->in_port, s);
3233 ds_put_format(s, "resubmit(");
3234 if (resubmit->in_port != OFPP_IN_PORT) {
3235 ofputil_format_port(resubmit->in_port, s);
3237 ds_put_char(s, ',');
3238 if (resubmit->table_id != 255) {
3239 ds_put_format(s, "%"PRIu8, resubmit->table_id);
3241 ds_put_char(s, ')');
3246 learn_format(ofpact_get_LEARN(a), s);
3249 case OFPACT_MULTIPATH:
3250 multipath_format(ofpact_get_MULTIPATH(a), s);
3254 print_note(ofpact_get_NOTE(a), s);
3257 case OFPACT_PUSH_MPLS:
3258 ds_put_format(s, "push_mpls:0x%04"PRIx16,
3259 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
3262 case OFPACT_POP_MPLS:
3263 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
3264 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
3268 ds_put_cstr(s, "exit");
3272 sample = ofpact_get_SAMPLE(a);
3274 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
3275 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
3276 sample->probability, sample->collector_set_id,
3277 sample->obs_domain_id, sample->obs_point_id);
3280 case OFPACT_WRITE_ACTIONS: {
3281 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3282 ds_put_format(s, "%s(",
3283 ovs_instruction_name_from_type(
3284 OVSINST_OFPIT11_WRITE_ACTIONS));
3285 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
3286 ds_put_char(s, ')');
3290 case OFPACT_CLEAR_ACTIONS:
3291 ds_put_format(s, "%s",
3292 ovs_instruction_name_from_type(
3293 OVSINST_OFPIT11_CLEAR_ACTIONS));
3296 case OFPACT_WRITE_METADATA:
3297 metadata = ofpact_get_WRITE_METADATA(a);
3298 ds_put_format(s, "%s:%#"PRIx64,
3299 ovs_instruction_name_from_type(
3300 OVSINST_OFPIT11_WRITE_METADATA),
3301 ntohll(metadata->metadata));
3302 if (metadata->mask != OVS_BE64_MAX) {
3303 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
3307 case OFPACT_GOTO_TABLE:
3308 ds_put_format(s, "%s:%"PRIu8,
3309 ovs_instruction_name_from_type(
3310 OVSINST_OFPIT11_GOTO_TABLE),
3311 ofpact_get_GOTO_TABLE(a)->table_id);
3315 ds_put_format(s, "%s:%"PRIu32,
3316 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
3317 ofpact_get_METER(a)->meter_id);
3321 ds_put_format(s, "group:%"PRIu32,
3322 ofpact_get_GROUP(a)->group_id);
3327 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
3328 * 'ofpacts' to 'string'. */
3330 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
3334 ds_put_cstr(string, "drop");
3336 const struct ofpact *a;
3338 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3340 ds_put_cstr(string, ",");
3343 /* XXX write-actions */
3344 ofpact_format(a, string);
3349 /* Internal use by helpers. */
3352 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
3354 struct ofpact *ofpact;
3356 ofpact_pad(ofpacts);
3357 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
3358 ofpact_init(ofpact, type, len);
3363 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
3365 memset(ofpact, 0, len);
3366 ofpact->type = type;
3367 ofpact->compat = OFPUTIL_ACTION_INVALID;
3371 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
3372 * starting at 'ofpact'.
3374 * This is the correct way to update a variable-length ofpact's length after
3375 * adding the variable-length part of the payload. (See the large comment
3376 * near the end of ofp-actions.h for more information.) */
3378 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
3380 ovs_assert(ofpact == ofpacts->l2);
3381 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
3384 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
3385 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
3386 * client must call this itself after adding the final ofpact to an array of
3389 * (The consequences of failing to call this function are probably not dire.
3390 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
3391 * not dereference it. That's undefined behavior, technically, but it will not
3392 * cause a real problem on common systems. Still, it seems better to call
3395 ofpact_pad(struct ofpbuf *ofpacts)
3397 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
3399 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);