2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 %"PRIuSIZE" bytes "
252 "allocated for controller ids. %"PRIuSIZE" 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, struct ofpbuf *out)
291 struct ofpact_push_mpls *oam;
293 if (!eth_type_mpls(ethertype)) {
294 return OFPERR_OFPBAC_BAD_ARGUMENT;
296 oam = ofpact_put_PUSH_MPLS(out);
297 oam->ethertype = ethertype;
303 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
305 const struct nx_action_header *nah = &a->nxa_header;
306 uint16_t len = ntohs(a->header.len);
308 if (len < sizeof(struct nx_action_header)) {
309 return OFPERR_OFPBAC_BAD_LEN;
310 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
311 return OFPERR_OFPBAC_BAD_VENDOR;
314 switch (nah->subtype) {
315 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
316 case CONSTANT_HTONS(ENUM): \
318 ? len >= sizeof(struct STRUCT) \
319 : len == sizeof(struct STRUCT)) { \
320 *code = OFPUTIL_##ENUM; \
323 return OFPERR_OFPBAC_BAD_LEN; \
326 #include "ofp-util.def"
328 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
329 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
331 return OFPERR_OFPBAC_BAD_TYPE;
335 /* Parses 'a' to determine its type. On success stores the correct type into
336 * '*code' and returns 0. On failure returns an OFPERR_* error code and
337 * '*code' is indeterminate.
339 * The caller must have already verified that 'a''s length is potentially
340 * correct (that is, a->header.len is nonzero and a multiple of
341 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
343 * This function verifies that 'a''s length is correct for the type of action
344 * that it represents. */
346 decode_openflow10_action(const union ofp_action *a,
347 enum ofputil_action_code *code)
350 case CONSTANT_HTONS(OFPAT10_VENDOR):
351 return decode_nxast_action(a, code);
353 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
354 case CONSTANT_HTONS(ENUM): \
355 if (a->header.len == htons(sizeof(struct STRUCT))) { \
356 *code = OFPUTIL_##ENUM; \
359 return OFPERR_OFPBAC_BAD_LEN; \
362 #include "ofp-util.def"
365 return OFPERR_OFPBAC_BAD_TYPE;
370 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
373 struct ofpact_tunnel *tunnel;
374 enum ofperr error = 0;
377 case OFPUTIL_ACTION_INVALID:
378 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
379 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
380 #include "ofp-util.def"
383 case OFPUTIL_NXAST_RESUBMIT:
384 resubmit_from_openflow(&a->resubmit, out);
387 case OFPUTIL_NXAST_SET_TUNNEL:
388 tunnel = ofpact_put_SET_TUNNEL(out);
389 tunnel->ofpact.compat = code;
390 tunnel->tun_id = ntohl(a->set_tunnel.tun_id);
393 case OFPUTIL_NXAST_WRITE_METADATA:
394 error = metadata_from_nxast(&a->write_metadata, out);
397 case OFPUTIL_NXAST_SET_QUEUE:
398 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(a->set_queue.queue_id);
401 case OFPUTIL_NXAST_POP_QUEUE:
402 ofpact_put_POP_QUEUE(out);
405 case OFPUTIL_NXAST_REG_MOVE:
406 error = nxm_reg_move_from_openflow(&a->reg_move, out);
409 case OFPUTIL_NXAST_REG_LOAD:
410 error = nxm_reg_load_from_openflow(&a->reg_load, out);
413 case OFPUTIL_NXAST_STACK_PUSH:
414 error = nxm_stack_push_from_openflow(&a->stack, out);
417 case OFPUTIL_NXAST_STACK_POP:
418 error = nxm_stack_pop_from_openflow(&a->stack, out);
421 case OFPUTIL_NXAST_NOTE:
422 note_from_openflow(&a->note, out);
425 case OFPUTIL_NXAST_SET_TUNNEL64:
426 tunnel = ofpact_put_SET_TUNNEL(out);
427 tunnel->ofpact.compat = code;
428 tunnel->tun_id = ntohll(a->set_tunnel64.tun_id);
431 case OFPUTIL_NXAST_MULTIPATH:
432 error = multipath_from_openflow(&a->multipath,
433 ofpact_put_MULTIPATH(out));
436 case OFPUTIL_NXAST_BUNDLE:
437 case OFPUTIL_NXAST_BUNDLE_LOAD:
438 error = bundle_from_openflow(&a->bundle, out);
441 case OFPUTIL_NXAST_OUTPUT_REG:
442 error = output_reg_from_openflow(&a->output_reg, out);
445 case OFPUTIL_NXAST_RESUBMIT_TABLE:
446 error = resubmit_table_from_openflow(&a->resubmit, out);
449 case OFPUTIL_NXAST_LEARN:
450 error = learn_from_openflow(&a->learn, out);
453 case OFPUTIL_NXAST_EXIT:
454 ofpact_put_EXIT(out);
457 case OFPUTIL_NXAST_DEC_TTL:
458 error = dec_ttl_from_openflow(out, code);
461 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
462 error = dec_ttl_cnt_ids_from_openflow(&a->cnt_ids, out);
465 case OFPUTIL_NXAST_FIN_TIMEOUT:
466 fin_timeout_from_openflow(&a->fin_timeout, out);
469 case OFPUTIL_NXAST_CONTROLLER:
470 controller_from_openflow(&a->controller, out);
473 case OFPUTIL_NXAST_PUSH_MPLS:
474 error = push_mpls_from_openflow(a->push_mpls.ethertype, out);
477 case OFPUTIL_NXAST_SET_MPLS_LABEL:
478 ofpact_put_SET_MPLS_LABEL(out)->label = a->mpls_label.label;
481 case OFPUTIL_NXAST_SET_MPLS_TC:
482 ofpact_put_SET_MPLS_TC(out)->tc = a->mpls_tc.tc;
485 case OFPUTIL_NXAST_SET_MPLS_TTL:
486 ofpact_put_SET_MPLS_TTL(out)->ttl = a->mpls_ttl.ttl;
489 case OFPUTIL_NXAST_DEC_MPLS_TTL:
490 ofpact_put_DEC_MPLS_TTL(out);
493 case OFPUTIL_NXAST_POP_MPLS:
494 if (eth_type_mpls(a->pop_mpls.ethertype)) {
495 return OFPERR_OFPBAC_BAD_ARGUMENT;
497 ofpact_put_POP_MPLS(out)->ethertype = a->pop_mpls.ethertype;
500 case OFPUTIL_NXAST_SAMPLE:
501 error = sample_from_openflow(&a->sample, out);
509 ofpact_from_openflow10(const union ofp_action *a,
510 enum ofp_version version OVS_UNUSED,
513 enum ofputil_action_code code;
515 struct ofpact_vlan_vid *vlan_vid;
516 struct ofpact_vlan_pcp *vlan_pcp;
518 error = decode_openflow10_action(a, &code);
524 case OFPUTIL_ACTION_INVALID:
525 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
526 #include "ofp-util.def"
529 case OFPUTIL_OFPAT10_OUTPUT:
530 return output_from_openflow10(&a->output10, out);
532 case OFPUTIL_OFPAT10_SET_VLAN_VID:
533 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
534 return OFPERR_OFPBAC_BAD_ARGUMENT;
536 vlan_vid = ofpact_put_SET_VLAN_VID(out);
537 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
538 vlan_vid->push_vlan_if_needed = true;
539 vlan_vid->ofpact.compat = code;
542 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
543 if (a->vlan_pcp.vlan_pcp & ~7) {
544 return OFPERR_OFPBAC_BAD_ARGUMENT;
546 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
547 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
548 vlan_pcp->push_vlan_if_needed = true;
549 vlan_pcp->ofpact.compat = code;
552 case OFPUTIL_OFPAT10_STRIP_VLAN:
553 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
556 case OFPUTIL_OFPAT10_SET_DL_SRC:
557 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
561 case OFPUTIL_OFPAT10_SET_DL_DST:
562 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
566 case OFPUTIL_OFPAT10_SET_NW_SRC:
567 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
570 case OFPUTIL_OFPAT10_SET_NW_DST:
571 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
574 case OFPUTIL_OFPAT10_SET_NW_TOS:
575 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
576 return OFPERR_OFPBAC_BAD_ARGUMENT;
578 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
581 case OFPUTIL_OFPAT10_SET_TP_SRC:
582 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
585 case OFPUTIL_OFPAT10_SET_TP_DST:
586 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
590 case OFPUTIL_OFPAT10_ENQUEUE:
591 error = enqueue_from_openflow10(&a->enqueue, out);
594 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
595 #include "ofp-util.def"
596 return ofpact_from_nxast(a, code, out);
602 static enum ofperr ofpact_from_openflow11(const union ofp_action *,
606 static inline union ofp_action *
607 action_next(const union ofp_action *a)
609 return ((union ofp_action *) (void *)
610 ((uint8_t *) a + ntohs(a->header.len)));
614 action_is_valid(const union ofp_action *a, size_t max_actions)
616 uint16_t len = ntohs(a->header.len);
617 return (!(len % OFP_ACTION_ALIGN)
618 && len >= OFP_ACTION_ALIGN
619 && len / OFP_ACTION_ALIGN <= max_actions);
622 /* This macro is careful to check for actions with bad lengths. */
623 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, MAX_ACTIONS) \
624 for ((ITER) = (ACTIONS), (LEFT) = (MAX_ACTIONS); \
625 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
626 ((LEFT) -= ntohs((ITER)->header.len) / OFP_ACTION_ALIGN, \
627 (ITER) = action_next(ITER)))
630 log_bad_action(const union ofp_action *actions, size_t max_actions,
631 const union ofp_action *bad_action, enum ofperr error)
633 if (!VLOG_DROP_WARN(&rl)) {
637 ds_put_hex_dump(&s, actions, max_actions * OFP_ACTION_ALIGN, 0, false);
638 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
639 (char *)bad_action - (char *)actions,
640 ofperr_get_name(error), ds_cstr(&s));
646 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
647 enum ofp_version version, struct ofpbuf *out)
649 const union ofp_action *a;
652 enum ofperr (*ofpact_from_openflow)(const union ofp_action *a,
654 struct ofpbuf *out) =
655 (version == OFP10_VERSION) ?
656 ofpact_from_openflow10 : ofpact_from_openflow11;
658 ACTION_FOR_EACH (a, left, in, n_in) {
659 enum ofperr error = ofpact_from_openflow(a, version, out);
661 log_bad_action(in, n_in, a, error);
666 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
667 log_bad_action(in, n_in, a, error);
675 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
676 * front of 'openflow' into ofpacts. On success, replaces any existing content
677 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
678 * Returns 0 if successful, otherwise an OpenFlow error.
680 * Actions are processed according to their OpenFlow version which
681 * is provided in the 'version' parameter.
683 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
684 * instructions, so you should call ofpacts_pull_openflow_instructions()
685 * instead of this function.
687 * The parsed actions are valid generically, but they may not be valid in a
688 * specific context. For example, port numbers up to OFPP_MAX are valid
689 * generically, but specific datapaths may only support port numbers in a
690 * smaller range. Use ofpacts_check() to additional check whether actions are
691 * valid in a specific context. */
693 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
694 unsigned int actions_len,
695 enum ofp_version version,
696 struct ofpbuf *ofpacts) {
697 const union ofp_action *actions;
700 ofpbuf_clear(ofpacts);
702 if (actions_len % OFP_ACTION_ALIGN != 0) {
703 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
704 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
705 return OFPERR_OFPBRC_BAD_LEN;
708 actions = ofpbuf_try_pull(openflow, actions_len);
709 if (actions == NULL) {
710 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
711 "remaining message length (%"PRIuSIZE")",
712 actions_len, openflow->size);
713 return OFPERR_OFPBRC_BAD_LEN;
716 error = ofpacts_from_openflow(actions, actions_len / OFP_ACTION_ALIGN,
719 ofpbuf_clear(ofpacts);
723 error = ofpacts_verify(ofpacts->data, ofpacts->size);
725 ofpbuf_clear(ofpacts);
731 /* OpenFlow 1.1 actions. */
733 /* Parses 'a' to determine its type. On success stores the correct type into
734 * '*code' and returns 0. On failure returns an OFPERR_* error code and
735 * '*code' is indeterminate.
737 * The caller must have already verified that 'a''s length is potentially
738 * correct (that is, a->header.len is nonzero and a multiple of
739 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
741 * This function verifies that 'a''s length is correct for the type of action
742 * that it represents. */
744 decode_openflow11_action(const union ofp_action *a,
745 enum ofputil_action_code *code)
750 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
751 return decode_nxast_action(a, code);
753 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
754 case CONSTANT_HTONS(ENUM): \
755 len = ntohs(a->header.len); \
757 ? len >= sizeof(struct STRUCT) \
758 : len == sizeof(struct STRUCT)) { \
759 *code = OFPUTIL_##ENUM; \
762 return OFPERR_OFPBAC_BAD_LEN; \
765 #include "ofp-util.def"
768 return OFPERR_OFPBAC_BAD_TYPE;
773 set_field_from_openflow(const struct ofp12_action_set_field *oasf,
774 struct ofpbuf *ofpacts)
776 uint16_t oasf_len = ntohs(oasf->len);
777 uint32_t oxm_header = ntohl(oasf->dst);
778 uint8_t oxm_length = NXM_LENGTH(oxm_header);
779 struct ofpact_set_field *sf;
780 const struct mf_field *mf;
782 /* ofp12_action_set_field is padded to 64 bits by zero */
783 if (oasf_len != ROUND_UP(sizeof *oasf + oxm_length, 8)) {
784 return OFPERR_OFPBAC_BAD_SET_LEN;
786 if (!is_all_zeros((const uint8_t *)oasf + sizeof *oasf + oxm_length,
787 oasf_len - oxm_length - sizeof *oasf)) {
788 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
791 if (NXM_HASMASK(oxm_header)) {
792 return OFPERR_OFPBAC_BAD_SET_TYPE;
794 mf = mf_from_nxm_header(oxm_header);
796 return OFPERR_OFPBAC_BAD_SET_TYPE;
798 ovs_assert(mf->n_bytes == oxm_length);
799 /* oxm_length is now validated to be compatible with mf_value. */
801 VLOG_WARN_RL(&rl, "destination field %s is not writable", mf->name);
802 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
804 sf = ofpact_put_SET_FIELD(ofpacts);
806 memcpy(&sf->value, oasf + 1, mf->n_bytes);
808 /* The value must be valid for match and must have the OFPVID_PRESENT bit
809 * on for OXM_OF_VLAN_VID. */
810 if (!mf_is_value_valid(mf, &sf->value)
811 || (mf->id == MFF_VLAN_VID
812 && !(sf->value.be16 & htons(OFPVID12_PRESENT)))) {
813 struct ds ds = DS_EMPTY_INITIALIZER;
814 mf_format(mf, &sf->value, NULL, &ds);
815 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
816 mf->name, ds_cstr(&ds));
819 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
825 set_field_to_openflow12(const struct ofpact_set_field *sf,
826 struct ofpbuf *openflow)
828 uint16_t padded_value_len = ROUND_UP(sf->field->n_bytes, 8);
829 struct ofp12_action_set_field *oasf;
832 oasf = ofputil_put_OFPAT12_SET_FIELD(openflow);
833 oasf->dst = htonl(sf->field->oxm_header);
834 oasf->len = htons(sizeof *oasf + padded_value_len);
836 value = ofpbuf_put_zeros(openflow, padded_value_len);
837 memcpy(value, &sf->value, sf->field->n_bytes);
840 /* Convert 'sf' to one or two REG_LOADs. */
842 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
844 const struct mf_field *mf = sf->field;
845 struct nx_action_reg_load *narl;
847 if (mf->n_bits > 64) {
848 ovs_assert(mf->n_bytes == 16); /* IPv6 addr. */
849 /* Split into 64bit chunks */
850 /* Lower bits first. */
851 narl = ofputil_put_NXAST_REG_LOAD(openflow);
852 narl->ofs_nbits = nxm_encode_ofs_nbits(0, 64);
853 narl->dst = htonl(mf->nxm_header);
854 memcpy(&narl->value, &sf->value.ipv6.s6_addr[8], sizeof narl->value);
855 /* Higher bits next. */
856 narl = ofputil_put_NXAST_REG_LOAD(openflow);
857 narl->ofs_nbits = nxm_encode_ofs_nbits(64, mf->n_bits - 64);
858 narl->dst = htonl(mf->nxm_header);
859 memcpy(&narl->value, &sf->value.ipv6.s6_addr[0], sizeof narl->value);
861 narl = ofputil_put_NXAST_REG_LOAD(openflow);
862 narl->ofs_nbits = nxm_encode_ofs_nbits(0, mf->n_bits);
863 narl->dst = htonl(mf->nxm_header);
864 memset(&narl->value, 0, 8 - mf->n_bytes);
865 memcpy((char*)&narl->value + (8 - mf->n_bytes),
866 &sf->value, mf->n_bytes);
870 /* Convert 'sf' to standard OpenFlow 1.1 actions, if we can, falling back
871 * to Nicira extensions if we must.
873 * We check only meta-flow types that can appear within set field actions and
874 * that have a mapping to compatible action types. These struct mf_field
875 * definitions have a defined OXM or NXM header value and specify the field as
878 set_field_to_openflow11(const struct ofpact_set_field *sf,
879 struct ofpbuf *openflow)
881 switch ((int) sf->field->id) {
883 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
885 * If CFI=1, Add or modify VLAN VID & PCP.
886 * OpenFlow 1.1 set actions only apply if the packet
887 * already has VLAN tags. To be sure that is the case
888 * we have to push a VLAN header. As we do not support
889 * multiple layers of VLANs, this is a no-op, if a VLAN
890 * header already exists. This may backfire, however,
891 * when we start supporting multiple layers of VLANs.
892 * If CFI=0, strip VLAN header, if any.
894 if (sf->value.be16 & htons(VLAN_CFI)) {
895 /* Push a VLAN tag, if one was not seen at action validation
897 if (!sf->flow_has_vlan) {
898 ofputil_put_OFPAT11_PUSH_VLAN(openflow)->ethertype
899 = htons(ETH_TYPE_VLAN_8021Q);
901 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
902 = sf->value.be16 & htons(VLAN_VID_MASK);
903 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp
904 = vlan_tci_to_pcp(sf->value.be16);
906 /* If the flow did not match on vlan, we have no way of
907 * knowing if the vlan tag exists, so we must POP just to be
909 ofputil_put_OFPAT11_POP_VLAN(openflow);
914 /* OXM VLAN_PCP to OpenFlow 1.1.
915 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
916 * tag. Clear the OFPVID_PRESENT bit.
918 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
919 = sf->value.be16 & htons(VLAN_VID_MASK);
923 /* OXM VLAN_PCP to OpenFlow 1.1.
924 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
925 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
929 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(openflow)->dl_addr,
930 sf->value.mac, ETH_ADDR_LEN);
934 memcpy(ofputil_put_OFPAT11_SET_DL_DST(openflow)->dl_addr,
935 sf->value.mac, ETH_ADDR_LEN);
939 ofputil_put_OFPAT11_SET_MPLS_LABEL(openflow)->mpls_label =
944 ofputil_put_OFPAT11_SET_MPLS_TC(openflow)->mpls_tc = sf->value.u8;
948 ofputil_put_OFPAT11_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
952 ofputil_put_OFPAT11_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
956 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
959 case MFF_IP_DSCP_SHIFTED:
960 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
964 ofputil_put_OFPAT11_SET_NW_ECN(openflow)->nw_ecn = sf->value.u8;
968 ofputil_put_OFPAT11_SET_NW_TTL(openflow)->nw_ttl = sf->value.u8;
974 ofputil_put_OFPAT11_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
980 ofputil_put_OFPAT11_SET_TP_DST(openflow)->tp_port = sf->value.be16;
984 set_field_to_nxast(sf, openflow);
989 /* Convert 'sf' to standard OpenFlow 1.0 actions, if we can, falling back
990 * to Nicira extensions if we must.
992 * We check only meta-flow types that can appear within set field actions and
993 * that have a mapping to compatible action types. These struct mf_field
994 * definitions have a defined OXM or NXM header value and specify the field as
997 set_field_to_openflow10(const struct ofpact_set_field *sf,
998 struct ofpbuf *openflow)
1000 switch ((int) sf->field->id) {
1002 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
1004 * If CFI=1, Add or modify VLAN VID & PCP.
1005 * If CFI=0, strip VLAN header, if any.
1007 if (sf->value.be16 & htons(VLAN_CFI)) {
1008 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1009 = sf->value.be16 & htons(VLAN_VID_MASK);
1010 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp
1011 = vlan_tci_to_pcp(sf->value.be16);
1013 ofputil_put_OFPAT10_STRIP_VLAN(openflow);
1018 /* OXM VLAN_VID to OpenFlow 1.0.
1019 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
1020 * tag. Clear the OFPVID_PRESENT bit.
1022 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1023 = sf->value.be16 & htons(VLAN_VID_MASK);
1027 /* OXM VLAN_PCP to OpenFlow 1.0.
1028 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
1029 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
1033 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(openflow)->dl_addr,
1034 sf->value.mac, ETH_ADDR_LEN);
1038 memcpy(ofputil_put_OFPAT10_SET_DL_DST(openflow)->dl_addr,
1039 sf->value.mac, ETH_ADDR_LEN);
1043 ofputil_put_OFPAT10_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
1047 ofputil_put_OFPAT10_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
1051 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
1054 case MFF_IP_DSCP_SHIFTED:
1055 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
1060 ofputil_put_OFPAT10_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
1065 ofputil_put_OFPAT10_SET_TP_DST(openflow)->tp_port = sf->value.be16;
1069 set_field_to_nxast(sf, openflow);
1075 set_field_to_openflow(const struct ofpact_set_field *sf,
1076 struct ofpbuf *openflow)
1078 struct ofp_header *oh = (struct ofp_header *)openflow->l2;
1080 if (oh->version >= OFP12_VERSION) {
1081 set_field_to_openflow12(sf, openflow);
1082 } else if (oh->version == OFP11_VERSION) {
1083 set_field_to_openflow11(sf, openflow);
1084 } else if (oh->version == OFP10_VERSION) {
1085 set_field_to_openflow10(sf, openflow);
1092 output_from_openflow11(const struct ofp11_action_output *oao,
1095 struct ofpact_output *output;
1098 output = ofpact_put_OUTPUT(out);
1099 output->max_len = ntohs(oao->max_len);
1101 error = ofputil_port_from_ofp11(oao->port, &output->port);
1106 return ofpact_check_output_port(output->port, OFPP_MAX);
1110 ofpact_from_openflow11(const union ofp_action *a, enum ofp_version version,
1113 enum ofputil_action_code code;
1115 struct ofpact_vlan_vid *vlan_vid;
1116 struct ofpact_vlan_pcp *vlan_pcp;
1118 error = decode_openflow11_action(a, &code);
1123 if (version >= OFP12_VERSION) {
1124 switch ((int)code) {
1125 case OFPUTIL_OFPAT11_SET_VLAN_VID:
1126 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1127 case OFPUTIL_OFPAT11_SET_DL_SRC:
1128 case OFPUTIL_OFPAT11_SET_DL_DST:
1129 case OFPUTIL_OFPAT11_SET_NW_SRC:
1130 case OFPUTIL_OFPAT11_SET_NW_DST:
1131 case OFPUTIL_OFPAT11_SET_NW_TOS:
1132 case OFPUTIL_OFPAT11_SET_NW_ECN:
1133 case OFPUTIL_OFPAT11_SET_TP_SRC:
1134 case OFPUTIL_OFPAT11_SET_TP_DST:
1135 VLOG_WARN_RL(&rl, "Deprecated action %s received over %s",
1136 ofputil_action_name_from_code(code),
1137 ofputil_version_to_string(version));
1142 case OFPUTIL_ACTION_INVALID:
1143 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
1144 #include "ofp-util.def"
1147 case OFPUTIL_OFPAT11_OUTPUT:
1148 return output_from_openflow11(&a->ofp11_output, out);
1150 case OFPUTIL_OFPAT11_SET_VLAN_VID:
1151 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1152 return OFPERR_OFPBAC_BAD_ARGUMENT;
1154 vlan_vid = ofpact_put_SET_VLAN_VID(out);
1155 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
1156 vlan_vid->push_vlan_if_needed = false;
1157 vlan_vid->ofpact.compat = code;
1160 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1161 if (a->vlan_pcp.vlan_pcp & ~7) {
1162 return OFPERR_OFPBAC_BAD_ARGUMENT;
1164 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1165 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
1166 vlan_pcp->push_vlan_if_needed = false;
1167 vlan_pcp->ofpact.compat = code;
1170 case OFPUTIL_OFPAT11_PUSH_VLAN:
1171 if (a->push.ethertype != htons(ETH_TYPE_VLAN_8021Q)) {
1172 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1173 return OFPERR_OFPBAC_BAD_ARGUMENT;
1175 ofpact_put_PUSH_VLAN(out);
1178 case OFPUTIL_OFPAT11_POP_VLAN:
1179 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
1182 case OFPUTIL_OFPAT11_SET_QUEUE:
1183 ofpact_put_SET_QUEUE(out)->queue_id =
1184 ntohl(a->ofp11_set_queue.queue_id);
1187 case OFPUTIL_OFPAT11_SET_DL_SRC:
1188 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
1192 case OFPUTIL_OFPAT11_SET_DL_DST:
1193 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
1197 case OFPUTIL_OFPAT11_DEC_NW_TTL:
1198 dec_ttl_from_openflow(out, code);
1201 case OFPUTIL_OFPAT11_SET_NW_SRC:
1202 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
1205 case OFPUTIL_OFPAT11_SET_NW_DST:
1206 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
1209 case OFPUTIL_OFPAT11_SET_NW_TOS:
1210 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
1211 return OFPERR_OFPBAC_BAD_ARGUMENT;
1213 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
1216 case OFPUTIL_OFPAT11_SET_NW_ECN:
1217 if (a->nw_ecn.nw_ecn & ~IP_ECN_MASK) {
1218 return OFPERR_OFPBAC_BAD_ARGUMENT;
1220 ofpact_put_SET_IP_ECN(out)->ecn = a->nw_ecn.nw_ecn;
1223 case OFPUTIL_OFPAT11_SET_NW_TTL:
1224 ofpact_put_SET_IP_TTL(out)->ttl = a->nw_ttl.nw_ttl;
1227 case OFPUTIL_OFPAT11_SET_TP_SRC:
1228 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
1231 case OFPUTIL_OFPAT11_SET_TP_DST:
1232 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
1235 case OFPUTIL_OFPAT12_SET_FIELD:
1236 return set_field_from_openflow(&a->set_field, out);
1238 case OFPUTIL_OFPAT11_SET_MPLS_LABEL:
1239 ofpact_put_SET_MPLS_LABEL(out)->label = a->ofp11_mpls_label.mpls_label;
1242 case OFPUTIL_OFPAT11_SET_MPLS_TC:
1243 ofpact_put_SET_MPLS_TC(out)->tc = a->ofp11_mpls_tc.mpls_tc;
1246 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
1247 ofpact_put_SET_MPLS_TTL(out)->ttl = a->ofp11_mpls_ttl.mpls_ttl;
1250 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
1251 ofpact_put_DEC_MPLS_TTL(out);
1254 case OFPUTIL_OFPAT11_PUSH_MPLS:
1255 error = push_mpls_from_openflow(a->push.ethertype, out);
1258 case OFPUTIL_OFPAT11_POP_MPLS:
1259 if (eth_type_mpls(a->ofp11_pop_mpls.ethertype)) {
1260 return OFPERR_OFPBAC_BAD_ARGUMENT;
1262 ofpact_put_POP_MPLS(out)->ethertype = a->ofp11_pop_mpls.ethertype;
1265 case OFPUTIL_OFPAT11_GROUP:
1266 ofpact_put_GROUP(out)->group_id = ntohl(a->group.group_id);
1269 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
1270 #include "ofp-util.def"
1271 return ofpact_from_nxast(a, code, out);
1277 /* True if an action sets the value of a field
1278 * in a way that is compatibile with the action set.
1279 * False otherwise. */
1281 ofpact_is_set_action(const struct ofpact *a)
1284 case OFPACT_SET_FIELD:
1285 case OFPACT_REG_LOAD:
1286 case OFPACT_SET_ETH_DST:
1287 case OFPACT_SET_ETH_SRC:
1288 case OFPACT_SET_IP_DSCP:
1289 case OFPACT_SET_IP_ECN:
1290 case OFPACT_SET_IP_TTL:
1291 case OFPACT_SET_IPV4_DST:
1292 case OFPACT_SET_IPV4_SRC:
1293 case OFPACT_SET_L4_DST_PORT:
1294 case OFPACT_SET_L4_SRC_PORT:
1295 case OFPACT_SET_MPLS_LABEL:
1296 case OFPACT_SET_MPLS_TC:
1297 case OFPACT_SET_MPLS_TTL:
1298 case OFPACT_SET_QUEUE:
1299 case OFPACT_SET_TUNNEL:
1300 case OFPACT_SET_VLAN_PCP:
1301 case OFPACT_SET_VLAN_VID:
1304 case OFPACT_CLEAR_ACTIONS:
1305 case OFPACT_CONTROLLER:
1306 case OFPACT_DEC_MPLS_TTL:
1307 case OFPACT_DEC_TTL:
1308 case OFPACT_ENQUEUE:
1310 case OFPACT_FIN_TIMEOUT:
1311 case OFPACT_GOTO_TABLE:
1315 case OFPACT_MULTIPATH:
1318 case OFPACT_OUTPUT_REG:
1319 case OFPACT_POP_MPLS:
1320 case OFPACT_POP_QUEUE:
1321 case OFPACT_PUSH_MPLS:
1322 case OFPACT_PUSH_VLAN:
1323 case OFPACT_REG_MOVE:
1324 case OFPACT_RESUBMIT:
1326 case OFPACT_STACK_POP:
1327 case OFPACT_STACK_PUSH:
1328 case OFPACT_STRIP_VLAN:
1329 case OFPACT_WRITE_ACTIONS:
1330 case OFPACT_WRITE_METADATA:
1337 /* True if an action is allowed in the action set.
1338 * False otherwise. */
1340 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
1343 case OFPACT_DEC_MPLS_TTL:
1344 case OFPACT_DEC_TTL:
1347 case OFPACT_POP_MPLS:
1348 case OFPACT_PUSH_MPLS:
1349 case OFPACT_PUSH_VLAN:
1350 case OFPACT_REG_LOAD:
1351 case OFPACT_SET_FIELD:
1352 case OFPACT_SET_ETH_DST:
1353 case OFPACT_SET_ETH_SRC:
1354 case OFPACT_SET_IP_DSCP:
1355 case OFPACT_SET_IP_ECN:
1356 case OFPACT_SET_IP_TTL:
1357 case OFPACT_SET_IPV4_DST:
1358 case OFPACT_SET_IPV4_SRC:
1359 case OFPACT_SET_L4_DST_PORT:
1360 case OFPACT_SET_L4_SRC_PORT:
1361 case OFPACT_SET_MPLS_LABEL:
1362 case OFPACT_SET_MPLS_TC:
1363 case OFPACT_SET_MPLS_TTL:
1364 case OFPACT_SET_QUEUE:
1365 case OFPACT_SET_TUNNEL:
1366 case OFPACT_SET_VLAN_PCP:
1367 case OFPACT_SET_VLAN_VID:
1368 case OFPACT_STRIP_VLAN:
1371 /* In general these actions are excluded because they are not part of
1372 * the OpenFlow specification nor map to actions that are defined in
1373 * the specification. Thus the order in which they should be applied
1374 * in the action set is undefined. */
1376 case OFPACT_CONTROLLER:
1377 case OFPACT_ENQUEUE:
1379 case OFPACT_FIN_TIMEOUT:
1381 case OFPACT_MULTIPATH:
1383 case OFPACT_OUTPUT_REG:
1384 case OFPACT_POP_QUEUE:
1385 case OFPACT_REG_MOVE:
1386 case OFPACT_RESUBMIT:
1388 case OFPACT_STACK_POP:
1389 case OFPACT_STACK_PUSH:
1391 /* The action set may only include actions and thus
1392 * may not include any instructions */
1393 case OFPACT_CLEAR_ACTIONS:
1394 case OFPACT_GOTO_TABLE:
1396 case OFPACT_WRITE_ACTIONS:
1397 case OFPACT_WRITE_METADATA:
1404 /* Append ofpact 'a' onto the tail of 'out' */
1406 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1408 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1411 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1413 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1414 enum ofpact_type filter)
1416 const struct ofpact *target;
1417 const struct ofpact *a;
1420 OFPACT_FOR_EACH (a, in->data, in->size) {
1421 if (a->type == filter) {
1426 ofpact_copy(out, target);
1428 return target != NULL;
1431 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1432 * The order of appended ofpacts is preserved between 'in' and 'out' */
1434 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1435 bool (*filter)(const struct ofpact *))
1437 const struct ofpact *a;
1439 OFPACT_FOR_EACH (a, in->data, in->size) {
1441 ofpact_copy(out, a);
1446 /* Reads 'action_set', which contains ofpacts accumulated by
1447 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1448 * executed directly into 'action_list'. (These names correspond to the
1449 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1451 * In general this involves appending the last instance of each action that is
1452 * adimissible in the action set in the order described in the OpenFlow
1456 * + output action is only appended if no group action was present in 'in'.
1457 * + As a simplification all set actions are copied in the order the are
1458 * provided in 'in' as many set actions applied to a field has the same
1459 * affect as only applying the last action that sets a field and
1460 * duplicates are removed by do_xlate_actions().
1461 * This has an unwanted side-effect of compsoting multiple
1462 * LOAD_REG actions that touch different regions of the same field. */
1464 ofpacts_execute_action_set(struct ofpbuf *action_list,
1465 const struct ofpbuf *action_set)
1467 /* The OpenFlow spec "Action Set" section specifies this order. */
1468 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1469 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1470 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1471 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1472 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1473 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1474 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1475 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1477 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1478 * we should execute only OFPACT_GROUP.
1480 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1481 * all the actions because there's no point in modifying a packet that will
1482 * not be sent anywhere. */
1483 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1484 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1485 ofpbuf_clear(action_list);
1491 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1492 size_t n_in, enum ofp_version version,
1497 size_t start = out->size;
1499 error = ofpacts_from_openflow(in, n_in, version, out);
1505 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1506 if (!ofpact_is_allowed_in_actions_set(a)) {
1507 VLOG_WARN_RL(&rl, "disallowed action in action set");
1508 return OFPERR_OFPBAC_BAD_TYPE;
1516 /* OpenFlow 1.1 instructions. */
1518 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1519 static inline const struct STRUCT * OVS_UNUSED \
1520 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1522 ovs_assert(inst->type == htons(ENUM)); \
1523 return ALIGNED_CAST(struct STRUCT *, inst); \
1526 static inline void OVS_UNUSED \
1527 instruction_init_##ENUM(struct STRUCT *s) \
1529 memset(s, 0, sizeof *s); \
1530 s->type = htons(ENUM); \
1531 s->len = htons(sizeof *s); \
1534 static inline struct STRUCT * OVS_UNUSED \
1535 instruction_put_##ENUM(struct ofpbuf *buf) \
1537 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1538 instruction_init_##ENUM(s); \
1544 struct instruction_type_info {
1545 enum ovs_instruction_type type;
1549 static const struct instruction_type_info inst_info[] = {
1550 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1556 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1558 return inst_info[type].name;
1562 ovs_instruction_type_from_name(const char *name)
1564 const struct instruction_type_info *p;
1565 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1566 if (!strcasecmp(name, p->name)) {
1573 enum ovs_instruction_type
1574 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1578 return OVSINST_OFPIT13_METER;
1579 case OFPACT_CLEAR_ACTIONS:
1580 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1581 case OFPACT_WRITE_ACTIONS:
1582 return OVSINST_OFPIT11_WRITE_ACTIONS;
1583 case OFPACT_WRITE_METADATA:
1584 return OVSINST_OFPIT11_WRITE_METADATA;
1585 case OFPACT_GOTO_TABLE:
1586 return OVSINST_OFPIT11_GOTO_TABLE;
1589 case OFPACT_CONTROLLER:
1590 case OFPACT_ENQUEUE:
1591 case OFPACT_OUTPUT_REG:
1593 case OFPACT_SET_VLAN_VID:
1594 case OFPACT_SET_VLAN_PCP:
1595 case OFPACT_STRIP_VLAN:
1596 case OFPACT_PUSH_VLAN:
1597 case OFPACT_SET_ETH_SRC:
1598 case OFPACT_SET_ETH_DST:
1599 case OFPACT_SET_IPV4_SRC:
1600 case OFPACT_SET_IPV4_DST:
1601 case OFPACT_SET_IP_DSCP:
1602 case OFPACT_SET_IP_ECN:
1603 case OFPACT_SET_IP_TTL:
1604 case OFPACT_SET_L4_SRC_PORT:
1605 case OFPACT_SET_L4_DST_PORT:
1606 case OFPACT_REG_MOVE:
1607 case OFPACT_REG_LOAD:
1608 case OFPACT_SET_FIELD:
1609 case OFPACT_STACK_PUSH:
1610 case OFPACT_STACK_POP:
1611 case OFPACT_DEC_TTL:
1612 case OFPACT_SET_MPLS_LABEL:
1613 case OFPACT_SET_MPLS_TC:
1614 case OFPACT_SET_MPLS_TTL:
1615 case OFPACT_DEC_MPLS_TTL:
1616 case OFPACT_PUSH_MPLS:
1617 case OFPACT_POP_MPLS:
1618 case OFPACT_SET_TUNNEL:
1619 case OFPACT_SET_QUEUE:
1620 case OFPACT_POP_QUEUE:
1621 case OFPACT_FIN_TIMEOUT:
1622 case OFPACT_RESUBMIT:
1624 case OFPACT_MULTIPATH:
1629 return OVSINST_OFPIT11_APPLY_ACTIONS;
1633 static inline struct ofp11_instruction *
1634 instruction_next(const struct ofp11_instruction *inst)
1636 return ((struct ofp11_instruction *) (void *)
1637 ((uint8_t *) inst + ntohs(inst->len)));
1641 instruction_is_valid(const struct ofp11_instruction *inst,
1642 size_t n_instructions)
1644 uint16_t len = ntohs(inst->len);
1645 return (!(len % OFP11_INSTRUCTION_ALIGN)
1646 && len >= sizeof *inst
1647 && len / sizeof *inst <= n_instructions);
1650 /* This macro is careful to check for instructions with bad lengths. */
1651 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1652 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1653 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1654 ((LEFT) -= (ntohs((ITER)->len) \
1655 / sizeof(struct ofp11_instruction)), \
1656 (ITER) = instruction_next(ITER)))
1659 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1660 enum ovs_instruction_type *type)
1662 uint16_t len = ntohs(inst->len);
1664 switch (inst->type) {
1665 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1666 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1668 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1669 case CONSTANT_HTONS(ENUM): \
1671 ? len >= sizeof(struct STRUCT) \
1672 : len == sizeof(struct STRUCT)) { \
1673 *type = OVSINST_##ENUM; \
1676 return OFPERR_OFPBIC_BAD_LEN; \
1682 return OFPERR_OFPBIC_UNKNOWN_INST;
1687 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1689 const struct ofp11_instruction *out[])
1691 const struct ofp11_instruction *inst;
1694 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1695 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1696 enum ovs_instruction_type type;
1699 error = decode_openflow11_instruction(inst, &type);
1705 return OFPERR_ONFBIC_DUP_INSTRUCTION;
1711 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
1712 (n_insts - left) * sizeof *inst);
1713 return OFPERR_OFPBIC_BAD_LEN;
1719 get_actions_from_instruction(const struct ofp11_instruction *inst,
1720 const union ofp_action **actions,
1721 size_t *max_actions)
1723 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1724 *max_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1728 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
1729 unsigned int instructions_len,
1730 enum ofp_version version,
1731 struct ofpbuf *ofpacts)
1733 const struct ofp11_instruction *instructions;
1734 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1737 ofpbuf_clear(ofpacts);
1739 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1740 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1742 instructions_len, OFP11_INSTRUCTION_ALIGN);
1743 error = OFPERR_OFPBIC_BAD_LEN;
1747 instructions = ofpbuf_try_pull(openflow, instructions_len);
1748 if (instructions == NULL) {
1749 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1750 "remaining message length (%"PRIuSIZE")",
1751 instructions_len, openflow->size);
1752 error = OFPERR_OFPBIC_BAD_LEN;
1756 error = decode_openflow11_instructions(
1757 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1763 if (insts[OVSINST_OFPIT13_METER]) {
1764 const struct ofp13_instruction_meter *oim;
1765 struct ofpact_meter *om;
1767 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1768 insts[OVSINST_OFPIT13_METER]);
1770 om = ofpact_put_METER(ofpacts);
1771 om->meter_id = ntohl(oim->meter_id);
1773 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1774 const union ofp_action *actions;
1777 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1778 &actions, &max_actions);
1779 error = ofpacts_from_openflow(actions, max_actions, version, ofpacts);
1784 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1785 instruction_get_OFPIT11_CLEAR_ACTIONS(
1786 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1787 ofpact_put_CLEAR_ACTIONS(ofpacts);
1789 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1790 struct ofpact_nest *on;
1791 const union ofp_action *actions;
1795 ofpact_pad(ofpacts);
1796 start = ofpacts->size;
1797 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1798 offsetof(struct ofpact_nest, actions));
1799 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1800 &actions, &max_actions);
1801 error = ofpacts_from_openflow11_for_action_set(actions, max_actions,
1806 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1807 on->ofpact.len = ofpacts->size - start;
1809 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1810 const struct ofp11_instruction_write_metadata *oiwm;
1811 struct ofpact_metadata *om;
1813 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1814 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1816 om = ofpact_put_WRITE_METADATA(ofpacts);
1817 om->metadata = oiwm->metadata;
1818 om->mask = oiwm->metadata_mask;
1820 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1821 const struct ofp11_instruction_goto_table *oigt;
1822 struct ofpact_goto_table *ogt;
1824 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1825 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1826 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1827 ogt->table_id = oigt->table_id;
1830 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1833 ofpbuf_clear(ofpacts);
1838 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
1839 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
1840 * valid, otherwise an OpenFlow error code. */
1842 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
1850 case OFPP_CONTROLLER:
1856 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
1859 return OFPERR_OFPBAC_BAD_OUT_PORT;
1863 /* Removes the protocols that require consistency between match and actions
1864 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
1866 * (An example of an inconsistency between match and actions is a flow that
1867 * does not match on an MPLS Ethertype but has an action that pops an MPLS
1870 inconsistent_match(enum ofputil_protocol *usable_protocols)
1872 *usable_protocols &= OFPUTIL_P_OF10_ANY;
1875 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
1876 * caller must restore them.
1878 * Modifies some actions, filling in fields that could not be properly set
1879 * without context. */
1881 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
1882 struct flow *flow, ofp_port_t max_ports,
1883 uint8_t table_id, uint8_t n_tables)
1885 const struct ofpact_enqueue *enqueue;
1886 const struct mf_field *mf;
1890 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
1893 case OFPACT_CONTROLLER:
1896 case OFPACT_ENQUEUE:
1897 enqueue = ofpact_get_ENQUEUE(a);
1898 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1899 && enqueue->port != OFPP_IN_PORT
1900 && enqueue->port != OFPP_LOCAL) {
1901 return OFPERR_OFPBAC_BAD_OUT_PORT;
1905 case OFPACT_OUTPUT_REG:
1906 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1909 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1911 case OFPACT_SET_VLAN_VID:
1912 /* Remember if we saw a vlan tag in the flow to aid translating to
1913 * OpenFlow 1.1+ if need be. */
1914 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
1915 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1916 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1917 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
1918 inconsistent_match(usable_protocols);
1920 /* Temporary mark that we have a vlan tag. */
1921 flow->vlan_tci |= htons(VLAN_CFI);
1924 case OFPACT_SET_VLAN_PCP:
1925 /* Remember if we saw a vlan tag in the flow to aid translating to
1926 * OpenFlow 1.1+ if need be. */
1927 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
1928 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1929 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1930 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
1931 inconsistent_match(usable_protocols);
1933 /* Temporary mark that we have a vlan tag. */
1934 flow->vlan_tci |= htons(VLAN_CFI);
1937 case OFPACT_STRIP_VLAN:
1938 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
1939 inconsistent_match(usable_protocols);
1941 /* Temporary mark that we have no vlan tag. */
1942 flow->vlan_tci = htons(0);
1945 case OFPACT_PUSH_VLAN:
1946 if (flow->vlan_tci & htons(VLAN_CFI)) {
1947 /* Multiple VLAN headers not supported. */
1948 return OFPERR_OFPBAC_BAD_TAG;
1950 /* Temporary mark that we have a vlan tag. */
1951 flow->vlan_tci |= htons(VLAN_CFI);
1954 case OFPACT_SET_ETH_SRC:
1955 case OFPACT_SET_ETH_DST:
1958 case OFPACT_SET_IPV4_SRC:
1959 case OFPACT_SET_IPV4_DST:
1960 if (flow->dl_type != htons(ETH_TYPE_IP)) {
1961 inconsistent_match(usable_protocols);
1965 case OFPACT_SET_IP_DSCP:
1966 case OFPACT_SET_IP_ECN:
1967 case OFPACT_SET_IP_TTL:
1968 case OFPACT_DEC_TTL:
1969 if (!is_ip_any(flow)) {
1970 inconsistent_match(usable_protocols);
1974 case OFPACT_SET_L4_SRC_PORT:
1975 if (!is_ip_any(flow) ||
1976 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1977 && flow->nw_proto != IPPROTO_SCTP)) {
1978 inconsistent_match(usable_protocols);
1980 /* Note on which transport protocol the port numbers are set.
1981 * This allows this set action to be converted to an OF1.2 set field
1983 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
1986 case OFPACT_SET_L4_DST_PORT:
1987 if (!is_ip_any(flow) ||
1988 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1989 && flow->nw_proto != IPPROTO_SCTP)) {
1990 inconsistent_match(usable_protocols);
1992 /* Note on which transport protocol the port numbers are set.
1993 * This allows this set action to be converted to an OF1.2 set field
1995 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
1998 case OFPACT_REG_MOVE:
1999 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
2001 case OFPACT_REG_LOAD:
2002 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
2004 case OFPACT_SET_FIELD:
2005 mf = ofpact_get_SET_FIELD(a)->field;
2006 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
2007 if (!mf_are_prereqs_ok(mf, flow) ||
2008 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
2009 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
2011 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2013 /* Remember if we saw a vlan tag in the flow to aid translating to
2014 * OpenFlow 1.1 if need be. */
2015 ofpact_get_SET_FIELD(a)->flow_has_vlan =
2016 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
2017 if (mf->id == MFF_VLAN_TCI) {
2018 /* The set field may add or remove the vlan tag,
2019 * Mark the status temporarily. */
2020 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
2024 case OFPACT_STACK_PUSH:
2025 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
2027 case OFPACT_STACK_POP:
2028 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
2030 case OFPACT_SET_MPLS_LABEL:
2031 case OFPACT_SET_MPLS_TC:
2032 case OFPACT_SET_MPLS_TTL:
2033 case OFPACT_DEC_MPLS_TTL:
2034 if (!eth_type_mpls(flow->dl_type)) {
2035 inconsistent_match(usable_protocols);
2039 case OFPACT_SET_TUNNEL:
2040 case OFPACT_SET_QUEUE:
2041 case OFPACT_POP_QUEUE:
2042 case OFPACT_RESUBMIT:
2045 case OFPACT_FIN_TIMEOUT:
2046 if (flow->nw_proto != IPPROTO_TCP) {
2047 inconsistent_match(usable_protocols);
2052 return learn_check(ofpact_get_LEARN(a), flow);
2054 case OFPACT_MULTIPATH:
2055 return multipath_check(ofpact_get_MULTIPATH(a), flow);
2061 case OFPACT_PUSH_MPLS:
2062 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
2063 /* The packet is now MPLS and the MPLS payload is opaque.
2064 * Thus nothing can be assumed about the network protocol.
2065 * Temporarily mark that we have no nw_proto. */
2069 case OFPACT_POP_MPLS:
2070 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
2071 if (!eth_type_mpls(flow->dl_type)) {
2072 inconsistent_match(usable_protocols);
2079 case OFPACT_CLEAR_ACTIONS:
2082 case OFPACT_WRITE_ACTIONS: {
2083 /* Use a temporary copy of 'usable_protocols' because we can't check
2084 * consistency of an action set. */
2085 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2086 enum ofputil_protocol p = *usable_protocols;
2087 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
2088 flow, max_ports, table_id, n_tables, &p);
2091 case OFPACT_WRITE_METADATA:
2094 case OFPACT_METER: {
2095 uint32_t mid = ofpact_get_METER(a)->meter_id;
2096 if (mid == 0 || mid > OFPM13_MAX) {
2097 return OFPERR_OFPMMFC_INVALID_METER;
2102 case OFPACT_GOTO_TABLE: {
2103 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
2104 if ((table_id != 255 && goto_table <= table_id)
2105 || (n_tables != 255 && goto_table >= n_tables)) {
2106 return OFPERR_OFPBRC_BAD_TABLE_ID;
2119 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2120 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
2121 * switch with no more than 'max_ports' ports.
2123 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
2124 * '*usable_protocols' the protocols that forbid the inconsistency. (An
2125 * example of an inconsistency between match and actions is a flow that does
2126 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
2128 * May annotate ofpacts with information gathered from the 'flow'.
2130 * May temporarily modify 'flow', but restores the changes before returning. */
2132 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
2133 struct flow *flow, ofp_port_t max_ports,
2134 uint8_t table_id, uint8_t n_tables,
2135 enum ofputil_protocol *usable_protocols)
2138 ovs_be16 dl_type = flow->dl_type;
2139 ovs_be16 vlan_tci = flow->vlan_tci;
2140 uint8_t nw_proto = flow->nw_proto;
2141 enum ofperr error = 0;
2143 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2144 error = ofpact_check__(usable_protocols, a, flow,
2145 max_ports, table_id, n_tables);
2150 /* Restore fields that may have been modified. */
2151 flow->dl_type = dl_type;
2152 flow->vlan_tci = vlan_tci;
2153 flow->nw_proto = nw_proto;
2157 /* Like ofpacts_check(), but reports inconsistencies as
2158 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
2160 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
2161 struct flow *flow, ofp_port_t max_ports,
2162 uint8_t table_id, uint8_t n_tables,
2163 enum ofputil_protocol usable_protocols)
2165 enum ofputil_protocol p = usable_protocols;
2168 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
2169 table_id, n_tables, &p);
2170 return (error ? error
2171 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
2175 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2176 * in the appropriate order as defined by the OpenFlow spec. */
2178 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
2180 const struct ofpact *a;
2181 enum ovs_instruction_type inst;
2183 inst = OVSINST_OFPIT13_METER;
2184 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2185 enum ovs_instruction_type next;
2187 next = ovs_instruction_type_from_ofpact_type(a->type);
2189 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
2192 const char *name = ovs_instruction_name_from_type(inst);
2193 const char *next_name = ovs_instruction_name_from_type(next);
2196 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
2197 "1.1+ compatibility", name);
2199 VLOG_WARN("invalid instruction ordering: %s must appear "
2200 "before %s, for OpenFlow 1.1+ compatibility",
2203 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
2212 /* Converting ofpacts to Nicira OpenFlow extensions. */
2215 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
2218 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
2220 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
2221 output_reg->src.n_bits);
2222 naor->src = htonl(output_reg->src.field->nxm_header);
2223 naor->max_len = htons(output_reg->max_len);
2227 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
2230 struct nx_action_resubmit *nar;
2232 if (resubmit->table_id == 0xff
2233 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
2234 nar = ofputil_put_NXAST_RESUBMIT(out);
2236 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
2237 nar->table = resubmit->table_id;
2239 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
2243 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
2246 uint64_t tun_id = tunnel->tun_id;
2248 if (tun_id <= UINT32_MAX
2249 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
2250 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
2252 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
2257 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
2260 struct nx_action_write_metadata *nawm;
2262 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
2263 nawm->metadata = om->metadata;
2264 nawm->mask = om->mask;
2268 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
2270 size_t start_ofs = out->size;
2271 struct nx_action_note *nan;
2272 unsigned int remainder;
2275 nan = ofputil_put_NXAST_NOTE(out);
2276 out->size -= sizeof nan->note;
2278 ofpbuf_put(out, note->data, note->length);
2280 len = out->size - start_ofs;
2281 remainder = len % OFP_ACTION_ALIGN;
2283 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
2285 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
2286 nan->len = htons(out->size - start_ofs);
2290 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
2293 struct nx_action_controller *nac;
2295 nac = ofputil_put_NXAST_CONTROLLER(out);
2296 nac->max_len = htons(oc->max_len);
2297 nac->controller_id = htons(oc->controller_id);
2298 nac->reason = oc->reason;
2302 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
2305 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
2306 ofputil_put_NXAST_DEC_TTL(out);
2308 struct nx_action_cnt_ids *nac_ids =
2309 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
2310 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
2314 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2315 nac_ids->n_controllers = htons(oc_ids->n_controllers);
2317 ids = ofpbuf_put_zeros(out, ids_len);
2318 for (i = 0; i < oc_ids->n_controllers; i++) {
2319 ids[i] = htons(oc_ids->cnt_ids[i]);
2325 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
2328 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
2329 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
2330 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
2334 ofpact_sample_to_nxast(const struct ofpact_sample *os,
2337 struct nx_action_sample *nas;
2339 nas = ofputil_put_NXAST_SAMPLE(out);
2340 nas->probability = htons(os->probability);
2341 nas->collector_set_id = htonl(os->collector_set_id);
2342 nas->obs_domain_id = htonl(os->obs_domain_id);
2343 nas->obs_point_id = htonl(os->obs_point_id);
2347 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
2350 case OFPACT_CONTROLLER:
2351 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
2354 case OFPACT_OUTPUT_REG:
2355 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
2359 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
2362 case OFPACT_REG_MOVE:
2363 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
2366 case OFPACT_REG_LOAD:
2367 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
2370 case OFPACT_STACK_PUSH:
2371 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
2374 case OFPACT_STACK_POP:
2375 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
2378 case OFPACT_DEC_TTL:
2379 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
2382 case OFPACT_SET_MPLS_LABEL:
2383 ofputil_put_NXAST_SET_MPLS_LABEL(out)->label
2384 = ofpact_get_SET_MPLS_LABEL(a)->label;
2387 case OFPACT_SET_MPLS_TC:
2388 ofputil_put_NXAST_SET_MPLS_TC(out)->tc
2389 = ofpact_get_SET_MPLS_TC(a)->tc;
2392 case OFPACT_SET_MPLS_TTL:
2393 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
2394 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2397 case OFPACT_DEC_MPLS_TTL:
2398 ofputil_put_NXAST_DEC_MPLS_TTL(out);
2401 case OFPACT_SET_TUNNEL:
2402 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
2405 case OFPACT_WRITE_METADATA:
2406 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
2409 case OFPACT_SET_QUEUE:
2410 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
2411 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2414 case OFPACT_POP_QUEUE:
2415 ofputil_put_NXAST_POP_QUEUE(out);
2418 case OFPACT_FIN_TIMEOUT:
2419 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
2422 case OFPACT_RESUBMIT:
2423 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
2427 learn_to_nxast(ofpact_get_LEARN(a), out);
2430 case OFPACT_MULTIPATH:
2431 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
2435 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
2439 ofputil_put_NXAST_EXIT(out);
2442 case OFPACT_PUSH_MPLS:
2443 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
2444 ofpact_get_PUSH_MPLS(a)->ethertype;
2447 case OFPACT_POP_MPLS:
2448 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2449 ofpact_get_POP_MPLS(a)->ethertype;
2453 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2458 case OFPACT_ENQUEUE:
2459 case OFPACT_SET_VLAN_VID:
2460 case OFPACT_SET_VLAN_PCP:
2461 case OFPACT_STRIP_VLAN:
2462 case OFPACT_PUSH_VLAN:
2463 case OFPACT_SET_ETH_SRC:
2464 case OFPACT_SET_ETH_DST:
2465 case OFPACT_SET_IPV4_SRC:
2466 case OFPACT_SET_IPV4_DST:
2467 case OFPACT_SET_IP_DSCP:
2468 case OFPACT_SET_IP_ECN:
2469 case OFPACT_SET_IP_TTL:
2470 case OFPACT_SET_L4_SRC_PORT:
2471 case OFPACT_SET_L4_DST_PORT:
2472 case OFPACT_WRITE_ACTIONS:
2473 case OFPACT_CLEAR_ACTIONS:
2474 case OFPACT_GOTO_TABLE:
2476 case OFPACT_SET_FIELD:
2481 /* Converting ofpacts to OpenFlow 1.0. */
2484 ofpact_output_to_openflow10(const struct ofpact_output *output,
2487 struct ofp10_action_output *oao;
2489 oao = ofputil_put_OFPAT10_OUTPUT(out);
2490 oao->port = htons(ofp_to_u16(output->port));
2491 oao->max_len = htons(output->max_len);
2495 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2498 struct ofp10_action_enqueue *oae;
2500 oae = ofputil_put_OFPAT10_ENQUEUE(out);
2501 oae->port = htons(ofp_to_u16(enqueue->port));
2502 oae->queue_id = htonl(enqueue->queue);
2506 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2510 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2513 case OFPACT_ENQUEUE:
2514 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2517 case OFPACT_SET_VLAN_VID:
2518 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2519 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2522 case OFPACT_SET_VLAN_PCP:
2523 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2524 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2527 case OFPACT_STRIP_VLAN:
2528 ofputil_put_OFPAT10_STRIP_VLAN(out);
2531 case OFPACT_SET_ETH_SRC:
2532 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2533 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2536 case OFPACT_SET_ETH_DST:
2537 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2538 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2541 case OFPACT_SET_IPV4_SRC:
2542 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2543 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2546 case OFPACT_SET_IPV4_DST:
2547 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2548 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2551 case OFPACT_SET_IP_DSCP:
2552 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2553 = ofpact_get_SET_IP_DSCP(a)->dscp;
2556 case OFPACT_SET_L4_SRC_PORT:
2557 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2558 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2561 case OFPACT_SET_L4_DST_PORT:
2562 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2563 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2566 case OFPACT_PUSH_VLAN:
2567 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
2568 * follow this action. */
2571 case OFPACT_CLEAR_ACTIONS:
2572 case OFPACT_WRITE_ACTIONS:
2573 case OFPACT_GOTO_TABLE:
2581 case OFPACT_SET_FIELD:
2582 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2585 case OFPACT_CONTROLLER:
2586 case OFPACT_OUTPUT_REG:
2588 case OFPACT_REG_MOVE:
2589 case OFPACT_REG_LOAD:
2590 case OFPACT_STACK_PUSH:
2591 case OFPACT_STACK_POP:
2592 case OFPACT_DEC_TTL:
2593 case OFPACT_SET_IP_ECN:
2594 case OFPACT_SET_IP_TTL:
2595 case OFPACT_SET_MPLS_LABEL:
2596 case OFPACT_SET_MPLS_TC:
2597 case OFPACT_SET_MPLS_TTL:
2598 case OFPACT_DEC_MPLS_TTL:
2599 case OFPACT_SET_TUNNEL:
2600 case OFPACT_WRITE_METADATA:
2601 case OFPACT_SET_QUEUE:
2602 case OFPACT_POP_QUEUE:
2603 case OFPACT_FIN_TIMEOUT:
2604 case OFPACT_RESUBMIT:
2606 case OFPACT_MULTIPATH:
2609 case OFPACT_PUSH_MPLS:
2610 case OFPACT_POP_MPLS:
2612 ofpact_to_nxast(a, out);
2617 /* Converting ofpacts to OpenFlow 1.1. */
2620 ofpact_output_to_openflow11(const struct ofpact_output *output,
2623 struct ofp11_action_output *oao;
2625 oao = ofputil_put_OFPAT11_OUTPUT(out);
2626 oao->port = ofputil_port_to_ofp11(output->port);
2627 oao->max_len = htons(output->max_len);
2631 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2634 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2635 && (!dec_ttl->ofpact.compat ||
2636 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2637 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2639 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2644 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2648 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2650 case OFPACT_ENQUEUE:
2654 case OFPACT_SET_VLAN_VID:
2655 /* Push a VLAN tag, if one was not seen at action validation time. */
2656 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan
2657 && ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2658 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2659 = htons(ETH_TYPE_VLAN_8021Q);
2661 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2662 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2665 case OFPACT_SET_VLAN_PCP:
2666 /* Push a VLAN tag, if one was not seen at action validation time. */
2667 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan
2668 && ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2669 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2670 = htons(ETH_TYPE_VLAN_8021Q);
2672 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2673 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2676 case OFPACT_STRIP_VLAN:
2677 ofputil_put_OFPAT11_POP_VLAN(out);
2680 case OFPACT_PUSH_VLAN:
2681 /* XXX ETH_TYPE_VLAN_8021AD case */
2682 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2683 htons(ETH_TYPE_VLAN_8021Q);
2686 case OFPACT_SET_QUEUE:
2687 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2688 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2691 case OFPACT_SET_ETH_SRC:
2692 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2693 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2696 case OFPACT_SET_ETH_DST:
2697 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2698 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2701 case OFPACT_SET_IPV4_SRC:
2702 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2703 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2706 case OFPACT_SET_IPV4_DST:
2707 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2708 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2711 case OFPACT_SET_IP_DSCP:
2712 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2713 = ofpact_get_SET_IP_DSCP(a)->dscp;
2716 case OFPACT_SET_IP_ECN:
2717 ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2718 = ofpact_get_SET_IP_ECN(a)->ecn;
2721 case OFPACT_SET_IP_TTL:
2722 ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2723 = ofpact_get_SET_IP_TTL(a)->ttl;
2726 case OFPACT_SET_L4_SRC_PORT:
2727 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2728 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2731 case OFPACT_SET_L4_DST_PORT:
2732 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2733 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2736 case OFPACT_DEC_TTL:
2737 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2740 case OFPACT_SET_MPLS_LABEL:
2741 ofputil_put_OFPAT11_SET_MPLS_LABEL(out)->mpls_label
2742 = ofpact_get_SET_MPLS_LABEL(a)->label;
2745 case OFPACT_SET_MPLS_TC:
2746 ofputil_put_OFPAT11_SET_MPLS_TC(out)->mpls_tc
2747 = ofpact_get_SET_MPLS_TC(a)->tc;
2750 case OFPACT_SET_MPLS_TTL:
2751 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2752 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2755 case OFPACT_DEC_MPLS_TTL:
2756 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2759 case OFPACT_WRITE_METADATA:
2760 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2763 case OFPACT_PUSH_MPLS:
2764 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2765 ofpact_get_PUSH_MPLS(a)->ethertype;
2768 case OFPACT_POP_MPLS:
2769 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2770 ofpact_get_POP_MPLS(a)->ethertype;
2774 case OFPACT_CLEAR_ACTIONS:
2775 case OFPACT_WRITE_ACTIONS:
2776 case OFPACT_GOTO_TABLE:
2781 ofputil_put_OFPAT11_GROUP(out)->group_id =
2782 htonl(ofpact_get_GROUP(a)->group_id);
2785 case OFPACT_SET_FIELD:
2786 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2789 case OFPACT_CONTROLLER:
2790 case OFPACT_OUTPUT_REG:
2792 case OFPACT_REG_MOVE:
2793 case OFPACT_REG_LOAD:
2794 case OFPACT_STACK_PUSH:
2795 case OFPACT_STACK_POP:
2796 case OFPACT_SET_TUNNEL:
2797 case OFPACT_POP_QUEUE:
2798 case OFPACT_FIN_TIMEOUT:
2799 case OFPACT_RESUBMIT:
2801 case OFPACT_MULTIPATH:
2805 ofpact_to_nxast(a, out);
2810 /* Output deprecated set actions as set_field actions. */
2812 ofpact_to_openflow12(const struct ofpact *a, struct ofpbuf *out)
2814 enum mf_field_id field;
2815 union mf_value value;
2816 struct ofpact_l4_port *l4port;
2820 * Convert actions deprecated in OpenFlow 1.2 to Set Field actions,
2823 switch ((int)a->type) {
2824 case OFPACT_SET_VLAN_VID:
2825 case OFPACT_SET_VLAN_PCP:
2826 case OFPACT_SET_ETH_SRC:
2827 case OFPACT_SET_ETH_DST:
2828 case OFPACT_SET_IPV4_SRC:
2829 case OFPACT_SET_IPV4_DST:
2830 case OFPACT_SET_IP_DSCP:
2831 case OFPACT_SET_IP_ECN:
2832 case OFPACT_SET_L4_SRC_PORT:
2833 case OFPACT_SET_L4_DST_PORT:
2834 case OFPACT_SET_MPLS_LABEL:
2835 case OFPACT_SET_MPLS_TC:
2836 case OFPACT_SET_TUNNEL: /* Convert to a set_field, too. */
2838 switch ((int)a->type) {
2840 case OFPACT_SET_VLAN_VID:
2841 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan &&
2842 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2843 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2844 = htons(ETH_TYPE_VLAN_8021Q);
2846 field = MFF_VLAN_VID;
2847 /* Set-Field on OXM_OF_VLAN_VID must have OFPVID_PRESENT set. */
2848 value.be16 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid
2849 | OFPVID12_PRESENT);
2852 case OFPACT_SET_VLAN_PCP:
2853 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan &&
2854 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2855 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2856 = htons(ETH_TYPE_VLAN_8021Q);
2858 field = MFF_VLAN_PCP;
2859 value.u8 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2862 case OFPACT_SET_ETH_SRC:
2863 field = MFF_ETH_SRC;
2864 memcpy(value.mac, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2867 case OFPACT_SET_ETH_DST:
2868 field = MFF_ETH_DST;
2869 memcpy(value.mac, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2872 case OFPACT_SET_IPV4_SRC:
2873 field = MFF_IPV4_SRC;
2874 value.be32 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2877 case OFPACT_SET_IPV4_DST:
2878 field = MFF_IPV4_DST;
2879 value.be32 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2882 case OFPACT_SET_IP_DSCP:
2883 field = MFF_IP_DSCP_SHIFTED; /* OXM_OF_IP_DSCP */
2884 value.u8 = ofpact_get_SET_IP_DSCP(a)->dscp >> 2;
2887 case OFPACT_SET_IP_ECN:
2889 value.u8 = ofpact_get_SET_IP_ECN(a)->ecn;
2892 case OFPACT_SET_L4_SRC_PORT:
2893 /* We keep track of IP protocol while translating actions to be
2894 * able to translate to the proper OXM type.
2895 * If the IP protocol type is unknown, the translation cannot
2896 * be performed and we will send the action using the original
2898 l4port = ofpact_get_SET_L4_SRC_PORT(a);
2899 proto = l4port->flow_ip_proto;
2900 field = proto == IPPROTO_TCP ? MFF_TCP_SRC
2901 : proto == IPPROTO_UDP ? MFF_UDP_SRC
2902 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
2903 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
2904 value.be16 = htons(l4port->port);
2907 case OFPACT_SET_L4_DST_PORT:
2908 l4port = ofpact_get_SET_L4_DST_PORT(a);
2909 proto = l4port->flow_ip_proto;
2910 field = proto == IPPROTO_TCP ? MFF_TCP_DST
2911 : proto == IPPROTO_UDP ? MFF_UDP_DST
2912 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
2913 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
2914 value.be16 = htons(l4port->port);
2917 case OFPACT_SET_MPLS_LABEL:
2918 field = MFF_MPLS_LABEL;
2919 value.be32 = ofpact_get_SET_MPLS_LABEL(a)->label;
2922 case OFPACT_SET_MPLS_TC:
2923 field = MFF_MPLS_TC;
2924 value.u8 = ofpact_get_SET_MPLS_TC(a)->tc;
2927 case OFPACT_SET_TUNNEL:
2929 value.be64 = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2936 /* Put the action out as a set field action, if possible. */
2937 if (field < MFF_N_IDS) {
2938 uint64_t ofpacts_stub[128 / 8];
2939 struct ofpbuf sf_act;
2940 struct ofpact_set_field *sf;
2942 ofpbuf_use_stub(&sf_act, ofpacts_stub, sizeof ofpacts_stub);
2943 sf = ofpact_put_SET_FIELD(&sf_act);
2944 sf->field = mf_from_id(field);
2945 memcpy(&sf->value, &value, sf->field->n_bytes);
2946 set_field_to_openflow(sf, out);
2951 ofpact_to_openflow11(a, out);
2954 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
2955 * actions in 'openflow', appending the actions to any existing data in
2958 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
2959 struct ofpbuf *openflow,
2960 enum ofp_version ofp_version)
2962 const struct ofpact *a;
2963 size_t start_size = openflow->size;
2965 void (*translate)(const struct ofpact *a, struct ofpbuf *out) =
2966 (ofp_version == OFP10_VERSION) ? ofpact_to_openflow10 :
2967 (ofp_version == OFP11_VERSION) ? ofpact_to_openflow11 :
2968 ofpact_to_openflow12;
2970 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2971 translate(a, openflow);
2973 return openflow->size - start_size;
2977 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2979 struct ofp11_instruction_actions *oia;
2981 /* Update the instruction's length (or, if it's empty, delete it). */
2982 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2983 if (openflow->size > ofs + sizeof *oia) {
2984 oia->len = htons(openflow->size - ofs);
2986 openflow->size = ofs;
2991 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
2993 struct ofpbuf *openflow,
2994 enum ofp_version ofp_version)
2996 const struct ofpact *a;
2998 ovs_assert(ofp_version >= OFP11_VERSION);
3000 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3001 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
3002 case OVSINST_OFPIT11_CLEAR_ACTIONS:
3003 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
3006 case OVSINST_OFPIT11_GOTO_TABLE: {
3007 struct ofp11_instruction_goto_table *oigt;
3008 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
3009 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
3010 memset(oigt->pad, 0, sizeof oigt->pad);
3014 case OVSINST_OFPIT11_WRITE_METADATA: {
3015 const struct ofpact_metadata *om;
3016 struct ofp11_instruction_write_metadata *oiwm;
3018 om = ofpact_get_WRITE_METADATA(a);
3019 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
3020 oiwm->metadata = om->metadata;
3021 oiwm->metadata_mask = om->mask;
3025 case OVSINST_OFPIT13_METER:
3026 if (ofp_version >= OFP13_VERSION) {
3027 const struct ofpact_meter *om;
3028 struct ofp13_instruction_meter *oim;
3030 om = ofpact_get_METER(a);
3031 oim = instruction_put_OFPIT13_METER(openflow);
3032 oim->meter_id = htonl(om->meter_id);
3036 case OVSINST_OFPIT11_APPLY_ACTIONS: {
3037 const size_t ofs = openflow->size;
3038 const size_t ofpacts_len_left =
3039 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
3040 const struct ofpact *action;
3041 const struct ofpact *processed = a;
3043 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
3044 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
3045 if (ovs_instruction_type_from_ofpact_type(action->type)
3046 != OVSINST_OFPIT11_APPLY_ACTIONS) {
3049 if (ofp_version == OFP11_VERSION) {
3050 ofpact_to_openflow11(action, openflow);
3052 ofpact_to_openflow12(action, openflow);
3056 ofpacts_update_instruction_actions(openflow, ofs);
3061 case OVSINST_OFPIT11_WRITE_ACTIONS: {
3062 const size_t ofs = openflow->size;
3063 const struct ofpact_nest *on;
3065 on = ofpact_get_WRITE_ACTIONS(a);
3066 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
3067 ofpacts_put_openflow_actions(on->actions,
3068 ofpact_nest_get_action_len(on),
3069 openflow, ofp_version);
3070 ofpacts_update_instruction_actions(openflow, ofs);
3078 /* Returns true if 'action' outputs to 'port', false otherwise. */
3080 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
3082 switch (ofpact->type) {
3084 return ofpact_get_OUTPUT(ofpact)->port == port;
3085 case OFPACT_ENQUEUE:
3086 return ofpact_get_ENQUEUE(ofpact)->port == port;
3087 case OFPACT_CONTROLLER:
3088 return port == OFPP_CONTROLLER;
3090 case OFPACT_OUTPUT_REG:
3092 case OFPACT_SET_VLAN_VID:
3093 case OFPACT_SET_VLAN_PCP:
3094 case OFPACT_STRIP_VLAN:
3095 case OFPACT_PUSH_VLAN:
3096 case OFPACT_SET_ETH_SRC:
3097 case OFPACT_SET_ETH_DST:
3098 case OFPACT_SET_IPV4_SRC:
3099 case OFPACT_SET_IPV4_DST:
3100 case OFPACT_SET_IP_DSCP:
3101 case OFPACT_SET_IP_ECN:
3102 case OFPACT_SET_IP_TTL:
3103 case OFPACT_SET_L4_SRC_PORT:
3104 case OFPACT_SET_L4_DST_PORT:
3105 case OFPACT_REG_MOVE:
3106 case OFPACT_REG_LOAD:
3107 case OFPACT_SET_FIELD:
3108 case OFPACT_STACK_PUSH:
3109 case OFPACT_STACK_POP:
3110 case OFPACT_DEC_TTL:
3111 case OFPACT_SET_MPLS_LABEL:
3112 case OFPACT_SET_MPLS_TC:
3113 case OFPACT_SET_MPLS_TTL:
3114 case OFPACT_DEC_MPLS_TTL:
3115 case OFPACT_SET_TUNNEL:
3116 case OFPACT_WRITE_METADATA:
3117 case OFPACT_SET_QUEUE:
3118 case OFPACT_POP_QUEUE:
3119 case OFPACT_FIN_TIMEOUT:
3120 case OFPACT_RESUBMIT:
3122 case OFPACT_MULTIPATH:
3125 case OFPACT_PUSH_MPLS:
3126 case OFPACT_POP_MPLS:
3128 case OFPACT_CLEAR_ACTIONS:
3129 case OFPACT_WRITE_ACTIONS:
3130 case OFPACT_GOTO_TABLE:
3138 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3139 * to 'port', false otherwise. */
3141 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
3144 const struct ofpact *a;
3146 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3147 if (ofpact_outputs_to_port(a, port)) {
3155 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3156 * to 'group', false otherwise. */
3158 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
3161 const struct ofpact *a;
3163 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3164 if (a->type == OFPACT_GROUP
3165 && ofpact_get_GROUP(a)->group_id == group_id) {
3174 ofpacts_equal(const struct ofpact *a, size_t a_len,
3175 const struct ofpact *b, size_t b_len)
3177 return a_len == b_len && !memcmp(a, b, a_len);
3180 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
3181 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
3183 * This function relies on the order of 'ofpacts' being correct (as checked by
3184 * ofpacts_verify()). */
3186 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
3188 const struct ofpact *a;
3190 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3191 enum ovs_instruction_type inst;
3193 inst = ovs_instruction_type_from_ofpact_type(a->type);
3194 if (a->type == OFPACT_METER) {
3195 return ofpact_get_METER(a)->meter_id;
3196 } else if (inst > OVSINST_OFPIT13_METER) {
3204 /* Formatting ofpacts. */
3207 print_note(const struct ofpact_note *note, struct ds *string)
3211 ds_put_cstr(string, "note:");
3212 for (i = 0; i < note->length; i++) {
3214 ds_put_char(string, '.');
3216 ds_put_format(string, "%02"PRIx8, note->data[i]);
3221 print_dec_ttl(const struct ofpact_cnt_ids *ids,
3226 ds_put_cstr(s, "dec_ttl");
3227 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
3228 ds_put_cstr(s, "(");
3229 for (i = 0; i < ids->n_controllers; i++) {
3231 ds_put_cstr(s, ",");
3233 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
3235 ds_put_cstr(s, ")");
3240 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
3243 ds_put_cstr(s, "fin_timeout(");
3244 if (fin_timeout->fin_idle_timeout) {
3245 ds_put_format(s, "idle_timeout=%"PRIu16",",
3246 fin_timeout->fin_idle_timeout);
3248 if (fin_timeout->fin_hard_timeout) {
3249 ds_put_format(s, "hard_timeout=%"PRIu16",",
3250 fin_timeout->fin_hard_timeout);
3253 ds_put_char(s, ')');
3257 ofpact_format(const struct ofpact *a, struct ds *s)
3259 const struct ofpact_enqueue *enqueue;
3260 const struct ofpact_resubmit *resubmit;
3261 const struct ofpact_controller *controller;
3262 const struct ofpact_metadata *metadata;
3263 const struct ofpact_tunnel *tunnel;
3264 const struct ofpact_sample *sample;
3265 const struct ofpact_set_field *set_field;
3266 const struct mf_field *mf;
3271 port = ofpact_get_OUTPUT(a)->port;
3272 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
3273 ds_put_format(s, "output:%"PRIu16, port);
3275 ofputil_format_port(port, s);
3276 if (port == OFPP_CONTROLLER) {
3277 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
3282 case OFPACT_CONTROLLER:
3283 controller = ofpact_get_CONTROLLER(a);
3284 if (controller->reason == OFPR_ACTION &&
3285 controller->controller_id == 0) {
3286 ds_put_format(s, "CONTROLLER:%"PRIu16,
3287 ofpact_get_CONTROLLER(a)->max_len);
3289 enum ofp_packet_in_reason reason = controller->reason;
3291 ds_put_cstr(s, "controller(");
3292 if (reason != OFPR_ACTION) {
3293 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3295 ds_put_format(s, "reason=%s,",
3296 ofputil_packet_in_reason_to_string(
3297 reason, reasonbuf, sizeof reasonbuf));
3299 if (controller->max_len != UINT16_MAX) {
3300 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
3302 if (controller->controller_id != 0) {
3303 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
3306 ds_put_char(s, ')');
3310 case OFPACT_ENQUEUE:
3311 enqueue = ofpact_get_ENQUEUE(a);
3312 ds_put_format(s, "enqueue:");
3313 ofputil_format_port(enqueue->port, s);
3314 ds_put_format(s, ":%"PRIu32, enqueue->queue);
3317 case OFPACT_OUTPUT_REG:
3318 ds_put_cstr(s, "output:");
3319 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
3323 bundle_format(ofpact_get_BUNDLE(a), s);
3326 case OFPACT_SET_VLAN_VID:
3327 ds_put_format(s, "%s:%"PRIu16,
3328 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_VID
3331 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
3334 case OFPACT_SET_VLAN_PCP:
3335 ds_put_format(s, "%s:%"PRIu8,
3336 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_PCP
3339 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
3342 case OFPACT_STRIP_VLAN:
3343 ds_put_cstr(s, a->compat == OFPUTIL_OFPAT11_POP_VLAN
3344 ? "pop_vlan" : "strip_vlan");
3347 case OFPACT_PUSH_VLAN:
3348 /* XXX 802.1AD case*/
3349 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
3352 case OFPACT_SET_ETH_SRC:
3353 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
3354 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
3357 case OFPACT_SET_ETH_DST:
3358 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
3359 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
3362 case OFPACT_SET_IPV4_SRC:
3363 ds_put_format(s, "mod_nw_src:"IP_FMT,
3364 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
3367 case OFPACT_SET_IPV4_DST:
3368 ds_put_format(s, "mod_nw_dst:"IP_FMT,
3369 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
3372 case OFPACT_SET_IP_DSCP:
3373 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
3376 case OFPACT_SET_IP_ECN:
3377 ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
3380 case OFPACT_SET_IP_TTL:
3381 ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
3384 case OFPACT_SET_L4_SRC_PORT:
3385 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
3388 case OFPACT_SET_L4_DST_PORT:
3389 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
3392 case OFPACT_REG_MOVE:
3393 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
3396 case OFPACT_REG_LOAD:
3397 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
3400 case OFPACT_SET_FIELD:
3401 set_field = ofpact_get_SET_FIELD(a);
3402 mf = set_field->field;
3403 ds_put_format(s, "set_field:");
3404 mf_format(mf, &set_field->value, NULL, s);
3405 ds_put_format(s, "->%s", mf->name);
3408 case OFPACT_STACK_PUSH:
3409 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
3412 case OFPACT_STACK_POP:
3413 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
3416 case OFPACT_DEC_TTL:
3417 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
3420 case OFPACT_SET_MPLS_LABEL:
3421 ds_put_format(s, "set_mpls_label(%"PRIu32")",
3422 ntohl(ofpact_get_SET_MPLS_LABEL(a)->label));
3425 case OFPACT_SET_MPLS_TC:
3426 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3427 ofpact_get_SET_MPLS_TC(a)->tc);
3430 case OFPACT_SET_MPLS_TTL:
3431 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3432 ofpact_get_SET_MPLS_TTL(a)->ttl);
3435 case OFPACT_DEC_MPLS_TTL:
3436 ds_put_cstr(s, "dec_mpls_ttl");
3439 case OFPACT_SET_TUNNEL:
3440 tunnel = ofpact_get_SET_TUNNEL(a);
3441 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3442 (tunnel->tun_id > UINT32_MAX
3443 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
3447 case OFPACT_SET_QUEUE:
3448 ds_put_format(s, "set_queue:%"PRIu32,
3449 ofpact_get_SET_QUEUE(a)->queue_id);
3452 case OFPACT_POP_QUEUE:
3453 ds_put_cstr(s, "pop_queue");
3456 case OFPACT_FIN_TIMEOUT:
3457 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
3460 case OFPACT_RESUBMIT:
3461 resubmit = ofpact_get_RESUBMIT(a);
3462 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
3463 ds_put_cstr(s, "resubmit:");
3464 ofputil_format_port(resubmit->in_port, s);
3466 ds_put_format(s, "resubmit(");
3467 if (resubmit->in_port != OFPP_IN_PORT) {
3468 ofputil_format_port(resubmit->in_port, s);
3470 ds_put_char(s, ',');
3471 if (resubmit->table_id != 255) {
3472 ds_put_format(s, "%"PRIu8, resubmit->table_id);
3474 ds_put_char(s, ')');
3479 learn_format(ofpact_get_LEARN(a), s);
3482 case OFPACT_MULTIPATH:
3483 multipath_format(ofpact_get_MULTIPATH(a), s);
3487 print_note(ofpact_get_NOTE(a), s);
3490 case OFPACT_PUSH_MPLS:
3491 ds_put_format(s, "push_mpls:0x%04"PRIx16,
3492 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
3495 case OFPACT_POP_MPLS:
3496 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
3497 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
3501 ds_put_cstr(s, "exit");
3505 sample = ofpact_get_SAMPLE(a);
3507 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
3508 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
3509 sample->probability, sample->collector_set_id,
3510 sample->obs_domain_id, sample->obs_point_id);
3513 case OFPACT_WRITE_ACTIONS: {
3514 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3515 ds_put_format(s, "%s(",
3516 ovs_instruction_name_from_type(
3517 OVSINST_OFPIT11_WRITE_ACTIONS));
3518 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
3519 ds_put_char(s, ')');
3523 case OFPACT_CLEAR_ACTIONS:
3524 ds_put_format(s, "%s",
3525 ovs_instruction_name_from_type(
3526 OVSINST_OFPIT11_CLEAR_ACTIONS));
3529 case OFPACT_WRITE_METADATA:
3530 metadata = ofpact_get_WRITE_METADATA(a);
3531 ds_put_format(s, "%s:%#"PRIx64,
3532 ovs_instruction_name_from_type(
3533 OVSINST_OFPIT11_WRITE_METADATA),
3534 ntohll(metadata->metadata));
3535 if (metadata->mask != OVS_BE64_MAX) {
3536 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
3540 case OFPACT_GOTO_TABLE:
3541 ds_put_format(s, "%s:%"PRIu8,
3542 ovs_instruction_name_from_type(
3543 OVSINST_OFPIT11_GOTO_TABLE),
3544 ofpact_get_GOTO_TABLE(a)->table_id);
3548 ds_put_format(s, "%s:%"PRIu32,
3549 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
3550 ofpact_get_METER(a)->meter_id);
3554 ds_put_format(s, "group:%"PRIu32,
3555 ofpact_get_GROUP(a)->group_id);
3560 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
3561 * 'ofpacts' to 'string'. */
3563 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
3567 ds_put_cstr(string, "drop");
3569 const struct ofpact *a;
3571 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3573 ds_put_cstr(string, ",");
3576 /* XXX write-actions */
3577 ofpact_format(a, string);
3582 /* Internal use by helpers. */
3585 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
3587 struct ofpact *ofpact;
3589 ofpact_pad(ofpacts);
3590 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
3591 ofpact_init(ofpact, type, len);
3596 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
3598 memset(ofpact, 0, len);
3599 ofpact->type = type;
3600 ofpact->compat = OFPUTIL_ACTION_INVALID;
3604 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
3605 * starting at 'ofpact'.
3607 * This is the correct way to update a variable-length ofpact's length after
3608 * adding the variable-length part of the payload. (See the large comment
3609 * near the end of ofp-actions.h for more information.) */
3611 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
3613 ovs_assert(ofpact == ofpacts->l2);
3614 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
3617 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
3618 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
3619 * client must call this itself after adding the final ofpact to an array of
3622 * (The consequences of failing to call this function are probably not dire.
3623 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
3624 * not dereference it. That's undefined behavior, technically, but it will not
3625 * cause a real problem on common systems. Still, it seems better to call
3628 ofpact_pad(struct ofpbuf *ofpacts)
3630 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
3632 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);