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 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
701 const union ofp_action *actions;
704 ofpbuf_clear(ofpacts);
706 if (actions_len % OFP_ACTION_ALIGN != 0) {
707 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
708 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
709 return OFPERR_OFPBRC_BAD_LEN;
712 actions = ofpbuf_try_pull(openflow, actions_len);
713 if (actions == NULL) {
714 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
715 "remaining message length (%zu)",
716 actions_len, openflow->size);
717 return OFPERR_OFPBRC_BAD_LEN;
720 error = ofpacts_from_openflow(actions, actions_len / OFP_ACTION_ALIGN,
723 ofpbuf_clear(ofpacts);
727 error = ofpacts_verify(ofpacts->data, ofpacts->size);
729 ofpbuf_clear(ofpacts);
735 /* OpenFlow 1.1 actions. */
737 /* Parses 'a' to determine its type. On success stores the correct type into
738 * '*code' and returns 0. On failure returns an OFPERR_* error code and
739 * '*code' is indeterminate.
741 * The caller must have already verified that 'a''s length is potentially
742 * correct (that is, a->header.len is nonzero and a multiple of
743 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
745 * This function verifies that 'a''s length is correct for the type of action
746 * that it represents. */
748 decode_openflow11_action(const union ofp_action *a,
749 enum ofputil_action_code *code)
754 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
755 return decode_nxast_action(a, code);
757 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
758 case CONSTANT_HTONS(ENUM): \
759 len = ntohs(a->header.len); \
761 ? len >= sizeof(struct STRUCT) \
762 : len == sizeof(struct STRUCT)) { \
763 *code = OFPUTIL_##ENUM; \
766 return OFPERR_OFPBAC_BAD_LEN; \
769 #include "ofp-util.def"
772 return OFPERR_OFPBAC_BAD_TYPE;
777 set_field_from_openflow(const struct ofp12_action_set_field *oasf,
778 struct ofpbuf *ofpacts)
780 uint16_t oasf_len = ntohs(oasf->len);
781 uint32_t oxm_header = ntohl(oasf->dst);
782 uint8_t oxm_length = NXM_LENGTH(oxm_header);
783 struct ofpact_set_field *sf;
784 const struct mf_field *mf;
786 /* ofp12_action_set_field is padded to 64 bits by zero */
787 if (oasf_len != ROUND_UP(sizeof *oasf + oxm_length, 8)) {
788 return OFPERR_OFPBAC_BAD_SET_LEN;
790 if (!is_all_zeros((const uint8_t *)oasf + sizeof *oasf + oxm_length,
791 oasf_len - oxm_length - sizeof *oasf)) {
792 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
795 if (NXM_HASMASK(oxm_header)) {
796 return OFPERR_OFPBAC_BAD_SET_TYPE;
798 mf = mf_from_nxm_header(oxm_header);
800 return OFPERR_OFPBAC_BAD_SET_TYPE;
802 ovs_assert(mf->n_bytes == oxm_length);
803 /* oxm_length is now validated to be compatible with mf_value. */
805 VLOG_WARN_RL(&rl, "destination field %s is not writable", mf->name);
806 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
808 sf = ofpact_put_SET_FIELD(ofpacts);
810 memcpy(&sf->value, oasf + 1, mf->n_bytes);
812 /* The value must be valid for match and must have the OFPVID_PRESENT bit
813 * on for OXM_OF_VLAN_VID. */
814 if (!mf_is_value_valid(mf, &sf->value)
815 || (mf->id == MFF_VLAN_VID
816 && !(sf->value.be16 & htons(OFPVID12_PRESENT)))) {
817 struct ds ds = DS_EMPTY_INITIALIZER;
818 mf_format(mf, &sf->value, NULL, &ds);
819 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
820 mf->name, ds_cstr(&ds));
823 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
829 set_field_to_openflow12(const struct ofpact_set_field *sf,
830 struct ofpbuf *openflow)
832 uint16_t padded_value_len = ROUND_UP(sf->field->n_bytes, 8);
833 struct ofp12_action_set_field *oasf;
836 oasf = ofputil_put_OFPAT12_SET_FIELD(openflow);
837 oasf->dst = htonl(sf->field->oxm_header);
838 oasf->len = htons(sizeof *oasf + padded_value_len);
840 value = ofpbuf_put_zeros(openflow, padded_value_len);
841 memcpy(value, &sf->value, sf->field->n_bytes);
844 /* Convert 'sf' to one or two REG_LOADs. */
846 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
848 const struct mf_field *mf = sf->field;
849 struct nx_action_reg_load *narl;
851 if (mf->n_bits > 64) {
852 ovs_assert(mf->n_bytes == 16); /* IPv6 addr. */
853 /* Split into 64bit chunks */
854 /* Lower bits first. */
855 narl = ofputil_put_NXAST_REG_LOAD(openflow);
856 narl->ofs_nbits = nxm_encode_ofs_nbits(0, 64);
857 narl->dst = htonl(mf->nxm_header);
858 memcpy(&narl->value, &sf->value.ipv6.s6_addr[8], sizeof narl->value);
859 /* Higher bits next. */
860 narl = ofputil_put_NXAST_REG_LOAD(openflow);
861 narl->ofs_nbits = nxm_encode_ofs_nbits(64, mf->n_bits - 64);
862 narl->dst = htonl(mf->nxm_header);
863 memcpy(&narl->value, &sf->value.ipv6.s6_addr[0], sizeof narl->value);
865 narl = ofputil_put_NXAST_REG_LOAD(openflow);
866 narl->ofs_nbits = nxm_encode_ofs_nbits(0, mf->n_bits);
867 narl->dst = htonl(mf->nxm_header);
868 memset(&narl->value, 0, 8 - mf->n_bytes);
869 memcpy((char*)&narl->value + (8 - mf->n_bytes),
870 &sf->value, mf->n_bytes);
874 /* Convert 'sf' to standard OpenFlow 1.1 actions, if we can, falling back
875 * to Nicira extensions if we must.
877 * We check only meta-flow types that can appear within set field actions and
878 * that have a mapping to compatible action types. These struct mf_field
879 * definitions have a defined OXM or NXM header value and specify the field as
882 set_field_to_openflow11(const struct ofpact_set_field *sf,
883 struct ofpbuf *openflow)
885 switch ((int) sf->field->id) {
887 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
889 * If CFI=1, Add or modify VLAN VID & PCP.
890 * OpenFlow 1.1 set actions only apply if the packet
891 * already has VLAN tags. To be sure that is the case
892 * we have to push a VLAN header. As we do not support
893 * multiple layers of VLANs, this is a no-op, if a VLAN
894 * header already exists. This may backfire, however,
895 * when we start supporting multiple layers of VLANs.
896 * If CFI=0, strip VLAN header, if any.
898 if (sf->value.be16 & htons(VLAN_CFI)) {
899 /* Push a VLAN tag, if one was not seen at action validation
901 if (!sf->flow_has_vlan) {
902 ofputil_put_OFPAT11_PUSH_VLAN(openflow)->ethertype
903 = htons(ETH_TYPE_VLAN_8021Q);
905 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
906 = sf->value.be16 & htons(VLAN_VID_MASK);
907 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp
908 = vlan_tci_to_pcp(sf->value.be16);
910 /* If the flow did not match on vlan, we have no way of
911 * knowing if the vlan tag exists, so we must POP just to be
913 ofputil_put_OFPAT11_POP_VLAN(openflow);
918 /* OXM VLAN_PCP to OpenFlow 1.1.
919 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
920 * tag. Clear the OFPVID_PRESENT bit.
922 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
923 = sf->value.be16 & htons(VLAN_VID_MASK);
927 /* OXM VLAN_PCP to OpenFlow 1.1.
928 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
929 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
933 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(openflow)->dl_addr,
934 sf->value.mac, ETH_ADDR_LEN);
938 memcpy(ofputil_put_OFPAT11_SET_DL_DST(openflow)->dl_addr,
939 sf->value.mac, ETH_ADDR_LEN);
943 ofputil_put_OFPAT11_SET_MPLS_LABEL(openflow)->mpls_label =
948 ofputil_put_OFPAT11_SET_MPLS_TC(openflow)->mpls_tc = sf->value.u8;
952 ofputil_put_OFPAT11_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
956 ofputil_put_OFPAT11_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
960 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
963 case MFF_IP_DSCP_SHIFTED:
964 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
968 ofputil_put_OFPAT11_SET_NW_ECN(openflow)->nw_ecn = sf->value.u8;
972 ofputil_put_OFPAT11_SET_NW_TTL(openflow)->nw_ttl = sf->value.u8;
978 ofputil_put_OFPAT11_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
984 ofputil_put_OFPAT11_SET_TP_DST(openflow)->tp_port = sf->value.be16;
988 set_field_to_nxast(sf, openflow);
993 /* Convert 'sf' to standard OpenFlow 1.0 actions, if we can, falling back
994 * to Nicira extensions if we must.
996 * We check only meta-flow types that can appear within set field actions and
997 * that have a mapping to compatible action types. These struct mf_field
998 * definitions have a defined OXM or NXM header value and specify the field as
1001 set_field_to_openflow10(const struct ofpact_set_field *sf,
1002 struct ofpbuf *openflow)
1004 switch ((int) sf->field->id) {
1006 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
1008 * If CFI=1, Add or modify VLAN VID & PCP.
1009 * If CFI=0, strip VLAN header, if any.
1011 if (sf->value.be16 & htons(VLAN_CFI)) {
1012 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1013 = sf->value.be16 & htons(VLAN_VID_MASK);
1014 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp
1015 = vlan_tci_to_pcp(sf->value.be16);
1017 ofputil_put_OFPAT10_STRIP_VLAN(openflow);
1022 /* OXM VLAN_VID to OpenFlow 1.0.
1023 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
1024 * tag. Clear the OFPVID_PRESENT bit.
1026 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1027 = sf->value.be16 & htons(VLAN_VID_MASK);
1031 /* OXM VLAN_PCP to OpenFlow 1.0.
1032 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
1033 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
1037 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(openflow)->dl_addr,
1038 sf->value.mac, ETH_ADDR_LEN);
1042 memcpy(ofputil_put_OFPAT10_SET_DL_DST(openflow)->dl_addr,
1043 sf->value.mac, ETH_ADDR_LEN);
1047 ofputil_put_OFPAT10_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
1051 ofputil_put_OFPAT10_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
1055 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
1058 case MFF_IP_DSCP_SHIFTED:
1059 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
1064 ofputil_put_OFPAT10_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
1069 ofputil_put_OFPAT10_SET_TP_DST(openflow)->tp_port = sf->value.be16;
1073 set_field_to_nxast(sf, openflow);
1079 set_field_to_openflow(const struct ofpact_set_field *sf,
1080 struct ofpbuf *openflow)
1082 struct ofp_header *oh = (struct ofp_header *)openflow->l2;
1084 if (oh->version >= OFP12_VERSION) {
1085 set_field_to_openflow12(sf, openflow);
1086 } else if (oh->version == OFP11_VERSION) {
1087 set_field_to_openflow11(sf, openflow);
1088 } else if (oh->version == OFP10_VERSION) {
1089 set_field_to_openflow10(sf, openflow);
1096 output_from_openflow11(const struct ofp11_action_output *oao,
1099 struct ofpact_output *output;
1102 output = ofpact_put_OUTPUT(out);
1103 output->max_len = ntohs(oao->max_len);
1105 error = ofputil_port_from_ofp11(oao->port, &output->port);
1110 return ofpact_check_output_port(output->port, OFPP_MAX);
1114 ofpact_from_openflow11(const union ofp_action *a, enum ofp_version version,
1117 enum ofputil_action_code code;
1119 struct ofpact_vlan_vid *vlan_vid;
1120 struct ofpact_vlan_pcp *vlan_pcp;
1122 error = decode_openflow11_action(a, &code);
1127 if (version >= OFP12_VERSION) {
1128 switch ((int)code) {
1129 case OFPUTIL_OFPAT11_SET_VLAN_VID:
1130 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1131 case OFPUTIL_OFPAT11_SET_DL_SRC:
1132 case OFPUTIL_OFPAT11_SET_DL_DST:
1133 case OFPUTIL_OFPAT11_SET_NW_SRC:
1134 case OFPUTIL_OFPAT11_SET_NW_DST:
1135 case OFPUTIL_OFPAT11_SET_NW_TOS:
1136 case OFPUTIL_OFPAT11_SET_NW_ECN:
1137 case OFPUTIL_OFPAT11_SET_TP_SRC:
1138 case OFPUTIL_OFPAT11_SET_TP_DST:
1139 VLOG_WARN_RL(&rl, "Deprecated action %s received over %s",
1140 ofputil_action_name_from_code(code),
1141 ofputil_version_to_string(version));
1146 case OFPUTIL_ACTION_INVALID:
1147 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
1148 #include "ofp-util.def"
1151 case OFPUTIL_OFPAT11_OUTPUT:
1152 return output_from_openflow11(&a->ofp11_output, out);
1154 case OFPUTIL_OFPAT11_SET_VLAN_VID:
1155 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1156 return OFPERR_OFPBAC_BAD_ARGUMENT;
1158 vlan_vid = ofpact_put_SET_VLAN_VID(out);
1159 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
1160 vlan_vid->push_vlan_if_needed = false;
1161 vlan_vid->ofpact.compat = code;
1164 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1165 if (a->vlan_pcp.vlan_pcp & ~7) {
1166 return OFPERR_OFPBAC_BAD_ARGUMENT;
1168 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1169 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
1170 vlan_pcp->push_vlan_if_needed = false;
1171 vlan_pcp->ofpact.compat = code;
1174 case OFPUTIL_OFPAT11_PUSH_VLAN:
1175 if (a->push.ethertype != htons(ETH_TYPE_VLAN_8021Q)) {
1176 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1177 return OFPERR_OFPBAC_BAD_ARGUMENT;
1179 ofpact_put_PUSH_VLAN(out);
1182 case OFPUTIL_OFPAT11_POP_VLAN:
1183 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
1186 case OFPUTIL_OFPAT11_SET_QUEUE:
1187 ofpact_put_SET_QUEUE(out)->queue_id =
1188 ntohl(a->ofp11_set_queue.queue_id);
1191 case OFPUTIL_OFPAT11_SET_DL_SRC:
1192 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
1196 case OFPUTIL_OFPAT11_SET_DL_DST:
1197 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
1201 case OFPUTIL_OFPAT11_DEC_NW_TTL:
1202 dec_ttl_from_openflow(out, code);
1205 case OFPUTIL_OFPAT11_SET_NW_SRC:
1206 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
1209 case OFPUTIL_OFPAT11_SET_NW_DST:
1210 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
1213 case OFPUTIL_OFPAT11_SET_NW_TOS:
1214 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
1215 return OFPERR_OFPBAC_BAD_ARGUMENT;
1217 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
1220 case OFPUTIL_OFPAT11_SET_NW_ECN:
1221 if (a->nw_ecn.nw_ecn & ~IP_ECN_MASK) {
1222 return OFPERR_OFPBAC_BAD_ARGUMENT;
1224 ofpact_put_SET_IP_ECN(out)->ecn = a->nw_ecn.nw_ecn;
1227 case OFPUTIL_OFPAT11_SET_NW_TTL:
1228 ofpact_put_SET_IP_TTL(out)->ttl = a->nw_ttl.nw_ttl;
1231 case OFPUTIL_OFPAT11_SET_TP_SRC:
1232 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
1235 case OFPUTIL_OFPAT11_SET_TP_DST:
1236 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
1239 case OFPUTIL_OFPAT12_SET_FIELD:
1240 return set_field_from_openflow(&a->set_field, out);
1242 case OFPUTIL_OFPAT11_SET_MPLS_LABEL:
1243 ofpact_put_SET_MPLS_LABEL(out)->label = a->ofp11_mpls_label.mpls_label;
1246 case OFPUTIL_OFPAT11_SET_MPLS_TC:
1247 ofpact_put_SET_MPLS_TC(out)->tc = a->ofp11_mpls_tc.mpls_tc;
1250 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
1251 ofpact_put_SET_MPLS_TTL(out)->ttl = a->ofp11_mpls_ttl.mpls_ttl;
1254 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
1255 ofpact_put_DEC_MPLS_TTL(out);
1258 case OFPUTIL_OFPAT11_PUSH_MPLS:
1259 /* OpenFlow 1.3 has different semantics. */
1260 error = push_mpls_from_openflow(a->push.ethertype,
1261 version >= OFP13_VERSION ?
1262 OFPACT_MPLS_BEFORE_VLAN :
1263 OFPACT_MPLS_AFTER_VLAN, out);
1266 case OFPUTIL_OFPAT11_POP_MPLS:
1267 if (eth_type_mpls(a->ofp11_pop_mpls.ethertype)) {
1268 return OFPERR_OFPBAC_BAD_ARGUMENT;
1270 ofpact_put_POP_MPLS(out)->ethertype = a->ofp11_pop_mpls.ethertype;
1273 case OFPUTIL_OFPAT11_GROUP:
1274 ofpact_put_GROUP(out)->group_id = ntohl(a->group.group_id);
1277 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
1278 #include "ofp-util.def"
1279 return ofpact_from_nxast(a, code, out);
1285 /* True if an action sets the value of a field
1286 * in a way that is compatibile with the action set.
1287 * False otherwise. */
1289 ofpact_is_set_action(const struct ofpact *a)
1292 case OFPACT_SET_FIELD:
1293 case OFPACT_REG_LOAD:
1294 case OFPACT_SET_ETH_DST:
1295 case OFPACT_SET_ETH_SRC:
1296 case OFPACT_SET_IP_DSCP:
1297 case OFPACT_SET_IP_ECN:
1298 case OFPACT_SET_IP_TTL:
1299 case OFPACT_SET_IPV4_DST:
1300 case OFPACT_SET_IPV4_SRC:
1301 case OFPACT_SET_L4_DST_PORT:
1302 case OFPACT_SET_L4_SRC_PORT:
1303 case OFPACT_SET_MPLS_LABEL:
1304 case OFPACT_SET_MPLS_TC:
1305 case OFPACT_SET_MPLS_TTL:
1306 case OFPACT_SET_QUEUE:
1307 case OFPACT_SET_TUNNEL:
1308 case OFPACT_SET_VLAN_PCP:
1309 case OFPACT_SET_VLAN_VID:
1312 case OFPACT_CLEAR_ACTIONS:
1313 case OFPACT_CONTROLLER:
1314 case OFPACT_DEC_MPLS_TTL:
1315 case OFPACT_DEC_TTL:
1316 case OFPACT_ENQUEUE:
1318 case OFPACT_FIN_TIMEOUT:
1319 case OFPACT_GOTO_TABLE:
1323 case OFPACT_MULTIPATH:
1326 case OFPACT_OUTPUT_REG:
1327 case OFPACT_POP_MPLS:
1328 case OFPACT_POP_QUEUE:
1329 case OFPACT_PUSH_MPLS:
1330 case OFPACT_PUSH_VLAN:
1331 case OFPACT_REG_MOVE:
1332 case OFPACT_RESUBMIT:
1334 case OFPACT_STACK_POP:
1335 case OFPACT_STACK_PUSH:
1336 case OFPACT_STRIP_VLAN:
1337 case OFPACT_WRITE_ACTIONS:
1338 case OFPACT_WRITE_METADATA:
1345 /* True if an action is allowed in the action set.
1346 * False otherwise. */
1348 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
1351 case OFPACT_DEC_MPLS_TTL:
1352 case OFPACT_DEC_TTL:
1355 case OFPACT_POP_MPLS:
1356 case OFPACT_PUSH_MPLS:
1357 case OFPACT_PUSH_VLAN:
1358 case OFPACT_REG_LOAD:
1359 case OFPACT_SET_FIELD:
1360 case OFPACT_SET_ETH_DST:
1361 case OFPACT_SET_ETH_SRC:
1362 case OFPACT_SET_IP_DSCP:
1363 case OFPACT_SET_IP_ECN:
1364 case OFPACT_SET_IP_TTL:
1365 case OFPACT_SET_IPV4_DST:
1366 case OFPACT_SET_IPV4_SRC:
1367 case OFPACT_SET_L4_DST_PORT:
1368 case OFPACT_SET_L4_SRC_PORT:
1369 case OFPACT_SET_MPLS_LABEL:
1370 case OFPACT_SET_MPLS_TC:
1371 case OFPACT_SET_MPLS_TTL:
1372 case OFPACT_SET_QUEUE:
1373 case OFPACT_SET_TUNNEL:
1374 case OFPACT_SET_VLAN_PCP:
1375 case OFPACT_SET_VLAN_VID:
1376 case OFPACT_STRIP_VLAN:
1379 /* In general these actions are excluded because they are not part of
1380 * the OpenFlow specification nor map to actions that are defined in
1381 * the specification. Thus the order in which they should be applied
1382 * in the action set is undefined. */
1384 case OFPACT_CONTROLLER:
1385 case OFPACT_ENQUEUE:
1387 case OFPACT_FIN_TIMEOUT:
1389 case OFPACT_MULTIPATH:
1391 case OFPACT_OUTPUT_REG:
1392 case OFPACT_POP_QUEUE:
1393 case OFPACT_REG_MOVE:
1394 case OFPACT_RESUBMIT:
1396 case OFPACT_STACK_POP:
1397 case OFPACT_STACK_PUSH:
1399 /* The action set may only include actions and thus
1400 * may not include any instructions */
1401 case OFPACT_CLEAR_ACTIONS:
1402 case OFPACT_GOTO_TABLE:
1404 case OFPACT_WRITE_ACTIONS:
1405 case OFPACT_WRITE_METADATA:
1412 /* Append ofpact 'a' onto the tail of 'out' */
1414 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1416 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1419 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1421 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1422 enum ofpact_type filter)
1424 const struct ofpact *target;
1425 const struct ofpact *a;
1428 OFPACT_FOR_EACH (a, in->data, in->size) {
1429 if (a->type == filter) {
1434 ofpact_copy(out, target);
1436 return target != NULL;
1439 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1440 * The order of appended ofpacts is preserved between 'in' and 'out' */
1442 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1443 bool (*filter)(const struct ofpact *))
1445 const struct ofpact *a;
1447 OFPACT_FOR_EACH (a, in->data, in->size) {
1449 ofpact_copy(out, a);
1454 /* Reads 'action_set', which contains ofpacts accumulated by
1455 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1456 * executed directly into 'action_list'. (These names correspond to the
1457 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1459 * In general this involves appending the last instance of each action that is
1460 * adimissible in the action set in the order described in the OpenFlow
1464 * + output action is only appended if no group action was present in 'in'.
1465 * + As a simplification all set actions are copied in the order the are
1466 * provided in 'in' as many set actions applied to a field has the same
1467 * affect as only applying the last action that sets a field and
1468 * duplicates are removed by do_xlate_actions().
1469 * This has an unwanted side-effect of compsoting multiple
1470 * LOAD_REG actions that touch different regions of the same field. */
1472 ofpacts_execute_action_set(struct ofpbuf *action_list,
1473 const struct ofpbuf *action_set)
1475 /* The OpenFlow spec "Action Set" section specifies this order. */
1476 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1477 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1478 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1479 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1480 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1481 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1482 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1483 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1485 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1486 * we should execute only OFPACT_GROUP.
1488 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1489 * all the actions because there's no point in modifying a packet that will
1490 * not be sent anywhere. */
1491 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1492 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1493 ofpbuf_clear(action_list);
1499 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1500 size_t n_in, enum ofp_version version,
1505 size_t start = out->size;
1507 error = ofpacts_from_openflow(in, n_in, version, out);
1513 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1514 if (!ofpact_is_allowed_in_actions_set(a)) {
1515 VLOG_WARN_RL(&rl, "disallowed action in action set");
1516 return OFPERR_OFPBAC_BAD_TYPE;
1524 /* OpenFlow 1.1 instructions. */
1526 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1527 static inline const struct STRUCT * OVS_UNUSED \
1528 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1530 ovs_assert(inst->type == htons(ENUM)); \
1531 return ALIGNED_CAST(struct STRUCT *, inst); \
1534 static inline void OVS_UNUSED \
1535 instruction_init_##ENUM(struct STRUCT *s) \
1537 memset(s, 0, sizeof *s); \
1538 s->type = htons(ENUM); \
1539 s->len = htons(sizeof *s); \
1542 static inline struct STRUCT * OVS_UNUSED \
1543 instruction_put_##ENUM(struct ofpbuf *buf) \
1545 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1546 instruction_init_##ENUM(s); \
1552 struct instruction_type_info {
1553 enum ovs_instruction_type type;
1557 static const struct instruction_type_info inst_info[] = {
1558 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1564 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1566 return inst_info[type].name;
1570 ovs_instruction_type_from_name(const char *name)
1572 const struct instruction_type_info *p;
1573 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1574 if (!strcasecmp(name, p->name)) {
1581 enum ovs_instruction_type
1582 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1586 return OVSINST_OFPIT13_METER;
1587 case OFPACT_CLEAR_ACTIONS:
1588 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1589 case OFPACT_WRITE_ACTIONS:
1590 return OVSINST_OFPIT11_WRITE_ACTIONS;
1591 case OFPACT_WRITE_METADATA:
1592 return OVSINST_OFPIT11_WRITE_METADATA;
1593 case OFPACT_GOTO_TABLE:
1594 return OVSINST_OFPIT11_GOTO_TABLE;
1597 case OFPACT_CONTROLLER:
1598 case OFPACT_ENQUEUE:
1599 case OFPACT_OUTPUT_REG:
1601 case OFPACT_SET_VLAN_VID:
1602 case OFPACT_SET_VLAN_PCP:
1603 case OFPACT_STRIP_VLAN:
1604 case OFPACT_PUSH_VLAN:
1605 case OFPACT_SET_ETH_SRC:
1606 case OFPACT_SET_ETH_DST:
1607 case OFPACT_SET_IPV4_SRC:
1608 case OFPACT_SET_IPV4_DST:
1609 case OFPACT_SET_IP_DSCP:
1610 case OFPACT_SET_IP_ECN:
1611 case OFPACT_SET_IP_TTL:
1612 case OFPACT_SET_L4_SRC_PORT:
1613 case OFPACT_SET_L4_DST_PORT:
1614 case OFPACT_REG_MOVE:
1615 case OFPACT_REG_LOAD:
1616 case OFPACT_SET_FIELD:
1617 case OFPACT_STACK_PUSH:
1618 case OFPACT_STACK_POP:
1619 case OFPACT_DEC_TTL:
1620 case OFPACT_SET_MPLS_LABEL:
1621 case OFPACT_SET_MPLS_TC:
1622 case OFPACT_SET_MPLS_TTL:
1623 case OFPACT_DEC_MPLS_TTL:
1624 case OFPACT_PUSH_MPLS:
1625 case OFPACT_POP_MPLS:
1626 case OFPACT_SET_TUNNEL:
1627 case OFPACT_SET_QUEUE:
1628 case OFPACT_POP_QUEUE:
1629 case OFPACT_FIN_TIMEOUT:
1630 case OFPACT_RESUBMIT:
1632 case OFPACT_MULTIPATH:
1637 return OVSINST_OFPIT11_APPLY_ACTIONS;
1641 static inline struct ofp11_instruction *
1642 instruction_next(const struct ofp11_instruction *inst)
1644 return ((struct ofp11_instruction *) (void *)
1645 ((uint8_t *) inst + ntohs(inst->len)));
1649 instruction_is_valid(const struct ofp11_instruction *inst,
1650 size_t n_instructions)
1652 uint16_t len = ntohs(inst->len);
1653 return (!(len % OFP11_INSTRUCTION_ALIGN)
1654 && len >= sizeof *inst
1655 && len / sizeof *inst <= n_instructions);
1658 /* This macro is careful to check for instructions with bad lengths. */
1659 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1660 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1661 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1662 ((LEFT) -= (ntohs((ITER)->len) \
1663 / sizeof(struct ofp11_instruction)), \
1664 (ITER) = instruction_next(ITER)))
1667 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1668 enum ovs_instruction_type *type)
1670 uint16_t len = ntohs(inst->len);
1672 switch (inst->type) {
1673 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1674 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1676 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1677 case CONSTANT_HTONS(ENUM): \
1679 ? len >= sizeof(struct STRUCT) \
1680 : len == sizeof(struct STRUCT)) { \
1681 *type = OVSINST_##ENUM; \
1684 return OFPERR_OFPBIC_BAD_LEN; \
1690 return OFPERR_OFPBIC_UNKNOWN_INST;
1695 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1697 const struct ofp11_instruction *out[])
1699 const struct ofp11_instruction *inst;
1702 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1703 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1704 enum ovs_instruction_type type;
1707 error = decode_openflow11_instruction(inst, &type);
1713 return OFPERR_ONFBIC_DUP_INSTRUCTION;
1719 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1720 (n_insts - left) * sizeof *inst);
1721 return OFPERR_OFPBIC_BAD_LEN;
1727 get_actions_from_instruction(const struct ofp11_instruction *inst,
1728 const union ofp_action **actions,
1729 size_t *max_actions)
1731 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1732 *max_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1736 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
1737 unsigned int instructions_len,
1738 enum ofp_version version,
1739 struct ofpbuf *ofpacts)
1741 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1742 const struct ofp11_instruction *instructions;
1743 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1746 ofpbuf_clear(ofpacts);
1748 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1749 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1751 instructions_len, OFP11_INSTRUCTION_ALIGN);
1752 error = OFPERR_OFPBIC_BAD_LEN;
1756 instructions = ofpbuf_try_pull(openflow, instructions_len);
1757 if (instructions == NULL) {
1758 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1759 "remaining message length (%zu)",
1760 instructions_len, openflow->size);
1761 error = OFPERR_OFPBIC_BAD_LEN;
1765 error = decode_openflow11_instructions(
1766 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1772 if (insts[OVSINST_OFPIT13_METER]) {
1773 const struct ofp13_instruction_meter *oim;
1774 struct ofpact_meter *om;
1776 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1777 insts[OVSINST_OFPIT13_METER]);
1779 om = ofpact_put_METER(ofpacts);
1780 om->meter_id = ntohl(oim->meter_id);
1782 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1783 const union ofp_action *actions;
1786 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1787 &actions, &max_actions);
1788 error = ofpacts_from_openflow(actions, max_actions, version, ofpacts);
1793 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1794 instruction_get_OFPIT11_CLEAR_ACTIONS(
1795 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1796 ofpact_put_CLEAR_ACTIONS(ofpacts);
1798 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1799 struct ofpact_nest *on;
1800 const union ofp_action *actions;
1804 ofpact_pad(ofpacts);
1805 start = ofpacts->size;
1806 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1807 offsetof(struct ofpact_nest, actions));
1808 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1809 &actions, &max_actions);
1810 error = ofpacts_from_openflow11_for_action_set(actions, max_actions,
1815 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1816 on->ofpact.len = ofpacts->size - start;
1818 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1819 const struct ofp11_instruction_write_metadata *oiwm;
1820 struct ofpact_metadata *om;
1822 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1823 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1825 om = ofpact_put_WRITE_METADATA(ofpacts);
1826 om->metadata = oiwm->metadata;
1827 om->mask = oiwm->metadata_mask;
1829 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1830 const struct ofp11_instruction_goto_table *oigt;
1831 struct ofpact_goto_table *ogt;
1833 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1834 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1835 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1836 ogt->table_id = oigt->table_id;
1839 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1842 ofpbuf_clear(ofpacts);
1847 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
1848 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
1849 * valid, otherwise an OpenFlow error code. */
1851 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
1859 case OFPP_CONTROLLER:
1865 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
1868 return OFPERR_OFPBAC_BAD_OUT_PORT;
1872 /* May modify flow->dl_type and flow->vlan_tci, caller must restore them.
1874 * Modifies some actions, filling in fields that could not be properly set
1875 * without context. */
1877 ofpact_check__(struct ofpact *a, struct flow *flow,
1878 bool enforce_consistency, ofp_port_t max_ports,
1879 uint8_t table_id, uint8_t n_tables)
1881 const struct ofpact_enqueue *enqueue;
1882 const struct mf_field *mf;
1886 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
1889 case OFPACT_CONTROLLER:
1892 case OFPACT_ENQUEUE:
1893 enqueue = ofpact_get_ENQUEUE(a);
1894 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1895 && enqueue->port != OFPP_IN_PORT
1896 && enqueue->port != OFPP_LOCAL) {
1897 return OFPERR_OFPBAC_BAD_OUT_PORT;
1901 case OFPACT_OUTPUT_REG:
1902 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1905 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1907 case OFPACT_SET_VLAN_VID:
1908 /* Remember if we saw a vlan tag in the flow to aid translating to
1909 * OpenFlow 1.1+ if need be. */
1910 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
1911 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1912 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1913 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
1916 /* Temporary mark that we have a vlan tag. */
1917 flow->vlan_tci |= htons(VLAN_CFI);
1920 case OFPACT_SET_VLAN_PCP:
1921 /* Remember if we saw a vlan tag in the flow to aid translating to
1922 * OpenFlow 1.1+ if need be. */
1923 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
1924 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1925 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1926 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
1929 /* Temporary mark that we have a vlan tag. */
1930 flow->vlan_tci |= htons(VLAN_CFI);
1933 case OFPACT_STRIP_VLAN:
1934 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
1937 /* Temporary mark that we have no vlan tag. */
1938 flow->vlan_tci = htons(0);
1941 case OFPACT_PUSH_VLAN:
1942 if (flow->vlan_tci & htons(VLAN_CFI)) {
1943 /* Multiple VLAN headers not supported. */
1944 return OFPERR_OFPBAC_BAD_TAG;
1946 /* Temporary mark that we have a vlan tag. */
1947 flow->vlan_tci |= htons(VLAN_CFI);
1950 case OFPACT_SET_ETH_SRC:
1951 case OFPACT_SET_ETH_DST:
1954 case OFPACT_SET_IPV4_SRC:
1955 case OFPACT_SET_IPV4_DST:
1956 if (flow->dl_type != htons(ETH_TYPE_IP)) {
1961 case OFPACT_SET_IP_DSCP:
1962 case OFPACT_SET_IP_ECN:
1963 case OFPACT_SET_IP_TTL:
1964 case OFPACT_DEC_TTL:
1965 if (!is_ip_any(flow)) {
1970 case OFPACT_SET_L4_SRC_PORT:
1971 if (!is_ip_any(flow) ||
1972 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1973 && flow->nw_proto != IPPROTO_SCTP)) {
1976 /* Note on which transport protocol the port numbers are set.
1977 * This allows this set action to be converted to an OF1.2 set field
1979 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
1982 case OFPACT_SET_L4_DST_PORT:
1983 if (!is_ip_any(flow) ||
1984 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1985 && flow->nw_proto != IPPROTO_SCTP)) {
1988 /* Note on which transport protocol the port numbers are set.
1989 * This allows this set action to be converted to an OF1.2 set field
1991 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
1994 case OFPACT_REG_MOVE:
1995 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1997 case OFPACT_REG_LOAD:
1998 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
2000 case OFPACT_SET_FIELD:
2001 mf = ofpact_get_SET_FIELD(a)->field;
2002 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
2003 if (!mf_are_prereqs_ok(mf, flow) ||
2004 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
2005 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
2007 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2009 /* Remember if we saw a vlan tag in the flow to aid translating to
2010 * OpenFlow 1.1 if need be. */
2011 ofpact_get_SET_FIELD(a)->flow_has_vlan =
2012 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
2013 if (mf->id == MFF_VLAN_TCI) {
2014 /* The set field may add or remove the vlan tag,
2015 * Mark the status temporarily. */
2016 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
2020 case OFPACT_STACK_PUSH:
2021 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
2023 case OFPACT_STACK_POP:
2024 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
2026 case OFPACT_SET_MPLS_LABEL:
2027 case OFPACT_SET_MPLS_TC:
2028 case OFPACT_SET_MPLS_TTL:
2029 case OFPACT_DEC_MPLS_TTL:
2030 if (!eth_type_mpls(flow->dl_type)) {
2035 case OFPACT_SET_TUNNEL:
2036 case OFPACT_SET_QUEUE:
2037 case OFPACT_POP_QUEUE:
2038 case OFPACT_RESUBMIT:
2041 case OFPACT_FIN_TIMEOUT:
2042 if (flow->nw_proto != IPPROTO_TCP) {
2048 return learn_check(ofpact_get_LEARN(a), flow);
2050 case OFPACT_MULTIPATH:
2051 return multipath_check(ofpact_get_MULTIPATH(a), flow);
2057 case OFPACT_PUSH_MPLS:
2058 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
2061 case OFPACT_POP_MPLS:
2062 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
2063 if (!eth_type_mpls(flow->dl_type)) {
2071 case OFPACT_CLEAR_ACTIONS:
2074 case OFPACT_WRITE_ACTIONS: {
2075 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2076 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
2077 flow, false, max_ports, table_id, n_tables);
2080 case OFPACT_WRITE_METADATA:
2083 case OFPACT_METER: {
2084 uint32_t mid = ofpact_get_METER(a)->meter_id;
2085 if (mid == 0 || mid > OFPM13_MAX) {
2086 return OFPERR_OFPMMFC_INVALID_METER;
2091 case OFPACT_GOTO_TABLE: {
2092 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
2093 if ((table_id != 255 && goto_table <= table_id)
2094 || (n_tables != 255 && goto_table >= n_tables)) {
2095 return OFPERR_OFPBRC_BAD_TABLE_ID;
2108 if (enforce_consistency) {
2109 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2114 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2115 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
2116 * switch with no more than 'max_ports' ports.
2118 * May annotate ofpacts with information gathered from the 'flow'.
2120 * May temporarily modify 'flow', but restores the changes before returning. */
2122 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
2123 struct flow *flow, bool enforce_consistency,
2124 ofp_port_t max_ports,
2125 uint8_t table_id, uint8_t n_tables)
2128 ovs_be16 dl_type = flow->dl_type;
2129 ovs_be16 vlan_tci = flow->vlan_tci;
2130 enum ofperr error = 0;
2132 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2133 error = ofpact_check__(a, flow, enforce_consistency,
2134 max_ports, table_id, n_tables);
2139 /* Restore fields that may have been modified. */
2140 flow->dl_type = dl_type;
2141 flow->vlan_tci = vlan_tci;
2145 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2146 * in the appropriate order as defined by the OpenFlow spec. */
2148 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
2150 const struct ofpact *a;
2151 enum ovs_instruction_type inst;
2153 inst = OVSINST_OFPIT13_METER;
2154 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2155 enum ovs_instruction_type next;
2157 next = ovs_instruction_type_from_ofpact_type(a->type);
2159 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
2162 const char *name = ovs_instruction_name_from_type(inst);
2163 const char *next_name = ovs_instruction_name_from_type(next);
2166 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
2167 "1.1+ compatibility", name);
2169 VLOG_WARN("invalid instruction ordering: %s must appear "
2170 "before %s, for OpenFlow 1.1+ compatibility",
2173 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
2182 /* Converting ofpacts to Nicira OpenFlow extensions. */
2185 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
2188 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
2190 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
2191 output_reg->src.n_bits);
2192 naor->src = htonl(output_reg->src.field->nxm_header);
2193 naor->max_len = htons(output_reg->max_len);
2197 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
2200 struct nx_action_resubmit *nar;
2202 if (resubmit->table_id == 0xff
2203 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
2204 nar = ofputil_put_NXAST_RESUBMIT(out);
2206 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
2207 nar->table = resubmit->table_id;
2209 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
2213 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
2216 uint64_t tun_id = tunnel->tun_id;
2218 if (tun_id <= UINT32_MAX
2219 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
2220 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
2222 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
2227 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
2230 struct nx_action_write_metadata *nawm;
2232 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
2233 nawm->metadata = om->metadata;
2234 nawm->mask = om->mask;
2238 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
2240 size_t start_ofs = out->size;
2241 struct nx_action_note *nan;
2242 unsigned int remainder;
2245 nan = ofputil_put_NXAST_NOTE(out);
2246 out->size -= sizeof nan->note;
2248 ofpbuf_put(out, note->data, note->length);
2250 len = out->size - start_ofs;
2251 remainder = len % OFP_ACTION_ALIGN;
2253 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
2255 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
2256 nan->len = htons(out->size - start_ofs);
2260 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
2263 struct nx_action_controller *nac;
2265 nac = ofputil_put_NXAST_CONTROLLER(out);
2266 nac->max_len = htons(oc->max_len);
2267 nac->controller_id = htons(oc->controller_id);
2268 nac->reason = oc->reason;
2272 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
2275 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
2276 ofputil_put_NXAST_DEC_TTL(out);
2278 struct nx_action_cnt_ids *nac_ids =
2279 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
2280 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
2284 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2285 nac_ids->n_controllers = htons(oc_ids->n_controllers);
2287 ids = ofpbuf_put_zeros(out, ids_len);
2288 for (i = 0; i < oc_ids->n_controllers; i++) {
2289 ids[i] = htons(oc_ids->cnt_ids[i]);
2295 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
2298 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
2299 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
2300 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
2304 ofpact_sample_to_nxast(const struct ofpact_sample *os,
2307 struct nx_action_sample *nas;
2309 nas = ofputil_put_NXAST_SAMPLE(out);
2310 nas->probability = htons(os->probability);
2311 nas->collector_set_id = htonl(os->collector_set_id);
2312 nas->obs_domain_id = htonl(os->obs_domain_id);
2313 nas->obs_point_id = htonl(os->obs_point_id);
2317 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
2320 case OFPACT_CONTROLLER:
2321 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
2324 case OFPACT_OUTPUT_REG:
2325 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
2329 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
2332 case OFPACT_REG_MOVE:
2333 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
2336 case OFPACT_REG_LOAD:
2337 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
2340 case OFPACT_STACK_PUSH:
2341 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
2344 case OFPACT_STACK_POP:
2345 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
2348 case OFPACT_DEC_TTL:
2349 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
2352 case OFPACT_SET_MPLS_LABEL:
2353 ofputil_put_NXAST_SET_MPLS_LABEL(out)->label
2354 = ofpact_get_SET_MPLS_LABEL(a)->label;
2357 case OFPACT_SET_MPLS_TC:
2358 ofputil_put_NXAST_SET_MPLS_TC(out)->tc
2359 = ofpact_get_SET_MPLS_TC(a)->tc;
2362 case OFPACT_SET_MPLS_TTL:
2363 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
2364 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2367 case OFPACT_DEC_MPLS_TTL:
2368 ofputil_put_NXAST_DEC_MPLS_TTL(out);
2371 case OFPACT_SET_TUNNEL:
2372 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
2375 case OFPACT_WRITE_METADATA:
2376 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
2379 case OFPACT_SET_QUEUE:
2380 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
2381 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2384 case OFPACT_POP_QUEUE:
2385 ofputil_put_NXAST_POP_QUEUE(out);
2388 case OFPACT_FIN_TIMEOUT:
2389 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
2392 case OFPACT_RESUBMIT:
2393 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
2397 learn_to_nxast(ofpact_get_LEARN(a), out);
2400 case OFPACT_MULTIPATH:
2401 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
2405 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
2409 ofputil_put_NXAST_EXIT(out);
2412 case OFPACT_PUSH_MPLS:
2413 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
2414 ofpact_get_PUSH_MPLS(a)->ethertype;
2417 case OFPACT_POP_MPLS:
2418 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2419 ofpact_get_POP_MPLS(a)->ethertype;
2423 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2428 case OFPACT_ENQUEUE:
2429 case OFPACT_SET_VLAN_VID:
2430 case OFPACT_SET_VLAN_PCP:
2431 case OFPACT_STRIP_VLAN:
2432 case OFPACT_PUSH_VLAN:
2433 case OFPACT_SET_ETH_SRC:
2434 case OFPACT_SET_ETH_DST:
2435 case OFPACT_SET_IPV4_SRC:
2436 case OFPACT_SET_IPV4_DST:
2437 case OFPACT_SET_IP_DSCP:
2438 case OFPACT_SET_IP_ECN:
2439 case OFPACT_SET_IP_TTL:
2440 case OFPACT_SET_L4_SRC_PORT:
2441 case OFPACT_SET_L4_DST_PORT:
2442 case OFPACT_WRITE_ACTIONS:
2443 case OFPACT_CLEAR_ACTIONS:
2444 case OFPACT_GOTO_TABLE:
2446 case OFPACT_SET_FIELD:
2451 /* Converting ofpacts to OpenFlow 1.0. */
2454 ofpact_output_to_openflow10(const struct ofpact_output *output,
2457 struct ofp10_action_output *oao;
2459 oao = ofputil_put_OFPAT10_OUTPUT(out);
2460 oao->port = htons(ofp_to_u16(output->port));
2461 oao->max_len = htons(output->max_len);
2465 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2468 struct ofp10_action_enqueue *oae;
2470 oae = ofputil_put_OFPAT10_ENQUEUE(out);
2471 oae->port = htons(ofp_to_u16(enqueue->port));
2472 oae->queue_id = htonl(enqueue->queue);
2476 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2480 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2483 case OFPACT_ENQUEUE:
2484 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2487 case OFPACT_SET_VLAN_VID:
2488 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2489 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2492 case OFPACT_SET_VLAN_PCP:
2493 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2494 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2497 case OFPACT_STRIP_VLAN:
2498 ofputil_put_OFPAT10_STRIP_VLAN(out);
2501 case OFPACT_SET_ETH_SRC:
2502 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2503 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2506 case OFPACT_SET_ETH_DST:
2507 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2508 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2511 case OFPACT_SET_IPV4_SRC:
2512 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2513 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2516 case OFPACT_SET_IPV4_DST:
2517 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2518 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2521 case OFPACT_SET_IP_DSCP:
2522 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2523 = ofpact_get_SET_IP_DSCP(a)->dscp;
2526 case OFPACT_SET_L4_SRC_PORT:
2527 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2528 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2531 case OFPACT_SET_L4_DST_PORT:
2532 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2533 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2536 case OFPACT_PUSH_VLAN:
2537 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
2538 * follow this action. */
2541 case OFPACT_CLEAR_ACTIONS:
2542 case OFPACT_WRITE_ACTIONS:
2543 case OFPACT_GOTO_TABLE:
2551 case OFPACT_SET_FIELD:
2552 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2555 case OFPACT_CONTROLLER:
2556 case OFPACT_OUTPUT_REG:
2558 case OFPACT_REG_MOVE:
2559 case OFPACT_REG_LOAD:
2560 case OFPACT_STACK_PUSH:
2561 case OFPACT_STACK_POP:
2562 case OFPACT_DEC_TTL:
2563 case OFPACT_SET_IP_ECN:
2564 case OFPACT_SET_IP_TTL:
2565 case OFPACT_SET_MPLS_LABEL:
2566 case OFPACT_SET_MPLS_TC:
2567 case OFPACT_SET_MPLS_TTL:
2568 case OFPACT_DEC_MPLS_TTL:
2569 case OFPACT_SET_TUNNEL:
2570 case OFPACT_WRITE_METADATA:
2571 case OFPACT_SET_QUEUE:
2572 case OFPACT_POP_QUEUE:
2573 case OFPACT_FIN_TIMEOUT:
2574 case OFPACT_RESUBMIT:
2576 case OFPACT_MULTIPATH:
2579 case OFPACT_PUSH_MPLS:
2580 case OFPACT_POP_MPLS:
2582 ofpact_to_nxast(a, out);
2587 /* Converting ofpacts to OpenFlow 1.1. */
2590 ofpact_output_to_openflow11(const struct ofpact_output *output,
2593 struct ofp11_action_output *oao;
2595 oao = ofputil_put_OFPAT11_OUTPUT(out);
2596 oao->port = ofputil_port_to_ofp11(output->port);
2597 oao->max_len = htons(output->max_len);
2601 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2604 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2605 && (!dec_ttl->ofpact.compat ||
2606 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2607 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2609 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2614 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2618 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2620 case OFPACT_ENQUEUE:
2624 case OFPACT_SET_VLAN_VID:
2625 /* Push a VLAN tag, if one was not seen at action validation time. */
2626 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan
2627 && ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2628 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2629 = htons(ETH_TYPE_VLAN_8021Q);
2631 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2632 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2635 case OFPACT_SET_VLAN_PCP:
2636 /* Push a VLAN tag, if one was not seen at action validation time. */
2637 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan
2638 && ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2639 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2640 = htons(ETH_TYPE_VLAN_8021Q);
2642 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2643 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2646 case OFPACT_STRIP_VLAN:
2647 ofputil_put_OFPAT11_POP_VLAN(out);
2650 case OFPACT_PUSH_VLAN:
2651 /* XXX ETH_TYPE_VLAN_8021AD case */
2652 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2653 htons(ETH_TYPE_VLAN_8021Q);
2656 case OFPACT_SET_QUEUE:
2657 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2658 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2661 case OFPACT_SET_ETH_SRC:
2662 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2663 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2666 case OFPACT_SET_ETH_DST:
2667 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2668 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2671 case OFPACT_SET_IPV4_SRC:
2672 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2673 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2676 case OFPACT_SET_IPV4_DST:
2677 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2678 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2681 case OFPACT_SET_IP_DSCP:
2682 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2683 = ofpact_get_SET_IP_DSCP(a)->dscp;
2686 case OFPACT_SET_IP_ECN:
2687 ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2688 = ofpact_get_SET_IP_ECN(a)->ecn;
2691 case OFPACT_SET_IP_TTL:
2692 ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2693 = ofpact_get_SET_IP_TTL(a)->ttl;
2696 case OFPACT_SET_L4_SRC_PORT:
2697 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2698 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2701 case OFPACT_SET_L4_DST_PORT:
2702 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2703 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2706 case OFPACT_DEC_TTL:
2707 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2710 case OFPACT_SET_MPLS_LABEL:
2711 ofputil_put_OFPAT11_SET_MPLS_LABEL(out)->mpls_label
2712 = ofpact_get_SET_MPLS_LABEL(a)->label;
2715 case OFPACT_SET_MPLS_TC:
2716 ofputil_put_OFPAT11_SET_MPLS_TC(out)->mpls_tc
2717 = ofpact_get_SET_MPLS_TC(a)->tc;
2720 case OFPACT_SET_MPLS_TTL:
2721 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2722 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2725 case OFPACT_DEC_MPLS_TTL:
2726 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2729 case OFPACT_WRITE_METADATA:
2730 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2733 case OFPACT_PUSH_MPLS:
2734 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2735 ofpact_get_PUSH_MPLS(a)->ethertype;
2738 case OFPACT_POP_MPLS:
2739 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2740 ofpact_get_POP_MPLS(a)->ethertype;
2744 case OFPACT_CLEAR_ACTIONS:
2745 case OFPACT_WRITE_ACTIONS:
2746 case OFPACT_GOTO_TABLE:
2751 ofputil_put_OFPAT11_GROUP(out)->group_id =
2752 htonl(ofpact_get_GROUP(a)->group_id);
2755 case OFPACT_SET_FIELD:
2756 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2759 case OFPACT_CONTROLLER:
2760 case OFPACT_OUTPUT_REG:
2762 case OFPACT_REG_MOVE:
2763 case OFPACT_REG_LOAD:
2764 case OFPACT_STACK_PUSH:
2765 case OFPACT_STACK_POP:
2766 case OFPACT_SET_TUNNEL:
2767 case OFPACT_POP_QUEUE:
2768 case OFPACT_FIN_TIMEOUT:
2769 case OFPACT_RESUBMIT:
2771 case OFPACT_MULTIPATH:
2775 ofpact_to_nxast(a, out);
2780 /* Output deprecated set actions as set_field actions. */
2782 ofpact_to_openflow12(const struct ofpact *a, struct ofpbuf *out)
2784 enum mf_field_id field;
2785 union mf_value value;
2786 struct ofpact_l4_port *l4port;
2790 * Convert actions deprecated in OpenFlow 1.2 to Set Field actions,
2793 switch ((int)a->type) {
2794 case OFPACT_SET_VLAN_VID:
2795 case OFPACT_SET_VLAN_PCP:
2796 case OFPACT_SET_ETH_SRC:
2797 case OFPACT_SET_ETH_DST:
2798 case OFPACT_SET_IPV4_SRC:
2799 case OFPACT_SET_IPV4_DST:
2800 case OFPACT_SET_IP_DSCP:
2801 case OFPACT_SET_IP_ECN:
2802 case OFPACT_SET_L4_SRC_PORT:
2803 case OFPACT_SET_L4_DST_PORT:
2804 case OFPACT_SET_MPLS_LABEL:
2805 case OFPACT_SET_MPLS_TC:
2806 case OFPACT_SET_TUNNEL: /* Convert to a set_field, too. */
2808 switch ((int)a->type) {
2810 case OFPACT_SET_VLAN_VID:
2811 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan &&
2812 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2813 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2814 = htons(ETH_TYPE_VLAN_8021Q);
2816 field = MFF_VLAN_VID;
2817 /* Set-Field on OXM_OF_VLAN_VID must have OFPVID_PRESENT set. */
2818 value.be16 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid
2819 | OFPVID12_PRESENT);
2822 case OFPACT_SET_VLAN_PCP:
2823 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan &&
2824 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2825 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2826 = htons(ETH_TYPE_VLAN_8021Q);
2828 field = MFF_VLAN_PCP;
2829 value.u8 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2832 case OFPACT_SET_ETH_SRC:
2833 field = MFF_ETH_SRC;
2834 memcpy(value.mac, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2837 case OFPACT_SET_ETH_DST:
2838 field = MFF_ETH_DST;
2839 memcpy(value.mac, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2842 case OFPACT_SET_IPV4_SRC:
2843 field = MFF_IPV4_SRC;
2844 value.be32 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2847 case OFPACT_SET_IPV4_DST:
2848 field = MFF_IPV4_DST;
2849 value.be32 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2852 case OFPACT_SET_IP_DSCP:
2853 field = MFF_IP_DSCP_SHIFTED; /* OXM_OF_IP_DSCP */
2854 value.u8 = ofpact_get_SET_IP_DSCP(a)->dscp >> 2;
2857 case OFPACT_SET_IP_ECN:
2859 value.u8 = ofpact_get_SET_IP_ECN(a)->ecn;
2862 case OFPACT_SET_L4_SRC_PORT:
2863 /* We keep track of IP protocol while translating actions to be
2864 * able to translate to the proper OXM type.
2865 * If the IP protocol type is unknown, the translation cannot
2866 * be performed and we will send the action using the original
2868 l4port = ofpact_get_SET_L4_SRC_PORT(a);
2869 proto = l4port->flow_ip_proto;
2870 field = proto == IPPROTO_TCP ? MFF_TCP_SRC
2871 : proto == IPPROTO_UDP ? MFF_UDP_SRC
2872 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
2873 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
2874 value.be16 = htons(l4port->port);
2877 case OFPACT_SET_L4_DST_PORT:
2878 l4port = ofpact_get_SET_L4_DST_PORT(a);
2879 proto = l4port->flow_ip_proto;
2880 field = proto == IPPROTO_TCP ? MFF_TCP_DST
2881 : proto == IPPROTO_UDP ? MFF_UDP_DST
2882 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
2883 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
2884 value.be16 = htons(l4port->port);
2887 case OFPACT_SET_MPLS_LABEL:
2888 field = MFF_MPLS_LABEL;
2889 value.be32 = ofpact_get_SET_MPLS_LABEL(a)->label;
2892 case OFPACT_SET_MPLS_TC:
2893 field = MFF_MPLS_TC;
2894 value.u8 = ofpact_get_SET_MPLS_TC(a)->tc;
2897 case OFPACT_SET_TUNNEL:
2899 value.be64 = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2906 /* Put the action out as a set field action, if possible. */
2907 if (field < MFF_N_IDS) {
2908 uint64_t ofpacts_stub[128 / 8];
2909 struct ofpbuf sf_act;
2910 struct ofpact_set_field *sf;
2912 ofpbuf_use_stub(&sf_act, ofpacts_stub, sizeof ofpacts_stub);
2913 sf = ofpact_put_SET_FIELD(&sf_act);
2914 sf->field = mf_from_id(field);
2915 memcpy(&sf->value, &value, sf->field->n_bytes);
2916 set_field_to_openflow(sf, out);
2921 ofpact_to_openflow11(a, out);
2924 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
2925 * actions in 'openflow', appending the actions to any existing data in
2928 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
2929 struct ofpbuf *openflow,
2930 enum ofp_version ofp_version)
2932 const struct ofpact *a;
2933 size_t start_size = openflow->size;
2935 void (*translate)(const struct ofpact *a, struct ofpbuf *out) =
2936 (ofp_version == OFP10_VERSION) ? ofpact_to_openflow10 :
2937 (ofp_version == OFP11_VERSION) ? ofpact_to_openflow11 :
2938 ofpact_to_openflow12;
2940 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2941 translate(a, openflow);
2943 return openflow->size - start_size;
2947 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2949 struct ofp11_instruction_actions *oia;
2951 /* Update the instruction's length (or, if it's empty, delete it). */
2952 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2953 if (openflow->size > ofs + sizeof *oia) {
2954 oia->len = htons(openflow->size - ofs);
2956 openflow->size = ofs;
2961 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
2963 struct ofpbuf *openflow,
2964 enum ofp_version ofp_version)
2966 const struct ofpact *a;
2968 ovs_assert(ofp_version >= OFP11_VERSION);
2970 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2971 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2972 case OVSINST_OFPIT11_CLEAR_ACTIONS:
2973 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2976 case OVSINST_OFPIT11_GOTO_TABLE: {
2977 struct ofp11_instruction_goto_table *oigt;
2978 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2979 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2980 memset(oigt->pad, 0, sizeof oigt->pad);
2984 case OVSINST_OFPIT11_WRITE_METADATA: {
2985 const struct ofpact_metadata *om;
2986 struct ofp11_instruction_write_metadata *oiwm;
2988 om = ofpact_get_WRITE_METADATA(a);
2989 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2990 oiwm->metadata = om->metadata;
2991 oiwm->metadata_mask = om->mask;
2995 case OVSINST_OFPIT13_METER:
2996 if (ofp_version >= OFP13_VERSION) {
2997 const struct ofpact_meter *om;
2998 struct ofp13_instruction_meter *oim;
3000 om = ofpact_get_METER(a);
3001 oim = instruction_put_OFPIT13_METER(openflow);
3002 oim->meter_id = htonl(om->meter_id);
3006 case OVSINST_OFPIT11_APPLY_ACTIONS: {
3007 const size_t ofs = openflow->size;
3008 const size_t ofpacts_len_left =
3009 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
3010 const struct ofpact *action;
3011 const struct ofpact *processed = a;
3013 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
3014 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
3015 if (ovs_instruction_type_from_ofpact_type(action->type)
3016 != OVSINST_OFPIT11_APPLY_ACTIONS) {
3019 if (ofp_version == OFP11_VERSION) {
3020 ofpact_to_openflow11(action, openflow);
3022 ofpact_to_openflow12(action, openflow);
3026 ofpacts_update_instruction_actions(openflow, ofs);
3031 case OVSINST_OFPIT11_WRITE_ACTIONS: {
3032 const size_t ofs = openflow->size;
3033 const struct ofpact_nest *on;
3035 on = ofpact_get_WRITE_ACTIONS(a);
3036 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
3037 ofpacts_put_openflow_actions(on->actions,
3038 ofpact_nest_get_action_len(on),
3039 openflow, ofp_version);
3040 ofpacts_update_instruction_actions(openflow, ofs);
3048 /* Returns true if 'action' outputs to 'port', false otherwise. */
3050 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
3052 switch (ofpact->type) {
3054 return ofpact_get_OUTPUT(ofpact)->port == port;
3055 case OFPACT_ENQUEUE:
3056 return ofpact_get_ENQUEUE(ofpact)->port == port;
3057 case OFPACT_CONTROLLER:
3058 return port == OFPP_CONTROLLER;
3060 case OFPACT_OUTPUT_REG:
3062 case OFPACT_SET_VLAN_VID:
3063 case OFPACT_SET_VLAN_PCP:
3064 case OFPACT_STRIP_VLAN:
3065 case OFPACT_PUSH_VLAN:
3066 case OFPACT_SET_ETH_SRC:
3067 case OFPACT_SET_ETH_DST:
3068 case OFPACT_SET_IPV4_SRC:
3069 case OFPACT_SET_IPV4_DST:
3070 case OFPACT_SET_IP_DSCP:
3071 case OFPACT_SET_IP_ECN:
3072 case OFPACT_SET_IP_TTL:
3073 case OFPACT_SET_L4_SRC_PORT:
3074 case OFPACT_SET_L4_DST_PORT:
3075 case OFPACT_REG_MOVE:
3076 case OFPACT_REG_LOAD:
3077 case OFPACT_SET_FIELD:
3078 case OFPACT_STACK_PUSH:
3079 case OFPACT_STACK_POP:
3080 case OFPACT_DEC_TTL:
3081 case OFPACT_SET_MPLS_LABEL:
3082 case OFPACT_SET_MPLS_TC:
3083 case OFPACT_SET_MPLS_TTL:
3084 case OFPACT_DEC_MPLS_TTL:
3085 case OFPACT_SET_TUNNEL:
3086 case OFPACT_WRITE_METADATA:
3087 case OFPACT_SET_QUEUE:
3088 case OFPACT_POP_QUEUE:
3089 case OFPACT_FIN_TIMEOUT:
3090 case OFPACT_RESUBMIT:
3092 case OFPACT_MULTIPATH:
3095 case OFPACT_PUSH_MPLS:
3096 case OFPACT_POP_MPLS:
3098 case OFPACT_CLEAR_ACTIONS:
3099 case OFPACT_WRITE_ACTIONS:
3100 case OFPACT_GOTO_TABLE:
3108 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3109 * to 'port', false otherwise. */
3111 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
3114 const struct ofpact *a;
3116 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3117 if (ofpact_outputs_to_port(a, port)) {
3125 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3126 * to 'group', false otherwise. */
3128 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
3131 const struct ofpact *a;
3133 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3134 if (a->type == OFPACT_GROUP
3135 && ofpact_get_GROUP(a)->group_id == group_id) {
3144 ofpacts_equal(const struct ofpact *a, size_t a_len,
3145 const struct ofpact *b, size_t b_len)
3147 return a_len == b_len && !memcmp(a, b, a_len);
3150 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
3151 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
3153 * This function relies on the order of 'ofpacts' being correct (as checked by
3154 * ofpacts_verify()). */
3156 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
3158 const struct ofpact *a;
3160 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3161 enum ovs_instruction_type inst;
3163 inst = ovs_instruction_type_from_ofpact_type(a->type);
3164 if (a->type == OFPACT_METER) {
3165 return ofpact_get_METER(a)->meter_id;
3166 } else if (inst > OVSINST_OFPIT13_METER) {
3174 /* Formatting ofpacts. */
3177 print_note(const struct ofpact_note *note, struct ds *string)
3181 ds_put_cstr(string, "note:");
3182 for (i = 0; i < note->length; i++) {
3184 ds_put_char(string, '.');
3186 ds_put_format(string, "%02"PRIx8, note->data[i]);
3191 print_dec_ttl(const struct ofpact_cnt_ids *ids,
3196 ds_put_cstr(s, "dec_ttl");
3197 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
3198 ds_put_cstr(s, "(");
3199 for (i = 0; i < ids->n_controllers; i++) {
3201 ds_put_cstr(s, ",");
3203 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
3205 ds_put_cstr(s, ")");
3210 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
3213 ds_put_cstr(s, "fin_timeout(");
3214 if (fin_timeout->fin_idle_timeout) {
3215 ds_put_format(s, "idle_timeout=%"PRIu16",",
3216 fin_timeout->fin_idle_timeout);
3218 if (fin_timeout->fin_hard_timeout) {
3219 ds_put_format(s, "hard_timeout=%"PRIu16",",
3220 fin_timeout->fin_hard_timeout);
3223 ds_put_char(s, ')');
3227 ofpact_format(const struct ofpact *a, struct ds *s)
3229 const struct ofpact_enqueue *enqueue;
3230 const struct ofpact_resubmit *resubmit;
3231 const struct ofpact_controller *controller;
3232 const struct ofpact_metadata *metadata;
3233 const struct ofpact_tunnel *tunnel;
3234 const struct ofpact_sample *sample;
3235 const struct ofpact_set_field *set_field;
3236 const struct mf_field *mf;
3241 port = ofpact_get_OUTPUT(a)->port;
3242 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
3243 ds_put_format(s, "output:%"PRIu16, port);
3245 ofputil_format_port(port, s);
3246 if (port == OFPP_CONTROLLER) {
3247 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
3252 case OFPACT_CONTROLLER:
3253 controller = ofpact_get_CONTROLLER(a);
3254 if (controller->reason == OFPR_ACTION &&
3255 controller->controller_id == 0) {
3256 ds_put_format(s, "CONTROLLER:%"PRIu16,
3257 ofpact_get_CONTROLLER(a)->max_len);
3259 enum ofp_packet_in_reason reason = controller->reason;
3261 ds_put_cstr(s, "controller(");
3262 if (reason != OFPR_ACTION) {
3263 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3265 ds_put_format(s, "reason=%s,",
3266 ofputil_packet_in_reason_to_string(
3267 reason, reasonbuf, sizeof reasonbuf));
3269 if (controller->max_len != UINT16_MAX) {
3270 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
3272 if (controller->controller_id != 0) {
3273 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
3276 ds_put_char(s, ')');
3280 case OFPACT_ENQUEUE:
3281 enqueue = ofpact_get_ENQUEUE(a);
3282 ds_put_format(s, "enqueue:");
3283 ofputil_format_port(enqueue->port, s);
3284 ds_put_format(s, ":%"PRIu32, enqueue->queue);
3287 case OFPACT_OUTPUT_REG:
3288 ds_put_cstr(s, "output:");
3289 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
3293 bundle_format(ofpact_get_BUNDLE(a), s);
3296 case OFPACT_SET_VLAN_VID:
3297 ds_put_format(s, "%s:%"PRIu16,
3298 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_VID
3301 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
3304 case OFPACT_SET_VLAN_PCP:
3305 ds_put_format(s, "%s:%"PRIu8,
3306 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_PCP
3309 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
3312 case OFPACT_STRIP_VLAN:
3313 ds_put_cstr(s, a->compat == OFPUTIL_OFPAT11_POP_VLAN
3314 ? "pop_vlan" : "strip_vlan");
3317 case OFPACT_PUSH_VLAN:
3318 /* XXX 802.1AD case*/
3319 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
3322 case OFPACT_SET_ETH_SRC:
3323 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
3324 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
3327 case OFPACT_SET_ETH_DST:
3328 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
3329 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
3332 case OFPACT_SET_IPV4_SRC:
3333 ds_put_format(s, "mod_nw_src:"IP_FMT,
3334 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
3337 case OFPACT_SET_IPV4_DST:
3338 ds_put_format(s, "mod_nw_dst:"IP_FMT,
3339 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
3342 case OFPACT_SET_IP_DSCP:
3343 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
3346 case OFPACT_SET_IP_ECN:
3347 ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
3350 case OFPACT_SET_IP_TTL:
3351 ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
3354 case OFPACT_SET_L4_SRC_PORT:
3355 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
3358 case OFPACT_SET_L4_DST_PORT:
3359 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
3362 case OFPACT_REG_MOVE:
3363 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
3366 case OFPACT_REG_LOAD:
3367 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
3370 case OFPACT_SET_FIELD:
3371 set_field = ofpact_get_SET_FIELD(a);
3372 mf = set_field->field;
3373 ds_put_format(s, "set_field:");
3374 mf_format(mf, &set_field->value, NULL, s);
3375 ds_put_format(s, "->%s", mf->name);
3378 case OFPACT_STACK_PUSH:
3379 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
3382 case OFPACT_STACK_POP:
3383 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
3386 case OFPACT_DEC_TTL:
3387 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
3390 case OFPACT_SET_MPLS_LABEL:
3391 ds_put_format(s, "set_mpls_label(%"PRIu32")",
3392 ntohl(ofpact_get_SET_MPLS_LABEL(a)->label));
3395 case OFPACT_SET_MPLS_TC:
3396 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3397 ofpact_get_SET_MPLS_TC(a)->tc);
3400 case OFPACT_SET_MPLS_TTL:
3401 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3402 ofpact_get_SET_MPLS_TTL(a)->ttl);
3405 case OFPACT_DEC_MPLS_TTL:
3406 ds_put_cstr(s, "dec_mpls_ttl");
3409 case OFPACT_SET_TUNNEL:
3410 tunnel = ofpact_get_SET_TUNNEL(a);
3411 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3412 (tunnel->tun_id > UINT32_MAX
3413 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
3417 case OFPACT_SET_QUEUE:
3418 ds_put_format(s, "set_queue:%"PRIu32,
3419 ofpact_get_SET_QUEUE(a)->queue_id);
3422 case OFPACT_POP_QUEUE:
3423 ds_put_cstr(s, "pop_queue");
3426 case OFPACT_FIN_TIMEOUT:
3427 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
3430 case OFPACT_RESUBMIT:
3431 resubmit = ofpact_get_RESUBMIT(a);
3432 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
3433 ds_put_cstr(s, "resubmit:");
3434 ofputil_format_port(resubmit->in_port, s);
3436 ds_put_format(s, "resubmit(");
3437 if (resubmit->in_port != OFPP_IN_PORT) {
3438 ofputil_format_port(resubmit->in_port, s);
3440 ds_put_char(s, ',');
3441 if (resubmit->table_id != 255) {
3442 ds_put_format(s, "%"PRIu8, resubmit->table_id);
3444 ds_put_char(s, ')');
3449 learn_format(ofpact_get_LEARN(a), s);
3452 case OFPACT_MULTIPATH:
3453 multipath_format(ofpact_get_MULTIPATH(a), s);
3457 print_note(ofpact_get_NOTE(a), s);
3460 case OFPACT_PUSH_MPLS:
3461 ds_put_format(s, "push_mpls:0x%04"PRIx16,
3462 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
3465 case OFPACT_POP_MPLS:
3466 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
3467 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
3471 ds_put_cstr(s, "exit");
3475 sample = ofpact_get_SAMPLE(a);
3477 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
3478 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
3479 sample->probability, sample->collector_set_id,
3480 sample->obs_domain_id, sample->obs_point_id);
3483 case OFPACT_WRITE_ACTIONS: {
3484 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3485 ds_put_format(s, "%s(",
3486 ovs_instruction_name_from_type(
3487 OVSINST_OFPIT11_WRITE_ACTIONS));
3488 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
3489 ds_put_char(s, ')');
3493 case OFPACT_CLEAR_ACTIONS:
3494 ds_put_format(s, "%s",
3495 ovs_instruction_name_from_type(
3496 OVSINST_OFPIT11_CLEAR_ACTIONS));
3499 case OFPACT_WRITE_METADATA:
3500 metadata = ofpact_get_WRITE_METADATA(a);
3501 ds_put_format(s, "%s:%#"PRIx64,
3502 ovs_instruction_name_from_type(
3503 OVSINST_OFPIT11_WRITE_METADATA),
3504 ntohll(metadata->metadata));
3505 if (metadata->mask != OVS_BE64_MAX) {
3506 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
3510 case OFPACT_GOTO_TABLE:
3511 ds_put_format(s, "%s:%"PRIu8,
3512 ovs_instruction_name_from_type(
3513 OVSINST_OFPIT11_GOTO_TABLE),
3514 ofpact_get_GOTO_TABLE(a)->table_id);
3518 ds_put_format(s, "%s:%"PRIu32,
3519 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
3520 ofpact_get_METER(a)->meter_id);
3524 ds_put_format(s, "group:%"PRIu32,
3525 ofpact_get_GROUP(a)->group_id);
3530 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
3531 * 'ofpacts' to 'string'. */
3533 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
3537 ds_put_cstr(string, "drop");
3539 const struct ofpact *a;
3541 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3543 ds_put_cstr(string, ",");
3546 /* XXX write-actions */
3547 ofpact_format(a, string);
3552 /* Internal use by helpers. */
3555 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
3557 struct ofpact *ofpact;
3559 ofpact_pad(ofpacts);
3560 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
3561 ofpact_init(ofpact, type, len);
3566 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
3568 memset(ofpact, 0, len);
3569 ofpact->type = type;
3570 ofpact->compat = OFPUTIL_ACTION_INVALID;
3574 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
3575 * starting at 'ofpact'.
3577 * This is the correct way to update a variable-length ofpact's length after
3578 * adding the variable-length part of the payload. (See the large comment
3579 * near the end of ofp-actions.h for more information.) */
3581 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
3583 ovs_assert(ofpact == ofpacts->l2);
3584 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
3587 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
3588 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
3589 * client must call this itself after adding the final ofpact to an array of
3592 * (The consequences of failing to call this function are probably not dire.
3593 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
3594 * not dereference it. That's undefined behavior, technically, but it will not
3595 * cause a real problem on common systems. Still, it seems better to call
3598 ofpact_pad(struct ofpbuf *ofpacts)
3600 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
3602 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);