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,
501 enum ofp_version version OVS_UNUSED,
504 enum ofputil_action_code code;
506 struct ofpact_vlan_vid *vlan_vid;
507 struct ofpact_vlan_pcp *vlan_pcp;
509 error = decode_openflow10_action(a, &code);
515 case OFPUTIL_ACTION_INVALID:
516 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
517 #include "ofp-util.def"
520 case OFPUTIL_OFPAT10_OUTPUT:
521 return output_from_openflow10(&a->output10, out);
523 case OFPUTIL_OFPAT10_SET_VLAN_VID:
524 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
525 return OFPERR_OFPBAC_BAD_ARGUMENT;
527 vlan_vid = ofpact_put_SET_VLAN_VID(out);
528 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
529 vlan_vid->push_vlan_if_needed = true;
530 vlan_vid->ofpact.compat = code;
533 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
534 if (a->vlan_pcp.vlan_pcp & ~7) {
535 return OFPERR_OFPBAC_BAD_ARGUMENT;
537 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
538 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
539 vlan_pcp->push_vlan_if_needed = true;
540 vlan_pcp->ofpact.compat = code;
543 case OFPUTIL_OFPAT10_STRIP_VLAN:
544 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
547 case OFPUTIL_OFPAT10_SET_DL_SRC:
548 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
552 case OFPUTIL_OFPAT10_SET_DL_DST:
553 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
557 case OFPUTIL_OFPAT10_SET_NW_SRC:
558 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
561 case OFPUTIL_OFPAT10_SET_NW_DST:
562 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
565 case OFPUTIL_OFPAT10_SET_NW_TOS:
566 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
567 return OFPERR_OFPBAC_BAD_ARGUMENT;
569 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
572 case OFPUTIL_OFPAT10_SET_TP_SRC:
573 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
576 case OFPUTIL_OFPAT10_SET_TP_DST:
577 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
581 case OFPUTIL_OFPAT10_ENQUEUE:
582 error = enqueue_from_openflow10(&a->enqueue, out);
585 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
586 #include "ofp-util.def"
587 return ofpact_from_nxast(a, code, out);
593 static enum ofperr ofpact_from_openflow11(const union ofp_action *,
597 static inline union ofp_action *
598 action_next(const union ofp_action *a)
600 return ((union ofp_action *) (void *)
601 ((uint8_t *) a + ntohs(a->header.len)));
605 action_is_valid(const union ofp_action *a, size_t max_actions)
607 uint16_t len = ntohs(a->header.len);
608 return (!(len % OFP_ACTION_ALIGN)
609 && len >= OFP_ACTION_ALIGN
610 && len / OFP_ACTION_ALIGN <= max_actions);
613 /* This macro is careful to check for actions with bad lengths. */
614 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, MAX_ACTIONS) \
615 for ((ITER) = (ACTIONS), (LEFT) = (MAX_ACTIONS); \
616 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
617 ((LEFT) -= ntohs((ITER)->header.len) / OFP_ACTION_ALIGN, \
618 (ITER) = action_next(ITER)))
621 log_bad_action(const union ofp_action *actions, size_t max_actions,
622 const union ofp_action *bad_action, enum ofperr error)
624 if (!VLOG_DROP_WARN(&rl)) {
628 ds_put_hex_dump(&s, actions, max_actions * OFP_ACTION_ALIGN, 0, false);
629 VLOG_WARN("bad action at offset %#tx (%s):\n%s",
630 (char *)bad_action - (char *)actions,
631 ofperr_get_name(error), ds_cstr(&s));
637 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
638 enum ofp_version version, struct ofpbuf *out)
640 const union ofp_action *a;
643 enum ofperr (*ofpact_from_openflow)(const union ofp_action *a,
645 struct ofpbuf *out) =
646 (version == OFP10_VERSION) ?
647 ofpact_from_openflow10 : ofpact_from_openflow11;
649 ACTION_FOR_EACH (a, left, in, n_in) {
650 enum ofperr error = ofpact_from_openflow(a, version, out);
652 log_bad_action(in, n_in, a, error);
657 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
658 log_bad_action(in, n_in, a, error);
666 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
667 * front of 'openflow' into ofpacts. On success, replaces any existing content
668 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
669 * Returns 0 if successful, otherwise an OpenFlow error.
671 * Actions are processed according to their OpenFlow version which
672 * is provided in the 'version' parameter.
674 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
675 * instructions, so you should call ofpacts_pull_openflow_instructions()
676 * instead of this function.
678 * The parsed actions are valid generically, but they may not be valid in a
679 * specific context. For example, port numbers up to OFPP_MAX are valid
680 * generically, but specific datapaths may only support port numbers in a
681 * smaller range. Use ofpacts_check() to additional check whether actions are
682 * valid in a specific context. */
684 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
685 unsigned int actions_len,
686 enum ofp_version version,
687 struct ofpbuf *ofpacts) {
688 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
689 const union ofp_action *actions;
692 ofpbuf_clear(ofpacts);
694 if (actions_len % OFP_ACTION_ALIGN != 0) {
695 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
696 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
697 return OFPERR_OFPBRC_BAD_LEN;
700 actions = ofpbuf_try_pull(openflow, actions_len);
701 if (actions == NULL) {
702 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
703 "remaining message length (%zu)",
704 actions_len, openflow->size);
705 return OFPERR_OFPBRC_BAD_LEN;
708 error = ofpacts_from_openflow(actions, actions_len / OFP_ACTION_ALIGN,
711 ofpbuf_clear(ofpacts);
715 error = ofpacts_verify(ofpacts->data, ofpacts->size);
717 ofpbuf_clear(ofpacts);
723 /* OpenFlow 1.1 actions. */
725 /* Parses 'a' to determine its type. On success stores the correct type into
726 * '*code' and returns 0. On failure returns an OFPERR_* error code and
727 * '*code' is indeterminate.
729 * The caller must have already verified that 'a''s length is potentially
730 * correct (that is, a->header.len is nonzero and a multiple of
731 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
733 * This function verifies that 'a''s length is correct for the type of action
734 * that it represents. */
736 decode_openflow11_action(const union ofp_action *a,
737 enum ofputil_action_code *code)
742 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
743 return decode_nxast_action(a, code);
745 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
746 case CONSTANT_HTONS(ENUM): \
747 len = ntohs(a->header.len); \
749 ? len >= sizeof(struct STRUCT) \
750 : len == sizeof(struct STRUCT)) { \
751 *code = OFPUTIL_##ENUM; \
754 return OFPERR_OFPBAC_BAD_LEN; \
757 #include "ofp-util.def"
760 return OFPERR_OFPBAC_BAD_TYPE;
765 set_field_from_openflow(const struct ofp12_action_set_field *oasf,
766 struct ofpbuf *ofpacts)
768 uint16_t oasf_len = ntohs(oasf->len);
769 uint32_t oxm_header = ntohl(oasf->dst);
770 uint8_t oxm_length = NXM_LENGTH(oxm_header);
771 struct ofpact_set_field *sf;
772 const struct mf_field *mf;
774 /* ofp12_action_set_field is padded to 64 bits by zero */
775 if (oasf_len != ROUND_UP(sizeof *oasf + oxm_length, 8)) {
776 return OFPERR_OFPBAC_BAD_SET_LEN;
778 if (!is_all_zeros((const uint8_t *)oasf + sizeof *oasf + oxm_length,
779 oasf_len - oxm_length - sizeof *oasf)) {
780 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
783 if (NXM_HASMASK(oxm_header)) {
784 return OFPERR_OFPBAC_BAD_SET_TYPE;
786 mf = mf_from_nxm_header(oxm_header);
788 return OFPERR_OFPBAC_BAD_SET_TYPE;
790 ovs_assert(mf->n_bytes == oxm_length);
791 /* oxm_length is now validated to be compatible with mf_value. */
793 VLOG_WARN_RL(&rl, "destination field %s is not writable", mf->name);
794 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
796 sf = ofpact_put_SET_FIELD(ofpacts);
798 memcpy(&sf->value, oasf + 1, mf->n_bytes);
800 /* The value must be valid for match and must have the OFPVID_PRESENT bit
801 * on for OXM_OF_VLAN_VID. */
802 if (!mf_is_value_valid(mf, &sf->value)
803 || (mf->id == MFF_VLAN_VID
804 && !(sf->value.be16 & htons(OFPVID12_PRESENT)))) {
805 struct ds ds = DS_EMPTY_INITIALIZER;
806 mf_format(mf, &sf->value, NULL, &ds);
807 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
808 mf->name, ds_cstr(&ds));
811 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
817 set_field_to_openflow12(const struct ofpact_set_field *sf,
818 struct ofpbuf *openflow)
820 uint16_t padded_value_len = ROUND_UP(sf->field->n_bytes, 8);
821 struct ofp12_action_set_field *oasf;
824 oasf = ofputil_put_OFPAT12_SET_FIELD(openflow);
825 oasf->dst = htonl(sf->field->oxm_header);
826 oasf->len = htons(sizeof *oasf + padded_value_len);
828 value = ofpbuf_put_zeros(openflow, padded_value_len);
829 memcpy(value, &sf->value, sf->field->n_bytes);
832 /* Convert 'sf' to one or two REG_LOADs. */
834 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
836 const struct mf_field *mf = sf->field;
837 struct nx_action_reg_load *narl;
839 if (mf->n_bits > 64) {
840 ovs_assert(mf->n_bytes == 16); /* IPv6 addr. */
841 /* Split into 64bit chunks */
842 /* Lower bits first. */
843 narl = ofputil_put_NXAST_REG_LOAD(openflow);
844 narl->ofs_nbits = nxm_encode_ofs_nbits(0, 64);
845 narl->dst = htonl(mf->nxm_header);
846 memcpy(&narl->value, &sf->value.ipv6.s6_addr[8], sizeof narl->value);
847 /* Higher bits next. */
848 narl = ofputil_put_NXAST_REG_LOAD(openflow);
849 narl->ofs_nbits = nxm_encode_ofs_nbits(64, mf->n_bits - 64);
850 narl->dst = htonl(mf->nxm_header);
851 memcpy(&narl->value, &sf->value.ipv6.s6_addr[0], sizeof narl->value);
853 narl = ofputil_put_NXAST_REG_LOAD(openflow);
854 narl->ofs_nbits = nxm_encode_ofs_nbits(0, mf->n_bits);
855 narl->dst = htonl(mf->nxm_header);
856 memset(&narl->value, 0, 8 - mf->n_bytes);
857 memcpy((char*)&narl->value + (8 - mf->n_bytes),
858 &sf->value, mf->n_bytes);
862 /* Convert 'sf' to standard OpenFlow 1.1 actions, if we can, falling back
863 * to Nicira extensions if we must.
865 * We check only meta-flow types that can appear within set field actions and
866 * that have a mapping to compatible action types. These struct mf_field
867 * definitions have a defined OXM or NXM header value and specify the field as
870 set_field_to_openflow11(const struct ofpact_set_field *sf,
871 struct ofpbuf *openflow)
873 switch ((int) sf->field->id) {
875 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
877 * If CFI=1, Add or modify VLAN VID & PCP.
878 * OpenFlow 1.1 set actions only apply if the packet
879 * already has VLAN tags. To be sure that is the case
880 * we have to push a VLAN header. As we do not support
881 * multiple layers of VLANs, this is a no-op, if a VLAN
882 * header already exists. This may backfire, however,
883 * when we start supporting multiple layers of VLANs.
884 * If CFI=0, strip VLAN header, if any.
886 if (sf->value.be16 & htons(VLAN_CFI)) {
887 /* Push a VLAN tag, if one was not seen at action validation
889 if (!sf->flow_has_vlan) {
890 ofputil_put_OFPAT11_PUSH_VLAN(openflow)->ethertype
891 = htons(ETH_TYPE_VLAN_8021Q);
893 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
894 = sf->value.be16 & htons(VLAN_VID_MASK);
895 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp
896 = vlan_tci_to_pcp(sf->value.be16);
898 /* If the flow did not match on vlan, we have no way of
899 * knowing if the vlan tag exists, so we must POP just to be
901 ofputil_put_OFPAT11_POP_VLAN(openflow);
906 /* OXM VLAN_PCP to OpenFlow 1.1.
907 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
908 * tag. Clear the OFPVID_PRESENT bit.
910 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
911 = sf->value.be16 & htons(VLAN_VID_MASK);
915 /* OXM VLAN_PCP to OpenFlow 1.1.
916 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
917 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
921 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(openflow)->dl_addr,
922 sf->value.mac, ETH_ADDR_LEN);
926 memcpy(ofputil_put_OFPAT11_SET_DL_DST(openflow)->dl_addr,
927 sf->value.mac, ETH_ADDR_LEN);
931 ofputil_put_OFPAT11_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
935 ofputil_put_OFPAT11_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
939 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
942 case MFF_IP_DSCP_SHIFTED:
943 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
947 ofputil_put_OFPAT11_SET_NW_ECN(openflow)->nw_ecn = sf->value.u8;
951 ofputil_put_OFPAT11_SET_NW_TTL(openflow)->nw_ttl = sf->value.u8;
957 ofputil_put_OFPAT11_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
963 ofputil_put_OFPAT11_SET_TP_DST(openflow)->tp_port = sf->value.be16;
966 case MFF_MPLS_TC: /* XXX */
967 case MFF_MPLS_LABEL: /* XXX */
969 set_field_to_nxast(sf, openflow);
974 /* Convert 'sf' to standard OpenFlow 1.0 actions, if we can, falling back
975 * to Nicira extensions if we must.
977 * We check only meta-flow types that can appear within set field actions and
978 * that have a mapping to compatible action types. These struct mf_field
979 * definitions have a defined OXM or NXM header value and specify the field as
982 set_field_to_openflow10(const struct ofpact_set_field *sf,
983 struct ofpbuf *openflow)
985 switch ((int) sf->field->id) {
987 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
989 * If CFI=1, Add or modify VLAN VID & PCP.
990 * If CFI=0, strip VLAN header, if any.
992 if (sf->value.be16 & htons(VLAN_CFI)) {
993 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
994 = sf->value.be16 & htons(VLAN_VID_MASK);
995 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp
996 = vlan_tci_to_pcp(sf->value.be16);
998 ofputil_put_OFPAT10_STRIP_VLAN(openflow);
1003 /* OXM VLAN_VID to OpenFlow 1.0.
1004 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
1005 * tag. Clear the OFPVID_PRESENT bit.
1007 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1008 = sf->value.be16 & htons(VLAN_VID_MASK);
1012 /* OXM VLAN_PCP to OpenFlow 1.0.
1013 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
1014 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
1018 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(openflow)->dl_addr,
1019 sf->value.mac, ETH_ADDR_LEN);
1023 memcpy(ofputil_put_OFPAT10_SET_DL_DST(openflow)->dl_addr,
1024 sf->value.mac, ETH_ADDR_LEN);
1028 ofputil_put_OFPAT10_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
1032 ofputil_put_OFPAT10_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
1036 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
1039 case MFF_IP_DSCP_SHIFTED:
1040 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
1045 ofputil_put_OFPAT10_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
1050 ofputil_put_OFPAT10_SET_TP_DST(openflow)->tp_port = sf->value.be16;
1054 set_field_to_nxast(sf, openflow);
1060 set_field_to_openflow(const struct ofpact_set_field *sf,
1061 struct ofpbuf *openflow)
1063 struct ofp_header *oh = (struct ofp_header *)openflow->l2;
1065 if (oh->version >= OFP12_VERSION) {
1066 set_field_to_openflow12(sf, openflow);
1067 } else if (oh->version == OFP11_VERSION) {
1068 set_field_to_openflow11(sf, openflow);
1069 } else if (oh->version == OFP10_VERSION) {
1070 set_field_to_openflow10(sf, openflow);
1077 output_from_openflow11(const struct ofp11_action_output *oao,
1080 struct ofpact_output *output;
1083 output = ofpact_put_OUTPUT(out);
1084 output->max_len = ntohs(oao->max_len);
1086 error = ofputil_port_from_ofp11(oao->port, &output->port);
1091 return ofputil_check_output_port(output->port, OFPP_MAX);
1095 ofpact_from_openflow11(const union ofp_action *a, enum ofp_version version,
1098 enum ofputil_action_code code;
1100 struct ofpact_vlan_vid *vlan_vid;
1101 struct ofpact_vlan_pcp *vlan_pcp;
1103 error = decode_openflow11_action(a, &code);
1109 case OFPUTIL_ACTION_INVALID:
1110 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
1111 #include "ofp-util.def"
1114 case OFPUTIL_OFPAT11_OUTPUT:
1115 return output_from_openflow11(&a->ofp11_output, out);
1117 case OFPUTIL_OFPAT11_SET_VLAN_VID:
1118 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1119 return OFPERR_OFPBAC_BAD_ARGUMENT;
1121 vlan_vid = ofpact_put_SET_VLAN_VID(out);
1122 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
1123 vlan_vid->push_vlan_if_needed = false;
1124 vlan_vid->ofpact.compat = code;
1127 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1128 if (a->vlan_pcp.vlan_pcp & ~7) {
1129 return OFPERR_OFPBAC_BAD_ARGUMENT;
1131 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1132 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
1133 vlan_pcp->push_vlan_if_needed = false;
1134 vlan_pcp->ofpact.compat = code;
1137 case OFPUTIL_OFPAT11_PUSH_VLAN:
1138 if (a->push.ethertype != htons(ETH_TYPE_VLAN_8021Q)) {
1139 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1140 return OFPERR_OFPBAC_BAD_ARGUMENT;
1142 ofpact_put_PUSH_VLAN(out);
1145 case OFPUTIL_OFPAT11_POP_VLAN:
1146 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
1149 case OFPUTIL_OFPAT11_SET_QUEUE:
1150 ofpact_put_SET_QUEUE(out)->queue_id =
1151 ntohl(a->ofp11_set_queue.queue_id);
1154 case OFPUTIL_OFPAT11_SET_DL_SRC:
1155 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
1159 case OFPUTIL_OFPAT11_SET_DL_DST:
1160 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
1164 case OFPUTIL_OFPAT11_DEC_NW_TTL:
1165 dec_ttl_from_openflow(out, code);
1168 case OFPUTIL_OFPAT11_SET_NW_SRC:
1169 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
1172 case OFPUTIL_OFPAT11_SET_NW_DST:
1173 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
1176 case OFPUTIL_OFPAT11_SET_NW_TOS:
1177 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
1178 return OFPERR_OFPBAC_BAD_ARGUMENT;
1180 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
1183 case OFPUTIL_OFPAT11_SET_NW_ECN:
1184 if (a->nw_ecn.nw_ecn & ~IP_ECN_MASK) {
1185 return OFPERR_OFPBAC_BAD_ARGUMENT;
1187 ofpact_put_SET_IP_ECN(out)->ecn = a->nw_ecn.nw_ecn;
1190 case OFPUTIL_OFPAT11_SET_NW_TTL:
1191 ofpact_put_SET_IP_TTL(out)->ttl = a->nw_ttl.nw_ttl;
1194 case OFPUTIL_OFPAT11_SET_TP_SRC:
1195 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
1198 case OFPUTIL_OFPAT11_SET_TP_DST:
1199 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
1202 case OFPUTIL_OFPAT12_SET_FIELD:
1203 return set_field_from_openflow(&a->set_field, out);
1205 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
1206 ofpact_put_SET_MPLS_TTL(out)->ttl = a->ofp11_mpls_ttl.mpls_ttl;
1209 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
1210 ofpact_put_DEC_MPLS_TTL(out);
1213 case OFPUTIL_OFPAT11_PUSH_MPLS:
1214 /* OpenFlow 1.3 has different semantics. */
1215 error = push_mpls_from_openflow(a->push.ethertype,
1216 version >= OFP13_VERSION ?
1217 OFPACT_MPLS_BEFORE_VLAN :
1218 OFPACT_MPLS_AFTER_VLAN, out);
1221 case OFPUTIL_OFPAT11_POP_MPLS:
1222 if (eth_type_mpls(a->ofp11_pop_mpls.ethertype)) {
1223 return OFPERR_OFPBAC_BAD_ARGUMENT;
1225 ofpact_put_POP_MPLS(out)->ethertype = a->ofp11_pop_mpls.ethertype;
1228 case OFPUTIL_OFPAT11_GROUP:
1229 ofpact_put_GROUP(out)->group_id = ntohl(a->group.group_id);
1232 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
1233 #include "ofp-util.def"
1234 return ofpact_from_nxast(a, code, out);
1240 /* True if an action sets the value of a field
1241 * in a way that is compatibile with the action set.
1242 * False otherwise. */
1244 ofpact_is_set_action(const struct ofpact *a)
1247 case OFPACT_SET_FIELD:
1248 case OFPACT_REG_LOAD:
1249 case OFPACT_SET_ETH_DST:
1250 case OFPACT_SET_ETH_SRC:
1251 case OFPACT_SET_IP_DSCP:
1252 case OFPACT_SET_IP_ECN:
1253 case OFPACT_SET_IP_TTL:
1254 case OFPACT_SET_IPV4_DST:
1255 case OFPACT_SET_IPV4_SRC:
1256 case OFPACT_SET_L4_DST_PORT:
1257 case OFPACT_SET_L4_SRC_PORT:
1258 case OFPACT_SET_MPLS_TTL:
1259 case OFPACT_SET_QUEUE:
1260 case OFPACT_SET_TUNNEL:
1261 case OFPACT_SET_VLAN_PCP:
1262 case OFPACT_SET_VLAN_VID:
1265 case OFPACT_CLEAR_ACTIONS:
1266 case OFPACT_CONTROLLER:
1267 case OFPACT_DEC_MPLS_TTL:
1268 case OFPACT_DEC_TTL:
1269 case OFPACT_ENQUEUE:
1271 case OFPACT_FIN_TIMEOUT:
1272 case OFPACT_GOTO_TABLE:
1276 case OFPACT_MULTIPATH:
1279 case OFPACT_OUTPUT_REG:
1280 case OFPACT_POP_MPLS:
1281 case OFPACT_POP_QUEUE:
1282 case OFPACT_PUSH_MPLS:
1283 case OFPACT_PUSH_VLAN:
1284 case OFPACT_REG_MOVE:
1285 case OFPACT_RESUBMIT:
1287 case OFPACT_STACK_POP:
1288 case OFPACT_STACK_PUSH:
1289 case OFPACT_STRIP_VLAN:
1290 case OFPACT_WRITE_ACTIONS:
1291 case OFPACT_WRITE_METADATA:
1298 /* True if an action is allowed in the action set.
1299 * False otherwise. */
1301 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
1304 case OFPACT_DEC_MPLS_TTL:
1305 case OFPACT_DEC_TTL:
1308 case OFPACT_POP_MPLS:
1309 case OFPACT_PUSH_MPLS:
1310 case OFPACT_PUSH_VLAN:
1311 case OFPACT_REG_LOAD:
1312 case OFPACT_SET_FIELD:
1313 case OFPACT_SET_ETH_DST:
1314 case OFPACT_SET_ETH_SRC:
1315 case OFPACT_SET_IP_DSCP:
1316 case OFPACT_SET_IP_ECN:
1317 case OFPACT_SET_IP_TTL:
1318 case OFPACT_SET_IPV4_DST:
1319 case OFPACT_SET_IPV4_SRC:
1320 case OFPACT_SET_L4_DST_PORT:
1321 case OFPACT_SET_L4_SRC_PORT:
1322 case OFPACT_SET_MPLS_TTL:
1323 case OFPACT_SET_QUEUE:
1324 case OFPACT_SET_TUNNEL:
1325 case OFPACT_SET_VLAN_PCP:
1326 case OFPACT_SET_VLAN_VID:
1327 case OFPACT_STRIP_VLAN:
1330 /* In general these actions are excluded because they are not part of
1331 * the OpenFlow specification nor map to actions that are defined in
1332 * the specification. Thus the order in which they should be applied
1333 * in the action set is undefined. */
1335 case OFPACT_CONTROLLER:
1336 case OFPACT_ENQUEUE:
1338 case OFPACT_FIN_TIMEOUT:
1340 case OFPACT_MULTIPATH:
1342 case OFPACT_OUTPUT_REG:
1343 case OFPACT_POP_QUEUE:
1344 case OFPACT_REG_MOVE:
1345 case OFPACT_RESUBMIT:
1347 case OFPACT_STACK_POP:
1348 case OFPACT_STACK_PUSH:
1350 /* The action set may only include actions and thus
1351 * may not include any instructions */
1352 case OFPACT_CLEAR_ACTIONS:
1353 case OFPACT_GOTO_TABLE:
1355 case OFPACT_WRITE_ACTIONS:
1356 case OFPACT_WRITE_METADATA:
1363 /* Append ofpact 'a' onto the tail of 'out' */
1365 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1367 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1370 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1372 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1373 enum ofpact_type filter)
1375 const struct ofpact *target;
1376 const struct ofpact *a;
1379 OFPACT_FOR_EACH (a, in->data, in->size) {
1380 if (a->type == filter) {
1385 ofpact_copy(out, target);
1387 return target != NULL;
1390 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1391 * The order of appended ofpacts is preserved between 'in' and 'out' */
1393 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1394 bool (*filter)(const struct ofpact *))
1396 const struct ofpact *a;
1398 OFPACT_FOR_EACH (a, in->data, in->size) {
1400 ofpact_copy(out, a);
1405 /* Reads 'action_set', which contains ofpacts accumulated by
1406 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1407 * executed directly into 'action_list'. (These names correspond to the
1408 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1410 * In general this involves appending the last instance of each action that is
1411 * adimissible in the action set in the order described in the OpenFlow
1415 * + output action is only appended if no group action was present in 'in'.
1416 * + As a simplification all set actions are copied in the order the are
1417 * provided in 'in' as many set actions applied to a field has the same
1418 * affect as only applying the last action that sets a field and
1419 * duplicates are removed by do_xlate_actions().
1420 * This has an unwanted side-effect of compsoting multiple
1421 * LOAD_REG actions that touch different regions of the same field. */
1423 ofpacts_execute_action_set(struct ofpbuf *action_list,
1424 const struct ofpbuf *action_set)
1426 /* The OpenFlow spec "Action Set" section specifies this order. */
1427 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1428 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1429 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1430 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1431 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1432 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1433 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1434 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1436 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1437 * we should execute only OFPACT_GROUP.
1439 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1440 * all the actions because there's no point in modifying a packet that will
1441 * not be sent anywhere. */
1442 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1443 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1444 ofpbuf_clear(action_list);
1450 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1451 size_t n_in, enum ofp_version version,
1456 size_t start = out->size;
1458 error = ofpacts_from_openflow(in, n_in, version, out);
1464 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1465 if (!ofpact_is_allowed_in_actions_set(a)) {
1466 VLOG_WARN_RL(&rl, "disallowed action in action set");
1467 return OFPERR_OFPBAC_BAD_TYPE;
1475 /* OpenFlow 1.1 instructions. */
1477 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1478 static inline const struct STRUCT * OVS_UNUSED \
1479 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1481 ovs_assert(inst->type == htons(ENUM)); \
1482 return ALIGNED_CAST(struct STRUCT *, inst); \
1485 static inline void OVS_UNUSED \
1486 instruction_init_##ENUM(struct STRUCT *s) \
1488 memset(s, 0, sizeof *s); \
1489 s->type = htons(ENUM); \
1490 s->len = htons(sizeof *s); \
1493 static inline struct STRUCT * OVS_UNUSED \
1494 instruction_put_##ENUM(struct ofpbuf *buf) \
1496 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1497 instruction_init_##ENUM(s); \
1503 struct instruction_type_info {
1504 enum ovs_instruction_type type;
1508 static const struct instruction_type_info inst_info[] = {
1509 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1515 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1517 return inst_info[type].name;
1521 ovs_instruction_type_from_name(const char *name)
1523 const struct instruction_type_info *p;
1524 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1525 if (!strcasecmp(name, p->name)) {
1532 enum ovs_instruction_type
1533 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1537 return OVSINST_OFPIT13_METER;
1538 case OFPACT_CLEAR_ACTIONS:
1539 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1540 case OFPACT_WRITE_ACTIONS:
1541 return OVSINST_OFPIT11_WRITE_ACTIONS;
1542 case OFPACT_WRITE_METADATA:
1543 return OVSINST_OFPIT11_WRITE_METADATA;
1544 case OFPACT_GOTO_TABLE:
1545 return OVSINST_OFPIT11_GOTO_TABLE;
1548 case OFPACT_CONTROLLER:
1549 case OFPACT_ENQUEUE:
1550 case OFPACT_OUTPUT_REG:
1552 case OFPACT_SET_VLAN_VID:
1553 case OFPACT_SET_VLAN_PCP:
1554 case OFPACT_STRIP_VLAN:
1555 case OFPACT_PUSH_VLAN:
1556 case OFPACT_SET_ETH_SRC:
1557 case OFPACT_SET_ETH_DST:
1558 case OFPACT_SET_IPV4_SRC:
1559 case OFPACT_SET_IPV4_DST:
1560 case OFPACT_SET_IP_DSCP:
1561 case OFPACT_SET_IP_ECN:
1562 case OFPACT_SET_IP_TTL:
1563 case OFPACT_SET_L4_SRC_PORT:
1564 case OFPACT_SET_L4_DST_PORT:
1565 case OFPACT_REG_MOVE:
1566 case OFPACT_REG_LOAD:
1567 case OFPACT_SET_FIELD:
1568 case OFPACT_STACK_PUSH:
1569 case OFPACT_STACK_POP:
1570 case OFPACT_DEC_TTL:
1571 case OFPACT_SET_MPLS_TTL:
1572 case OFPACT_DEC_MPLS_TTL:
1573 case OFPACT_PUSH_MPLS:
1574 case OFPACT_POP_MPLS:
1575 case OFPACT_SET_TUNNEL:
1576 case OFPACT_SET_QUEUE:
1577 case OFPACT_POP_QUEUE:
1578 case OFPACT_FIN_TIMEOUT:
1579 case OFPACT_RESUBMIT:
1581 case OFPACT_MULTIPATH:
1586 return OVSINST_OFPIT11_APPLY_ACTIONS;
1590 static inline struct ofp11_instruction *
1591 instruction_next(const struct ofp11_instruction *inst)
1593 return ((struct ofp11_instruction *) (void *)
1594 ((uint8_t *) inst + ntohs(inst->len)));
1598 instruction_is_valid(const struct ofp11_instruction *inst,
1599 size_t n_instructions)
1601 uint16_t len = ntohs(inst->len);
1602 return (!(len % OFP11_INSTRUCTION_ALIGN)
1603 && len >= sizeof *inst
1604 && len / sizeof *inst <= n_instructions);
1607 /* This macro is careful to check for instructions with bad lengths. */
1608 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1609 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1610 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1611 ((LEFT) -= (ntohs((ITER)->len) \
1612 / sizeof(struct ofp11_instruction)), \
1613 (ITER) = instruction_next(ITER)))
1616 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1617 enum ovs_instruction_type *type)
1619 uint16_t len = ntohs(inst->len);
1621 switch (inst->type) {
1622 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1623 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1625 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1626 case CONSTANT_HTONS(ENUM): \
1628 ? len >= sizeof(struct STRUCT) \
1629 : len == sizeof(struct STRUCT)) { \
1630 *type = OVSINST_##ENUM; \
1633 return OFPERR_OFPBIC_BAD_LEN; \
1639 return OFPERR_OFPBIC_UNKNOWN_INST;
1644 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1646 const struct ofp11_instruction *out[])
1648 const struct ofp11_instruction *inst;
1651 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1652 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1653 enum ovs_instruction_type type;
1656 error = decode_openflow11_instruction(inst, &type);
1662 return OFPERR_ONFBIC_DUP_INSTRUCTION;
1668 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1669 (n_insts - left) * sizeof *inst);
1670 return OFPERR_OFPBIC_BAD_LEN;
1676 get_actions_from_instruction(const struct ofp11_instruction *inst,
1677 const union ofp_action **actions,
1678 size_t *max_actions)
1680 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1681 *max_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1685 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
1686 unsigned int instructions_len,
1687 enum ofp_version version,
1688 struct ofpbuf *ofpacts)
1690 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1691 const struct ofp11_instruction *instructions;
1692 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1695 ofpbuf_clear(ofpacts);
1697 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1698 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1700 instructions_len, OFP11_INSTRUCTION_ALIGN);
1701 error = OFPERR_OFPBIC_BAD_LEN;
1705 instructions = ofpbuf_try_pull(openflow, instructions_len);
1706 if (instructions == NULL) {
1707 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1708 "remaining message length (%zu)",
1709 instructions_len, openflow->size);
1710 error = OFPERR_OFPBIC_BAD_LEN;
1714 error = decode_openflow11_instructions(
1715 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1721 if (insts[OVSINST_OFPIT13_METER]) {
1722 const struct ofp13_instruction_meter *oim;
1723 struct ofpact_meter *om;
1725 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1726 insts[OVSINST_OFPIT13_METER]);
1728 om = ofpact_put_METER(ofpacts);
1729 om->meter_id = ntohl(oim->meter_id);
1731 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1732 const union ofp_action *actions;
1735 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1736 &actions, &max_actions);
1737 error = ofpacts_from_openflow(actions, max_actions, version, ofpacts);
1742 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1743 instruction_get_OFPIT11_CLEAR_ACTIONS(
1744 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1745 ofpact_put_CLEAR_ACTIONS(ofpacts);
1747 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1748 struct ofpact_nest *on;
1749 const union ofp_action *actions;
1753 ofpact_pad(ofpacts);
1754 start = ofpacts->size;
1755 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1756 offsetof(struct ofpact_nest, actions));
1757 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1758 &actions, &max_actions);
1759 error = ofpacts_from_openflow11_for_action_set(actions, max_actions,
1764 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1765 on->ofpact.len = ofpacts->size - start;
1767 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1768 const struct ofp11_instruction_write_metadata *oiwm;
1769 struct ofpact_metadata *om;
1771 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1772 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1774 om = ofpact_put_WRITE_METADATA(ofpacts);
1775 om->metadata = oiwm->metadata;
1776 om->mask = oiwm->metadata_mask;
1778 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1779 const struct ofp11_instruction_goto_table *oigt;
1780 struct ofpact_goto_table *ogt;
1782 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1783 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1784 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1785 ogt->table_id = oigt->table_id;
1788 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1791 ofpbuf_clear(ofpacts);
1796 /* May modify flow->dl_type and flow->vlan_tci, caller must restore them.
1798 * Modifies some actions, filling in fields that could not be properly set
1799 * without context. */
1801 ofpact_check__(struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
1802 uint8_t table_id, bool enforce_consistency)
1804 const struct ofpact_enqueue *enqueue;
1805 const struct mf_field *mf;
1809 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1812 case OFPACT_CONTROLLER:
1815 case OFPACT_ENQUEUE:
1816 enqueue = ofpact_get_ENQUEUE(a);
1817 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1818 && enqueue->port != OFPP_IN_PORT
1819 && enqueue->port != OFPP_LOCAL) {
1820 return OFPERR_OFPBAC_BAD_OUT_PORT;
1824 case OFPACT_OUTPUT_REG:
1825 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1828 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1830 case OFPACT_SET_VLAN_VID:
1831 /* Remember if we saw a vlan tag in the flow to aid translating to
1832 * OpenFlow 1.1+ if need be. */
1833 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
1834 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1835 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1836 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
1839 /* Temporary mark that we have a vlan tag. */
1840 flow->vlan_tci |= htons(VLAN_CFI);
1843 case OFPACT_SET_VLAN_PCP:
1844 /* Remember if we saw a vlan tag in the flow to aid translating to
1845 * OpenFlow 1.1+ if need be. */
1846 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
1847 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1848 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1849 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
1852 /* Temporary mark that we have a vlan tag. */
1853 flow->vlan_tci |= htons(VLAN_CFI);
1856 case OFPACT_STRIP_VLAN:
1857 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
1860 /* Temporary mark that we have no vlan tag. */
1861 flow->vlan_tci = htons(0);
1864 case OFPACT_PUSH_VLAN:
1865 if (flow->vlan_tci & htons(VLAN_CFI)) {
1866 /* Multiple VLAN headers not supported. */
1867 return OFPERR_OFPBAC_BAD_TAG;
1869 /* Temporary mark that we have a vlan tag. */
1870 flow->vlan_tci |= htons(VLAN_CFI);
1873 case OFPACT_SET_ETH_SRC:
1874 case OFPACT_SET_ETH_DST:
1877 case OFPACT_SET_IPV4_SRC:
1878 case OFPACT_SET_IPV4_DST:
1879 if (flow->dl_type != htons(ETH_TYPE_IP)) {
1884 case OFPACT_SET_IP_DSCP:
1885 case OFPACT_SET_IP_ECN:
1886 case OFPACT_SET_IP_TTL:
1887 case OFPACT_DEC_TTL:
1888 if (!is_ip_any(flow)) {
1893 case OFPACT_SET_L4_SRC_PORT:
1894 case OFPACT_SET_L4_DST_PORT:
1895 if (!is_ip_any(flow) ||
1896 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1897 && flow->nw_proto != IPPROTO_SCTP)) {
1902 case OFPACT_REG_MOVE:
1903 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1905 case OFPACT_REG_LOAD:
1906 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1908 case OFPACT_SET_FIELD:
1909 mf = ofpact_get_SET_FIELD(a)->field;
1910 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
1911 if (!mf_are_prereqs_ok(mf, flow) ||
1912 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
1913 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
1915 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1917 /* Remember if we saw a vlan tag in the flow to aid translating to
1918 * OpenFlow 1.1 if need be. */
1919 ofpact_get_SET_FIELD(a)->flow_has_vlan =
1920 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1921 if (mf->id == MFF_VLAN_TCI) {
1922 /* The set field may add or remove the vlan tag,
1923 * Mark the status temporarily. */
1924 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
1928 case OFPACT_STACK_PUSH:
1929 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1931 case OFPACT_STACK_POP:
1932 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1934 case OFPACT_SET_MPLS_TTL:
1935 case OFPACT_DEC_MPLS_TTL:
1936 if (!eth_type_mpls(flow->dl_type)) {
1941 case OFPACT_SET_TUNNEL:
1942 case OFPACT_SET_QUEUE:
1943 case OFPACT_POP_QUEUE:
1944 case OFPACT_RESUBMIT:
1947 case OFPACT_FIN_TIMEOUT:
1948 if (flow->nw_proto != IPPROTO_TCP) {
1954 return learn_check(ofpact_get_LEARN(a), flow);
1956 case OFPACT_MULTIPATH:
1957 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1963 case OFPACT_PUSH_MPLS:
1964 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1967 case OFPACT_POP_MPLS:
1968 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1969 if (!eth_type_mpls(flow->dl_type)) {
1977 case OFPACT_CLEAR_ACTIONS:
1980 case OFPACT_WRITE_ACTIONS: {
1981 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
1982 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
1983 flow, max_ports, table_id, false);
1986 case OFPACT_WRITE_METADATA:
1989 case OFPACT_METER: {
1990 uint32_t mid = ofpact_get_METER(a)->meter_id;
1991 if (mid == 0 || mid > OFPM13_MAX) {
1992 return OFPERR_OFPMMFC_INVALID_METER;
1997 case OFPACT_GOTO_TABLE:
1998 if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
1999 return OFPERR_OFPBRC_BAD_TABLE_ID;
2011 if (enforce_consistency) {
2012 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2017 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2018 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
2019 * switch with no more than 'max_ports' ports.
2021 * May annotate ofpacts with information gathered from the 'flow'.
2023 * May temporarily modify 'flow', but restores the changes before returning. */
2025 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
2026 struct flow *flow, ofp_port_t max_ports, uint8_t table_id,
2027 bool enforce_consistency)
2030 ovs_be16 dl_type = flow->dl_type;
2031 ovs_be16 vlan_tci = flow->vlan_tci;
2032 enum ofperr error = 0;
2034 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2035 error = ofpact_check__(a, flow, max_ports, table_id,
2036 enforce_consistency);
2041 /* Restore fields that may have been modified. */
2042 flow->dl_type = dl_type;
2043 flow->vlan_tci = vlan_tci;
2047 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2048 * in the appropriate order as defined by the OpenFlow spec. */
2050 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
2052 const struct ofpact *a;
2053 enum ovs_instruction_type inst;
2055 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
2056 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2057 enum ovs_instruction_type next;
2059 next = ovs_instruction_type_from_ofpact_type(a->type);
2060 if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
2063 const char *name = ovs_instruction_name_from_type(inst);
2064 const char *next_name = ovs_instruction_name_from_type(next);
2067 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
2068 "1.1+ compatibility", name);
2070 VLOG_WARN("invalid instruction ordering: %s must appear "
2071 "before %s, for OpenFlow 1.1+ compatibility",
2074 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
2083 /* Converting ofpacts to Nicira OpenFlow extensions. */
2086 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
2089 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
2091 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
2092 output_reg->src.n_bits);
2093 naor->src = htonl(output_reg->src.field->nxm_header);
2094 naor->max_len = htons(output_reg->max_len);
2098 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
2101 struct nx_action_resubmit *nar;
2103 if (resubmit->table_id == 0xff
2104 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
2105 nar = ofputil_put_NXAST_RESUBMIT(out);
2107 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
2108 nar->table = resubmit->table_id;
2110 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
2114 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
2117 uint64_t tun_id = tunnel->tun_id;
2119 if (tun_id <= UINT32_MAX
2120 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
2121 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
2123 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
2128 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
2131 struct nx_action_write_metadata *nawm;
2133 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
2134 nawm->metadata = om->metadata;
2135 nawm->mask = om->mask;
2139 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
2141 size_t start_ofs = out->size;
2142 struct nx_action_note *nan;
2143 unsigned int remainder;
2146 nan = ofputil_put_NXAST_NOTE(out);
2147 out->size -= sizeof nan->note;
2149 ofpbuf_put(out, note->data, note->length);
2151 len = out->size - start_ofs;
2152 remainder = len % OFP_ACTION_ALIGN;
2154 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
2156 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
2157 nan->len = htons(out->size - start_ofs);
2161 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
2164 struct nx_action_controller *nac;
2166 nac = ofputil_put_NXAST_CONTROLLER(out);
2167 nac->max_len = htons(oc->max_len);
2168 nac->controller_id = htons(oc->controller_id);
2169 nac->reason = oc->reason;
2173 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
2176 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
2177 ofputil_put_NXAST_DEC_TTL(out);
2179 struct nx_action_cnt_ids *nac_ids =
2180 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
2181 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
2185 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2186 nac_ids->n_controllers = htons(oc_ids->n_controllers);
2188 ids = ofpbuf_put_zeros(out, ids_len);
2189 for (i = 0; i < oc_ids->n_controllers; i++) {
2190 ids[i] = htons(oc_ids->cnt_ids[i]);
2196 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
2199 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
2200 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
2201 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
2205 ofpact_sample_to_nxast(const struct ofpact_sample *os,
2208 struct nx_action_sample *nas;
2210 nas = ofputil_put_NXAST_SAMPLE(out);
2211 nas->probability = htons(os->probability);
2212 nas->collector_set_id = htonl(os->collector_set_id);
2213 nas->obs_domain_id = htonl(os->obs_domain_id);
2214 nas->obs_point_id = htonl(os->obs_point_id);
2218 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
2221 case OFPACT_CONTROLLER:
2222 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
2225 case OFPACT_OUTPUT_REG:
2226 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
2230 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
2233 case OFPACT_REG_MOVE:
2234 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
2237 case OFPACT_REG_LOAD:
2238 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
2241 case OFPACT_STACK_PUSH:
2242 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
2245 case OFPACT_STACK_POP:
2246 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
2249 case OFPACT_DEC_TTL:
2250 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
2253 case OFPACT_SET_MPLS_TTL:
2254 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
2255 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2258 case OFPACT_DEC_MPLS_TTL:
2259 ofputil_put_NXAST_DEC_MPLS_TTL(out);
2262 case OFPACT_SET_TUNNEL:
2263 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
2266 case OFPACT_WRITE_METADATA:
2267 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
2270 case OFPACT_SET_QUEUE:
2271 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
2272 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2275 case OFPACT_POP_QUEUE:
2276 ofputil_put_NXAST_POP_QUEUE(out);
2279 case OFPACT_FIN_TIMEOUT:
2280 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
2283 case OFPACT_RESUBMIT:
2284 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
2288 learn_to_nxast(ofpact_get_LEARN(a), out);
2291 case OFPACT_MULTIPATH:
2292 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
2296 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
2300 ofputil_put_NXAST_EXIT(out);
2303 case OFPACT_PUSH_MPLS:
2304 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
2305 ofpact_get_PUSH_MPLS(a)->ethertype;
2308 case OFPACT_POP_MPLS:
2309 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2310 ofpact_get_POP_MPLS(a)->ethertype;
2314 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2319 case OFPACT_ENQUEUE:
2320 case OFPACT_SET_VLAN_VID:
2321 case OFPACT_SET_VLAN_PCP:
2322 case OFPACT_STRIP_VLAN:
2323 case OFPACT_PUSH_VLAN:
2324 case OFPACT_SET_ETH_SRC:
2325 case OFPACT_SET_ETH_DST:
2326 case OFPACT_SET_IPV4_SRC:
2327 case OFPACT_SET_IPV4_DST:
2328 case OFPACT_SET_IP_DSCP:
2329 case OFPACT_SET_IP_ECN:
2330 case OFPACT_SET_IP_TTL:
2331 case OFPACT_SET_L4_SRC_PORT:
2332 case OFPACT_SET_L4_DST_PORT:
2333 case OFPACT_WRITE_ACTIONS:
2334 case OFPACT_CLEAR_ACTIONS:
2335 case OFPACT_GOTO_TABLE:
2337 case OFPACT_SET_FIELD:
2342 /* Converting ofpacts to OpenFlow 1.0. */
2345 ofpact_output_to_openflow10(const struct ofpact_output *output,
2348 struct ofp10_action_output *oao;
2350 oao = ofputil_put_OFPAT10_OUTPUT(out);
2351 oao->port = htons(ofp_to_u16(output->port));
2352 oao->max_len = htons(output->max_len);
2356 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2359 struct ofp10_action_enqueue *oae;
2361 oae = ofputil_put_OFPAT10_ENQUEUE(out);
2362 oae->port = htons(ofp_to_u16(enqueue->port));
2363 oae->queue_id = htonl(enqueue->queue);
2367 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2371 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2374 case OFPACT_ENQUEUE:
2375 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2378 case OFPACT_SET_VLAN_VID:
2379 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2380 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2383 case OFPACT_SET_VLAN_PCP:
2384 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2385 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2388 case OFPACT_STRIP_VLAN:
2389 ofputil_put_OFPAT10_STRIP_VLAN(out);
2392 case OFPACT_SET_ETH_SRC:
2393 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2394 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2397 case OFPACT_SET_ETH_DST:
2398 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2399 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2402 case OFPACT_SET_IPV4_SRC:
2403 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2404 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2407 case OFPACT_SET_IPV4_DST:
2408 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2409 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2412 case OFPACT_SET_IP_DSCP:
2413 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2414 = ofpact_get_SET_IP_DSCP(a)->dscp;
2417 case OFPACT_SET_L4_SRC_PORT:
2418 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2419 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2422 case OFPACT_SET_L4_DST_PORT:
2423 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2424 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2427 case OFPACT_PUSH_VLAN:
2428 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
2429 * follow this action. */
2432 case OFPACT_CLEAR_ACTIONS:
2433 case OFPACT_WRITE_ACTIONS:
2434 case OFPACT_GOTO_TABLE:
2442 case OFPACT_SET_FIELD:
2443 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2446 case OFPACT_CONTROLLER:
2447 case OFPACT_OUTPUT_REG:
2449 case OFPACT_REG_MOVE:
2450 case OFPACT_REG_LOAD:
2451 case OFPACT_STACK_PUSH:
2452 case OFPACT_STACK_POP:
2453 case OFPACT_DEC_TTL:
2454 case OFPACT_SET_IP_ECN:
2455 case OFPACT_SET_IP_TTL:
2456 case OFPACT_SET_MPLS_TTL:
2457 case OFPACT_DEC_MPLS_TTL:
2458 case OFPACT_SET_TUNNEL:
2459 case OFPACT_WRITE_METADATA:
2460 case OFPACT_SET_QUEUE:
2461 case OFPACT_POP_QUEUE:
2462 case OFPACT_FIN_TIMEOUT:
2463 case OFPACT_RESUBMIT:
2465 case OFPACT_MULTIPATH:
2468 case OFPACT_PUSH_MPLS:
2469 case OFPACT_POP_MPLS:
2471 ofpact_to_nxast(a, out);
2476 /* Converting ofpacts to OpenFlow 1.1. */
2479 ofpact_output_to_openflow11(const struct ofpact_output *output,
2482 struct ofp11_action_output *oao;
2484 oao = ofputil_put_OFPAT11_OUTPUT(out);
2485 oao->port = ofputil_port_to_ofp11(output->port);
2486 oao->max_len = htons(output->max_len);
2490 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2493 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2494 && (!dec_ttl->ofpact.compat ||
2495 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2496 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2498 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2503 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2507 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2509 case OFPACT_ENQUEUE:
2513 case OFPACT_SET_VLAN_VID:
2514 /* Push a VLAN tag, if one was not seen at action validation time. */
2515 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan
2516 && ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2517 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2518 = htons(ETH_TYPE_VLAN_8021Q);
2520 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2521 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2524 case OFPACT_SET_VLAN_PCP:
2525 /* Push a VLAN tag, if one was not seen at action validation time. */
2526 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan
2527 && ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2528 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2529 = htons(ETH_TYPE_VLAN_8021Q);
2531 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2532 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2535 case OFPACT_STRIP_VLAN:
2536 ofputil_put_OFPAT11_POP_VLAN(out);
2539 case OFPACT_PUSH_VLAN:
2540 /* XXX ETH_TYPE_VLAN_8021AD case */
2541 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2542 htons(ETH_TYPE_VLAN_8021Q);
2545 case OFPACT_SET_QUEUE:
2546 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2547 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2550 case OFPACT_SET_ETH_SRC:
2551 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2552 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2555 case OFPACT_SET_ETH_DST:
2556 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2557 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2560 case OFPACT_SET_IPV4_SRC:
2561 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2562 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2565 case OFPACT_SET_IPV4_DST:
2566 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2567 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2570 case OFPACT_SET_IP_DSCP:
2571 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2572 = ofpact_get_SET_IP_DSCP(a)->dscp;
2575 case OFPACT_SET_IP_ECN:
2576 ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2577 = ofpact_get_SET_IP_ECN(a)->ecn;
2580 case OFPACT_SET_IP_TTL:
2581 ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2582 = ofpact_get_SET_IP_TTL(a)->ttl;
2585 case OFPACT_SET_L4_SRC_PORT:
2586 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2587 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2590 case OFPACT_SET_L4_DST_PORT:
2591 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2592 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2595 case OFPACT_DEC_TTL:
2596 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2599 case OFPACT_SET_MPLS_TTL:
2600 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2601 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2604 case OFPACT_DEC_MPLS_TTL:
2605 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2608 case OFPACT_WRITE_METADATA:
2609 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2612 case OFPACT_PUSH_MPLS:
2613 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2614 ofpact_get_PUSH_MPLS(a)->ethertype;
2617 case OFPACT_POP_MPLS:
2618 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2619 ofpact_get_POP_MPLS(a)->ethertype;
2623 case OFPACT_CLEAR_ACTIONS:
2624 case OFPACT_WRITE_ACTIONS:
2625 case OFPACT_GOTO_TABLE:
2630 ofputil_put_OFPAT11_GROUP(out)->group_id =
2631 htonl(ofpact_get_GROUP(a)->group_id);
2634 case OFPACT_SET_FIELD:
2635 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2638 case OFPACT_CONTROLLER:
2639 case OFPACT_OUTPUT_REG:
2641 case OFPACT_REG_MOVE:
2642 case OFPACT_REG_LOAD:
2643 case OFPACT_STACK_PUSH:
2644 case OFPACT_STACK_POP:
2645 case OFPACT_SET_TUNNEL:
2646 case OFPACT_POP_QUEUE:
2647 case OFPACT_FIN_TIMEOUT:
2648 case OFPACT_RESUBMIT:
2650 case OFPACT_MULTIPATH:
2654 ofpact_to_nxast(a, out);
2660 ofpact_to_openflow12(const struct ofpact *a, struct ofpbuf *out)
2662 ofpact_to_openflow11(a, out);
2665 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
2666 * actions in 'openflow', appending the actions to any existing data in
2669 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
2670 struct ofpbuf *openflow,
2671 enum ofp_version ofp_version)
2673 const struct ofpact *a;
2674 size_t start_size = openflow->size;
2676 void (*translate)(const struct ofpact *a, struct ofpbuf *out) =
2677 (ofp_version == OFP10_VERSION) ? ofpact_to_openflow10 :
2678 (ofp_version == OFP11_VERSION) ? ofpact_to_openflow11 :
2679 ofpact_to_openflow12;
2681 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2682 translate(a, openflow);
2684 return openflow->size - start_size;
2688 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2690 struct ofp11_instruction_actions *oia;
2692 /* Update the instruction's length (or, if it's empty, delete it). */
2693 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2694 if (openflow->size > ofs + sizeof *oia) {
2695 oia->len = htons(openflow->size - ofs);
2697 openflow->size = ofs;
2702 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
2704 struct ofpbuf *openflow,
2705 enum ofp_version ofp_version)
2707 const struct ofpact *a;
2709 ovs_assert(ofp_version >= OFP11_VERSION);
2711 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2712 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2713 case OVSINST_OFPIT11_CLEAR_ACTIONS:
2714 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2717 case OVSINST_OFPIT11_GOTO_TABLE: {
2718 struct ofp11_instruction_goto_table *oigt;
2719 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2720 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2721 memset(oigt->pad, 0, sizeof oigt->pad);
2725 case OVSINST_OFPIT11_WRITE_METADATA: {
2726 const struct ofpact_metadata *om;
2727 struct ofp11_instruction_write_metadata *oiwm;
2729 om = ofpact_get_WRITE_METADATA(a);
2730 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2731 oiwm->metadata = om->metadata;
2732 oiwm->metadata_mask = om->mask;
2736 case OVSINST_OFPIT13_METER:
2737 if (ofp_version >= OFP13_VERSION) {
2738 const struct ofpact_meter *om;
2739 struct ofp13_instruction_meter *oim;
2741 om = ofpact_get_METER(a);
2742 oim = instruction_put_OFPIT13_METER(openflow);
2743 oim->meter_id = htonl(om->meter_id);
2747 case OVSINST_OFPIT11_APPLY_ACTIONS: {
2748 const size_t ofs = openflow->size;
2749 const size_t ofpacts_len_left =
2750 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2751 const struct ofpact *action;
2752 const struct ofpact *processed = a;
2754 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2755 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
2756 if (ovs_instruction_type_from_ofpact_type(action->type)
2757 != OVSINST_OFPIT11_APPLY_ACTIONS) {
2760 if (ofp_version == OFP11_VERSION) {
2761 ofpact_to_openflow11(action, openflow);
2763 ofpact_to_openflow12(action, openflow);
2767 ofpacts_update_instruction_actions(openflow, ofs);
2772 case OVSINST_OFPIT11_WRITE_ACTIONS: {
2773 const size_t ofs = openflow->size;
2774 const struct ofpact_nest *on;
2776 on = ofpact_get_WRITE_ACTIONS(a);
2777 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2778 ofpacts_put_openflow_actions(on->actions,
2779 ofpact_nest_get_action_len(on),
2780 openflow, ofp_version);
2781 ofpacts_update_instruction_actions(openflow, ofs);
2789 /* Returns true if 'action' outputs to 'port', false otherwise. */
2791 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
2793 switch (ofpact->type) {
2795 return ofpact_get_OUTPUT(ofpact)->port == port;
2796 case OFPACT_ENQUEUE:
2797 return ofpact_get_ENQUEUE(ofpact)->port == port;
2798 case OFPACT_CONTROLLER:
2799 return port == OFPP_CONTROLLER;
2801 case OFPACT_OUTPUT_REG:
2803 case OFPACT_SET_VLAN_VID:
2804 case OFPACT_SET_VLAN_PCP:
2805 case OFPACT_STRIP_VLAN:
2806 case OFPACT_PUSH_VLAN:
2807 case OFPACT_SET_ETH_SRC:
2808 case OFPACT_SET_ETH_DST:
2809 case OFPACT_SET_IPV4_SRC:
2810 case OFPACT_SET_IPV4_DST:
2811 case OFPACT_SET_IP_DSCP:
2812 case OFPACT_SET_IP_ECN:
2813 case OFPACT_SET_IP_TTL:
2814 case OFPACT_SET_L4_SRC_PORT:
2815 case OFPACT_SET_L4_DST_PORT:
2816 case OFPACT_REG_MOVE:
2817 case OFPACT_REG_LOAD:
2818 case OFPACT_SET_FIELD:
2819 case OFPACT_STACK_PUSH:
2820 case OFPACT_STACK_POP:
2821 case OFPACT_DEC_TTL:
2822 case OFPACT_SET_MPLS_TTL:
2823 case OFPACT_DEC_MPLS_TTL:
2824 case OFPACT_SET_TUNNEL:
2825 case OFPACT_WRITE_METADATA:
2826 case OFPACT_SET_QUEUE:
2827 case OFPACT_POP_QUEUE:
2828 case OFPACT_FIN_TIMEOUT:
2829 case OFPACT_RESUBMIT:
2831 case OFPACT_MULTIPATH:
2834 case OFPACT_PUSH_MPLS:
2835 case OFPACT_POP_MPLS:
2837 case OFPACT_CLEAR_ACTIONS:
2838 case OFPACT_WRITE_ACTIONS:
2839 case OFPACT_GOTO_TABLE:
2847 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2848 * to 'port', false otherwise. */
2850 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
2853 const struct ofpact *a;
2855 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2856 if (ofpact_outputs_to_port(a, port)) {
2864 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2865 * to 'group', false otherwise. */
2867 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
2870 const struct ofpact *a;
2872 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2873 if (a->type == OFPACT_GROUP
2874 && ofpact_get_GROUP(a)->group_id == group_id) {
2883 ofpacts_equal(const struct ofpact *a, size_t a_len,
2884 const struct ofpact *b, size_t b_len)
2886 return a_len == b_len && !memcmp(a, b, a_len);
2889 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2890 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2892 * This function relies on the order of 'ofpacts' being correct (as checked by
2893 * ofpacts_verify()). */
2895 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2897 const struct ofpact *a;
2899 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2900 enum ovs_instruction_type inst;
2902 inst = ovs_instruction_type_from_ofpact_type(a->type);
2903 if (a->type == OFPACT_METER) {
2904 return ofpact_get_METER(a)->meter_id;
2905 } else if (inst > OVSINST_OFPIT13_METER) {
2913 /* Formatting ofpacts. */
2916 print_note(const struct ofpact_note *note, struct ds *string)
2920 ds_put_cstr(string, "note:");
2921 for (i = 0; i < note->length; i++) {
2923 ds_put_char(string, '.');
2925 ds_put_format(string, "%02"PRIx8, note->data[i]);
2930 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2935 ds_put_cstr(s, "dec_ttl");
2936 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2937 ds_put_cstr(s, "(");
2938 for (i = 0; i < ids->n_controllers; i++) {
2940 ds_put_cstr(s, ",");
2942 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2944 ds_put_cstr(s, ")");
2949 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2952 ds_put_cstr(s, "fin_timeout(");
2953 if (fin_timeout->fin_idle_timeout) {
2954 ds_put_format(s, "idle_timeout=%"PRIu16",",
2955 fin_timeout->fin_idle_timeout);
2957 if (fin_timeout->fin_hard_timeout) {
2958 ds_put_format(s, "hard_timeout=%"PRIu16",",
2959 fin_timeout->fin_hard_timeout);
2962 ds_put_char(s, ')');
2966 ofpact_format(const struct ofpact *a, struct ds *s)
2968 const struct ofpact_enqueue *enqueue;
2969 const struct ofpact_resubmit *resubmit;
2970 const struct ofpact_controller *controller;
2971 const struct ofpact_metadata *metadata;
2972 const struct ofpact_tunnel *tunnel;
2973 const struct ofpact_sample *sample;
2974 const struct ofpact_set_field *set_field;
2975 const struct mf_field *mf;
2980 port = ofpact_get_OUTPUT(a)->port;
2981 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
2982 ds_put_format(s, "output:%"PRIu16, port);
2984 ofputil_format_port(port, s);
2985 if (port == OFPP_CONTROLLER) {
2986 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2991 case OFPACT_CONTROLLER:
2992 controller = ofpact_get_CONTROLLER(a);
2993 if (controller->reason == OFPR_ACTION &&
2994 controller->controller_id == 0) {
2995 ds_put_format(s, "CONTROLLER:%"PRIu16,
2996 ofpact_get_CONTROLLER(a)->max_len);
2998 enum ofp_packet_in_reason reason = controller->reason;
3000 ds_put_cstr(s, "controller(");
3001 if (reason != OFPR_ACTION) {
3002 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3004 ds_put_format(s, "reason=%s,",
3005 ofputil_packet_in_reason_to_string(
3006 reason, reasonbuf, sizeof reasonbuf));
3008 if (controller->max_len != UINT16_MAX) {
3009 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
3011 if (controller->controller_id != 0) {
3012 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
3015 ds_put_char(s, ')');
3019 case OFPACT_ENQUEUE:
3020 enqueue = ofpact_get_ENQUEUE(a);
3021 ds_put_format(s, "enqueue:");
3022 ofputil_format_port(enqueue->port, s);
3023 ds_put_format(s, "q%"PRIu32, enqueue->queue);
3026 case OFPACT_OUTPUT_REG:
3027 ds_put_cstr(s, "output:");
3028 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
3032 bundle_format(ofpact_get_BUNDLE(a), s);
3035 case OFPACT_SET_VLAN_VID:
3036 ds_put_format(s, "%s:%"PRIu16,
3037 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_VID
3040 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
3043 case OFPACT_SET_VLAN_PCP:
3044 ds_put_format(s, "%s:%"PRIu8,
3045 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_PCP
3048 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
3051 case OFPACT_STRIP_VLAN:
3052 ds_put_cstr(s, a->compat == OFPUTIL_OFPAT11_POP_VLAN
3053 ? "pop_vlan" : "strip_vlan");
3056 case OFPACT_PUSH_VLAN:
3057 /* XXX 802.1AD case*/
3058 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
3061 case OFPACT_SET_ETH_SRC:
3062 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
3063 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
3066 case OFPACT_SET_ETH_DST:
3067 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
3068 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
3071 case OFPACT_SET_IPV4_SRC:
3072 ds_put_format(s, "mod_nw_src:"IP_FMT,
3073 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
3076 case OFPACT_SET_IPV4_DST:
3077 ds_put_format(s, "mod_nw_dst:"IP_FMT,
3078 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
3081 case OFPACT_SET_IP_DSCP:
3082 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
3085 case OFPACT_SET_IP_ECN:
3086 ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
3089 case OFPACT_SET_IP_TTL:
3090 ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
3093 case OFPACT_SET_L4_SRC_PORT:
3094 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
3097 case OFPACT_SET_L4_DST_PORT:
3098 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
3101 case OFPACT_REG_MOVE:
3102 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
3105 case OFPACT_REG_LOAD:
3106 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
3109 case OFPACT_SET_FIELD:
3110 set_field = ofpact_get_SET_FIELD(a);
3111 mf = set_field->field;
3112 ds_put_format(s, "set_field:");
3113 mf_format(mf, &set_field->value, NULL, s);
3114 ds_put_format(s, "->%s", mf->name);
3117 case OFPACT_STACK_PUSH:
3118 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
3121 case OFPACT_STACK_POP:
3122 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
3125 case OFPACT_DEC_TTL:
3126 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
3129 case OFPACT_SET_MPLS_TTL:
3130 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3131 ofpact_get_SET_MPLS_TTL(a)->ttl);
3134 case OFPACT_DEC_MPLS_TTL:
3135 ds_put_cstr(s, "dec_mpls_ttl");
3138 case OFPACT_SET_TUNNEL:
3139 tunnel = ofpact_get_SET_TUNNEL(a);
3140 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3141 (tunnel->tun_id > UINT32_MAX
3142 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
3146 case OFPACT_SET_QUEUE:
3147 ds_put_format(s, "set_queue:%"PRIu32,
3148 ofpact_get_SET_QUEUE(a)->queue_id);
3151 case OFPACT_POP_QUEUE:
3152 ds_put_cstr(s, "pop_queue");
3155 case OFPACT_FIN_TIMEOUT:
3156 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
3159 case OFPACT_RESUBMIT:
3160 resubmit = ofpact_get_RESUBMIT(a);
3161 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
3162 ds_put_cstr(s, "resubmit:");
3163 ofputil_format_port(resubmit->in_port, s);
3165 ds_put_format(s, "resubmit(");
3166 if (resubmit->in_port != OFPP_IN_PORT) {
3167 ofputil_format_port(resubmit->in_port, s);
3169 ds_put_char(s, ',');
3170 if (resubmit->table_id != 255) {
3171 ds_put_format(s, "%"PRIu8, resubmit->table_id);
3173 ds_put_char(s, ')');
3178 learn_format(ofpact_get_LEARN(a), s);
3181 case OFPACT_MULTIPATH:
3182 multipath_format(ofpact_get_MULTIPATH(a), s);
3186 print_note(ofpact_get_NOTE(a), s);
3189 case OFPACT_PUSH_MPLS:
3190 ds_put_format(s, "push_mpls:0x%04"PRIx16,
3191 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
3194 case OFPACT_POP_MPLS:
3195 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
3196 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
3200 ds_put_cstr(s, "exit");
3204 sample = ofpact_get_SAMPLE(a);
3206 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
3207 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
3208 sample->probability, sample->collector_set_id,
3209 sample->obs_domain_id, sample->obs_point_id);
3212 case OFPACT_WRITE_ACTIONS: {
3213 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3214 ds_put_format(s, "%s(",
3215 ovs_instruction_name_from_type(
3216 OVSINST_OFPIT11_WRITE_ACTIONS));
3217 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
3218 ds_put_char(s, ')');
3222 case OFPACT_CLEAR_ACTIONS:
3223 ds_put_format(s, "%s",
3224 ovs_instruction_name_from_type(
3225 OVSINST_OFPIT11_CLEAR_ACTIONS));
3228 case OFPACT_WRITE_METADATA:
3229 metadata = ofpact_get_WRITE_METADATA(a);
3230 ds_put_format(s, "%s:%#"PRIx64,
3231 ovs_instruction_name_from_type(
3232 OVSINST_OFPIT11_WRITE_METADATA),
3233 ntohll(metadata->metadata));
3234 if (metadata->mask != OVS_BE64_MAX) {
3235 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
3239 case OFPACT_GOTO_TABLE:
3240 ds_put_format(s, "%s:%"PRIu8,
3241 ovs_instruction_name_from_type(
3242 OVSINST_OFPIT11_GOTO_TABLE),
3243 ofpact_get_GOTO_TABLE(a)->table_id);
3247 ds_put_format(s, "%s:%"PRIu32,
3248 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
3249 ofpact_get_METER(a)->meter_id);
3253 ds_put_format(s, "group:%"PRIu32,
3254 ofpact_get_GROUP(a)->group_id);
3259 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
3260 * 'ofpacts' to 'string'. */
3262 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
3266 ds_put_cstr(string, "drop");
3268 const struct ofpact *a;
3270 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3272 ds_put_cstr(string, ",");
3275 /* XXX write-actions */
3276 ofpact_format(a, string);
3281 /* Internal use by helpers. */
3284 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
3286 struct ofpact *ofpact;
3288 ofpact_pad(ofpacts);
3289 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
3290 ofpact_init(ofpact, type, len);
3295 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
3297 memset(ofpact, 0, len);
3298 ofpact->type = type;
3299 ofpact->compat = OFPUTIL_ACTION_INVALID;
3303 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
3304 * starting at 'ofpact'.
3306 * This is the correct way to update a variable-length ofpact's length after
3307 * adding the variable-length part of the payload. (See the large comment
3308 * near the end of ofp-actions.h for more information.) */
3310 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
3312 ovs_assert(ofpact == ofpacts->l2);
3313 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
3316 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
3317 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
3318 * client must call this itself after adding the final ofpact to an array of
3321 * (The consequences of failing to call this function are probably not dire.
3322 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
3323 * not dereference it. That's undefined behavior, technically, but it will not
3324 * cause a real problem on common systems. Still, it seems better to call
3327 ofpact_pad(struct ofpbuf *ofpacts)
3329 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
3331 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);