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 ofpact_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 ofpact_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 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
1797 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
1798 * valid, otherwise an OpenFlow error code. */
1800 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
1808 case OFPP_CONTROLLER:
1814 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
1817 return OFPERR_OFPBAC_BAD_OUT_PORT;
1821 /* May modify flow->dl_type and flow->vlan_tci, caller must restore them.
1823 * Modifies some actions, filling in fields that could not be properly set
1824 * without context. */
1826 ofpact_check__(struct ofpact *a, struct flow *flow,
1827 bool enforce_consistency, ofp_port_t max_ports,
1828 uint8_t table_id, uint8_t n_tables)
1830 const struct ofpact_enqueue *enqueue;
1831 const struct mf_field *mf;
1835 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
1838 case OFPACT_CONTROLLER:
1841 case OFPACT_ENQUEUE:
1842 enqueue = ofpact_get_ENQUEUE(a);
1843 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1844 && enqueue->port != OFPP_IN_PORT
1845 && enqueue->port != OFPP_LOCAL) {
1846 return OFPERR_OFPBAC_BAD_OUT_PORT;
1850 case OFPACT_OUTPUT_REG:
1851 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1854 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1856 case OFPACT_SET_VLAN_VID:
1857 /* Remember if we saw a vlan tag in the flow to aid translating to
1858 * OpenFlow 1.1+ if need be. */
1859 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
1860 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1861 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1862 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
1865 /* Temporary mark that we have a vlan tag. */
1866 flow->vlan_tci |= htons(VLAN_CFI);
1869 case OFPACT_SET_VLAN_PCP:
1870 /* Remember if we saw a vlan tag in the flow to aid translating to
1871 * OpenFlow 1.1+ if need be. */
1872 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
1873 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1874 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1875 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
1878 /* Temporary mark that we have a vlan tag. */
1879 flow->vlan_tci |= htons(VLAN_CFI);
1882 case OFPACT_STRIP_VLAN:
1883 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
1886 /* Temporary mark that we have no vlan tag. */
1887 flow->vlan_tci = htons(0);
1890 case OFPACT_PUSH_VLAN:
1891 if (flow->vlan_tci & htons(VLAN_CFI)) {
1892 /* Multiple VLAN headers not supported. */
1893 return OFPERR_OFPBAC_BAD_TAG;
1895 /* Temporary mark that we have a vlan tag. */
1896 flow->vlan_tci |= htons(VLAN_CFI);
1899 case OFPACT_SET_ETH_SRC:
1900 case OFPACT_SET_ETH_DST:
1903 case OFPACT_SET_IPV4_SRC:
1904 case OFPACT_SET_IPV4_DST:
1905 if (flow->dl_type != htons(ETH_TYPE_IP)) {
1910 case OFPACT_SET_IP_DSCP:
1911 case OFPACT_SET_IP_ECN:
1912 case OFPACT_SET_IP_TTL:
1913 case OFPACT_DEC_TTL:
1914 if (!is_ip_any(flow)) {
1919 case OFPACT_SET_L4_SRC_PORT:
1920 if (!is_ip_any(flow) ||
1921 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1922 && flow->nw_proto != IPPROTO_SCTP)) {
1925 /* Note on which transport protocol the port numbers are set.
1926 * This allows this set action to be converted to an OF1.2 set field
1928 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
1931 case OFPACT_SET_L4_DST_PORT:
1932 if (!is_ip_any(flow) ||
1933 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1934 && flow->nw_proto != IPPROTO_SCTP)) {
1937 /* Note on which transport protocol the port numbers are set.
1938 * This allows this set action to be converted to an OF1.2 set field
1940 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
1943 case OFPACT_REG_MOVE:
1944 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1946 case OFPACT_REG_LOAD:
1947 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1949 case OFPACT_SET_FIELD:
1950 mf = ofpact_get_SET_FIELD(a)->field;
1951 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
1952 if (!mf_are_prereqs_ok(mf, flow) ||
1953 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
1954 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
1956 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1958 /* Remember if we saw a vlan tag in the flow to aid translating to
1959 * OpenFlow 1.1 if need be. */
1960 ofpact_get_SET_FIELD(a)->flow_has_vlan =
1961 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1962 if (mf->id == MFF_VLAN_TCI) {
1963 /* The set field may add or remove the vlan tag,
1964 * Mark the status temporarily. */
1965 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
1969 case OFPACT_STACK_PUSH:
1970 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1972 case OFPACT_STACK_POP:
1973 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1975 case OFPACT_SET_MPLS_TTL:
1976 case OFPACT_DEC_MPLS_TTL:
1977 if (!eth_type_mpls(flow->dl_type)) {
1982 case OFPACT_SET_TUNNEL:
1983 case OFPACT_SET_QUEUE:
1984 case OFPACT_POP_QUEUE:
1985 case OFPACT_RESUBMIT:
1988 case OFPACT_FIN_TIMEOUT:
1989 if (flow->nw_proto != IPPROTO_TCP) {
1995 return learn_check(ofpact_get_LEARN(a), flow);
1997 case OFPACT_MULTIPATH:
1998 return multipath_check(ofpact_get_MULTIPATH(a), flow);
2004 case OFPACT_PUSH_MPLS:
2005 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
2008 case OFPACT_POP_MPLS:
2009 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
2010 if (!eth_type_mpls(flow->dl_type)) {
2018 case OFPACT_CLEAR_ACTIONS:
2021 case OFPACT_WRITE_ACTIONS: {
2022 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2023 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
2024 flow, false, max_ports, table_id, n_tables);
2027 case OFPACT_WRITE_METADATA:
2030 case OFPACT_METER: {
2031 uint32_t mid = ofpact_get_METER(a)->meter_id;
2032 if (mid == 0 || mid > OFPM13_MAX) {
2033 return OFPERR_OFPMMFC_INVALID_METER;
2038 case OFPACT_GOTO_TABLE: {
2039 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
2040 if ((table_id != 255 && goto_table <= table_id)
2041 || (n_tables != 255 && goto_table >= n_tables)) {
2042 return OFPERR_OFPBRC_BAD_TABLE_ID;
2055 if (enforce_consistency) {
2056 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2061 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2062 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
2063 * switch with no more than 'max_ports' ports.
2065 * May annotate ofpacts with information gathered from the 'flow'.
2067 * May temporarily modify 'flow', but restores the changes before returning. */
2069 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
2070 struct flow *flow, bool enforce_consistency,
2071 ofp_port_t max_ports,
2072 uint8_t table_id, uint8_t n_tables)
2075 ovs_be16 dl_type = flow->dl_type;
2076 ovs_be16 vlan_tci = flow->vlan_tci;
2077 enum ofperr error = 0;
2079 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2080 error = ofpact_check__(a, flow, enforce_consistency,
2081 max_ports, table_id, n_tables);
2086 /* Restore fields that may have been modified. */
2087 flow->dl_type = dl_type;
2088 flow->vlan_tci = vlan_tci;
2092 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2093 * in the appropriate order as defined by the OpenFlow spec. */
2095 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
2097 const struct ofpact *a;
2098 enum ovs_instruction_type inst;
2100 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
2101 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2102 enum ovs_instruction_type next;
2104 next = ovs_instruction_type_from_ofpact_type(a->type);
2105 if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
2108 const char *name = ovs_instruction_name_from_type(inst);
2109 const char *next_name = ovs_instruction_name_from_type(next);
2112 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
2113 "1.1+ compatibility", name);
2115 VLOG_WARN("invalid instruction ordering: %s must appear "
2116 "before %s, for OpenFlow 1.1+ compatibility",
2119 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
2128 /* Converting ofpacts to Nicira OpenFlow extensions. */
2131 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
2134 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
2136 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
2137 output_reg->src.n_bits);
2138 naor->src = htonl(output_reg->src.field->nxm_header);
2139 naor->max_len = htons(output_reg->max_len);
2143 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
2146 struct nx_action_resubmit *nar;
2148 if (resubmit->table_id == 0xff
2149 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
2150 nar = ofputil_put_NXAST_RESUBMIT(out);
2152 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
2153 nar->table = resubmit->table_id;
2155 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
2159 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
2162 uint64_t tun_id = tunnel->tun_id;
2164 if (tun_id <= UINT32_MAX
2165 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
2166 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
2168 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
2173 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
2176 struct nx_action_write_metadata *nawm;
2178 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
2179 nawm->metadata = om->metadata;
2180 nawm->mask = om->mask;
2184 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
2186 size_t start_ofs = out->size;
2187 struct nx_action_note *nan;
2188 unsigned int remainder;
2191 nan = ofputil_put_NXAST_NOTE(out);
2192 out->size -= sizeof nan->note;
2194 ofpbuf_put(out, note->data, note->length);
2196 len = out->size - start_ofs;
2197 remainder = len % OFP_ACTION_ALIGN;
2199 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
2201 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
2202 nan->len = htons(out->size - start_ofs);
2206 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
2209 struct nx_action_controller *nac;
2211 nac = ofputil_put_NXAST_CONTROLLER(out);
2212 nac->max_len = htons(oc->max_len);
2213 nac->controller_id = htons(oc->controller_id);
2214 nac->reason = oc->reason;
2218 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
2221 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
2222 ofputil_put_NXAST_DEC_TTL(out);
2224 struct nx_action_cnt_ids *nac_ids =
2225 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
2226 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
2230 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2231 nac_ids->n_controllers = htons(oc_ids->n_controllers);
2233 ids = ofpbuf_put_zeros(out, ids_len);
2234 for (i = 0; i < oc_ids->n_controllers; i++) {
2235 ids[i] = htons(oc_ids->cnt_ids[i]);
2241 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
2244 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
2245 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
2246 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
2250 ofpact_sample_to_nxast(const struct ofpact_sample *os,
2253 struct nx_action_sample *nas;
2255 nas = ofputil_put_NXAST_SAMPLE(out);
2256 nas->probability = htons(os->probability);
2257 nas->collector_set_id = htonl(os->collector_set_id);
2258 nas->obs_domain_id = htonl(os->obs_domain_id);
2259 nas->obs_point_id = htonl(os->obs_point_id);
2263 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
2266 case OFPACT_CONTROLLER:
2267 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
2270 case OFPACT_OUTPUT_REG:
2271 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
2275 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
2278 case OFPACT_REG_MOVE:
2279 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
2282 case OFPACT_REG_LOAD:
2283 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
2286 case OFPACT_STACK_PUSH:
2287 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
2290 case OFPACT_STACK_POP:
2291 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
2294 case OFPACT_DEC_TTL:
2295 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
2298 case OFPACT_SET_MPLS_TTL:
2299 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
2300 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2303 case OFPACT_DEC_MPLS_TTL:
2304 ofputil_put_NXAST_DEC_MPLS_TTL(out);
2307 case OFPACT_SET_TUNNEL:
2308 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
2311 case OFPACT_WRITE_METADATA:
2312 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
2315 case OFPACT_SET_QUEUE:
2316 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
2317 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2320 case OFPACT_POP_QUEUE:
2321 ofputil_put_NXAST_POP_QUEUE(out);
2324 case OFPACT_FIN_TIMEOUT:
2325 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
2328 case OFPACT_RESUBMIT:
2329 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
2333 learn_to_nxast(ofpact_get_LEARN(a), out);
2336 case OFPACT_MULTIPATH:
2337 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
2341 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
2345 ofputil_put_NXAST_EXIT(out);
2348 case OFPACT_PUSH_MPLS:
2349 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
2350 ofpact_get_PUSH_MPLS(a)->ethertype;
2353 case OFPACT_POP_MPLS:
2354 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2355 ofpact_get_POP_MPLS(a)->ethertype;
2359 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2364 case OFPACT_ENQUEUE:
2365 case OFPACT_SET_VLAN_VID:
2366 case OFPACT_SET_VLAN_PCP:
2367 case OFPACT_STRIP_VLAN:
2368 case OFPACT_PUSH_VLAN:
2369 case OFPACT_SET_ETH_SRC:
2370 case OFPACT_SET_ETH_DST:
2371 case OFPACT_SET_IPV4_SRC:
2372 case OFPACT_SET_IPV4_DST:
2373 case OFPACT_SET_IP_DSCP:
2374 case OFPACT_SET_IP_ECN:
2375 case OFPACT_SET_IP_TTL:
2376 case OFPACT_SET_L4_SRC_PORT:
2377 case OFPACT_SET_L4_DST_PORT:
2378 case OFPACT_WRITE_ACTIONS:
2379 case OFPACT_CLEAR_ACTIONS:
2380 case OFPACT_GOTO_TABLE:
2382 case OFPACT_SET_FIELD:
2387 /* Converting ofpacts to OpenFlow 1.0. */
2390 ofpact_output_to_openflow10(const struct ofpact_output *output,
2393 struct ofp10_action_output *oao;
2395 oao = ofputil_put_OFPAT10_OUTPUT(out);
2396 oao->port = htons(ofp_to_u16(output->port));
2397 oao->max_len = htons(output->max_len);
2401 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2404 struct ofp10_action_enqueue *oae;
2406 oae = ofputil_put_OFPAT10_ENQUEUE(out);
2407 oae->port = htons(ofp_to_u16(enqueue->port));
2408 oae->queue_id = htonl(enqueue->queue);
2412 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2416 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2419 case OFPACT_ENQUEUE:
2420 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2423 case OFPACT_SET_VLAN_VID:
2424 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2425 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2428 case OFPACT_SET_VLAN_PCP:
2429 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2430 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2433 case OFPACT_STRIP_VLAN:
2434 ofputil_put_OFPAT10_STRIP_VLAN(out);
2437 case OFPACT_SET_ETH_SRC:
2438 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2439 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2442 case OFPACT_SET_ETH_DST:
2443 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2444 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2447 case OFPACT_SET_IPV4_SRC:
2448 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2449 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2452 case OFPACT_SET_IPV4_DST:
2453 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2454 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2457 case OFPACT_SET_IP_DSCP:
2458 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2459 = ofpact_get_SET_IP_DSCP(a)->dscp;
2462 case OFPACT_SET_L4_SRC_PORT:
2463 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2464 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2467 case OFPACT_SET_L4_DST_PORT:
2468 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2469 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2472 case OFPACT_PUSH_VLAN:
2473 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
2474 * follow this action. */
2477 case OFPACT_CLEAR_ACTIONS:
2478 case OFPACT_WRITE_ACTIONS:
2479 case OFPACT_GOTO_TABLE:
2487 case OFPACT_SET_FIELD:
2488 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2491 case OFPACT_CONTROLLER:
2492 case OFPACT_OUTPUT_REG:
2494 case OFPACT_REG_MOVE:
2495 case OFPACT_REG_LOAD:
2496 case OFPACT_STACK_PUSH:
2497 case OFPACT_STACK_POP:
2498 case OFPACT_DEC_TTL:
2499 case OFPACT_SET_IP_ECN:
2500 case OFPACT_SET_IP_TTL:
2501 case OFPACT_SET_MPLS_TTL:
2502 case OFPACT_DEC_MPLS_TTL:
2503 case OFPACT_SET_TUNNEL:
2504 case OFPACT_WRITE_METADATA:
2505 case OFPACT_SET_QUEUE:
2506 case OFPACT_POP_QUEUE:
2507 case OFPACT_FIN_TIMEOUT:
2508 case OFPACT_RESUBMIT:
2510 case OFPACT_MULTIPATH:
2513 case OFPACT_PUSH_MPLS:
2514 case OFPACT_POP_MPLS:
2516 ofpact_to_nxast(a, out);
2521 /* Converting ofpacts to OpenFlow 1.1. */
2524 ofpact_output_to_openflow11(const struct ofpact_output *output,
2527 struct ofp11_action_output *oao;
2529 oao = ofputil_put_OFPAT11_OUTPUT(out);
2530 oao->port = ofputil_port_to_ofp11(output->port);
2531 oao->max_len = htons(output->max_len);
2535 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2538 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2539 && (!dec_ttl->ofpact.compat ||
2540 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2541 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2543 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2548 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2552 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2554 case OFPACT_ENQUEUE:
2558 case OFPACT_SET_VLAN_VID:
2559 /* Push a VLAN tag, if one was not seen at action validation time. */
2560 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan
2561 && ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2562 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2563 = htons(ETH_TYPE_VLAN_8021Q);
2565 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2566 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2569 case OFPACT_SET_VLAN_PCP:
2570 /* Push a VLAN tag, if one was not seen at action validation time. */
2571 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan
2572 && ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2573 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2574 = htons(ETH_TYPE_VLAN_8021Q);
2576 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2577 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2580 case OFPACT_STRIP_VLAN:
2581 ofputil_put_OFPAT11_POP_VLAN(out);
2584 case OFPACT_PUSH_VLAN:
2585 /* XXX ETH_TYPE_VLAN_8021AD case */
2586 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2587 htons(ETH_TYPE_VLAN_8021Q);
2590 case OFPACT_SET_QUEUE:
2591 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2592 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2595 case OFPACT_SET_ETH_SRC:
2596 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2597 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2600 case OFPACT_SET_ETH_DST:
2601 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2602 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2605 case OFPACT_SET_IPV4_SRC:
2606 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2607 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2610 case OFPACT_SET_IPV4_DST:
2611 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2612 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2615 case OFPACT_SET_IP_DSCP:
2616 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2617 = ofpact_get_SET_IP_DSCP(a)->dscp;
2620 case OFPACT_SET_IP_ECN:
2621 ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2622 = ofpact_get_SET_IP_ECN(a)->ecn;
2625 case OFPACT_SET_IP_TTL:
2626 ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2627 = ofpact_get_SET_IP_TTL(a)->ttl;
2630 case OFPACT_SET_L4_SRC_PORT:
2631 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2632 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2635 case OFPACT_SET_L4_DST_PORT:
2636 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2637 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2640 case OFPACT_DEC_TTL:
2641 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2644 case OFPACT_SET_MPLS_TTL:
2645 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2646 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2649 case OFPACT_DEC_MPLS_TTL:
2650 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2653 case OFPACT_WRITE_METADATA:
2654 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2657 case OFPACT_PUSH_MPLS:
2658 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2659 ofpact_get_PUSH_MPLS(a)->ethertype;
2662 case OFPACT_POP_MPLS:
2663 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2664 ofpact_get_POP_MPLS(a)->ethertype;
2668 case OFPACT_CLEAR_ACTIONS:
2669 case OFPACT_WRITE_ACTIONS:
2670 case OFPACT_GOTO_TABLE:
2675 ofputil_put_OFPAT11_GROUP(out)->group_id =
2676 htonl(ofpact_get_GROUP(a)->group_id);
2679 case OFPACT_SET_FIELD:
2680 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2683 case OFPACT_CONTROLLER:
2684 case OFPACT_OUTPUT_REG:
2686 case OFPACT_REG_MOVE:
2687 case OFPACT_REG_LOAD:
2688 case OFPACT_STACK_PUSH:
2689 case OFPACT_STACK_POP:
2690 case OFPACT_SET_TUNNEL:
2691 case OFPACT_POP_QUEUE:
2692 case OFPACT_FIN_TIMEOUT:
2693 case OFPACT_RESUBMIT:
2695 case OFPACT_MULTIPATH:
2699 ofpact_to_nxast(a, out);
2704 /* Output deprecated set actions as set_field actions. */
2706 ofpact_to_openflow12(const struct ofpact *a, struct ofpbuf *out)
2708 enum mf_field_id field;
2709 union mf_value value;
2710 struct ofpact_l4_port *l4port;
2714 * Convert actions deprecated in OpenFlow 1.2 to Set Field actions,
2717 switch ((int)a->type) {
2718 case OFPACT_SET_VLAN_VID:
2719 case OFPACT_SET_VLAN_PCP:
2720 case OFPACT_SET_ETH_SRC:
2721 case OFPACT_SET_ETH_DST:
2722 case OFPACT_SET_IPV4_SRC:
2723 case OFPACT_SET_IPV4_DST:
2724 case OFPACT_SET_IP_DSCP:
2725 case OFPACT_SET_IP_ECN:
2726 case OFPACT_SET_L4_SRC_PORT:
2727 case OFPACT_SET_L4_DST_PORT:
2728 case OFPACT_SET_TUNNEL: /* Convert to a set_field, too. */
2730 switch ((int)a->type) {
2732 case OFPACT_SET_VLAN_VID:
2733 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan &&
2734 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2735 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2736 = htons(ETH_TYPE_VLAN_8021Q);
2738 field = MFF_VLAN_VID;
2739 /* Set-Field on OXM_OF_VLAN_VID must have OFPVID_PRESENT set. */
2740 value.be16 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid
2741 | OFPVID12_PRESENT);
2744 case OFPACT_SET_VLAN_PCP:
2745 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan &&
2746 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2747 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2748 = htons(ETH_TYPE_VLAN_8021Q);
2750 field = MFF_VLAN_PCP;
2751 value.u8 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2754 case OFPACT_SET_ETH_SRC:
2755 field = MFF_ETH_SRC;
2756 memcpy(value.mac, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2759 case OFPACT_SET_ETH_DST:
2760 field = MFF_ETH_DST;
2761 memcpy(value.mac, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2764 case OFPACT_SET_IPV4_SRC:
2765 field = MFF_IPV4_SRC;
2766 value.be32 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2769 case OFPACT_SET_IPV4_DST:
2770 field = MFF_IPV4_DST;
2771 value.be32 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2774 case OFPACT_SET_IP_DSCP:
2775 field = MFF_IP_DSCP_SHIFTED; /* OXM_OF_IP_DSCP */
2776 value.u8 = ofpact_get_SET_IP_DSCP(a)->dscp >> 2;
2779 case OFPACT_SET_IP_ECN:
2781 value.u8 = ofpact_get_SET_IP_ECN(a)->ecn;
2784 case OFPACT_SET_L4_SRC_PORT:
2785 /* We keep track of IP protocol while translating actions to be
2786 * able to translate to the proper OXM type.
2787 * If the IP protocol type is unknown, the translation cannot
2788 * be performed and we will send the action using the original
2790 l4port = ofpact_get_SET_L4_SRC_PORT(a);
2791 proto = l4port->flow_ip_proto;
2792 field = proto == IPPROTO_TCP ? MFF_TCP_SRC
2793 : proto == IPPROTO_UDP ? MFF_UDP_SRC
2794 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
2795 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
2796 value.be16 = htons(l4port->port);
2799 case OFPACT_SET_L4_DST_PORT:
2800 l4port = ofpact_get_SET_L4_DST_PORT(a);
2801 proto = l4port->flow_ip_proto;
2802 field = proto == IPPROTO_TCP ? MFF_TCP_DST
2803 : proto == IPPROTO_UDP ? MFF_UDP_DST
2804 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
2805 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
2806 value.be16 = htons(l4port->port);
2809 case OFPACT_SET_TUNNEL:
2811 value.be64 = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2818 /* Put the action out as a set field action, if possible. */
2819 if (field < MFF_N_IDS) {
2820 uint64_t ofpacts_stub[128 / 8];
2821 struct ofpbuf sf_act;
2822 struct ofpact_set_field *sf;
2824 ofpbuf_use_stub(&sf_act, ofpacts_stub, sizeof ofpacts_stub);
2825 sf = ofpact_put_SET_FIELD(&sf_act);
2826 sf->field = mf_from_id(field);
2827 memcpy(&sf->value, &value, sf->field->n_bytes);
2828 set_field_to_openflow(sf, out);
2833 ofpact_to_openflow11(a, out);
2836 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
2837 * actions in 'openflow', appending the actions to any existing data in
2840 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
2841 struct ofpbuf *openflow,
2842 enum ofp_version ofp_version)
2844 const struct ofpact *a;
2845 size_t start_size = openflow->size;
2847 void (*translate)(const struct ofpact *a, struct ofpbuf *out) =
2848 (ofp_version == OFP10_VERSION) ? ofpact_to_openflow10 :
2849 (ofp_version == OFP11_VERSION) ? ofpact_to_openflow11 :
2850 ofpact_to_openflow12;
2852 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2853 translate(a, openflow);
2855 return openflow->size - start_size;
2859 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2861 struct ofp11_instruction_actions *oia;
2863 /* Update the instruction's length (or, if it's empty, delete it). */
2864 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2865 if (openflow->size > ofs + sizeof *oia) {
2866 oia->len = htons(openflow->size - ofs);
2868 openflow->size = ofs;
2873 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
2875 struct ofpbuf *openflow,
2876 enum ofp_version ofp_version)
2878 const struct ofpact *a;
2880 ovs_assert(ofp_version >= OFP11_VERSION);
2882 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2883 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2884 case OVSINST_OFPIT11_CLEAR_ACTIONS:
2885 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2888 case OVSINST_OFPIT11_GOTO_TABLE: {
2889 struct ofp11_instruction_goto_table *oigt;
2890 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2891 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2892 memset(oigt->pad, 0, sizeof oigt->pad);
2896 case OVSINST_OFPIT11_WRITE_METADATA: {
2897 const struct ofpact_metadata *om;
2898 struct ofp11_instruction_write_metadata *oiwm;
2900 om = ofpact_get_WRITE_METADATA(a);
2901 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2902 oiwm->metadata = om->metadata;
2903 oiwm->metadata_mask = om->mask;
2907 case OVSINST_OFPIT13_METER:
2908 if (ofp_version >= OFP13_VERSION) {
2909 const struct ofpact_meter *om;
2910 struct ofp13_instruction_meter *oim;
2912 om = ofpact_get_METER(a);
2913 oim = instruction_put_OFPIT13_METER(openflow);
2914 oim->meter_id = htonl(om->meter_id);
2918 case OVSINST_OFPIT11_APPLY_ACTIONS: {
2919 const size_t ofs = openflow->size;
2920 const size_t ofpacts_len_left =
2921 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2922 const struct ofpact *action;
2923 const struct ofpact *processed = a;
2925 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2926 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
2927 if (ovs_instruction_type_from_ofpact_type(action->type)
2928 != OVSINST_OFPIT11_APPLY_ACTIONS) {
2931 if (ofp_version == OFP11_VERSION) {
2932 ofpact_to_openflow11(action, openflow);
2934 ofpact_to_openflow12(action, openflow);
2938 ofpacts_update_instruction_actions(openflow, ofs);
2943 case OVSINST_OFPIT11_WRITE_ACTIONS: {
2944 const size_t ofs = openflow->size;
2945 const struct ofpact_nest *on;
2947 on = ofpact_get_WRITE_ACTIONS(a);
2948 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2949 ofpacts_put_openflow_actions(on->actions,
2950 ofpact_nest_get_action_len(on),
2951 openflow, ofp_version);
2952 ofpacts_update_instruction_actions(openflow, ofs);
2960 /* Returns true if 'action' outputs to 'port', false otherwise. */
2962 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
2964 switch (ofpact->type) {
2966 return ofpact_get_OUTPUT(ofpact)->port == port;
2967 case OFPACT_ENQUEUE:
2968 return ofpact_get_ENQUEUE(ofpact)->port == port;
2969 case OFPACT_CONTROLLER:
2970 return port == OFPP_CONTROLLER;
2972 case OFPACT_OUTPUT_REG:
2974 case OFPACT_SET_VLAN_VID:
2975 case OFPACT_SET_VLAN_PCP:
2976 case OFPACT_STRIP_VLAN:
2977 case OFPACT_PUSH_VLAN:
2978 case OFPACT_SET_ETH_SRC:
2979 case OFPACT_SET_ETH_DST:
2980 case OFPACT_SET_IPV4_SRC:
2981 case OFPACT_SET_IPV4_DST:
2982 case OFPACT_SET_IP_DSCP:
2983 case OFPACT_SET_IP_ECN:
2984 case OFPACT_SET_IP_TTL:
2985 case OFPACT_SET_L4_SRC_PORT:
2986 case OFPACT_SET_L4_DST_PORT:
2987 case OFPACT_REG_MOVE:
2988 case OFPACT_REG_LOAD:
2989 case OFPACT_SET_FIELD:
2990 case OFPACT_STACK_PUSH:
2991 case OFPACT_STACK_POP:
2992 case OFPACT_DEC_TTL:
2993 case OFPACT_SET_MPLS_TTL:
2994 case OFPACT_DEC_MPLS_TTL:
2995 case OFPACT_SET_TUNNEL:
2996 case OFPACT_WRITE_METADATA:
2997 case OFPACT_SET_QUEUE:
2998 case OFPACT_POP_QUEUE:
2999 case OFPACT_FIN_TIMEOUT:
3000 case OFPACT_RESUBMIT:
3002 case OFPACT_MULTIPATH:
3005 case OFPACT_PUSH_MPLS:
3006 case OFPACT_POP_MPLS:
3008 case OFPACT_CLEAR_ACTIONS:
3009 case OFPACT_WRITE_ACTIONS:
3010 case OFPACT_GOTO_TABLE:
3018 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3019 * to 'port', false otherwise. */
3021 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
3024 const struct ofpact *a;
3026 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3027 if (ofpact_outputs_to_port(a, port)) {
3035 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3036 * to 'group', false otherwise. */
3038 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
3041 const struct ofpact *a;
3043 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3044 if (a->type == OFPACT_GROUP
3045 && ofpact_get_GROUP(a)->group_id == group_id) {
3054 ofpacts_equal(const struct ofpact *a, size_t a_len,
3055 const struct ofpact *b, size_t b_len)
3057 return a_len == b_len && !memcmp(a, b, a_len);
3060 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
3061 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
3063 * This function relies on the order of 'ofpacts' being correct (as checked by
3064 * ofpacts_verify()). */
3066 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
3068 const struct ofpact *a;
3070 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3071 enum ovs_instruction_type inst;
3073 inst = ovs_instruction_type_from_ofpact_type(a->type);
3074 if (a->type == OFPACT_METER) {
3075 return ofpact_get_METER(a)->meter_id;
3076 } else if (inst > OVSINST_OFPIT13_METER) {
3084 /* Formatting ofpacts. */
3087 print_note(const struct ofpact_note *note, struct ds *string)
3091 ds_put_cstr(string, "note:");
3092 for (i = 0; i < note->length; i++) {
3094 ds_put_char(string, '.');
3096 ds_put_format(string, "%02"PRIx8, note->data[i]);
3101 print_dec_ttl(const struct ofpact_cnt_ids *ids,
3106 ds_put_cstr(s, "dec_ttl");
3107 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
3108 ds_put_cstr(s, "(");
3109 for (i = 0; i < ids->n_controllers; i++) {
3111 ds_put_cstr(s, ",");
3113 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
3115 ds_put_cstr(s, ")");
3120 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
3123 ds_put_cstr(s, "fin_timeout(");
3124 if (fin_timeout->fin_idle_timeout) {
3125 ds_put_format(s, "idle_timeout=%"PRIu16",",
3126 fin_timeout->fin_idle_timeout);
3128 if (fin_timeout->fin_hard_timeout) {
3129 ds_put_format(s, "hard_timeout=%"PRIu16",",
3130 fin_timeout->fin_hard_timeout);
3133 ds_put_char(s, ')');
3137 ofpact_format(const struct ofpact *a, struct ds *s)
3139 const struct ofpact_enqueue *enqueue;
3140 const struct ofpact_resubmit *resubmit;
3141 const struct ofpact_controller *controller;
3142 const struct ofpact_metadata *metadata;
3143 const struct ofpact_tunnel *tunnel;
3144 const struct ofpact_sample *sample;
3145 const struct ofpact_set_field *set_field;
3146 const struct mf_field *mf;
3151 port = ofpact_get_OUTPUT(a)->port;
3152 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
3153 ds_put_format(s, "output:%"PRIu16, port);
3155 ofputil_format_port(port, s);
3156 if (port == OFPP_CONTROLLER) {
3157 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
3162 case OFPACT_CONTROLLER:
3163 controller = ofpact_get_CONTROLLER(a);
3164 if (controller->reason == OFPR_ACTION &&
3165 controller->controller_id == 0) {
3166 ds_put_format(s, "CONTROLLER:%"PRIu16,
3167 ofpact_get_CONTROLLER(a)->max_len);
3169 enum ofp_packet_in_reason reason = controller->reason;
3171 ds_put_cstr(s, "controller(");
3172 if (reason != OFPR_ACTION) {
3173 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3175 ds_put_format(s, "reason=%s,",
3176 ofputil_packet_in_reason_to_string(
3177 reason, reasonbuf, sizeof reasonbuf));
3179 if (controller->max_len != UINT16_MAX) {
3180 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
3182 if (controller->controller_id != 0) {
3183 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
3186 ds_put_char(s, ')');
3190 case OFPACT_ENQUEUE:
3191 enqueue = ofpact_get_ENQUEUE(a);
3192 ds_put_format(s, "enqueue:");
3193 ofputil_format_port(enqueue->port, s);
3194 ds_put_format(s, "q%"PRIu32, enqueue->queue);
3197 case OFPACT_OUTPUT_REG:
3198 ds_put_cstr(s, "output:");
3199 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
3203 bundle_format(ofpact_get_BUNDLE(a), s);
3206 case OFPACT_SET_VLAN_VID:
3207 ds_put_format(s, "%s:%"PRIu16,
3208 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_VID
3211 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
3214 case OFPACT_SET_VLAN_PCP:
3215 ds_put_format(s, "%s:%"PRIu8,
3216 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_PCP
3219 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
3222 case OFPACT_STRIP_VLAN:
3223 ds_put_cstr(s, a->compat == OFPUTIL_OFPAT11_POP_VLAN
3224 ? "pop_vlan" : "strip_vlan");
3227 case OFPACT_PUSH_VLAN:
3228 /* XXX 802.1AD case*/
3229 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
3232 case OFPACT_SET_ETH_SRC:
3233 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
3234 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
3237 case OFPACT_SET_ETH_DST:
3238 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
3239 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
3242 case OFPACT_SET_IPV4_SRC:
3243 ds_put_format(s, "mod_nw_src:"IP_FMT,
3244 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
3247 case OFPACT_SET_IPV4_DST:
3248 ds_put_format(s, "mod_nw_dst:"IP_FMT,
3249 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
3252 case OFPACT_SET_IP_DSCP:
3253 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
3256 case OFPACT_SET_IP_ECN:
3257 ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
3260 case OFPACT_SET_IP_TTL:
3261 ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
3264 case OFPACT_SET_L4_SRC_PORT:
3265 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
3268 case OFPACT_SET_L4_DST_PORT:
3269 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
3272 case OFPACT_REG_MOVE:
3273 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
3276 case OFPACT_REG_LOAD:
3277 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
3280 case OFPACT_SET_FIELD:
3281 set_field = ofpact_get_SET_FIELD(a);
3282 mf = set_field->field;
3283 ds_put_format(s, "set_field:");
3284 mf_format(mf, &set_field->value, NULL, s);
3285 ds_put_format(s, "->%s", mf->name);
3288 case OFPACT_STACK_PUSH:
3289 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
3292 case OFPACT_STACK_POP:
3293 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
3296 case OFPACT_DEC_TTL:
3297 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
3300 case OFPACT_SET_MPLS_TTL:
3301 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3302 ofpact_get_SET_MPLS_TTL(a)->ttl);
3305 case OFPACT_DEC_MPLS_TTL:
3306 ds_put_cstr(s, "dec_mpls_ttl");
3309 case OFPACT_SET_TUNNEL:
3310 tunnel = ofpact_get_SET_TUNNEL(a);
3311 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3312 (tunnel->tun_id > UINT32_MAX
3313 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
3317 case OFPACT_SET_QUEUE:
3318 ds_put_format(s, "set_queue:%"PRIu32,
3319 ofpact_get_SET_QUEUE(a)->queue_id);
3322 case OFPACT_POP_QUEUE:
3323 ds_put_cstr(s, "pop_queue");
3326 case OFPACT_FIN_TIMEOUT:
3327 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
3330 case OFPACT_RESUBMIT:
3331 resubmit = ofpact_get_RESUBMIT(a);
3332 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
3333 ds_put_cstr(s, "resubmit:");
3334 ofputil_format_port(resubmit->in_port, s);
3336 ds_put_format(s, "resubmit(");
3337 if (resubmit->in_port != OFPP_IN_PORT) {
3338 ofputil_format_port(resubmit->in_port, s);
3340 ds_put_char(s, ',');
3341 if (resubmit->table_id != 255) {
3342 ds_put_format(s, "%"PRIu8, resubmit->table_id);
3344 ds_put_char(s, ')');
3349 learn_format(ofpact_get_LEARN(a), s);
3352 case OFPACT_MULTIPATH:
3353 multipath_format(ofpact_get_MULTIPATH(a), s);
3357 print_note(ofpact_get_NOTE(a), s);
3360 case OFPACT_PUSH_MPLS:
3361 ds_put_format(s, "push_mpls:0x%04"PRIx16,
3362 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
3365 case OFPACT_POP_MPLS:
3366 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
3367 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
3371 ds_put_cstr(s, "exit");
3375 sample = ofpact_get_SAMPLE(a);
3377 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
3378 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
3379 sample->probability, sample->collector_set_id,
3380 sample->obs_domain_id, sample->obs_point_id);
3383 case OFPACT_WRITE_ACTIONS: {
3384 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3385 ds_put_format(s, "%s(",
3386 ovs_instruction_name_from_type(
3387 OVSINST_OFPIT11_WRITE_ACTIONS));
3388 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
3389 ds_put_char(s, ')');
3393 case OFPACT_CLEAR_ACTIONS:
3394 ds_put_format(s, "%s",
3395 ovs_instruction_name_from_type(
3396 OVSINST_OFPIT11_CLEAR_ACTIONS));
3399 case OFPACT_WRITE_METADATA:
3400 metadata = ofpact_get_WRITE_METADATA(a);
3401 ds_put_format(s, "%s:%#"PRIx64,
3402 ovs_instruction_name_from_type(
3403 OVSINST_OFPIT11_WRITE_METADATA),
3404 ntohll(metadata->metadata));
3405 if (metadata->mask != OVS_BE64_MAX) {
3406 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
3410 case OFPACT_GOTO_TABLE:
3411 ds_put_format(s, "%s:%"PRIu8,
3412 ovs_instruction_name_from_type(
3413 OVSINST_OFPIT11_GOTO_TABLE),
3414 ofpact_get_GOTO_TABLE(a)->table_id);
3418 ds_put_format(s, "%s:%"PRIu32,
3419 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
3420 ofpact_get_METER(a)->meter_id);
3424 ds_put_format(s, "group:%"PRIu32,
3425 ofpact_get_GROUP(a)->group_id);
3430 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
3431 * 'ofpacts' to 'string'. */
3433 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
3437 ds_put_cstr(string, "drop");
3439 const struct ofpact *a;
3441 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3443 ds_put_cstr(string, ",");
3446 /* XXX write-actions */
3447 ofpact_format(a, string);
3452 /* Internal use by helpers. */
3455 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
3457 struct ofpact *ofpact;
3459 ofpact_pad(ofpacts);
3460 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
3461 ofpact_init(ofpact, type, len);
3466 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
3468 memset(ofpact, 0, len);
3469 ofpact->type = type;
3470 ofpact->compat = OFPUTIL_ACTION_INVALID;
3474 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
3475 * starting at 'ofpact'.
3477 * This is the correct way to update a variable-length ofpact's length after
3478 * adding the variable-length part of the payload. (See the large comment
3479 * near the end of ofp-actions.h for more information.) */
3481 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
3483 ovs_assert(ofpact == ofpacts->l2);
3484 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
3487 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
3488 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
3489 * client must call this itself after adding the final ofpact to an array of
3492 * (The consequences of failing to call this function are probably not dire.
3493 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
3494 * not dereference it. That's undefined behavior, technically, but it will not
3495 * cause a real problem on common systems. Still, it seems better to call
3498 ofpact_pad(struct ofpbuf *ofpacts)
3500 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
3502 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);