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_label ofp11_mpls_label;
57 struct ofp11_action_mpls_tc ofp11_mpls_tc;
58 struct ofp11_action_mpls_ttl ofp11_mpls_ttl;
59 struct ofp11_action_group group;
60 struct ofp12_action_set_field set_field;
61 struct nx_action_header nxa_header;
62 struct nx_action_resubmit resubmit;
63 struct nx_action_set_tunnel set_tunnel;
64 struct nx_action_set_tunnel64 set_tunnel64;
65 struct nx_action_write_metadata write_metadata;
66 struct nx_action_set_queue set_queue;
67 struct nx_action_reg_move reg_move;
68 struct nx_action_reg_load reg_load;
69 struct nx_action_stack stack;
70 struct nx_action_note note;
71 struct nx_action_multipath multipath;
72 struct nx_action_bundle bundle;
73 struct nx_action_output_reg output_reg;
74 struct nx_action_cnt_ids cnt_ids;
75 struct nx_action_fin_timeout fin_timeout;
76 struct nx_action_controller controller;
77 struct nx_action_push_mpls push_mpls;
78 struct nx_action_mpls_ttl mpls_ttl;
79 struct nx_action_pop_mpls pop_mpls;
80 struct nx_action_sample sample;
81 struct nx_action_learn learn;
82 struct nx_action_mpls_label mpls_label;
83 struct nx_action_mpls_tc mpls_tc;
87 output_from_openflow10(const struct ofp10_action_output *oao,
90 struct ofpact_output *output;
92 output = ofpact_put_OUTPUT(out);
93 output->port = u16_to_ofp(ntohs(oao->port));
94 output->max_len = ntohs(oao->max_len);
96 return ofpact_check_output_port(output->port, OFPP_MAX);
100 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
103 struct ofpact_enqueue *enqueue;
105 enqueue = ofpact_put_ENQUEUE(out);
106 enqueue->port = u16_to_ofp(ntohs(oae->port));
107 enqueue->queue = ntohl(oae->queue_id);
108 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
109 && enqueue->port != OFPP_IN_PORT
110 && enqueue->port != OFPP_LOCAL) {
111 return OFPERR_OFPBAC_BAD_OUT_PORT;
117 resubmit_from_openflow(const struct nx_action_resubmit *nar,
120 struct ofpact_resubmit *resubmit;
122 resubmit = ofpact_put_RESUBMIT(out);
123 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
124 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
125 resubmit->table_id = 0xff;
129 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
132 struct ofpact_resubmit *resubmit;
134 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
135 return OFPERR_OFPBAC_BAD_ARGUMENT;
138 resubmit = ofpact_put_RESUBMIT(out);
139 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
140 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
141 resubmit->table_id = nar->table;
146 output_reg_from_openflow(const struct nx_action_output_reg *naor,
149 struct ofpact_output_reg *output_reg;
151 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
152 return OFPERR_OFPBAC_BAD_ARGUMENT;
155 output_reg = ofpact_put_OUTPUT_REG(out);
156 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
157 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
158 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
159 output_reg->max_len = ntohs(naor->max_len);
161 return mf_check_src(&output_reg->src, NULL);
165 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
168 struct ofpact_fin_timeout *oft;
170 oft = ofpact_put_FIN_TIMEOUT(out);
171 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
172 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
176 controller_from_openflow(const struct nx_action_controller *nac,
179 struct ofpact_controller *oc;
181 oc = ofpact_put_CONTROLLER(out);
182 oc->max_len = ntohs(nac->max_len);
183 oc->controller_id = ntohs(nac->controller_id);
184 oc->reason = nac->reason;
188 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
191 struct ofpact_metadata *om;
193 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
194 return OFPERR_NXBRC_MUST_BE_ZERO;
197 om = ofpact_put_WRITE_METADATA(out);
198 om->metadata = nawm->metadata;
199 om->mask = nawm->mask;
205 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
207 struct ofpact_note *note;
210 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
211 note = ofpact_put(out, OFPACT_NOTE,
212 offsetof(struct ofpact_note, data) + length);
213 note->length = length;
214 memcpy(note->data, nan->note, length);
218 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
221 struct ofpact_cnt_ids *ids;
222 enum ofperr error = 0;
224 ids = ofpact_put_DEC_TTL(out);
225 ids->ofpact.compat = compat;
226 ids->n_controllers = 1;
227 ofpbuf_put(out, &id, sizeof id);
229 ofpact_update_len(out, &ids->ofpact);
234 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
237 struct ofpact_cnt_ids *ids;
241 ids = ofpact_put_DEC_TTL(out);
242 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
243 ids->n_controllers = ntohs(nac_ids->n_controllers);
244 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
246 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
247 return OFPERR_NXBRC_MUST_BE_ZERO;
250 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
251 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
252 "allocated for controller ids. %zu bytes are required for "
253 "%"PRIu16" controllers.", ids_size,
254 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
255 return OFPERR_OFPBAC_BAD_LEN;
258 for (i = 0; i < ids->n_controllers; i++) {
259 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
260 ofpbuf_put(out, &id, sizeof id);
264 ofpact_update_len(out, &ids->ofpact);
270 sample_from_openflow(const struct nx_action_sample *nas,
273 struct ofpact_sample *sample;
275 sample = ofpact_put_SAMPLE(out);
276 sample->probability = ntohs(nas->probability);
277 sample->collector_set_id = ntohl(nas->collector_set_id);
278 sample->obs_domain_id = ntohl(nas->obs_domain_id);
279 sample->obs_point_id = ntohl(nas->obs_point_id);
281 if (sample->probability == 0) {
282 return OFPERR_OFPBAC_BAD_ARGUMENT;
289 push_mpls_from_openflow(ovs_be16 ethertype, enum ofpact_mpls_position position,
292 struct ofpact_push_mpls *oam;
294 if (!eth_type_mpls(ethertype)) {
295 return OFPERR_OFPBAC_BAD_ARGUMENT;
297 oam = ofpact_put_PUSH_MPLS(out);
298 oam->ethertype = ethertype;
299 oam->position = position;
305 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
307 const struct nx_action_header *nah = &a->nxa_header;
308 uint16_t len = ntohs(a->header.len);
310 if (len < sizeof(struct nx_action_header)) {
311 return OFPERR_OFPBAC_BAD_LEN;
312 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
313 return OFPERR_OFPBAC_BAD_VENDOR;
316 switch (nah->subtype) {
317 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
318 case CONSTANT_HTONS(ENUM): \
320 ? len >= sizeof(struct STRUCT) \
321 : len == sizeof(struct STRUCT)) { \
322 *code = OFPUTIL_##ENUM; \
325 return OFPERR_OFPBAC_BAD_LEN; \
328 #include "ofp-util.def"
330 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
331 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
333 return OFPERR_OFPBAC_BAD_TYPE;
337 /* Parses 'a' to determine its type. On success stores the correct type into
338 * '*code' and returns 0. On failure returns an OFPERR_* error code and
339 * '*code' is indeterminate.
341 * The caller must have already verified that 'a''s length is potentially
342 * correct (that is, a->header.len is nonzero and a multiple of
343 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
345 * This function verifies that 'a''s length is correct for the type of action
346 * that it represents. */
348 decode_openflow10_action(const union ofp_action *a,
349 enum ofputil_action_code *code)
352 case CONSTANT_HTONS(OFPAT10_VENDOR):
353 return decode_nxast_action(a, code);
355 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
356 case CONSTANT_HTONS(ENUM): \
357 if (a->header.len == htons(sizeof(struct STRUCT))) { \
358 *code = OFPUTIL_##ENUM; \
361 return OFPERR_OFPBAC_BAD_LEN; \
364 #include "ofp-util.def"
367 return OFPERR_OFPBAC_BAD_TYPE;
372 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
375 struct ofpact_tunnel *tunnel;
376 enum ofperr error = 0;
379 case OFPUTIL_ACTION_INVALID:
380 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
381 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
382 #include "ofp-util.def"
385 case OFPUTIL_NXAST_RESUBMIT:
386 resubmit_from_openflow(&a->resubmit, out);
389 case OFPUTIL_NXAST_SET_TUNNEL:
390 tunnel = ofpact_put_SET_TUNNEL(out);
391 tunnel->ofpact.compat = code;
392 tunnel->tun_id = ntohl(a->set_tunnel.tun_id);
395 case OFPUTIL_NXAST_WRITE_METADATA:
396 error = metadata_from_nxast(&a->write_metadata, out);
399 case OFPUTIL_NXAST_SET_QUEUE:
400 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(a->set_queue.queue_id);
403 case OFPUTIL_NXAST_POP_QUEUE:
404 ofpact_put_POP_QUEUE(out);
407 case OFPUTIL_NXAST_REG_MOVE:
408 error = nxm_reg_move_from_openflow(&a->reg_move, out);
411 case OFPUTIL_NXAST_REG_LOAD:
412 error = nxm_reg_load_from_openflow(&a->reg_load, out);
415 case OFPUTIL_NXAST_STACK_PUSH:
416 error = nxm_stack_push_from_openflow(&a->stack, out);
419 case OFPUTIL_NXAST_STACK_POP:
420 error = nxm_stack_pop_from_openflow(&a->stack, out);
423 case OFPUTIL_NXAST_NOTE:
424 note_from_openflow(&a->note, out);
427 case OFPUTIL_NXAST_SET_TUNNEL64:
428 tunnel = ofpact_put_SET_TUNNEL(out);
429 tunnel->ofpact.compat = code;
430 tunnel->tun_id = ntohll(a->set_tunnel64.tun_id);
433 case OFPUTIL_NXAST_MULTIPATH:
434 error = multipath_from_openflow(&a->multipath,
435 ofpact_put_MULTIPATH(out));
438 case OFPUTIL_NXAST_BUNDLE:
439 case OFPUTIL_NXAST_BUNDLE_LOAD:
440 error = bundle_from_openflow(&a->bundle, out);
443 case OFPUTIL_NXAST_OUTPUT_REG:
444 error = output_reg_from_openflow(&a->output_reg, out);
447 case OFPUTIL_NXAST_RESUBMIT_TABLE:
448 error = resubmit_table_from_openflow(&a->resubmit, out);
451 case OFPUTIL_NXAST_LEARN:
452 error = learn_from_openflow(&a->learn, out);
455 case OFPUTIL_NXAST_EXIT:
456 ofpact_put_EXIT(out);
459 case OFPUTIL_NXAST_DEC_TTL:
460 error = dec_ttl_from_openflow(out, code);
463 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
464 error = dec_ttl_cnt_ids_from_openflow(&a->cnt_ids, out);
467 case OFPUTIL_NXAST_FIN_TIMEOUT:
468 fin_timeout_from_openflow(&a->fin_timeout, out);
471 case OFPUTIL_NXAST_CONTROLLER:
472 controller_from_openflow(&a->controller, out);
475 case OFPUTIL_NXAST_PUSH_MPLS:
476 error = push_mpls_from_openflow(a->push_mpls.ethertype,
477 OFPACT_MPLS_AFTER_VLAN, out);
480 case OFPUTIL_NXAST_SET_MPLS_LABEL:
481 ofpact_put_SET_MPLS_LABEL(out)->label = a->mpls_label.label;
484 case OFPUTIL_NXAST_SET_MPLS_TC:
485 ofpact_put_SET_MPLS_TC(out)->tc = a->mpls_tc.tc;
488 case OFPUTIL_NXAST_SET_MPLS_TTL:
489 ofpact_put_SET_MPLS_TTL(out)->ttl = a->mpls_ttl.ttl;
492 case OFPUTIL_NXAST_DEC_MPLS_TTL:
493 ofpact_put_DEC_MPLS_TTL(out);
496 case OFPUTIL_NXAST_POP_MPLS:
497 if (eth_type_mpls(a->pop_mpls.ethertype)) {
498 return OFPERR_OFPBAC_BAD_ARGUMENT;
500 ofpact_put_POP_MPLS(out)->ethertype = a->pop_mpls.ethertype;
503 case OFPUTIL_NXAST_SAMPLE:
504 error = sample_from_openflow(&a->sample, out);
512 ofpact_from_openflow10(const union ofp_action *a,
513 enum ofp_version version OVS_UNUSED,
516 enum ofputil_action_code code;
518 struct ofpact_vlan_vid *vlan_vid;
519 struct ofpact_vlan_pcp *vlan_pcp;
521 error = decode_openflow10_action(a, &code);
527 case OFPUTIL_ACTION_INVALID:
528 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
529 #include "ofp-util.def"
532 case OFPUTIL_OFPAT10_OUTPUT:
533 return output_from_openflow10(&a->output10, out);
535 case OFPUTIL_OFPAT10_SET_VLAN_VID:
536 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
537 return OFPERR_OFPBAC_BAD_ARGUMENT;
539 vlan_vid = ofpact_put_SET_VLAN_VID(out);
540 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
541 vlan_vid->push_vlan_if_needed = true;
542 vlan_vid->ofpact.compat = code;
545 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
546 if (a->vlan_pcp.vlan_pcp & ~7) {
547 return OFPERR_OFPBAC_BAD_ARGUMENT;
549 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
550 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
551 vlan_pcp->push_vlan_if_needed = true;
552 vlan_pcp->ofpact.compat = code;
555 case OFPUTIL_OFPAT10_STRIP_VLAN:
556 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
559 case OFPUTIL_OFPAT10_SET_DL_SRC:
560 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
564 case OFPUTIL_OFPAT10_SET_DL_DST:
565 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
569 case OFPUTIL_OFPAT10_SET_NW_SRC:
570 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
573 case OFPUTIL_OFPAT10_SET_NW_DST:
574 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
577 case OFPUTIL_OFPAT10_SET_NW_TOS:
578 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
579 return OFPERR_OFPBAC_BAD_ARGUMENT;
581 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
584 case OFPUTIL_OFPAT10_SET_TP_SRC:
585 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
588 case OFPUTIL_OFPAT10_SET_TP_DST:
589 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
593 case OFPUTIL_OFPAT10_ENQUEUE:
594 error = enqueue_from_openflow10(&a->enqueue, out);
597 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
598 #include "ofp-util.def"
599 return ofpact_from_nxast(a, code, out);
605 static enum ofperr ofpact_from_openflow11(const union ofp_action *,
609 static inline union ofp_action *
610 action_next(const union ofp_action *a)
612 return ((union ofp_action *) (void *)
613 ((uint8_t *) a + ntohs(a->header.len)));
617 action_is_valid(const union ofp_action *a, size_t max_actions)
619 uint16_t len = ntohs(a->header.len);
620 return (!(len % OFP_ACTION_ALIGN)
621 && len >= OFP_ACTION_ALIGN
622 && len / OFP_ACTION_ALIGN <= max_actions);
625 /* This macro is careful to check for actions with bad lengths. */
626 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, MAX_ACTIONS) \
627 for ((ITER) = (ACTIONS), (LEFT) = (MAX_ACTIONS); \
628 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
629 ((LEFT) -= ntohs((ITER)->header.len) / OFP_ACTION_ALIGN, \
630 (ITER) = action_next(ITER)))
633 log_bad_action(const union ofp_action *actions, size_t max_actions,
634 const union ofp_action *bad_action, enum ofperr error)
636 if (!VLOG_DROP_WARN(&rl)) {
640 ds_put_hex_dump(&s, actions, max_actions * OFP_ACTION_ALIGN, 0, false);
641 VLOG_WARN("bad action at offset %#tx (%s):\n%s",
642 (char *)bad_action - (char *)actions,
643 ofperr_get_name(error), ds_cstr(&s));
649 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
650 enum ofp_version version, struct ofpbuf *out)
652 const union ofp_action *a;
655 enum ofperr (*ofpact_from_openflow)(const union ofp_action *a,
657 struct ofpbuf *out) =
658 (version == OFP10_VERSION) ?
659 ofpact_from_openflow10 : ofpact_from_openflow11;
661 ACTION_FOR_EACH (a, left, in, n_in) {
662 enum ofperr error = ofpact_from_openflow(a, version, out);
664 log_bad_action(in, n_in, a, error);
669 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
670 log_bad_action(in, n_in, a, error);
678 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
679 * front of 'openflow' into ofpacts. On success, replaces any existing content
680 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
681 * Returns 0 if successful, otherwise an OpenFlow error.
683 * Actions are processed according to their OpenFlow version which
684 * is provided in the 'version' parameter.
686 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
687 * instructions, so you should call ofpacts_pull_openflow_instructions()
688 * instead of this function.
690 * The parsed actions are valid generically, but they may not be valid in a
691 * specific context. For example, port numbers up to OFPP_MAX are valid
692 * generically, but specific datapaths may only support port numbers in a
693 * smaller range. Use ofpacts_check() to additional check whether actions are
694 * valid in a specific context. */
696 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
697 unsigned int actions_len,
698 enum ofp_version version,
699 struct ofpbuf *ofpacts) {
700 const union ofp_action *actions;
703 ofpbuf_clear(ofpacts);
705 if (actions_len % OFP_ACTION_ALIGN != 0) {
706 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
707 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
708 return OFPERR_OFPBRC_BAD_LEN;
711 actions = ofpbuf_try_pull(openflow, actions_len);
712 if (actions == NULL) {
713 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
714 "remaining message length (%zu)",
715 actions_len, openflow->size);
716 return OFPERR_OFPBRC_BAD_LEN;
719 error = ofpacts_from_openflow(actions, actions_len / OFP_ACTION_ALIGN,
722 ofpbuf_clear(ofpacts);
726 error = ofpacts_verify(ofpacts->data, ofpacts->size);
728 ofpbuf_clear(ofpacts);
734 /* OpenFlow 1.1 actions. */
736 /* Parses 'a' to determine its type. On success stores the correct type into
737 * '*code' and returns 0. On failure returns an OFPERR_* error code and
738 * '*code' is indeterminate.
740 * The caller must have already verified that 'a''s length is potentially
741 * correct (that is, a->header.len is nonzero and a multiple of
742 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
744 * This function verifies that 'a''s length is correct for the type of action
745 * that it represents. */
747 decode_openflow11_action(const union ofp_action *a,
748 enum ofputil_action_code *code)
753 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
754 return decode_nxast_action(a, code);
756 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
757 case CONSTANT_HTONS(ENUM): \
758 len = ntohs(a->header.len); \
760 ? len >= sizeof(struct STRUCT) \
761 : len == sizeof(struct STRUCT)) { \
762 *code = OFPUTIL_##ENUM; \
765 return OFPERR_OFPBAC_BAD_LEN; \
768 #include "ofp-util.def"
771 return OFPERR_OFPBAC_BAD_TYPE;
776 set_field_from_openflow(const struct ofp12_action_set_field *oasf,
777 struct ofpbuf *ofpacts)
779 uint16_t oasf_len = ntohs(oasf->len);
780 uint32_t oxm_header = ntohl(oasf->dst);
781 uint8_t oxm_length = NXM_LENGTH(oxm_header);
782 struct ofpact_set_field *sf;
783 const struct mf_field *mf;
785 /* ofp12_action_set_field is padded to 64 bits by zero */
786 if (oasf_len != ROUND_UP(sizeof *oasf + oxm_length, 8)) {
787 return OFPERR_OFPBAC_BAD_SET_LEN;
789 if (!is_all_zeros((const uint8_t *)oasf + sizeof *oasf + oxm_length,
790 oasf_len - oxm_length - sizeof *oasf)) {
791 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
794 if (NXM_HASMASK(oxm_header)) {
795 return OFPERR_OFPBAC_BAD_SET_TYPE;
797 mf = mf_from_nxm_header(oxm_header);
799 return OFPERR_OFPBAC_BAD_SET_TYPE;
801 ovs_assert(mf->n_bytes == oxm_length);
802 /* oxm_length is now validated to be compatible with mf_value. */
804 VLOG_WARN_RL(&rl, "destination field %s is not writable", mf->name);
805 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
807 sf = ofpact_put_SET_FIELD(ofpacts);
809 memcpy(&sf->value, oasf + 1, mf->n_bytes);
811 /* The value must be valid for match and must have the OFPVID_PRESENT bit
812 * on for OXM_OF_VLAN_VID. */
813 if (!mf_is_value_valid(mf, &sf->value)
814 || (mf->id == MFF_VLAN_VID
815 && !(sf->value.be16 & htons(OFPVID12_PRESENT)))) {
816 struct ds ds = DS_EMPTY_INITIALIZER;
817 mf_format(mf, &sf->value, NULL, &ds);
818 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
819 mf->name, ds_cstr(&ds));
822 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
828 set_field_to_openflow12(const struct ofpact_set_field *sf,
829 struct ofpbuf *openflow)
831 uint16_t padded_value_len = ROUND_UP(sf->field->n_bytes, 8);
832 struct ofp12_action_set_field *oasf;
835 oasf = ofputil_put_OFPAT12_SET_FIELD(openflow);
836 oasf->dst = htonl(sf->field->oxm_header);
837 oasf->len = htons(sizeof *oasf + padded_value_len);
839 value = ofpbuf_put_zeros(openflow, padded_value_len);
840 memcpy(value, &sf->value, sf->field->n_bytes);
843 /* Convert 'sf' to one or two REG_LOADs. */
845 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
847 const struct mf_field *mf = sf->field;
848 struct nx_action_reg_load *narl;
850 if (mf->n_bits > 64) {
851 ovs_assert(mf->n_bytes == 16); /* IPv6 addr. */
852 /* Split into 64bit chunks */
853 /* Lower bits first. */
854 narl = ofputil_put_NXAST_REG_LOAD(openflow);
855 narl->ofs_nbits = nxm_encode_ofs_nbits(0, 64);
856 narl->dst = htonl(mf->nxm_header);
857 memcpy(&narl->value, &sf->value.ipv6.s6_addr[8], sizeof narl->value);
858 /* Higher bits next. */
859 narl = ofputil_put_NXAST_REG_LOAD(openflow);
860 narl->ofs_nbits = nxm_encode_ofs_nbits(64, mf->n_bits - 64);
861 narl->dst = htonl(mf->nxm_header);
862 memcpy(&narl->value, &sf->value.ipv6.s6_addr[0], sizeof narl->value);
864 narl = ofputil_put_NXAST_REG_LOAD(openflow);
865 narl->ofs_nbits = nxm_encode_ofs_nbits(0, mf->n_bits);
866 narl->dst = htonl(mf->nxm_header);
867 memset(&narl->value, 0, 8 - mf->n_bytes);
868 memcpy((char*)&narl->value + (8 - mf->n_bytes),
869 &sf->value, mf->n_bytes);
873 /* Convert 'sf' to standard OpenFlow 1.1 actions, if we can, falling back
874 * to Nicira extensions if we must.
876 * We check only meta-flow types that can appear within set field actions and
877 * that have a mapping to compatible action types. These struct mf_field
878 * definitions have a defined OXM or NXM header value and specify the field as
881 set_field_to_openflow11(const struct ofpact_set_field *sf,
882 struct ofpbuf *openflow)
884 switch ((int) sf->field->id) {
886 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
888 * If CFI=1, Add or modify VLAN VID & PCP.
889 * OpenFlow 1.1 set actions only apply if the packet
890 * already has VLAN tags. To be sure that is the case
891 * we have to push a VLAN header. As we do not support
892 * multiple layers of VLANs, this is a no-op, if a VLAN
893 * header already exists. This may backfire, however,
894 * when we start supporting multiple layers of VLANs.
895 * If CFI=0, strip VLAN header, if any.
897 if (sf->value.be16 & htons(VLAN_CFI)) {
898 /* Push a VLAN tag, if one was not seen at action validation
900 if (!sf->flow_has_vlan) {
901 ofputil_put_OFPAT11_PUSH_VLAN(openflow)->ethertype
902 = htons(ETH_TYPE_VLAN_8021Q);
904 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
905 = sf->value.be16 & htons(VLAN_VID_MASK);
906 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp
907 = vlan_tci_to_pcp(sf->value.be16);
909 /* If the flow did not match on vlan, we have no way of
910 * knowing if the vlan tag exists, so we must POP just to be
912 ofputil_put_OFPAT11_POP_VLAN(openflow);
917 /* OXM VLAN_PCP to OpenFlow 1.1.
918 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
919 * tag. Clear the OFPVID_PRESENT bit.
921 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
922 = sf->value.be16 & htons(VLAN_VID_MASK);
926 /* OXM VLAN_PCP to OpenFlow 1.1.
927 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
928 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
932 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(openflow)->dl_addr,
933 sf->value.mac, ETH_ADDR_LEN);
937 memcpy(ofputil_put_OFPAT11_SET_DL_DST(openflow)->dl_addr,
938 sf->value.mac, ETH_ADDR_LEN);
942 ofputil_put_OFPAT11_SET_MPLS_LABEL(openflow)->mpls_label =
947 ofputil_put_OFPAT11_SET_MPLS_TC(openflow)->mpls_tc = sf->value.u8;
951 ofputil_put_OFPAT11_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
955 ofputil_put_OFPAT11_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
959 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
962 case MFF_IP_DSCP_SHIFTED:
963 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
967 ofputil_put_OFPAT11_SET_NW_ECN(openflow)->nw_ecn = sf->value.u8;
971 ofputil_put_OFPAT11_SET_NW_TTL(openflow)->nw_ttl = sf->value.u8;
977 ofputil_put_OFPAT11_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
983 ofputil_put_OFPAT11_SET_TP_DST(openflow)->tp_port = sf->value.be16;
987 set_field_to_nxast(sf, openflow);
992 /* Convert 'sf' to standard OpenFlow 1.0 actions, if we can, falling back
993 * to Nicira extensions if we must.
995 * We check only meta-flow types that can appear within set field actions and
996 * that have a mapping to compatible action types. These struct mf_field
997 * definitions have a defined OXM or NXM header value and specify the field as
1000 set_field_to_openflow10(const struct ofpact_set_field *sf,
1001 struct ofpbuf *openflow)
1003 switch ((int) sf->field->id) {
1005 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
1007 * If CFI=1, Add or modify VLAN VID & PCP.
1008 * If CFI=0, strip VLAN header, if any.
1010 if (sf->value.be16 & htons(VLAN_CFI)) {
1011 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1012 = sf->value.be16 & htons(VLAN_VID_MASK);
1013 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp
1014 = vlan_tci_to_pcp(sf->value.be16);
1016 ofputil_put_OFPAT10_STRIP_VLAN(openflow);
1021 /* OXM VLAN_VID to OpenFlow 1.0.
1022 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
1023 * tag. Clear the OFPVID_PRESENT bit.
1025 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1026 = sf->value.be16 & htons(VLAN_VID_MASK);
1030 /* OXM VLAN_PCP to OpenFlow 1.0.
1031 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
1032 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
1036 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(openflow)->dl_addr,
1037 sf->value.mac, ETH_ADDR_LEN);
1041 memcpy(ofputil_put_OFPAT10_SET_DL_DST(openflow)->dl_addr,
1042 sf->value.mac, ETH_ADDR_LEN);
1046 ofputil_put_OFPAT10_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
1050 ofputil_put_OFPAT10_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
1054 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
1057 case MFF_IP_DSCP_SHIFTED:
1058 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
1063 ofputil_put_OFPAT10_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
1068 ofputil_put_OFPAT10_SET_TP_DST(openflow)->tp_port = sf->value.be16;
1072 set_field_to_nxast(sf, openflow);
1078 set_field_to_openflow(const struct ofpact_set_field *sf,
1079 struct ofpbuf *openflow)
1081 struct ofp_header *oh = (struct ofp_header *)openflow->l2;
1083 if (oh->version >= OFP12_VERSION) {
1084 set_field_to_openflow12(sf, openflow);
1085 } else if (oh->version == OFP11_VERSION) {
1086 set_field_to_openflow11(sf, openflow);
1087 } else if (oh->version == OFP10_VERSION) {
1088 set_field_to_openflow10(sf, openflow);
1095 output_from_openflow11(const struct ofp11_action_output *oao,
1098 struct ofpact_output *output;
1101 output = ofpact_put_OUTPUT(out);
1102 output->max_len = ntohs(oao->max_len);
1104 error = ofputil_port_from_ofp11(oao->port, &output->port);
1109 return ofpact_check_output_port(output->port, OFPP_MAX);
1113 ofpact_from_openflow11(const union ofp_action *a, enum ofp_version version,
1116 enum ofputil_action_code code;
1118 struct ofpact_vlan_vid *vlan_vid;
1119 struct ofpact_vlan_pcp *vlan_pcp;
1121 error = decode_openflow11_action(a, &code);
1126 if (version >= OFP12_VERSION) {
1127 switch ((int)code) {
1128 case OFPUTIL_OFPAT11_SET_VLAN_VID:
1129 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1130 case OFPUTIL_OFPAT11_SET_DL_SRC:
1131 case OFPUTIL_OFPAT11_SET_DL_DST:
1132 case OFPUTIL_OFPAT11_SET_NW_SRC:
1133 case OFPUTIL_OFPAT11_SET_NW_DST:
1134 case OFPUTIL_OFPAT11_SET_NW_TOS:
1135 case OFPUTIL_OFPAT11_SET_NW_ECN:
1136 case OFPUTIL_OFPAT11_SET_TP_SRC:
1137 case OFPUTIL_OFPAT11_SET_TP_DST:
1138 VLOG_WARN_RL(&rl, "Deprecated action %s received over %s",
1139 ofputil_action_name_from_code(code),
1140 ofputil_version_to_string(version));
1145 case OFPUTIL_ACTION_INVALID:
1146 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
1147 #include "ofp-util.def"
1150 case OFPUTIL_OFPAT11_OUTPUT:
1151 return output_from_openflow11(&a->ofp11_output, out);
1153 case OFPUTIL_OFPAT11_SET_VLAN_VID:
1154 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1155 return OFPERR_OFPBAC_BAD_ARGUMENT;
1157 vlan_vid = ofpact_put_SET_VLAN_VID(out);
1158 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
1159 vlan_vid->push_vlan_if_needed = false;
1160 vlan_vid->ofpact.compat = code;
1163 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1164 if (a->vlan_pcp.vlan_pcp & ~7) {
1165 return OFPERR_OFPBAC_BAD_ARGUMENT;
1167 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1168 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
1169 vlan_pcp->push_vlan_if_needed = false;
1170 vlan_pcp->ofpact.compat = code;
1173 case OFPUTIL_OFPAT11_PUSH_VLAN:
1174 if (a->push.ethertype != htons(ETH_TYPE_VLAN_8021Q)) {
1175 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1176 return OFPERR_OFPBAC_BAD_ARGUMENT;
1178 ofpact_put_PUSH_VLAN(out);
1181 case OFPUTIL_OFPAT11_POP_VLAN:
1182 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
1185 case OFPUTIL_OFPAT11_SET_QUEUE:
1186 ofpact_put_SET_QUEUE(out)->queue_id =
1187 ntohl(a->ofp11_set_queue.queue_id);
1190 case OFPUTIL_OFPAT11_SET_DL_SRC:
1191 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
1195 case OFPUTIL_OFPAT11_SET_DL_DST:
1196 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
1200 case OFPUTIL_OFPAT11_DEC_NW_TTL:
1201 dec_ttl_from_openflow(out, code);
1204 case OFPUTIL_OFPAT11_SET_NW_SRC:
1205 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
1208 case OFPUTIL_OFPAT11_SET_NW_DST:
1209 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
1212 case OFPUTIL_OFPAT11_SET_NW_TOS:
1213 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
1214 return OFPERR_OFPBAC_BAD_ARGUMENT;
1216 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
1219 case OFPUTIL_OFPAT11_SET_NW_ECN:
1220 if (a->nw_ecn.nw_ecn & ~IP_ECN_MASK) {
1221 return OFPERR_OFPBAC_BAD_ARGUMENT;
1223 ofpact_put_SET_IP_ECN(out)->ecn = a->nw_ecn.nw_ecn;
1226 case OFPUTIL_OFPAT11_SET_NW_TTL:
1227 ofpact_put_SET_IP_TTL(out)->ttl = a->nw_ttl.nw_ttl;
1230 case OFPUTIL_OFPAT11_SET_TP_SRC:
1231 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
1234 case OFPUTIL_OFPAT11_SET_TP_DST:
1235 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
1238 case OFPUTIL_OFPAT12_SET_FIELD:
1239 return set_field_from_openflow(&a->set_field, out);
1241 case OFPUTIL_OFPAT11_SET_MPLS_LABEL:
1242 ofpact_put_SET_MPLS_LABEL(out)->label = a->ofp11_mpls_label.mpls_label;
1245 case OFPUTIL_OFPAT11_SET_MPLS_TC:
1246 ofpact_put_SET_MPLS_TC(out)->tc = a->ofp11_mpls_tc.mpls_tc;
1249 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
1250 ofpact_put_SET_MPLS_TTL(out)->ttl = a->ofp11_mpls_ttl.mpls_ttl;
1253 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
1254 ofpact_put_DEC_MPLS_TTL(out);
1257 case OFPUTIL_OFPAT11_PUSH_MPLS:
1258 /* OpenFlow 1.3 has different semantics. */
1259 error = push_mpls_from_openflow(a->push.ethertype,
1260 version >= OFP13_VERSION ?
1261 OFPACT_MPLS_BEFORE_VLAN :
1262 OFPACT_MPLS_AFTER_VLAN, out);
1265 case OFPUTIL_OFPAT11_POP_MPLS:
1266 if (eth_type_mpls(a->ofp11_pop_mpls.ethertype)) {
1267 return OFPERR_OFPBAC_BAD_ARGUMENT;
1269 ofpact_put_POP_MPLS(out)->ethertype = a->ofp11_pop_mpls.ethertype;
1272 case OFPUTIL_OFPAT11_GROUP:
1273 ofpact_put_GROUP(out)->group_id = ntohl(a->group.group_id);
1276 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
1277 #include "ofp-util.def"
1278 return ofpact_from_nxast(a, code, out);
1284 /* True if an action sets the value of a field
1285 * in a way that is compatibile with the action set.
1286 * False otherwise. */
1288 ofpact_is_set_action(const struct ofpact *a)
1291 case OFPACT_SET_FIELD:
1292 case OFPACT_REG_LOAD:
1293 case OFPACT_SET_ETH_DST:
1294 case OFPACT_SET_ETH_SRC:
1295 case OFPACT_SET_IP_DSCP:
1296 case OFPACT_SET_IP_ECN:
1297 case OFPACT_SET_IP_TTL:
1298 case OFPACT_SET_IPV4_DST:
1299 case OFPACT_SET_IPV4_SRC:
1300 case OFPACT_SET_L4_DST_PORT:
1301 case OFPACT_SET_L4_SRC_PORT:
1302 case OFPACT_SET_MPLS_LABEL:
1303 case OFPACT_SET_MPLS_TC:
1304 case OFPACT_SET_MPLS_TTL:
1305 case OFPACT_SET_QUEUE:
1306 case OFPACT_SET_TUNNEL:
1307 case OFPACT_SET_VLAN_PCP:
1308 case OFPACT_SET_VLAN_VID:
1311 case OFPACT_CLEAR_ACTIONS:
1312 case OFPACT_CONTROLLER:
1313 case OFPACT_DEC_MPLS_TTL:
1314 case OFPACT_DEC_TTL:
1315 case OFPACT_ENQUEUE:
1317 case OFPACT_FIN_TIMEOUT:
1318 case OFPACT_GOTO_TABLE:
1322 case OFPACT_MULTIPATH:
1325 case OFPACT_OUTPUT_REG:
1326 case OFPACT_POP_MPLS:
1327 case OFPACT_POP_QUEUE:
1328 case OFPACT_PUSH_MPLS:
1329 case OFPACT_PUSH_VLAN:
1330 case OFPACT_REG_MOVE:
1331 case OFPACT_RESUBMIT:
1333 case OFPACT_STACK_POP:
1334 case OFPACT_STACK_PUSH:
1335 case OFPACT_STRIP_VLAN:
1336 case OFPACT_WRITE_ACTIONS:
1337 case OFPACT_WRITE_METADATA:
1344 /* True if an action is allowed in the action set.
1345 * False otherwise. */
1347 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
1350 case OFPACT_DEC_MPLS_TTL:
1351 case OFPACT_DEC_TTL:
1354 case OFPACT_POP_MPLS:
1355 case OFPACT_PUSH_MPLS:
1356 case OFPACT_PUSH_VLAN:
1357 case OFPACT_REG_LOAD:
1358 case OFPACT_SET_FIELD:
1359 case OFPACT_SET_ETH_DST:
1360 case OFPACT_SET_ETH_SRC:
1361 case OFPACT_SET_IP_DSCP:
1362 case OFPACT_SET_IP_ECN:
1363 case OFPACT_SET_IP_TTL:
1364 case OFPACT_SET_IPV4_DST:
1365 case OFPACT_SET_IPV4_SRC:
1366 case OFPACT_SET_L4_DST_PORT:
1367 case OFPACT_SET_L4_SRC_PORT:
1368 case OFPACT_SET_MPLS_LABEL:
1369 case OFPACT_SET_MPLS_TC:
1370 case OFPACT_SET_MPLS_TTL:
1371 case OFPACT_SET_QUEUE:
1372 case OFPACT_SET_TUNNEL:
1373 case OFPACT_SET_VLAN_PCP:
1374 case OFPACT_SET_VLAN_VID:
1375 case OFPACT_STRIP_VLAN:
1378 /* In general these actions are excluded because they are not part of
1379 * the OpenFlow specification nor map to actions that are defined in
1380 * the specification. Thus the order in which they should be applied
1381 * in the action set is undefined. */
1383 case OFPACT_CONTROLLER:
1384 case OFPACT_ENQUEUE:
1386 case OFPACT_FIN_TIMEOUT:
1388 case OFPACT_MULTIPATH:
1390 case OFPACT_OUTPUT_REG:
1391 case OFPACT_POP_QUEUE:
1392 case OFPACT_REG_MOVE:
1393 case OFPACT_RESUBMIT:
1395 case OFPACT_STACK_POP:
1396 case OFPACT_STACK_PUSH:
1398 /* The action set may only include actions and thus
1399 * may not include any instructions */
1400 case OFPACT_CLEAR_ACTIONS:
1401 case OFPACT_GOTO_TABLE:
1403 case OFPACT_WRITE_ACTIONS:
1404 case OFPACT_WRITE_METADATA:
1411 /* Append ofpact 'a' onto the tail of 'out' */
1413 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1415 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1418 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1420 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1421 enum ofpact_type filter)
1423 const struct ofpact *target;
1424 const struct ofpact *a;
1427 OFPACT_FOR_EACH (a, in->data, in->size) {
1428 if (a->type == filter) {
1433 ofpact_copy(out, target);
1435 return target != NULL;
1438 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1439 * The order of appended ofpacts is preserved between 'in' and 'out' */
1441 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1442 bool (*filter)(const struct ofpact *))
1444 const struct ofpact *a;
1446 OFPACT_FOR_EACH (a, in->data, in->size) {
1448 ofpact_copy(out, a);
1453 /* Reads 'action_set', which contains ofpacts accumulated by
1454 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1455 * executed directly into 'action_list'. (These names correspond to the
1456 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1458 * In general this involves appending the last instance of each action that is
1459 * adimissible in the action set in the order described in the OpenFlow
1463 * + output action is only appended if no group action was present in 'in'.
1464 * + As a simplification all set actions are copied in the order the are
1465 * provided in 'in' as many set actions applied to a field has the same
1466 * affect as only applying the last action that sets a field and
1467 * duplicates are removed by do_xlate_actions().
1468 * This has an unwanted side-effect of compsoting multiple
1469 * LOAD_REG actions that touch different regions of the same field. */
1471 ofpacts_execute_action_set(struct ofpbuf *action_list,
1472 const struct ofpbuf *action_set)
1474 /* The OpenFlow spec "Action Set" section specifies this order. */
1475 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1476 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1477 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1478 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1479 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1480 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1481 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1482 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1484 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1485 * we should execute only OFPACT_GROUP.
1487 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1488 * all the actions because there's no point in modifying a packet that will
1489 * not be sent anywhere. */
1490 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1491 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1492 ofpbuf_clear(action_list);
1498 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1499 size_t n_in, enum ofp_version version,
1504 size_t start = out->size;
1506 error = ofpacts_from_openflow(in, n_in, version, out);
1512 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1513 if (!ofpact_is_allowed_in_actions_set(a)) {
1514 VLOG_WARN_RL(&rl, "disallowed action in action set");
1515 return OFPERR_OFPBAC_BAD_TYPE;
1523 /* OpenFlow 1.1 instructions. */
1525 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1526 static inline const struct STRUCT * OVS_UNUSED \
1527 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1529 ovs_assert(inst->type == htons(ENUM)); \
1530 return ALIGNED_CAST(struct STRUCT *, inst); \
1533 static inline void OVS_UNUSED \
1534 instruction_init_##ENUM(struct STRUCT *s) \
1536 memset(s, 0, sizeof *s); \
1537 s->type = htons(ENUM); \
1538 s->len = htons(sizeof *s); \
1541 static inline struct STRUCT * OVS_UNUSED \
1542 instruction_put_##ENUM(struct ofpbuf *buf) \
1544 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1545 instruction_init_##ENUM(s); \
1551 struct instruction_type_info {
1552 enum ovs_instruction_type type;
1556 static const struct instruction_type_info inst_info[] = {
1557 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1563 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1565 return inst_info[type].name;
1569 ovs_instruction_type_from_name(const char *name)
1571 const struct instruction_type_info *p;
1572 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1573 if (!strcasecmp(name, p->name)) {
1580 enum ovs_instruction_type
1581 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1585 return OVSINST_OFPIT13_METER;
1586 case OFPACT_CLEAR_ACTIONS:
1587 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1588 case OFPACT_WRITE_ACTIONS:
1589 return OVSINST_OFPIT11_WRITE_ACTIONS;
1590 case OFPACT_WRITE_METADATA:
1591 return OVSINST_OFPIT11_WRITE_METADATA;
1592 case OFPACT_GOTO_TABLE:
1593 return OVSINST_OFPIT11_GOTO_TABLE;
1596 case OFPACT_CONTROLLER:
1597 case OFPACT_ENQUEUE:
1598 case OFPACT_OUTPUT_REG:
1600 case OFPACT_SET_VLAN_VID:
1601 case OFPACT_SET_VLAN_PCP:
1602 case OFPACT_STRIP_VLAN:
1603 case OFPACT_PUSH_VLAN:
1604 case OFPACT_SET_ETH_SRC:
1605 case OFPACT_SET_ETH_DST:
1606 case OFPACT_SET_IPV4_SRC:
1607 case OFPACT_SET_IPV4_DST:
1608 case OFPACT_SET_IP_DSCP:
1609 case OFPACT_SET_IP_ECN:
1610 case OFPACT_SET_IP_TTL:
1611 case OFPACT_SET_L4_SRC_PORT:
1612 case OFPACT_SET_L4_DST_PORT:
1613 case OFPACT_REG_MOVE:
1614 case OFPACT_REG_LOAD:
1615 case OFPACT_SET_FIELD:
1616 case OFPACT_STACK_PUSH:
1617 case OFPACT_STACK_POP:
1618 case OFPACT_DEC_TTL:
1619 case OFPACT_SET_MPLS_LABEL:
1620 case OFPACT_SET_MPLS_TC:
1621 case OFPACT_SET_MPLS_TTL:
1622 case OFPACT_DEC_MPLS_TTL:
1623 case OFPACT_PUSH_MPLS:
1624 case OFPACT_POP_MPLS:
1625 case OFPACT_SET_TUNNEL:
1626 case OFPACT_SET_QUEUE:
1627 case OFPACT_POP_QUEUE:
1628 case OFPACT_FIN_TIMEOUT:
1629 case OFPACT_RESUBMIT:
1631 case OFPACT_MULTIPATH:
1636 return OVSINST_OFPIT11_APPLY_ACTIONS;
1640 static inline struct ofp11_instruction *
1641 instruction_next(const struct ofp11_instruction *inst)
1643 return ((struct ofp11_instruction *) (void *)
1644 ((uint8_t *) inst + ntohs(inst->len)));
1648 instruction_is_valid(const struct ofp11_instruction *inst,
1649 size_t n_instructions)
1651 uint16_t len = ntohs(inst->len);
1652 return (!(len % OFP11_INSTRUCTION_ALIGN)
1653 && len >= sizeof *inst
1654 && len / sizeof *inst <= n_instructions);
1657 /* This macro is careful to check for instructions with bad lengths. */
1658 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1659 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1660 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1661 ((LEFT) -= (ntohs((ITER)->len) \
1662 / sizeof(struct ofp11_instruction)), \
1663 (ITER) = instruction_next(ITER)))
1666 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1667 enum ovs_instruction_type *type)
1669 uint16_t len = ntohs(inst->len);
1671 switch (inst->type) {
1672 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1673 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1675 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1676 case CONSTANT_HTONS(ENUM): \
1678 ? len >= sizeof(struct STRUCT) \
1679 : len == sizeof(struct STRUCT)) { \
1680 *type = OVSINST_##ENUM; \
1683 return OFPERR_OFPBIC_BAD_LEN; \
1689 return OFPERR_OFPBIC_UNKNOWN_INST;
1694 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1696 const struct ofp11_instruction *out[])
1698 const struct ofp11_instruction *inst;
1701 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1702 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1703 enum ovs_instruction_type type;
1706 error = decode_openflow11_instruction(inst, &type);
1712 return OFPERR_ONFBIC_DUP_INSTRUCTION;
1718 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1719 (n_insts - left) * sizeof *inst);
1720 return OFPERR_OFPBIC_BAD_LEN;
1726 get_actions_from_instruction(const struct ofp11_instruction *inst,
1727 const union ofp_action **actions,
1728 size_t *max_actions)
1730 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1731 *max_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1735 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
1736 unsigned int instructions_len,
1737 enum ofp_version version,
1738 struct ofpbuf *ofpacts)
1740 const struct ofp11_instruction *instructions;
1741 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1744 ofpbuf_clear(ofpacts);
1746 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1747 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1749 instructions_len, OFP11_INSTRUCTION_ALIGN);
1750 error = OFPERR_OFPBIC_BAD_LEN;
1754 instructions = ofpbuf_try_pull(openflow, instructions_len);
1755 if (instructions == NULL) {
1756 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1757 "remaining message length (%zu)",
1758 instructions_len, openflow->size);
1759 error = OFPERR_OFPBIC_BAD_LEN;
1763 error = decode_openflow11_instructions(
1764 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1770 if (insts[OVSINST_OFPIT13_METER]) {
1771 const struct ofp13_instruction_meter *oim;
1772 struct ofpact_meter *om;
1774 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1775 insts[OVSINST_OFPIT13_METER]);
1777 om = ofpact_put_METER(ofpacts);
1778 om->meter_id = ntohl(oim->meter_id);
1780 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1781 const union ofp_action *actions;
1784 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1785 &actions, &max_actions);
1786 error = ofpacts_from_openflow(actions, max_actions, version, ofpacts);
1791 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1792 instruction_get_OFPIT11_CLEAR_ACTIONS(
1793 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1794 ofpact_put_CLEAR_ACTIONS(ofpacts);
1796 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1797 struct ofpact_nest *on;
1798 const union ofp_action *actions;
1802 ofpact_pad(ofpacts);
1803 start = ofpacts->size;
1804 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1805 offsetof(struct ofpact_nest, actions));
1806 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1807 &actions, &max_actions);
1808 error = ofpacts_from_openflow11_for_action_set(actions, max_actions,
1813 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1814 on->ofpact.len = ofpacts->size - start;
1816 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1817 const struct ofp11_instruction_write_metadata *oiwm;
1818 struct ofpact_metadata *om;
1820 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1821 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1823 om = ofpact_put_WRITE_METADATA(ofpacts);
1824 om->metadata = oiwm->metadata;
1825 om->mask = oiwm->metadata_mask;
1827 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1828 const struct ofp11_instruction_goto_table *oigt;
1829 struct ofpact_goto_table *ogt;
1831 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1832 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1833 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1834 ogt->table_id = oigt->table_id;
1837 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1840 ofpbuf_clear(ofpacts);
1845 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
1846 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
1847 * valid, otherwise an OpenFlow error code. */
1849 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
1857 case OFPP_CONTROLLER:
1863 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
1866 return OFPERR_OFPBAC_BAD_OUT_PORT;
1870 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
1871 * caller must restore them.
1873 * Modifies some actions, filling in fields that could not be properly set
1874 * without context. */
1876 ofpact_check__(struct ofpact *a, struct flow *flow,
1877 bool enforce_consistency, ofp_port_t max_ports,
1878 uint8_t table_id, uint8_t n_tables)
1880 const struct ofpact_enqueue *enqueue;
1881 const struct mf_field *mf;
1885 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
1888 case OFPACT_CONTROLLER:
1891 case OFPACT_ENQUEUE:
1892 enqueue = ofpact_get_ENQUEUE(a);
1893 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1894 && enqueue->port != OFPP_IN_PORT
1895 && enqueue->port != OFPP_LOCAL) {
1896 return OFPERR_OFPBAC_BAD_OUT_PORT;
1900 case OFPACT_OUTPUT_REG:
1901 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1904 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1906 case OFPACT_SET_VLAN_VID:
1907 /* Remember if we saw a vlan tag in the flow to aid translating to
1908 * OpenFlow 1.1+ if need be. */
1909 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
1910 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1911 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1912 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
1915 /* Temporary mark that we have a vlan tag. */
1916 flow->vlan_tci |= htons(VLAN_CFI);
1919 case OFPACT_SET_VLAN_PCP:
1920 /* Remember if we saw a vlan tag in the flow to aid translating to
1921 * OpenFlow 1.1+ if need be. */
1922 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
1923 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1924 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1925 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
1928 /* Temporary mark that we have a vlan tag. */
1929 flow->vlan_tci |= htons(VLAN_CFI);
1932 case OFPACT_STRIP_VLAN:
1933 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
1936 /* Temporary mark that we have no vlan tag. */
1937 flow->vlan_tci = htons(0);
1940 case OFPACT_PUSH_VLAN:
1941 if (flow->vlan_tci & htons(VLAN_CFI)) {
1942 /* Multiple VLAN headers not supported. */
1943 return OFPERR_OFPBAC_BAD_TAG;
1945 /* Temporary mark that we have a vlan tag. */
1946 flow->vlan_tci |= htons(VLAN_CFI);
1949 case OFPACT_SET_ETH_SRC:
1950 case OFPACT_SET_ETH_DST:
1953 case OFPACT_SET_IPV4_SRC:
1954 case OFPACT_SET_IPV4_DST:
1955 if (flow->dl_type != htons(ETH_TYPE_IP)) {
1960 case OFPACT_SET_IP_DSCP:
1961 case OFPACT_SET_IP_ECN:
1962 case OFPACT_SET_IP_TTL:
1963 case OFPACT_DEC_TTL:
1964 if (!is_ip_any(flow)) {
1969 case OFPACT_SET_L4_SRC_PORT:
1970 if (!is_ip_any(flow) ||
1971 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1972 && flow->nw_proto != IPPROTO_SCTP)) {
1975 /* Note on which transport protocol the port numbers are set.
1976 * This allows this set action to be converted to an OF1.2 set field
1978 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
1981 case OFPACT_SET_L4_DST_PORT:
1982 if (!is_ip_any(flow) ||
1983 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1984 && flow->nw_proto != IPPROTO_SCTP)) {
1987 /* Note on which transport protocol the port numbers are set.
1988 * This allows this set action to be converted to an OF1.2 set field
1990 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
1993 case OFPACT_REG_MOVE:
1994 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1996 case OFPACT_REG_LOAD:
1997 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1999 case OFPACT_SET_FIELD:
2000 mf = ofpact_get_SET_FIELD(a)->field;
2001 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
2002 if (!mf_are_prereqs_ok(mf, flow) ||
2003 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
2004 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
2006 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2008 /* Remember if we saw a vlan tag in the flow to aid translating to
2009 * OpenFlow 1.1 if need be. */
2010 ofpact_get_SET_FIELD(a)->flow_has_vlan =
2011 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
2012 if (mf->id == MFF_VLAN_TCI) {
2013 /* The set field may add or remove the vlan tag,
2014 * Mark the status temporarily. */
2015 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
2019 case OFPACT_STACK_PUSH:
2020 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
2022 case OFPACT_STACK_POP:
2023 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
2025 case OFPACT_SET_MPLS_LABEL:
2026 case OFPACT_SET_MPLS_TC:
2027 case OFPACT_SET_MPLS_TTL:
2028 case OFPACT_DEC_MPLS_TTL:
2029 if (!eth_type_mpls(flow->dl_type)) {
2034 case OFPACT_SET_TUNNEL:
2035 case OFPACT_SET_QUEUE:
2036 case OFPACT_POP_QUEUE:
2037 case OFPACT_RESUBMIT:
2040 case OFPACT_FIN_TIMEOUT:
2041 if (flow->nw_proto != IPPROTO_TCP) {
2047 return learn_check(ofpact_get_LEARN(a), flow);
2049 case OFPACT_MULTIPATH:
2050 return multipath_check(ofpact_get_MULTIPATH(a), flow);
2056 case OFPACT_PUSH_MPLS:
2057 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
2058 /* The packet is now MPLS and the MPLS payload is opaque.
2059 * Thus nothing can be assumed about the network protocol.
2060 * Temporarily mark that we have no nw_proto. */
2064 case OFPACT_POP_MPLS:
2065 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
2066 if (!eth_type_mpls(flow->dl_type)) {
2074 case OFPACT_CLEAR_ACTIONS:
2077 case OFPACT_WRITE_ACTIONS: {
2078 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2079 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
2080 flow, false, max_ports, table_id, n_tables);
2083 case OFPACT_WRITE_METADATA:
2086 case OFPACT_METER: {
2087 uint32_t mid = ofpact_get_METER(a)->meter_id;
2088 if (mid == 0 || mid > OFPM13_MAX) {
2089 return OFPERR_OFPMMFC_INVALID_METER;
2094 case OFPACT_GOTO_TABLE: {
2095 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
2096 if ((table_id != 255 && goto_table <= table_id)
2097 || (n_tables != 255 && goto_table >= n_tables)) {
2098 return OFPERR_OFPBRC_BAD_TABLE_ID;
2111 if (enforce_consistency) {
2112 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2117 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2118 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
2119 * switch with no more than 'max_ports' ports.
2121 * May annotate ofpacts with information gathered from the 'flow'.
2123 * May temporarily modify 'flow', but restores the changes before returning. */
2125 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
2126 struct flow *flow, bool enforce_consistency,
2127 ofp_port_t max_ports,
2128 uint8_t table_id, uint8_t n_tables)
2131 ovs_be16 dl_type = flow->dl_type;
2132 ovs_be16 vlan_tci = flow->vlan_tci;
2133 uint8_t nw_proto = flow->nw_proto;
2134 enum ofperr error = 0;
2136 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2137 error = ofpact_check__(a, flow, enforce_consistency,
2138 max_ports, table_id, n_tables);
2143 /* Restore fields that may have been modified. */
2144 flow->dl_type = dl_type;
2145 flow->vlan_tci = vlan_tci;
2146 flow->nw_proto = nw_proto;
2150 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2151 * in the appropriate order as defined by the OpenFlow spec. */
2153 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
2155 const struct ofpact *a;
2156 enum ovs_instruction_type inst;
2158 inst = OVSINST_OFPIT13_METER;
2159 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2160 enum ovs_instruction_type next;
2162 next = ovs_instruction_type_from_ofpact_type(a->type);
2164 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
2167 const char *name = ovs_instruction_name_from_type(inst);
2168 const char *next_name = ovs_instruction_name_from_type(next);
2171 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
2172 "1.1+ compatibility", name);
2174 VLOG_WARN("invalid instruction ordering: %s must appear "
2175 "before %s, for OpenFlow 1.1+ compatibility",
2178 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
2187 /* Converting ofpacts to Nicira OpenFlow extensions. */
2190 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
2193 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
2195 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
2196 output_reg->src.n_bits);
2197 naor->src = htonl(output_reg->src.field->nxm_header);
2198 naor->max_len = htons(output_reg->max_len);
2202 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
2205 struct nx_action_resubmit *nar;
2207 if (resubmit->table_id == 0xff
2208 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
2209 nar = ofputil_put_NXAST_RESUBMIT(out);
2211 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
2212 nar->table = resubmit->table_id;
2214 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
2218 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
2221 uint64_t tun_id = tunnel->tun_id;
2223 if (tun_id <= UINT32_MAX
2224 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
2225 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
2227 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
2232 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
2235 struct nx_action_write_metadata *nawm;
2237 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
2238 nawm->metadata = om->metadata;
2239 nawm->mask = om->mask;
2243 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
2245 size_t start_ofs = out->size;
2246 struct nx_action_note *nan;
2247 unsigned int remainder;
2250 nan = ofputil_put_NXAST_NOTE(out);
2251 out->size -= sizeof nan->note;
2253 ofpbuf_put(out, note->data, note->length);
2255 len = out->size - start_ofs;
2256 remainder = len % OFP_ACTION_ALIGN;
2258 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
2260 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
2261 nan->len = htons(out->size - start_ofs);
2265 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
2268 struct nx_action_controller *nac;
2270 nac = ofputil_put_NXAST_CONTROLLER(out);
2271 nac->max_len = htons(oc->max_len);
2272 nac->controller_id = htons(oc->controller_id);
2273 nac->reason = oc->reason;
2277 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
2280 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
2281 ofputil_put_NXAST_DEC_TTL(out);
2283 struct nx_action_cnt_ids *nac_ids =
2284 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
2285 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
2289 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2290 nac_ids->n_controllers = htons(oc_ids->n_controllers);
2292 ids = ofpbuf_put_zeros(out, ids_len);
2293 for (i = 0; i < oc_ids->n_controllers; i++) {
2294 ids[i] = htons(oc_ids->cnt_ids[i]);
2300 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
2303 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
2304 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
2305 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
2309 ofpact_sample_to_nxast(const struct ofpact_sample *os,
2312 struct nx_action_sample *nas;
2314 nas = ofputil_put_NXAST_SAMPLE(out);
2315 nas->probability = htons(os->probability);
2316 nas->collector_set_id = htonl(os->collector_set_id);
2317 nas->obs_domain_id = htonl(os->obs_domain_id);
2318 nas->obs_point_id = htonl(os->obs_point_id);
2322 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
2325 case OFPACT_CONTROLLER:
2326 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
2329 case OFPACT_OUTPUT_REG:
2330 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
2334 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
2337 case OFPACT_REG_MOVE:
2338 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
2341 case OFPACT_REG_LOAD:
2342 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
2345 case OFPACT_STACK_PUSH:
2346 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
2349 case OFPACT_STACK_POP:
2350 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
2353 case OFPACT_DEC_TTL:
2354 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
2357 case OFPACT_SET_MPLS_LABEL:
2358 ofputil_put_NXAST_SET_MPLS_LABEL(out)->label
2359 = ofpact_get_SET_MPLS_LABEL(a)->label;
2362 case OFPACT_SET_MPLS_TC:
2363 ofputil_put_NXAST_SET_MPLS_TC(out)->tc
2364 = ofpact_get_SET_MPLS_TC(a)->tc;
2367 case OFPACT_SET_MPLS_TTL:
2368 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
2369 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2372 case OFPACT_DEC_MPLS_TTL:
2373 ofputil_put_NXAST_DEC_MPLS_TTL(out);
2376 case OFPACT_SET_TUNNEL:
2377 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
2380 case OFPACT_WRITE_METADATA:
2381 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
2384 case OFPACT_SET_QUEUE:
2385 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
2386 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2389 case OFPACT_POP_QUEUE:
2390 ofputil_put_NXAST_POP_QUEUE(out);
2393 case OFPACT_FIN_TIMEOUT:
2394 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
2397 case OFPACT_RESUBMIT:
2398 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
2402 learn_to_nxast(ofpact_get_LEARN(a), out);
2405 case OFPACT_MULTIPATH:
2406 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
2410 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
2414 ofputil_put_NXAST_EXIT(out);
2417 case OFPACT_PUSH_MPLS:
2418 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
2419 ofpact_get_PUSH_MPLS(a)->ethertype;
2422 case OFPACT_POP_MPLS:
2423 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2424 ofpact_get_POP_MPLS(a)->ethertype;
2428 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2433 case OFPACT_ENQUEUE:
2434 case OFPACT_SET_VLAN_VID:
2435 case OFPACT_SET_VLAN_PCP:
2436 case OFPACT_STRIP_VLAN:
2437 case OFPACT_PUSH_VLAN:
2438 case OFPACT_SET_ETH_SRC:
2439 case OFPACT_SET_ETH_DST:
2440 case OFPACT_SET_IPV4_SRC:
2441 case OFPACT_SET_IPV4_DST:
2442 case OFPACT_SET_IP_DSCP:
2443 case OFPACT_SET_IP_ECN:
2444 case OFPACT_SET_IP_TTL:
2445 case OFPACT_SET_L4_SRC_PORT:
2446 case OFPACT_SET_L4_DST_PORT:
2447 case OFPACT_WRITE_ACTIONS:
2448 case OFPACT_CLEAR_ACTIONS:
2449 case OFPACT_GOTO_TABLE:
2451 case OFPACT_SET_FIELD:
2456 /* Converting ofpacts to OpenFlow 1.0. */
2459 ofpact_output_to_openflow10(const struct ofpact_output *output,
2462 struct ofp10_action_output *oao;
2464 oao = ofputil_put_OFPAT10_OUTPUT(out);
2465 oao->port = htons(ofp_to_u16(output->port));
2466 oao->max_len = htons(output->max_len);
2470 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2473 struct ofp10_action_enqueue *oae;
2475 oae = ofputil_put_OFPAT10_ENQUEUE(out);
2476 oae->port = htons(ofp_to_u16(enqueue->port));
2477 oae->queue_id = htonl(enqueue->queue);
2481 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2485 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2488 case OFPACT_ENQUEUE:
2489 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2492 case OFPACT_SET_VLAN_VID:
2493 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2494 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2497 case OFPACT_SET_VLAN_PCP:
2498 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2499 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2502 case OFPACT_STRIP_VLAN:
2503 ofputil_put_OFPAT10_STRIP_VLAN(out);
2506 case OFPACT_SET_ETH_SRC:
2507 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2508 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2511 case OFPACT_SET_ETH_DST:
2512 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2513 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2516 case OFPACT_SET_IPV4_SRC:
2517 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2518 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2521 case OFPACT_SET_IPV4_DST:
2522 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2523 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2526 case OFPACT_SET_IP_DSCP:
2527 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2528 = ofpact_get_SET_IP_DSCP(a)->dscp;
2531 case OFPACT_SET_L4_SRC_PORT:
2532 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2533 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2536 case OFPACT_SET_L4_DST_PORT:
2537 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2538 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2541 case OFPACT_PUSH_VLAN:
2542 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
2543 * follow this action. */
2546 case OFPACT_CLEAR_ACTIONS:
2547 case OFPACT_WRITE_ACTIONS:
2548 case OFPACT_GOTO_TABLE:
2556 case OFPACT_SET_FIELD:
2557 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2560 case OFPACT_CONTROLLER:
2561 case OFPACT_OUTPUT_REG:
2563 case OFPACT_REG_MOVE:
2564 case OFPACT_REG_LOAD:
2565 case OFPACT_STACK_PUSH:
2566 case OFPACT_STACK_POP:
2567 case OFPACT_DEC_TTL:
2568 case OFPACT_SET_IP_ECN:
2569 case OFPACT_SET_IP_TTL:
2570 case OFPACT_SET_MPLS_LABEL:
2571 case OFPACT_SET_MPLS_TC:
2572 case OFPACT_SET_MPLS_TTL:
2573 case OFPACT_DEC_MPLS_TTL:
2574 case OFPACT_SET_TUNNEL:
2575 case OFPACT_WRITE_METADATA:
2576 case OFPACT_SET_QUEUE:
2577 case OFPACT_POP_QUEUE:
2578 case OFPACT_FIN_TIMEOUT:
2579 case OFPACT_RESUBMIT:
2581 case OFPACT_MULTIPATH:
2584 case OFPACT_PUSH_MPLS:
2585 case OFPACT_POP_MPLS:
2587 ofpact_to_nxast(a, out);
2592 /* Converting ofpacts to OpenFlow 1.1. */
2595 ofpact_output_to_openflow11(const struct ofpact_output *output,
2598 struct ofp11_action_output *oao;
2600 oao = ofputil_put_OFPAT11_OUTPUT(out);
2601 oao->port = ofputil_port_to_ofp11(output->port);
2602 oao->max_len = htons(output->max_len);
2606 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2609 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2610 && (!dec_ttl->ofpact.compat ||
2611 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2612 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2614 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2619 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2623 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2625 case OFPACT_ENQUEUE:
2629 case OFPACT_SET_VLAN_VID:
2630 /* Push a VLAN tag, if one was not seen at action validation time. */
2631 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan
2632 && ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2633 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2634 = htons(ETH_TYPE_VLAN_8021Q);
2636 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2637 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2640 case OFPACT_SET_VLAN_PCP:
2641 /* Push a VLAN tag, if one was not seen at action validation time. */
2642 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan
2643 && ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2644 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2645 = htons(ETH_TYPE_VLAN_8021Q);
2647 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2648 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2651 case OFPACT_STRIP_VLAN:
2652 ofputil_put_OFPAT11_POP_VLAN(out);
2655 case OFPACT_PUSH_VLAN:
2656 /* XXX ETH_TYPE_VLAN_8021AD case */
2657 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2658 htons(ETH_TYPE_VLAN_8021Q);
2661 case OFPACT_SET_QUEUE:
2662 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2663 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2666 case OFPACT_SET_ETH_SRC:
2667 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2668 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2671 case OFPACT_SET_ETH_DST:
2672 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2673 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2676 case OFPACT_SET_IPV4_SRC:
2677 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2678 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2681 case OFPACT_SET_IPV4_DST:
2682 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2683 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2686 case OFPACT_SET_IP_DSCP:
2687 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2688 = ofpact_get_SET_IP_DSCP(a)->dscp;
2691 case OFPACT_SET_IP_ECN:
2692 ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2693 = ofpact_get_SET_IP_ECN(a)->ecn;
2696 case OFPACT_SET_IP_TTL:
2697 ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2698 = ofpact_get_SET_IP_TTL(a)->ttl;
2701 case OFPACT_SET_L4_SRC_PORT:
2702 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2703 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2706 case OFPACT_SET_L4_DST_PORT:
2707 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2708 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2711 case OFPACT_DEC_TTL:
2712 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2715 case OFPACT_SET_MPLS_LABEL:
2716 ofputil_put_OFPAT11_SET_MPLS_LABEL(out)->mpls_label
2717 = ofpact_get_SET_MPLS_LABEL(a)->label;
2720 case OFPACT_SET_MPLS_TC:
2721 ofputil_put_OFPAT11_SET_MPLS_TC(out)->mpls_tc
2722 = ofpact_get_SET_MPLS_TC(a)->tc;
2725 case OFPACT_SET_MPLS_TTL:
2726 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2727 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2730 case OFPACT_DEC_MPLS_TTL:
2731 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2734 case OFPACT_WRITE_METADATA:
2735 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2738 case OFPACT_PUSH_MPLS:
2739 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2740 ofpact_get_PUSH_MPLS(a)->ethertype;
2743 case OFPACT_POP_MPLS:
2744 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2745 ofpact_get_POP_MPLS(a)->ethertype;
2749 case OFPACT_CLEAR_ACTIONS:
2750 case OFPACT_WRITE_ACTIONS:
2751 case OFPACT_GOTO_TABLE:
2756 ofputil_put_OFPAT11_GROUP(out)->group_id =
2757 htonl(ofpact_get_GROUP(a)->group_id);
2760 case OFPACT_SET_FIELD:
2761 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2764 case OFPACT_CONTROLLER:
2765 case OFPACT_OUTPUT_REG:
2767 case OFPACT_REG_MOVE:
2768 case OFPACT_REG_LOAD:
2769 case OFPACT_STACK_PUSH:
2770 case OFPACT_STACK_POP:
2771 case OFPACT_SET_TUNNEL:
2772 case OFPACT_POP_QUEUE:
2773 case OFPACT_FIN_TIMEOUT:
2774 case OFPACT_RESUBMIT:
2776 case OFPACT_MULTIPATH:
2780 ofpact_to_nxast(a, out);
2785 /* Output deprecated set actions as set_field actions. */
2787 ofpact_to_openflow12(const struct ofpact *a, struct ofpbuf *out)
2789 enum mf_field_id field;
2790 union mf_value value;
2791 struct ofpact_l4_port *l4port;
2795 * Convert actions deprecated in OpenFlow 1.2 to Set Field actions,
2798 switch ((int)a->type) {
2799 case OFPACT_SET_VLAN_VID:
2800 case OFPACT_SET_VLAN_PCP:
2801 case OFPACT_SET_ETH_SRC:
2802 case OFPACT_SET_ETH_DST:
2803 case OFPACT_SET_IPV4_SRC:
2804 case OFPACT_SET_IPV4_DST:
2805 case OFPACT_SET_IP_DSCP:
2806 case OFPACT_SET_IP_ECN:
2807 case OFPACT_SET_L4_SRC_PORT:
2808 case OFPACT_SET_L4_DST_PORT:
2809 case OFPACT_SET_MPLS_LABEL:
2810 case OFPACT_SET_MPLS_TC:
2811 case OFPACT_SET_TUNNEL: /* Convert to a set_field, too. */
2813 switch ((int)a->type) {
2815 case OFPACT_SET_VLAN_VID:
2816 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan &&
2817 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2818 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2819 = htons(ETH_TYPE_VLAN_8021Q);
2821 field = MFF_VLAN_VID;
2822 /* Set-Field on OXM_OF_VLAN_VID must have OFPVID_PRESENT set. */
2823 value.be16 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid
2824 | OFPVID12_PRESENT);
2827 case OFPACT_SET_VLAN_PCP:
2828 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan &&
2829 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2830 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2831 = htons(ETH_TYPE_VLAN_8021Q);
2833 field = MFF_VLAN_PCP;
2834 value.u8 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2837 case OFPACT_SET_ETH_SRC:
2838 field = MFF_ETH_SRC;
2839 memcpy(value.mac, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2842 case OFPACT_SET_ETH_DST:
2843 field = MFF_ETH_DST;
2844 memcpy(value.mac, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2847 case OFPACT_SET_IPV4_SRC:
2848 field = MFF_IPV4_SRC;
2849 value.be32 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2852 case OFPACT_SET_IPV4_DST:
2853 field = MFF_IPV4_DST;
2854 value.be32 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2857 case OFPACT_SET_IP_DSCP:
2858 field = MFF_IP_DSCP_SHIFTED; /* OXM_OF_IP_DSCP */
2859 value.u8 = ofpact_get_SET_IP_DSCP(a)->dscp >> 2;
2862 case OFPACT_SET_IP_ECN:
2864 value.u8 = ofpact_get_SET_IP_ECN(a)->ecn;
2867 case OFPACT_SET_L4_SRC_PORT:
2868 /* We keep track of IP protocol while translating actions to be
2869 * able to translate to the proper OXM type.
2870 * If the IP protocol type is unknown, the translation cannot
2871 * be performed and we will send the action using the original
2873 l4port = ofpact_get_SET_L4_SRC_PORT(a);
2874 proto = l4port->flow_ip_proto;
2875 field = proto == IPPROTO_TCP ? MFF_TCP_SRC
2876 : proto == IPPROTO_UDP ? MFF_UDP_SRC
2877 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
2878 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
2879 value.be16 = htons(l4port->port);
2882 case OFPACT_SET_L4_DST_PORT:
2883 l4port = ofpact_get_SET_L4_DST_PORT(a);
2884 proto = l4port->flow_ip_proto;
2885 field = proto == IPPROTO_TCP ? MFF_TCP_DST
2886 : proto == IPPROTO_UDP ? MFF_UDP_DST
2887 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
2888 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
2889 value.be16 = htons(l4port->port);
2892 case OFPACT_SET_MPLS_LABEL:
2893 field = MFF_MPLS_LABEL;
2894 value.be32 = ofpact_get_SET_MPLS_LABEL(a)->label;
2897 case OFPACT_SET_MPLS_TC:
2898 field = MFF_MPLS_TC;
2899 value.u8 = ofpact_get_SET_MPLS_TC(a)->tc;
2902 case OFPACT_SET_TUNNEL:
2904 value.be64 = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2911 /* Put the action out as a set field action, if possible. */
2912 if (field < MFF_N_IDS) {
2913 uint64_t ofpacts_stub[128 / 8];
2914 struct ofpbuf sf_act;
2915 struct ofpact_set_field *sf;
2917 ofpbuf_use_stub(&sf_act, ofpacts_stub, sizeof ofpacts_stub);
2918 sf = ofpact_put_SET_FIELD(&sf_act);
2919 sf->field = mf_from_id(field);
2920 memcpy(&sf->value, &value, sf->field->n_bytes);
2921 set_field_to_openflow(sf, out);
2926 ofpact_to_openflow11(a, out);
2929 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
2930 * actions in 'openflow', appending the actions to any existing data in
2933 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
2934 struct ofpbuf *openflow,
2935 enum ofp_version ofp_version)
2937 const struct ofpact *a;
2938 size_t start_size = openflow->size;
2940 void (*translate)(const struct ofpact *a, struct ofpbuf *out) =
2941 (ofp_version == OFP10_VERSION) ? ofpact_to_openflow10 :
2942 (ofp_version == OFP11_VERSION) ? ofpact_to_openflow11 :
2943 ofpact_to_openflow12;
2945 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2946 translate(a, openflow);
2948 return openflow->size - start_size;
2952 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2954 struct ofp11_instruction_actions *oia;
2956 /* Update the instruction's length (or, if it's empty, delete it). */
2957 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2958 if (openflow->size > ofs + sizeof *oia) {
2959 oia->len = htons(openflow->size - ofs);
2961 openflow->size = ofs;
2966 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
2968 struct ofpbuf *openflow,
2969 enum ofp_version ofp_version)
2971 const struct ofpact *a;
2973 ovs_assert(ofp_version >= OFP11_VERSION);
2975 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2976 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2977 case OVSINST_OFPIT11_CLEAR_ACTIONS:
2978 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2981 case OVSINST_OFPIT11_GOTO_TABLE: {
2982 struct ofp11_instruction_goto_table *oigt;
2983 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2984 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2985 memset(oigt->pad, 0, sizeof oigt->pad);
2989 case OVSINST_OFPIT11_WRITE_METADATA: {
2990 const struct ofpact_metadata *om;
2991 struct ofp11_instruction_write_metadata *oiwm;
2993 om = ofpact_get_WRITE_METADATA(a);
2994 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2995 oiwm->metadata = om->metadata;
2996 oiwm->metadata_mask = om->mask;
3000 case OVSINST_OFPIT13_METER:
3001 if (ofp_version >= OFP13_VERSION) {
3002 const struct ofpact_meter *om;
3003 struct ofp13_instruction_meter *oim;
3005 om = ofpact_get_METER(a);
3006 oim = instruction_put_OFPIT13_METER(openflow);
3007 oim->meter_id = htonl(om->meter_id);
3011 case OVSINST_OFPIT11_APPLY_ACTIONS: {
3012 const size_t ofs = openflow->size;
3013 const size_t ofpacts_len_left =
3014 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
3015 const struct ofpact *action;
3016 const struct ofpact *processed = a;
3018 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
3019 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
3020 if (ovs_instruction_type_from_ofpact_type(action->type)
3021 != OVSINST_OFPIT11_APPLY_ACTIONS) {
3024 if (ofp_version == OFP11_VERSION) {
3025 ofpact_to_openflow11(action, openflow);
3027 ofpact_to_openflow12(action, openflow);
3031 ofpacts_update_instruction_actions(openflow, ofs);
3036 case OVSINST_OFPIT11_WRITE_ACTIONS: {
3037 const size_t ofs = openflow->size;
3038 const struct ofpact_nest *on;
3040 on = ofpact_get_WRITE_ACTIONS(a);
3041 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
3042 ofpacts_put_openflow_actions(on->actions,
3043 ofpact_nest_get_action_len(on),
3044 openflow, ofp_version);
3045 ofpacts_update_instruction_actions(openflow, ofs);
3053 /* Returns true if 'action' outputs to 'port', false otherwise. */
3055 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
3057 switch (ofpact->type) {
3059 return ofpact_get_OUTPUT(ofpact)->port == port;
3060 case OFPACT_ENQUEUE:
3061 return ofpact_get_ENQUEUE(ofpact)->port == port;
3062 case OFPACT_CONTROLLER:
3063 return port == OFPP_CONTROLLER;
3065 case OFPACT_OUTPUT_REG:
3067 case OFPACT_SET_VLAN_VID:
3068 case OFPACT_SET_VLAN_PCP:
3069 case OFPACT_STRIP_VLAN:
3070 case OFPACT_PUSH_VLAN:
3071 case OFPACT_SET_ETH_SRC:
3072 case OFPACT_SET_ETH_DST:
3073 case OFPACT_SET_IPV4_SRC:
3074 case OFPACT_SET_IPV4_DST:
3075 case OFPACT_SET_IP_DSCP:
3076 case OFPACT_SET_IP_ECN:
3077 case OFPACT_SET_IP_TTL:
3078 case OFPACT_SET_L4_SRC_PORT:
3079 case OFPACT_SET_L4_DST_PORT:
3080 case OFPACT_REG_MOVE:
3081 case OFPACT_REG_LOAD:
3082 case OFPACT_SET_FIELD:
3083 case OFPACT_STACK_PUSH:
3084 case OFPACT_STACK_POP:
3085 case OFPACT_DEC_TTL:
3086 case OFPACT_SET_MPLS_LABEL:
3087 case OFPACT_SET_MPLS_TC:
3088 case OFPACT_SET_MPLS_TTL:
3089 case OFPACT_DEC_MPLS_TTL:
3090 case OFPACT_SET_TUNNEL:
3091 case OFPACT_WRITE_METADATA:
3092 case OFPACT_SET_QUEUE:
3093 case OFPACT_POP_QUEUE:
3094 case OFPACT_FIN_TIMEOUT:
3095 case OFPACT_RESUBMIT:
3097 case OFPACT_MULTIPATH:
3100 case OFPACT_PUSH_MPLS:
3101 case OFPACT_POP_MPLS:
3103 case OFPACT_CLEAR_ACTIONS:
3104 case OFPACT_WRITE_ACTIONS:
3105 case OFPACT_GOTO_TABLE:
3113 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3114 * to 'port', false otherwise. */
3116 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
3119 const struct ofpact *a;
3121 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3122 if (ofpact_outputs_to_port(a, port)) {
3130 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3131 * to 'group', false otherwise. */
3133 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
3136 const struct ofpact *a;
3138 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3139 if (a->type == OFPACT_GROUP
3140 && ofpact_get_GROUP(a)->group_id == group_id) {
3149 ofpacts_equal(const struct ofpact *a, size_t a_len,
3150 const struct ofpact *b, size_t b_len)
3152 return a_len == b_len && !memcmp(a, b, a_len);
3155 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
3156 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
3158 * This function relies on the order of 'ofpacts' being correct (as checked by
3159 * ofpacts_verify()). */
3161 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
3163 const struct ofpact *a;
3165 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3166 enum ovs_instruction_type inst;
3168 inst = ovs_instruction_type_from_ofpact_type(a->type);
3169 if (a->type == OFPACT_METER) {
3170 return ofpact_get_METER(a)->meter_id;
3171 } else if (inst > OVSINST_OFPIT13_METER) {
3179 /* Formatting ofpacts. */
3182 print_note(const struct ofpact_note *note, struct ds *string)
3186 ds_put_cstr(string, "note:");
3187 for (i = 0; i < note->length; i++) {
3189 ds_put_char(string, '.');
3191 ds_put_format(string, "%02"PRIx8, note->data[i]);
3196 print_dec_ttl(const struct ofpact_cnt_ids *ids,
3201 ds_put_cstr(s, "dec_ttl");
3202 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
3203 ds_put_cstr(s, "(");
3204 for (i = 0; i < ids->n_controllers; i++) {
3206 ds_put_cstr(s, ",");
3208 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
3210 ds_put_cstr(s, ")");
3215 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
3218 ds_put_cstr(s, "fin_timeout(");
3219 if (fin_timeout->fin_idle_timeout) {
3220 ds_put_format(s, "idle_timeout=%"PRIu16",",
3221 fin_timeout->fin_idle_timeout);
3223 if (fin_timeout->fin_hard_timeout) {
3224 ds_put_format(s, "hard_timeout=%"PRIu16",",
3225 fin_timeout->fin_hard_timeout);
3228 ds_put_char(s, ')');
3232 ofpact_format(const struct ofpact *a, struct ds *s)
3234 const struct ofpact_enqueue *enqueue;
3235 const struct ofpact_resubmit *resubmit;
3236 const struct ofpact_controller *controller;
3237 const struct ofpact_metadata *metadata;
3238 const struct ofpact_tunnel *tunnel;
3239 const struct ofpact_sample *sample;
3240 const struct ofpact_set_field *set_field;
3241 const struct mf_field *mf;
3246 port = ofpact_get_OUTPUT(a)->port;
3247 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
3248 ds_put_format(s, "output:%"PRIu16, port);
3250 ofputil_format_port(port, s);
3251 if (port == OFPP_CONTROLLER) {
3252 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
3257 case OFPACT_CONTROLLER:
3258 controller = ofpact_get_CONTROLLER(a);
3259 if (controller->reason == OFPR_ACTION &&
3260 controller->controller_id == 0) {
3261 ds_put_format(s, "CONTROLLER:%"PRIu16,
3262 ofpact_get_CONTROLLER(a)->max_len);
3264 enum ofp_packet_in_reason reason = controller->reason;
3266 ds_put_cstr(s, "controller(");
3267 if (reason != OFPR_ACTION) {
3268 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3270 ds_put_format(s, "reason=%s,",
3271 ofputil_packet_in_reason_to_string(
3272 reason, reasonbuf, sizeof reasonbuf));
3274 if (controller->max_len != UINT16_MAX) {
3275 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
3277 if (controller->controller_id != 0) {
3278 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
3281 ds_put_char(s, ')');
3285 case OFPACT_ENQUEUE:
3286 enqueue = ofpact_get_ENQUEUE(a);
3287 ds_put_format(s, "enqueue:");
3288 ofputil_format_port(enqueue->port, s);
3289 ds_put_format(s, ":%"PRIu32, enqueue->queue);
3292 case OFPACT_OUTPUT_REG:
3293 ds_put_cstr(s, "output:");
3294 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
3298 bundle_format(ofpact_get_BUNDLE(a), s);
3301 case OFPACT_SET_VLAN_VID:
3302 ds_put_format(s, "%s:%"PRIu16,
3303 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_VID
3306 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
3309 case OFPACT_SET_VLAN_PCP:
3310 ds_put_format(s, "%s:%"PRIu8,
3311 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_PCP
3314 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
3317 case OFPACT_STRIP_VLAN:
3318 ds_put_cstr(s, a->compat == OFPUTIL_OFPAT11_POP_VLAN
3319 ? "pop_vlan" : "strip_vlan");
3322 case OFPACT_PUSH_VLAN:
3323 /* XXX 802.1AD case*/
3324 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
3327 case OFPACT_SET_ETH_SRC:
3328 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
3329 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
3332 case OFPACT_SET_ETH_DST:
3333 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
3334 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
3337 case OFPACT_SET_IPV4_SRC:
3338 ds_put_format(s, "mod_nw_src:"IP_FMT,
3339 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
3342 case OFPACT_SET_IPV4_DST:
3343 ds_put_format(s, "mod_nw_dst:"IP_FMT,
3344 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
3347 case OFPACT_SET_IP_DSCP:
3348 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
3351 case OFPACT_SET_IP_ECN:
3352 ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
3355 case OFPACT_SET_IP_TTL:
3356 ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
3359 case OFPACT_SET_L4_SRC_PORT:
3360 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
3363 case OFPACT_SET_L4_DST_PORT:
3364 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
3367 case OFPACT_REG_MOVE:
3368 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
3371 case OFPACT_REG_LOAD:
3372 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
3375 case OFPACT_SET_FIELD:
3376 set_field = ofpact_get_SET_FIELD(a);
3377 mf = set_field->field;
3378 ds_put_format(s, "set_field:");
3379 mf_format(mf, &set_field->value, NULL, s);
3380 ds_put_format(s, "->%s", mf->name);
3383 case OFPACT_STACK_PUSH:
3384 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
3387 case OFPACT_STACK_POP:
3388 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
3391 case OFPACT_DEC_TTL:
3392 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
3395 case OFPACT_SET_MPLS_LABEL:
3396 ds_put_format(s, "set_mpls_label(%"PRIu32")",
3397 ntohl(ofpact_get_SET_MPLS_LABEL(a)->label));
3400 case OFPACT_SET_MPLS_TC:
3401 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3402 ofpact_get_SET_MPLS_TC(a)->tc);
3405 case OFPACT_SET_MPLS_TTL:
3406 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3407 ofpact_get_SET_MPLS_TTL(a)->ttl);
3410 case OFPACT_DEC_MPLS_TTL:
3411 ds_put_cstr(s, "dec_mpls_ttl");
3414 case OFPACT_SET_TUNNEL:
3415 tunnel = ofpact_get_SET_TUNNEL(a);
3416 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3417 (tunnel->tun_id > UINT32_MAX
3418 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
3422 case OFPACT_SET_QUEUE:
3423 ds_put_format(s, "set_queue:%"PRIu32,
3424 ofpact_get_SET_QUEUE(a)->queue_id);
3427 case OFPACT_POP_QUEUE:
3428 ds_put_cstr(s, "pop_queue");
3431 case OFPACT_FIN_TIMEOUT:
3432 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
3435 case OFPACT_RESUBMIT:
3436 resubmit = ofpact_get_RESUBMIT(a);
3437 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
3438 ds_put_cstr(s, "resubmit:");
3439 ofputil_format_port(resubmit->in_port, s);
3441 ds_put_format(s, "resubmit(");
3442 if (resubmit->in_port != OFPP_IN_PORT) {
3443 ofputil_format_port(resubmit->in_port, s);
3445 ds_put_char(s, ',');
3446 if (resubmit->table_id != 255) {
3447 ds_put_format(s, "%"PRIu8, resubmit->table_id);
3449 ds_put_char(s, ')');
3454 learn_format(ofpact_get_LEARN(a), s);
3457 case OFPACT_MULTIPATH:
3458 multipath_format(ofpact_get_MULTIPATH(a), s);
3462 print_note(ofpact_get_NOTE(a), s);
3465 case OFPACT_PUSH_MPLS:
3466 ds_put_format(s, "push_mpls:0x%04"PRIx16,
3467 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
3470 case OFPACT_POP_MPLS:
3471 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
3472 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
3476 ds_put_cstr(s, "exit");
3480 sample = ofpact_get_SAMPLE(a);
3482 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
3483 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
3484 sample->probability, sample->collector_set_id,
3485 sample->obs_domain_id, sample->obs_point_id);
3488 case OFPACT_WRITE_ACTIONS: {
3489 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3490 ds_put_format(s, "%s(",
3491 ovs_instruction_name_from_type(
3492 OVSINST_OFPIT11_WRITE_ACTIONS));
3493 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
3494 ds_put_char(s, ')');
3498 case OFPACT_CLEAR_ACTIONS:
3499 ds_put_format(s, "%s",
3500 ovs_instruction_name_from_type(
3501 OVSINST_OFPIT11_CLEAR_ACTIONS));
3504 case OFPACT_WRITE_METADATA:
3505 metadata = ofpact_get_WRITE_METADATA(a);
3506 ds_put_format(s, "%s:%#"PRIx64,
3507 ovs_instruction_name_from_type(
3508 OVSINST_OFPIT11_WRITE_METADATA),
3509 ntohll(metadata->metadata));
3510 if (metadata->mask != OVS_BE64_MAX) {
3511 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
3515 case OFPACT_GOTO_TABLE:
3516 ds_put_format(s, "%s:%"PRIu8,
3517 ovs_instruction_name_from_type(
3518 OVSINST_OFPIT11_GOTO_TABLE),
3519 ofpact_get_GOTO_TABLE(a)->table_id);
3523 ds_put_format(s, "%s:%"PRIu32,
3524 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
3525 ofpact_get_METER(a)->meter_id);
3529 ds_put_format(s, "group:%"PRIu32,
3530 ofpact_get_GROUP(a)->group_id);
3535 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
3536 * 'ofpacts' to 'string'. */
3538 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
3542 ds_put_cstr(string, "drop");
3544 const struct ofpact *a;
3546 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3548 ds_put_cstr(string, ",");
3551 /* XXX write-actions */
3552 ofpact_format(a, string);
3557 /* Internal use by helpers. */
3560 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
3562 struct ofpact *ofpact;
3564 ofpact_pad(ofpacts);
3565 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
3566 ofpact_init(ofpact, type, len);
3571 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
3573 memset(ofpact, 0, len);
3574 ofpact->type = type;
3575 ofpact->compat = OFPUTIL_ACTION_INVALID;
3579 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
3580 * starting at 'ofpact'.
3582 * This is the correct way to update a variable-length ofpact's length after
3583 * adding the variable-length part of the payload. (See the large comment
3584 * near the end of ofp-actions.h for more information.) */
3586 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
3588 ovs_assert(ofpact == ofpacts->l2);
3589 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
3592 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
3593 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
3594 * client must call this itself after adding the final ofpact to an array of
3597 * (The consequences of failing to call this function are probably not dire.
3598 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
3599 * not dereference it. That's undefined behavior, technically, but it will not
3600 * cause a real problem on common systems. Still, it seems better to call
3603 ofpact_pad(struct ofpbuf *ofpacts)
3605 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
3607 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);