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 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
381 #include "ofp-util.def"
384 case OFPUTIL_NXAST_RESUBMIT:
385 resubmit_from_openflow(&a->resubmit, out);
388 case OFPUTIL_NXAST_SET_TUNNEL:
389 tunnel = ofpact_put_SET_TUNNEL(out);
390 tunnel->ofpact.compat = code;
391 tunnel->tun_id = ntohl(a->set_tunnel.tun_id);
394 case OFPUTIL_NXAST_WRITE_METADATA:
395 error = metadata_from_nxast(&a->write_metadata, out);
398 case OFPUTIL_NXAST_SET_QUEUE:
399 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(a->set_queue.queue_id);
402 case OFPUTIL_NXAST_POP_QUEUE:
403 ofpact_put_POP_QUEUE(out);
406 case OFPUTIL_NXAST_REG_MOVE:
407 error = nxm_reg_move_from_openflow(&a->reg_move, out);
410 case OFPUTIL_NXAST_REG_LOAD:
411 error = nxm_reg_load_from_openflow(&a->reg_load, out);
414 case OFPUTIL_NXAST_STACK_PUSH:
415 error = nxm_stack_push_from_openflow(&a->stack, out);
418 case OFPUTIL_NXAST_STACK_POP:
419 error = nxm_stack_pop_from_openflow(&a->stack, out);
422 case OFPUTIL_NXAST_NOTE:
423 note_from_openflow(&a->note, out);
426 case OFPUTIL_NXAST_SET_TUNNEL64:
427 tunnel = ofpact_put_SET_TUNNEL(out);
428 tunnel->ofpact.compat = code;
429 tunnel->tun_id = ntohll(a->set_tunnel64.tun_id);
432 case OFPUTIL_NXAST_MULTIPATH:
433 error = multipath_from_openflow(&a->multipath,
434 ofpact_put_MULTIPATH(out));
437 case OFPUTIL_NXAST_BUNDLE:
438 case OFPUTIL_NXAST_BUNDLE_LOAD:
439 error = bundle_from_openflow(&a->bundle, out);
442 case OFPUTIL_NXAST_OUTPUT_REG:
443 error = output_reg_from_openflow(&a->output_reg, out);
446 case OFPUTIL_NXAST_RESUBMIT_TABLE:
447 error = resubmit_table_from_openflow(&a->resubmit, out);
450 case OFPUTIL_NXAST_LEARN:
451 error = learn_from_openflow(&a->learn, out);
454 case OFPUTIL_NXAST_EXIT:
455 ofpact_put_EXIT(out);
458 case OFPUTIL_NXAST_DEC_TTL:
459 error = dec_ttl_from_openflow(out, code);
462 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
463 error = dec_ttl_cnt_ids_from_openflow(&a->cnt_ids, out);
466 case OFPUTIL_NXAST_FIN_TIMEOUT:
467 fin_timeout_from_openflow(&a->fin_timeout, out);
470 case OFPUTIL_NXAST_CONTROLLER:
471 controller_from_openflow(&a->controller, out);
474 case OFPUTIL_NXAST_PUSH_MPLS:
475 error = push_mpls_from_openflow(a->push_mpls.ethertype, out);
478 case OFPUTIL_NXAST_SET_MPLS_LABEL:
479 ofpact_put_SET_MPLS_LABEL(out)->label = a->mpls_label.label;
482 case OFPUTIL_NXAST_SET_MPLS_TC:
483 ofpact_put_SET_MPLS_TC(out)->tc = a->mpls_tc.tc;
486 case OFPUTIL_NXAST_SET_MPLS_TTL:
487 ofpact_put_SET_MPLS_TTL(out)->ttl = a->mpls_ttl.ttl;
490 case OFPUTIL_NXAST_DEC_MPLS_TTL:
491 ofpact_put_DEC_MPLS_TTL(out);
494 case OFPUTIL_NXAST_POP_MPLS:
495 ofpact_put_POP_MPLS(out)->ethertype = a->pop_mpls.ethertype;
498 case OFPUTIL_NXAST_SAMPLE:
499 error = sample_from_openflow(&a->sample, out);
507 ofpact_from_openflow10(const union ofp_action *a,
508 enum ofp_version version OVS_UNUSED,
511 enum ofputil_action_code code;
513 struct ofpact_vlan_vid *vlan_vid;
514 struct ofpact_vlan_pcp *vlan_pcp;
516 error = decode_openflow10_action(a, &code);
522 case OFPUTIL_ACTION_INVALID:
523 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
524 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
525 #include "ofp-util.def"
528 case OFPUTIL_OFPAT10_OUTPUT:
529 return output_from_openflow10(&a->output10, out);
531 case OFPUTIL_OFPAT10_SET_VLAN_VID:
532 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
533 return OFPERR_OFPBAC_BAD_ARGUMENT;
535 vlan_vid = ofpact_put_SET_VLAN_VID(out);
536 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
537 vlan_vid->push_vlan_if_needed = true;
538 vlan_vid->ofpact.compat = code;
541 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
542 if (a->vlan_pcp.vlan_pcp & ~7) {
543 return OFPERR_OFPBAC_BAD_ARGUMENT;
545 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
546 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
547 vlan_pcp->push_vlan_if_needed = true;
548 vlan_pcp->ofpact.compat = code;
551 case OFPUTIL_OFPAT10_STRIP_VLAN:
552 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
555 case OFPUTIL_OFPAT10_SET_DL_SRC:
556 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
560 case OFPUTIL_OFPAT10_SET_DL_DST:
561 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
565 case OFPUTIL_OFPAT10_SET_NW_SRC:
566 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
569 case OFPUTIL_OFPAT10_SET_NW_DST:
570 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
573 case OFPUTIL_OFPAT10_SET_NW_TOS:
574 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
575 return OFPERR_OFPBAC_BAD_ARGUMENT;
577 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
580 case OFPUTIL_OFPAT10_SET_TP_SRC:
581 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
584 case OFPUTIL_OFPAT10_SET_TP_DST:
585 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
589 case OFPUTIL_OFPAT10_ENQUEUE:
590 error = enqueue_from_openflow10(&a->enqueue, out);
593 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
594 #include "ofp-util.def"
595 return ofpact_from_nxast(a, code, out);
601 static enum ofperr ofpact_from_openflow11(const union ofp_action *,
605 static inline union ofp_action *
606 action_next(const union ofp_action *a)
608 return ((union ofp_action *) (void *)
609 ((uint8_t *) a + ntohs(a->header.len)));
613 action_is_valid(const union ofp_action *a, size_t max_actions)
615 uint16_t len = ntohs(a->header.len);
616 return (!(len % OFP_ACTION_ALIGN)
617 && len >= OFP_ACTION_ALIGN
618 && len / OFP_ACTION_ALIGN <= max_actions);
621 /* This macro is careful to check for actions with bad lengths. */
622 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, MAX_ACTIONS) \
623 for ((ITER) = (ACTIONS), (LEFT) = (MAX_ACTIONS); \
624 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
625 ((LEFT) -= ntohs((ITER)->header.len) / OFP_ACTION_ALIGN, \
626 (ITER) = action_next(ITER)))
629 log_bad_action(const union ofp_action *actions, size_t max_actions,
630 const union ofp_action *bad_action, enum ofperr error)
632 if (!VLOG_DROP_WARN(&rl)) {
636 ds_put_hex_dump(&s, actions, max_actions * OFP_ACTION_ALIGN, 0, false);
637 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
638 (char *)bad_action - (char *)actions,
639 ofperr_get_name(error), ds_cstr(&s));
645 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
646 enum ofp_version version, struct ofpbuf *out)
648 const union ofp_action *a;
651 enum ofperr (*ofpact_from_openflow)(const union ofp_action *a,
653 struct ofpbuf *out) =
654 (version == OFP10_VERSION) ?
655 ofpact_from_openflow10 : ofpact_from_openflow11;
657 ACTION_FOR_EACH (a, left, in, n_in) {
658 enum ofperr error = ofpact_from_openflow(a, version, out);
660 log_bad_action(in, n_in, a, error);
665 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
666 log_bad_action(in, n_in, a, error);
675 ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
676 unsigned int actions_len,
677 enum ofp_version version,
678 uint32_t allowed_ovsinsts,
679 struct ofpbuf *ofpacts)
681 const union ofp_action *actions;
684 ofpbuf_clear(ofpacts);
686 if (actions_len % OFP_ACTION_ALIGN != 0) {
687 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
688 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
689 return OFPERR_OFPBRC_BAD_LEN;
692 actions = ofpbuf_try_pull(openflow, actions_len);
693 if (actions == NULL) {
694 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
695 "remaining message length (%"PRIu32")",
696 actions_len, ofpbuf_size(openflow));
697 return OFPERR_OFPBRC_BAD_LEN;
700 error = ofpacts_from_openflow(actions, actions_len / OFP_ACTION_ALIGN,
703 ofpbuf_clear(ofpacts);
707 error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
710 ofpbuf_clear(ofpacts);
715 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
716 * front of 'openflow' into ofpacts. On success, replaces any existing content
717 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
718 * Returns 0 if successful, otherwise an OpenFlow error.
720 * Actions are processed according to their OpenFlow version which
721 * is provided in the 'version' parameter.
723 * In most places in OpenFlow, actions appear encapsulated in instructions, so
724 * you should call ofpacts_pull_openflow_instructions() instead of this
727 * The parsed actions are valid generically, but they may not be valid in a
728 * specific context. For example, port numbers up to OFPP_MAX are valid
729 * generically, but specific datapaths may only support port numbers in a
730 * smaller range. Use ofpacts_check() to additional check whether actions are
731 * valid in a specific context. */
733 ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
734 unsigned int actions_len,
735 enum ofp_version version,
736 struct ofpbuf *ofpacts)
738 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
739 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
743 /* OpenFlow 1.1 actions. */
745 /* Parses 'a' to determine its type. On success stores the correct type into
746 * '*code' and returns 0. On failure returns an OFPERR_* error code and
747 * '*code' is indeterminate.
749 * The caller must have already verified that 'a''s length is potentially
750 * correct (that is, a->header.len is nonzero and a multiple of
751 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
753 * This function verifies that 'a''s length is correct for the type of action
754 * that it represents. */
756 decode_openflow11_action(const union ofp_action *a,
757 enum ofputil_action_code *code)
762 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
763 return decode_nxast_action(a, code);
765 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
766 case CONSTANT_HTONS(ENUM): \
767 len = ntohs(a->header.len); \
769 ? len >= sizeof(struct STRUCT) \
770 : len == sizeof(struct STRUCT)) { \
771 *code = OFPUTIL_##ENUM; \
774 return OFPERR_OFPBAC_BAD_LEN; \
777 #include "ofp-util.def"
780 return OFPERR_OFPBAC_BAD_TYPE;
785 set_field_from_openflow(const struct ofp12_action_set_field *oasf,
786 struct ofpbuf *ofpacts)
788 uint16_t oasf_len = ntohs(oasf->len);
789 uint32_t oxm_header = ntohl(oasf->dst);
790 uint8_t oxm_length = NXM_LENGTH(oxm_header);
791 struct ofpact_set_field *sf;
792 const struct mf_field *mf;
794 /* ofp12_action_set_field is padded to 64 bits by zero */
795 if (oasf_len != ROUND_UP(sizeof *oasf + oxm_length, 8)) {
796 return OFPERR_OFPBAC_BAD_SET_LEN;
798 if (!is_all_zeros((const uint8_t *)oasf + sizeof *oasf + oxm_length,
799 oasf_len - oxm_length - sizeof *oasf)) {
800 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
803 if (NXM_HASMASK(oxm_header)) {
804 return OFPERR_OFPBAC_BAD_SET_TYPE;
806 mf = mf_from_nxm_header(oxm_header);
808 return OFPERR_OFPBAC_BAD_SET_TYPE;
810 ovs_assert(mf->n_bytes == oxm_length);
811 /* oxm_length is now validated to be compatible with mf_value. */
813 VLOG_WARN_RL(&rl, "destination field %s is not writable", mf->name);
814 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
816 sf = ofpact_put_SET_FIELD(ofpacts);
818 memcpy(&sf->value, oasf + 1, mf->n_bytes);
820 /* The value must be valid for match and must have the OFPVID_PRESENT bit
821 * on for OXM_OF_VLAN_VID. */
822 if (!mf_is_value_valid(mf, &sf->value)
823 || (mf->id == MFF_VLAN_VID
824 && !(sf->value.be16 & htons(OFPVID12_PRESENT)))) {
825 struct ds ds = DS_EMPTY_INITIALIZER;
826 mf_format(mf, &sf->value, NULL, &ds);
827 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
828 mf->name, ds_cstr(&ds));
831 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
837 set_field_to_openflow12(const struct ofpact_set_field *sf,
838 struct ofpbuf *openflow,
839 enum ofp_version version)
841 uint16_t padded_value_len = ROUND_UP(sf->field->n_bytes, 8);
842 struct ofp12_action_set_field *oasf;
845 oasf = ofputil_put_OFPAT12_SET_FIELD(openflow);
846 oasf->dst = htonl(mf_oxm_header(sf->field->id, version));
847 oasf->len = htons(sizeof *oasf + padded_value_len);
849 value = ofpbuf_put_zeros(openflow, padded_value_len);
850 memcpy(value, &sf->value, sf->field->n_bytes);
853 /* Convert 'sf' to one or two REG_LOADs. */
855 set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
857 const struct mf_field *mf = sf->field;
858 struct nx_action_reg_load *narl;
860 if (mf->n_bits > 64) {
861 ovs_assert(mf->n_bytes == 16); /* IPv6 addr. */
862 /* Split into 64bit chunks */
863 /* Lower bits first. */
864 narl = ofputil_put_NXAST_REG_LOAD(openflow);
865 narl->ofs_nbits = nxm_encode_ofs_nbits(0, 64);
866 narl->dst = htonl(mf->nxm_header);
867 memcpy(&narl->value, &sf->value.ipv6.s6_addr[8], sizeof narl->value);
868 /* Higher bits next. */
869 narl = ofputil_put_NXAST_REG_LOAD(openflow);
870 narl->ofs_nbits = nxm_encode_ofs_nbits(64, mf->n_bits - 64);
871 narl->dst = htonl(mf->nxm_header);
872 memcpy(&narl->value, &sf->value.ipv6.s6_addr[0], sizeof narl->value);
874 narl = ofputil_put_NXAST_REG_LOAD(openflow);
875 narl->ofs_nbits = nxm_encode_ofs_nbits(0, mf->n_bits);
876 narl->dst = htonl(mf->nxm_header);
877 memset(&narl->value, 0, 8 - mf->n_bytes);
878 memcpy((char*)&narl->value + (8 - mf->n_bytes),
879 &sf->value, mf->n_bytes);
883 /* Convert 'sf' to standard OpenFlow 1.1 actions, if we can, falling back
884 * to Nicira extensions if we must.
886 * We check only meta-flow types that can appear within set field actions and
887 * that have a mapping to compatible action types. These struct mf_field
888 * definitions have a defined OXM or NXM header value and specify the field as
891 set_field_to_openflow11(const struct ofpact_set_field *sf,
892 struct ofpbuf *openflow)
894 switch ((int) sf->field->id) {
896 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
898 * If CFI=1, Add or modify VLAN VID & PCP.
899 * OpenFlow 1.1 set actions only apply if the packet
900 * already has VLAN tags. To be sure that is the case
901 * we have to push a VLAN header. As we do not support
902 * multiple layers of VLANs, this is a no-op, if a VLAN
903 * header already exists. This may backfire, however,
904 * when we start supporting multiple layers of VLANs.
905 * If CFI=0, strip VLAN header, if any.
907 if (sf->value.be16 & htons(VLAN_CFI)) {
908 /* Push a VLAN tag, if one was not seen at action validation
910 if (!sf->flow_has_vlan) {
911 ofputil_put_OFPAT11_PUSH_VLAN(openflow)->ethertype
912 = htons(ETH_TYPE_VLAN_8021Q);
914 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
915 = sf->value.be16 & htons(VLAN_VID_MASK);
916 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp
917 = vlan_tci_to_pcp(sf->value.be16);
919 /* If the flow did not match on vlan, we have no way of
920 * knowing if the vlan tag exists, so we must POP just to be
922 ofputil_put_OFPAT11_POP_VLAN(openflow);
927 /* OXM VLAN_PCP to OpenFlow 1.1.
928 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
929 * tag. Clear the OFPVID_PRESENT bit.
931 ofputil_put_OFPAT11_SET_VLAN_VID(openflow)->vlan_vid
932 = sf->value.be16 & htons(VLAN_VID_MASK);
936 /* OXM VLAN_PCP to OpenFlow 1.1.
937 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
938 ofputil_put_OFPAT11_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
942 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(openflow)->dl_addr,
943 sf->value.mac, ETH_ADDR_LEN);
947 memcpy(ofputil_put_OFPAT11_SET_DL_DST(openflow)->dl_addr,
948 sf->value.mac, ETH_ADDR_LEN);
952 ofputil_put_OFPAT11_SET_MPLS_LABEL(openflow)->mpls_label =
957 ofputil_put_OFPAT11_SET_MPLS_TC(openflow)->mpls_tc = sf->value.u8;
961 ofputil_put_OFPAT11_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
965 ofputil_put_OFPAT11_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
969 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
972 case MFF_IP_DSCP_SHIFTED:
973 ofputil_put_OFPAT11_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
977 ofputil_put_OFPAT11_SET_NW_ECN(openflow)->nw_ecn = sf->value.u8;
981 ofputil_put_OFPAT11_SET_NW_TTL(openflow)->nw_ttl = sf->value.u8;
987 ofputil_put_OFPAT11_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
993 ofputil_put_OFPAT11_SET_TP_DST(openflow)->tp_port = sf->value.be16;
997 set_field_to_nxast(sf, openflow);
1002 /* Convert 'sf' to standard OpenFlow 1.0 actions, if we can, falling back
1003 * to Nicira extensions if we must.
1005 * We check only meta-flow types that can appear within set field actions and
1006 * that have a mapping to compatible action types. These struct mf_field
1007 * definitions have a defined OXM or NXM header value and specify the field as
1010 set_field_to_openflow10(const struct ofpact_set_field *sf,
1011 struct ofpbuf *openflow)
1013 switch ((int) sf->field->id) {
1015 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
1017 * If CFI=1, Add or modify VLAN VID & PCP.
1018 * If CFI=0, strip VLAN header, if any.
1020 if (sf->value.be16 & htons(VLAN_CFI)) {
1021 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1022 = sf->value.be16 & htons(VLAN_VID_MASK);
1023 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp
1024 = vlan_tci_to_pcp(sf->value.be16);
1026 ofputil_put_OFPAT10_STRIP_VLAN(openflow);
1031 /* OXM VLAN_VID to OpenFlow 1.0.
1032 * Set field on OXM_OF_VLAN_VID onlyapplies to an existing vlan
1033 * tag. Clear the OFPVID_PRESENT bit.
1035 ofputil_put_OFPAT10_SET_VLAN_VID(openflow)->vlan_vid
1036 = sf->value.be16 & htons(VLAN_VID_MASK);
1040 /* OXM VLAN_PCP to OpenFlow 1.0.
1041 * OXM_OF_VLAN_PCP only applies to existing vlan tag. */
1042 ofputil_put_OFPAT10_SET_VLAN_PCP(openflow)->vlan_pcp = sf->value.u8;
1046 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(openflow)->dl_addr,
1047 sf->value.mac, ETH_ADDR_LEN);
1051 memcpy(ofputil_put_OFPAT10_SET_DL_DST(openflow)->dl_addr,
1052 sf->value.mac, ETH_ADDR_LEN);
1056 ofputil_put_OFPAT10_SET_NW_SRC(openflow)->nw_addr = sf->value.be32;
1060 ofputil_put_OFPAT10_SET_NW_DST(openflow)->nw_addr = sf->value.be32;
1064 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8;
1067 case MFF_IP_DSCP_SHIFTED:
1068 ofputil_put_OFPAT10_SET_NW_TOS(openflow)->nw_tos = sf->value.u8 << 2;
1073 ofputil_put_OFPAT10_SET_TP_SRC(openflow)->tp_port = sf->value.be16;
1078 ofputil_put_OFPAT10_SET_TP_DST(openflow)->tp_port = sf->value.be16;
1082 set_field_to_nxast(sf, openflow);
1088 set_field_to_openflow(const struct ofpact_set_field *sf,
1089 struct ofpbuf *openflow)
1091 struct ofp_header *oh = (struct ofp_header *)openflow->frame;
1093 if (oh->version >= OFP12_VERSION) {
1094 set_field_to_openflow12(sf, openflow, oh->version);
1095 } else if (oh->version == OFP11_VERSION) {
1096 set_field_to_openflow11(sf, openflow);
1097 } else if (oh->version == OFP10_VERSION) {
1098 set_field_to_openflow10(sf, openflow);
1105 output_from_openflow11(const struct ofp11_action_output *oao,
1108 struct ofpact_output *output;
1111 output = ofpact_put_OUTPUT(out);
1112 output->max_len = ntohs(oao->max_len);
1114 error = ofputil_port_from_ofp11(oao->port, &output->port);
1119 return ofpact_check_output_port(output->port, OFPP_MAX);
1123 ofpact_from_openflow11(const union ofp_action *a, enum ofp_version version,
1126 enum ofputil_action_code code;
1128 struct ofpact_vlan_vid *vlan_vid;
1129 struct ofpact_vlan_pcp *vlan_pcp;
1131 error = decode_openflow11_action(a, &code);
1136 if (version >= OFP12_VERSION) {
1137 switch ((int)code) {
1138 case OFPUTIL_OFPAT11_SET_VLAN_VID:
1139 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1140 case OFPUTIL_OFPAT11_SET_DL_SRC:
1141 case OFPUTIL_OFPAT11_SET_DL_DST:
1142 case OFPUTIL_OFPAT11_SET_NW_SRC:
1143 case OFPUTIL_OFPAT11_SET_NW_DST:
1144 case OFPUTIL_OFPAT11_SET_NW_TOS:
1145 case OFPUTIL_OFPAT11_SET_NW_ECN:
1146 case OFPUTIL_OFPAT11_SET_TP_SRC:
1147 case OFPUTIL_OFPAT11_SET_TP_DST:
1148 VLOG_WARN_RL(&rl, "Deprecated action %s received over %s",
1149 ofputil_action_name_from_code(code),
1150 ofputil_version_to_string(version));
1155 case OFPUTIL_ACTION_INVALID:
1156 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
1157 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
1158 #include "ofp-util.def"
1161 case OFPUTIL_OFPAT11_OUTPUT:
1162 return output_from_openflow11(&a->ofp11_output, out);
1164 case OFPUTIL_OFPAT11_SET_VLAN_VID:
1165 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
1166 return OFPERR_OFPBAC_BAD_ARGUMENT;
1168 vlan_vid = ofpact_put_SET_VLAN_VID(out);
1169 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
1170 vlan_vid->push_vlan_if_needed = false;
1171 vlan_vid->ofpact.compat = code;
1174 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
1175 if (a->vlan_pcp.vlan_pcp & ~7) {
1176 return OFPERR_OFPBAC_BAD_ARGUMENT;
1178 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1179 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
1180 vlan_pcp->push_vlan_if_needed = false;
1181 vlan_pcp->ofpact.compat = code;
1184 case OFPUTIL_OFPAT11_PUSH_VLAN:
1185 if (a->push.ethertype != htons(ETH_TYPE_VLAN_8021Q)) {
1186 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1187 return OFPERR_OFPBAC_BAD_ARGUMENT;
1189 ofpact_put_PUSH_VLAN(out);
1192 case OFPUTIL_OFPAT11_POP_VLAN:
1193 ofpact_put_STRIP_VLAN(out)->ofpact.compat = code;
1196 case OFPUTIL_OFPAT11_SET_QUEUE:
1197 ofpact_put_SET_QUEUE(out)->queue_id =
1198 ntohl(a->ofp11_set_queue.queue_id);
1201 case OFPUTIL_OFPAT11_SET_DL_SRC:
1202 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
1206 case OFPUTIL_OFPAT11_SET_DL_DST:
1207 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
1211 case OFPUTIL_OFPAT11_DEC_NW_TTL:
1212 dec_ttl_from_openflow(out, code);
1215 case OFPUTIL_OFPAT11_SET_NW_SRC:
1216 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
1219 case OFPUTIL_OFPAT11_SET_NW_DST:
1220 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
1223 case OFPUTIL_OFPAT11_SET_NW_TOS:
1224 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
1225 return OFPERR_OFPBAC_BAD_ARGUMENT;
1227 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
1230 case OFPUTIL_OFPAT11_SET_NW_ECN:
1231 if (a->nw_ecn.nw_ecn & ~IP_ECN_MASK) {
1232 return OFPERR_OFPBAC_BAD_ARGUMENT;
1234 ofpact_put_SET_IP_ECN(out)->ecn = a->nw_ecn.nw_ecn;
1237 case OFPUTIL_OFPAT11_SET_NW_TTL:
1238 ofpact_put_SET_IP_TTL(out)->ttl = a->nw_ttl.nw_ttl;
1241 case OFPUTIL_OFPAT11_SET_TP_SRC:
1242 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
1245 case OFPUTIL_OFPAT11_SET_TP_DST:
1246 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
1249 case OFPUTIL_OFPAT12_SET_FIELD:
1250 return set_field_from_openflow(&a->set_field, out);
1252 case OFPUTIL_OFPAT11_SET_MPLS_LABEL:
1253 ofpact_put_SET_MPLS_LABEL(out)->label = a->ofp11_mpls_label.mpls_label;
1256 case OFPUTIL_OFPAT11_SET_MPLS_TC:
1257 ofpact_put_SET_MPLS_TC(out)->tc = a->ofp11_mpls_tc.mpls_tc;
1260 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
1261 ofpact_put_SET_MPLS_TTL(out)->ttl = a->ofp11_mpls_ttl.mpls_ttl;
1264 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
1265 ofpact_put_DEC_MPLS_TTL(out);
1268 case OFPUTIL_OFPAT11_PUSH_MPLS:
1269 error = push_mpls_from_openflow(a->push.ethertype, out);
1272 case OFPUTIL_OFPAT11_POP_MPLS:
1273 ofpact_put_POP_MPLS(out)->ethertype = a->ofp11_pop_mpls.ethertype;
1276 case OFPUTIL_OFPAT11_GROUP:
1277 ofpact_put_GROUP(out)->group_id = ntohl(a->group.group_id);
1280 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
1281 #include "ofp-util.def"
1282 return ofpact_from_nxast(a, code, out);
1288 /* True if an action sets the value of a field
1289 * in a way that is compatibile with the action set.
1290 * False otherwise. */
1292 ofpact_is_set_action(const struct ofpact *a)
1295 case OFPACT_SET_FIELD:
1296 case OFPACT_REG_LOAD:
1297 case OFPACT_SET_ETH_DST:
1298 case OFPACT_SET_ETH_SRC:
1299 case OFPACT_SET_IP_DSCP:
1300 case OFPACT_SET_IP_ECN:
1301 case OFPACT_SET_IP_TTL:
1302 case OFPACT_SET_IPV4_DST:
1303 case OFPACT_SET_IPV4_SRC:
1304 case OFPACT_SET_L4_DST_PORT:
1305 case OFPACT_SET_L4_SRC_PORT:
1306 case OFPACT_SET_MPLS_LABEL:
1307 case OFPACT_SET_MPLS_TC:
1308 case OFPACT_SET_MPLS_TTL:
1309 case OFPACT_SET_QUEUE:
1310 case OFPACT_SET_TUNNEL:
1311 case OFPACT_SET_VLAN_PCP:
1312 case OFPACT_SET_VLAN_VID:
1315 case OFPACT_CLEAR_ACTIONS:
1316 case OFPACT_CONTROLLER:
1317 case OFPACT_DEC_MPLS_TTL:
1318 case OFPACT_DEC_TTL:
1319 case OFPACT_ENQUEUE:
1321 case OFPACT_FIN_TIMEOUT:
1322 case OFPACT_GOTO_TABLE:
1326 case OFPACT_MULTIPATH:
1329 case OFPACT_OUTPUT_REG:
1330 case OFPACT_POP_MPLS:
1331 case OFPACT_POP_QUEUE:
1332 case OFPACT_PUSH_MPLS:
1333 case OFPACT_PUSH_VLAN:
1334 case OFPACT_REG_MOVE:
1335 case OFPACT_RESUBMIT:
1337 case OFPACT_STACK_POP:
1338 case OFPACT_STACK_PUSH:
1339 case OFPACT_STRIP_VLAN:
1340 case OFPACT_WRITE_ACTIONS:
1341 case OFPACT_WRITE_METADATA:
1348 /* True if an action is allowed in the action set.
1349 * False otherwise. */
1351 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
1354 case OFPACT_DEC_MPLS_TTL:
1355 case OFPACT_DEC_TTL:
1358 case OFPACT_POP_MPLS:
1359 case OFPACT_PUSH_MPLS:
1360 case OFPACT_PUSH_VLAN:
1361 case OFPACT_REG_LOAD:
1362 case OFPACT_SET_FIELD:
1363 case OFPACT_SET_ETH_DST:
1364 case OFPACT_SET_ETH_SRC:
1365 case OFPACT_SET_IP_DSCP:
1366 case OFPACT_SET_IP_ECN:
1367 case OFPACT_SET_IP_TTL:
1368 case OFPACT_SET_IPV4_DST:
1369 case OFPACT_SET_IPV4_SRC:
1370 case OFPACT_SET_L4_DST_PORT:
1371 case OFPACT_SET_L4_SRC_PORT:
1372 case OFPACT_SET_MPLS_LABEL:
1373 case OFPACT_SET_MPLS_TC:
1374 case OFPACT_SET_MPLS_TTL:
1375 case OFPACT_SET_QUEUE:
1376 case OFPACT_SET_TUNNEL:
1377 case OFPACT_SET_VLAN_PCP:
1378 case OFPACT_SET_VLAN_VID:
1379 case OFPACT_STRIP_VLAN:
1382 /* In general these actions are excluded because they are not part of
1383 * the OpenFlow specification nor map to actions that are defined in
1384 * the specification. Thus the order in which they should be applied
1385 * in the action set is undefined. */
1387 case OFPACT_CONTROLLER:
1388 case OFPACT_ENQUEUE:
1390 case OFPACT_FIN_TIMEOUT:
1392 case OFPACT_MULTIPATH:
1394 case OFPACT_OUTPUT_REG:
1395 case OFPACT_POP_QUEUE:
1396 case OFPACT_REG_MOVE:
1397 case OFPACT_RESUBMIT:
1399 case OFPACT_STACK_POP:
1400 case OFPACT_STACK_PUSH:
1402 /* The action set may only include actions and thus
1403 * may not include any instructions */
1404 case OFPACT_CLEAR_ACTIONS:
1405 case OFPACT_GOTO_TABLE:
1407 case OFPACT_WRITE_ACTIONS:
1408 case OFPACT_WRITE_METADATA:
1415 /* Append ofpact 'a' onto the tail of 'out' */
1417 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1419 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1422 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1424 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1425 enum ofpact_type filter)
1427 const struct ofpact *target;
1428 const struct ofpact *a;
1431 OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
1432 if (a->type == filter) {
1437 ofpact_copy(out, target);
1439 return target != NULL;
1442 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1443 * The order of appended ofpacts is preserved between 'in' and 'out' */
1445 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1446 bool (*filter)(const struct ofpact *))
1448 const struct ofpact *a;
1450 OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
1452 ofpact_copy(out, a);
1457 /* Reads 'action_set', which contains ofpacts accumulated by
1458 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1459 * executed directly into 'action_list'. (These names correspond to the
1460 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1462 * In general this involves appending the last instance of each action that is
1463 * adimissible in the action set in the order described in the OpenFlow
1467 * + output action is only appended if no group action was present in 'in'.
1468 * + As a simplification all set actions are copied in the order the are
1469 * provided in 'in' as many set actions applied to a field has the same
1470 * affect as only applying the last action that sets a field and
1471 * duplicates are removed by do_xlate_actions().
1472 * This has an unwanted side-effect of compsoting multiple
1473 * LOAD_REG actions that touch different regions of the same field. */
1475 ofpacts_execute_action_set(struct ofpbuf *action_list,
1476 const struct ofpbuf *action_set)
1478 /* The OpenFlow spec "Action Set" section specifies this order. */
1479 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1480 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1481 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1482 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1483 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1484 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1485 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1486 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1488 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1489 * we should execute only OFPACT_GROUP.
1491 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1492 * all the actions because there's no point in modifying a packet that will
1493 * not be sent anywhere. */
1494 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1495 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1496 ofpbuf_clear(action_list);
1502 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1503 size_t n_in, enum ofp_version version,
1508 size_t start = ofpbuf_size(out);
1510 error = ofpacts_from_openflow(in, n_in, version, out);
1516 OFPACT_FOR_EACH (a, ofpact_end(ofpbuf_data(out), start), ofpbuf_size(out) - start) {
1517 if (!ofpact_is_allowed_in_actions_set(a)) {
1518 VLOG_WARN_RL(&rl, "disallowed action in action set");
1519 return OFPERR_OFPBAC_BAD_TYPE;
1527 /* OpenFlow 1.1 instructions. */
1529 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1530 static inline const struct STRUCT * OVS_UNUSED \
1531 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1533 ovs_assert(inst->type == htons(ENUM)); \
1534 return ALIGNED_CAST(struct STRUCT *, inst); \
1537 static inline void OVS_UNUSED \
1538 instruction_init_##ENUM(struct STRUCT *s) \
1540 memset(s, 0, sizeof *s); \
1541 s->type = htons(ENUM); \
1542 s->len = htons(sizeof *s); \
1545 static inline struct STRUCT * OVS_UNUSED \
1546 instruction_put_##ENUM(struct ofpbuf *buf) \
1548 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1549 instruction_init_##ENUM(s); \
1555 struct instruction_type_info {
1556 enum ovs_instruction_type type;
1560 static const struct instruction_type_info inst_info[] = {
1561 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1567 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1569 return inst_info[type].name;
1573 ovs_instruction_type_from_name(const char *name)
1575 const struct instruction_type_info *p;
1576 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1577 if (!strcasecmp(name, p->name)) {
1584 enum ovs_instruction_type
1585 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1589 return OVSINST_OFPIT13_METER;
1590 case OFPACT_CLEAR_ACTIONS:
1591 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1592 case OFPACT_WRITE_ACTIONS:
1593 return OVSINST_OFPIT11_WRITE_ACTIONS;
1594 case OFPACT_WRITE_METADATA:
1595 return OVSINST_OFPIT11_WRITE_METADATA;
1596 case OFPACT_GOTO_TABLE:
1597 return OVSINST_OFPIT11_GOTO_TABLE;
1600 case OFPACT_CONTROLLER:
1601 case OFPACT_ENQUEUE:
1602 case OFPACT_OUTPUT_REG:
1604 case OFPACT_SET_VLAN_VID:
1605 case OFPACT_SET_VLAN_PCP:
1606 case OFPACT_STRIP_VLAN:
1607 case OFPACT_PUSH_VLAN:
1608 case OFPACT_SET_ETH_SRC:
1609 case OFPACT_SET_ETH_DST:
1610 case OFPACT_SET_IPV4_SRC:
1611 case OFPACT_SET_IPV4_DST:
1612 case OFPACT_SET_IP_DSCP:
1613 case OFPACT_SET_IP_ECN:
1614 case OFPACT_SET_IP_TTL:
1615 case OFPACT_SET_L4_SRC_PORT:
1616 case OFPACT_SET_L4_DST_PORT:
1617 case OFPACT_REG_MOVE:
1618 case OFPACT_REG_LOAD:
1619 case OFPACT_SET_FIELD:
1620 case OFPACT_STACK_PUSH:
1621 case OFPACT_STACK_POP:
1622 case OFPACT_DEC_TTL:
1623 case OFPACT_SET_MPLS_LABEL:
1624 case OFPACT_SET_MPLS_TC:
1625 case OFPACT_SET_MPLS_TTL:
1626 case OFPACT_DEC_MPLS_TTL:
1627 case OFPACT_PUSH_MPLS:
1628 case OFPACT_POP_MPLS:
1629 case OFPACT_SET_TUNNEL:
1630 case OFPACT_SET_QUEUE:
1631 case OFPACT_POP_QUEUE:
1632 case OFPACT_FIN_TIMEOUT:
1633 case OFPACT_RESUBMIT:
1635 case OFPACT_MULTIPATH:
1640 return OVSINST_OFPIT11_APPLY_ACTIONS;
1645 ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
1646 const uint16_t inst_type)
1648 switch (inst_type) {
1650 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1652 *instruction_type = OVSINST_##ENUM; \
1658 return OFPERR_OFPBIC_UNKNOWN_INST;
1662 /* Two-way translation between OVS's internal "OVSINST_*" representation of
1663 * instructions and the "OFPIT_*" representation used in OpenFlow. */
1664 struct ovsinst_map {
1665 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
1666 int ofpit; /* OFPIT_* number from OpenFlow spec. */
1669 static const struct ovsinst_map *
1670 get_ovsinst_map(enum ofp_version version)
1672 /* OpenFlow 1.1 and 1.2 instructions. */
1673 static const struct ovsinst_map of11[] = {
1674 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
1675 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
1676 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
1677 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
1678 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
1682 /* OpenFlow 1.3+ instructions. */
1683 static const struct ovsinst_map of13[] = {
1684 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
1685 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
1686 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
1687 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
1688 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
1689 { OVSINST_OFPIT13_METER, 6 },
1693 return version < OFP13_VERSION ? of11 : of13;
1696 /* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
1697 * values, into a bitmap of instructions suitable for OpenFlow 'version'
1698 * (OFP11_VERSION or later), and returns the result. */
1700 ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
1702 uint32_t ofpit_bitmap = 0;
1703 const struct ovsinst_map *x;
1705 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
1706 if (ovsinst_bitmap & (1u << x->ovsinst)) {
1707 ofpit_bitmap |= 1u << x->ofpit;
1710 return htonl(ofpit_bitmap);
1713 /* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
1714 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
1715 * correspond to OVSINST_* values, and returns the result. */
1717 ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap_, enum ofp_version version)
1719 uint32_t ofpit_bitmap = ntohl(ofpit_bitmap_);
1720 uint32_t ovsinst_bitmap = 0;
1721 const struct ovsinst_map *x;
1723 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
1724 if (ofpit_bitmap & (1u << x->ofpit)) {
1725 ovsinst_bitmap |= 1u << x->ovsinst;
1728 return ovsinst_bitmap;
1731 static inline struct ofp11_instruction *
1732 instruction_next(const struct ofp11_instruction *inst)
1734 return ((struct ofp11_instruction *) (void *)
1735 ((uint8_t *) inst + ntohs(inst->len)));
1739 instruction_is_valid(const struct ofp11_instruction *inst,
1740 size_t n_instructions)
1742 uint16_t len = ntohs(inst->len);
1743 return (!(len % OFP11_INSTRUCTION_ALIGN)
1744 && len >= sizeof *inst
1745 && len / sizeof *inst <= n_instructions);
1748 /* This macro is careful to check for instructions with bad lengths. */
1749 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1750 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1751 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1752 ((LEFT) -= (ntohs((ITER)->len) \
1753 / sizeof(struct ofp11_instruction)), \
1754 (ITER) = instruction_next(ITER)))
1757 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1758 enum ovs_instruction_type *type)
1760 uint16_t len = ntohs(inst->len);
1762 switch (inst->type) {
1763 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1764 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1766 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1767 case CONSTANT_HTONS(ENUM): \
1769 ? len >= sizeof(struct STRUCT) \
1770 : len == sizeof(struct STRUCT)) { \
1771 *type = OVSINST_##ENUM; \
1774 return OFPERR_OFPBIC_BAD_LEN; \
1780 return OFPERR_OFPBIC_UNKNOWN_INST;
1785 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1787 const struct ofp11_instruction *out[])
1789 const struct ofp11_instruction *inst;
1792 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1793 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1794 enum ovs_instruction_type type;
1797 error = decode_openflow11_instruction(inst, &type);
1803 return OFPERR_OFPBIC_DUP_INST;
1809 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
1810 (n_insts - left) * sizeof *inst);
1811 return OFPERR_OFPBIC_BAD_LEN;
1817 get_actions_from_instruction(const struct ofp11_instruction *inst,
1818 const union ofp_action **actions,
1819 size_t *max_actions)
1821 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1822 *max_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1826 ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
1827 unsigned int instructions_len,
1828 enum ofp_version version,
1829 struct ofpbuf *ofpacts)
1831 const struct ofp11_instruction *instructions;
1832 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1835 if (version == OFP10_VERSION) {
1836 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
1838 (1u << N_OVS_INSTRUCTIONS) - 1,
1842 ofpbuf_clear(ofpacts);
1844 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1845 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1847 instructions_len, OFP11_INSTRUCTION_ALIGN);
1848 error = OFPERR_OFPBIC_BAD_LEN;
1852 instructions = ofpbuf_try_pull(openflow, instructions_len);
1853 if (instructions == NULL) {
1854 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1855 "remaining message length (%"PRIu32")",
1856 instructions_len, ofpbuf_size(openflow));
1857 error = OFPERR_OFPBIC_BAD_LEN;
1861 error = decode_openflow11_instructions(
1862 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1868 if (insts[OVSINST_OFPIT13_METER]) {
1869 const struct ofp13_instruction_meter *oim;
1870 struct ofpact_meter *om;
1872 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1873 insts[OVSINST_OFPIT13_METER]);
1875 om = ofpact_put_METER(ofpacts);
1876 om->meter_id = ntohl(oim->meter_id);
1878 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1879 const union ofp_action *actions;
1882 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1883 &actions, &max_actions);
1884 error = ofpacts_from_openflow(actions, max_actions, version, ofpacts);
1889 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1890 instruction_get_OFPIT11_CLEAR_ACTIONS(
1891 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1892 ofpact_put_CLEAR_ACTIONS(ofpacts);
1894 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1895 struct ofpact_nest *on;
1896 const union ofp_action *actions;
1900 ofpact_pad(ofpacts);
1901 start = ofpbuf_size(ofpacts);
1902 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1903 offsetof(struct ofpact_nest, actions));
1904 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1905 &actions, &max_actions);
1906 error = ofpacts_from_openflow11_for_action_set(actions, max_actions,
1911 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1912 on->ofpact.len = ofpbuf_size(ofpacts) - start;
1914 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1915 const struct ofp11_instruction_write_metadata *oiwm;
1916 struct ofpact_metadata *om;
1918 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1919 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1921 om = ofpact_put_WRITE_METADATA(ofpacts);
1922 om->metadata = oiwm->metadata;
1923 om->mask = oiwm->metadata_mask;
1925 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1926 const struct ofp11_instruction_goto_table *oigt;
1927 struct ofpact_goto_table *ogt;
1929 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1930 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1931 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1932 ogt->table_id = oigt->table_id;
1935 error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
1936 (1u << N_OVS_INSTRUCTIONS) - 1);
1939 ofpbuf_clear(ofpacts);
1944 /* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
1945 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
1946 * valid, otherwise an OpenFlow error code. */
1948 ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
1956 case OFPP_CONTROLLER:
1962 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
1965 return OFPERR_OFPBAC_BAD_OUT_PORT;
1969 /* Removes the protocols that require consistency between match and actions
1970 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
1972 * (An example of an inconsistency between match and actions is a flow that
1973 * does not match on an MPLS Ethertype but has an action that pops an MPLS
1976 inconsistent_match(enum ofputil_protocol *usable_protocols)
1978 *usable_protocols &= OFPUTIL_P_OF10_ANY;
1981 /* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
1982 * caller must restore them.
1984 * Modifies some actions, filling in fields that could not be properly set
1985 * without context. */
1987 ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
1988 struct flow *flow, ofp_port_t max_ports,
1989 uint8_t table_id, uint8_t n_tables)
1991 const struct ofpact_enqueue *enqueue;
1992 const struct mf_field *mf;
1996 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
1999 case OFPACT_CONTROLLER:
2002 case OFPACT_ENQUEUE:
2003 enqueue = ofpact_get_ENQUEUE(a);
2004 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
2005 && enqueue->port != OFPP_IN_PORT
2006 && enqueue->port != OFPP_LOCAL) {
2007 return OFPERR_OFPBAC_BAD_OUT_PORT;
2011 case OFPACT_OUTPUT_REG:
2012 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
2015 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
2017 case OFPACT_SET_VLAN_VID:
2018 /* Remember if we saw a vlan tag in the flow to aid translating to
2019 * OpenFlow 1.1+ if need be. */
2020 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
2021 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
2022 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
2023 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2024 inconsistent_match(usable_protocols);
2026 /* Temporary mark that we have a vlan tag. */
2027 flow->vlan_tci |= htons(VLAN_CFI);
2030 case OFPACT_SET_VLAN_PCP:
2031 /* Remember if we saw a vlan tag in the flow to aid translating to
2032 * OpenFlow 1.1+ if need be. */
2033 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
2034 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
2035 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
2036 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2037 inconsistent_match(usable_protocols);
2039 /* Temporary mark that we have a vlan tag. */
2040 flow->vlan_tci |= htons(VLAN_CFI);
2043 case OFPACT_STRIP_VLAN:
2044 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
2045 inconsistent_match(usable_protocols);
2047 /* Temporary mark that we have no vlan tag. */
2048 flow->vlan_tci = htons(0);
2051 case OFPACT_PUSH_VLAN:
2052 if (flow->vlan_tci & htons(VLAN_CFI)) {
2053 /* Multiple VLAN headers not supported. */
2054 return OFPERR_OFPBAC_BAD_TAG;
2056 /* Temporary mark that we have a vlan tag. */
2057 flow->vlan_tci |= htons(VLAN_CFI);
2060 case OFPACT_SET_ETH_SRC:
2061 case OFPACT_SET_ETH_DST:
2064 case OFPACT_SET_IPV4_SRC:
2065 case OFPACT_SET_IPV4_DST:
2066 if (flow->dl_type != htons(ETH_TYPE_IP)) {
2067 inconsistent_match(usable_protocols);
2071 case OFPACT_SET_IP_DSCP:
2072 case OFPACT_SET_IP_ECN:
2073 case OFPACT_SET_IP_TTL:
2074 case OFPACT_DEC_TTL:
2075 if (!is_ip_any(flow)) {
2076 inconsistent_match(usable_protocols);
2080 case OFPACT_SET_L4_SRC_PORT:
2081 if (!is_ip_any(flow) ||
2082 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
2083 && flow->nw_proto != IPPROTO_SCTP)) {
2084 inconsistent_match(usable_protocols);
2086 /* Note on which transport protocol the port numbers are set.
2087 * This allows this set action to be converted to an OF1.2 set field
2089 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
2092 case OFPACT_SET_L4_DST_PORT:
2093 if (!is_ip_any(flow) ||
2094 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
2095 && flow->nw_proto != IPPROTO_SCTP)) {
2096 inconsistent_match(usable_protocols);
2098 /* Note on which transport protocol the port numbers are set.
2099 * This allows this set action to be converted to an OF1.2 set field
2101 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
2104 case OFPACT_REG_MOVE:
2105 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
2107 case OFPACT_REG_LOAD:
2108 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
2110 case OFPACT_SET_FIELD:
2111 mf = ofpact_get_SET_FIELD(a)->field;
2112 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
2113 if (!mf_are_prereqs_ok(mf, flow) ||
2114 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
2115 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
2117 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
2119 /* Remember if we saw a vlan tag in the flow to aid translating to
2120 * OpenFlow 1.1 if need be. */
2121 ofpact_get_SET_FIELD(a)->flow_has_vlan =
2122 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
2123 if (mf->id == MFF_VLAN_TCI) {
2124 /* The set field may add or remove the vlan tag,
2125 * Mark the status temporarily. */
2126 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
2130 case OFPACT_STACK_PUSH:
2131 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
2133 case OFPACT_STACK_POP:
2134 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
2136 case OFPACT_SET_MPLS_LABEL:
2137 case OFPACT_SET_MPLS_TC:
2138 case OFPACT_SET_MPLS_TTL:
2139 case OFPACT_DEC_MPLS_TTL:
2140 if (!eth_type_mpls(flow->dl_type)) {
2141 inconsistent_match(usable_protocols);
2145 case OFPACT_SET_TUNNEL:
2146 case OFPACT_SET_QUEUE:
2147 case OFPACT_POP_QUEUE:
2148 case OFPACT_RESUBMIT:
2151 case OFPACT_FIN_TIMEOUT:
2152 if (flow->nw_proto != IPPROTO_TCP) {
2153 inconsistent_match(usable_protocols);
2158 return learn_check(ofpact_get_LEARN(a), flow);
2160 case OFPACT_MULTIPATH:
2161 return multipath_check(ofpact_get_MULTIPATH(a), flow);
2167 case OFPACT_PUSH_MPLS:
2168 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
2169 /* The packet is now MPLS and the MPLS payload is opaque.
2170 * Thus nothing can be assumed about the network protocol.
2171 * Temporarily mark that we have no nw_proto. */
2175 case OFPACT_POP_MPLS:
2176 if (!eth_type_mpls(flow->dl_type)) {
2177 inconsistent_match(usable_protocols);
2179 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
2185 case OFPACT_CLEAR_ACTIONS:
2188 case OFPACT_WRITE_ACTIONS: {
2189 /* Use a temporary copy of 'usable_protocols' because we can't check
2190 * consistency of an action set. */
2191 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2192 enum ofputil_protocol p = *usable_protocols;
2193 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
2194 flow, max_ports, table_id, n_tables, &p);
2197 case OFPACT_WRITE_METADATA:
2200 case OFPACT_METER: {
2201 uint32_t mid = ofpact_get_METER(a)->meter_id;
2202 if (mid == 0 || mid > OFPM13_MAX) {
2203 return OFPERR_OFPMMFC_INVALID_METER;
2208 case OFPACT_GOTO_TABLE: {
2209 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
2210 if ((table_id != 255 && goto_table <= table_id)
2211 || (n_tables != 255 && goto_table >= n_tables)) {
2212 return OFPERR_OFPBRC_BAD_TABLE_ID;
2225 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2226 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
2227 * switch with no more than 'max_ports' ports.
2229 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
2230 * '*usable_protocols' the protocols that forbid the inconsistency. (An
2231 * example of an inconsistency between match and actions is a flow that does
2232 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
2234 * May annotate ofpacts with information gathered from the 'flow'.
2236 * May temporarily modify 'flow', but restores the changes before returning. */
2238 ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
2239 struct flow *flow, ofp_port_t max_ports,
2240 uint8_t table_id, uint8_t n_tables,
2241 enum ofputil_protocol *usable_protocols)
2244 ovs_be16 dl_type = flow->dl_type;
2245 ovs_be16 vlan_tci = flow->vlan_tci;
2246 uint8_t nw_proto = flow->nw_proto;
2247 enum ofperr error = 0;
2249 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2250 error = ofpact_check__(usable_protocols, a, flow,
2251 max_ports, table_id, n_tables);
2256 /* Restore fields that may have been modified. */
2257 flow->dl_type = dl_type;
2258 flow->vlan_tci = vlan_tci;
2259 flow->nw_proto = nw_proto;
2263 /* Like ofpacts_check(), but reports inconsistencies as
2264 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
2266 ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
2267 struct flow *flow, ofp_port_t max_ports,
2268 uint8_t table_id, uint8_t n_tables,
2269 enum ofputil_protocol usable_protocols)
2271 enum ofputil_protocol p = usable_protocols;
2274 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
2275 table_id, n_tables, &p);
2276 return (error ? error
2277 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
2281 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
2282 * in the appropriate order as defined by the OpenFlow spec. */
2284 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
2285 uint32_t allowed_ovsinsts)
2287 const struct ofpact *a;
2288 enum ovs_instruction_type inst;
2290 inst = OVSINST_OFPIT13_METER;
2291 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2292 enum ovs_instruction_type next;
2294 next = ovs_instruction_type_from_ofpact_type(a->type);
2296 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
2299 const char *name = ovs_instruction_name_from_type(inst);
2300 const char *next_name = ovs_instruction_name_from_type(next);
2303 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
2304 "1.1+ compatibility", name);
2306 VLOG_WARN("invalid instruction ordering: %s must appear "
2307 "before %s, for OpenFlow 1.1+ compatibility",
2310 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
2312 if (!((1u << next) & allowed_ovsinsts)) {
2313 const char *name = ovs_instruction_name_from_type(next);
2315 VLOG_WARN("%s instruction not allowed here", name);
2316 return OFPERR_OFPBIC_UNSUP_INST;
2325 /* Converting ofpacts to Nicira OpenFlow extensions. */
2328 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
2331 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
2333 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
2334 output_reg->src.n_bits);
2335 naor->src = htonl(output_reg->src.field->nxm_header);
2336 naor->max_len = htons(output_reg->max_len);
2340 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
2343 struct nx_action_resubmit *nar;
2345 if (resubmit->table_id == 0xff
2346 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
2347 nar = ofputil_put_NXAST_RESUBMIT(out);
2349 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
2350 nar->table = resubmit->table_id;
2352 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
2356 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
2359 uint64_t tun_id = tunnel->tun_id;
2361 if (tun_id <= UINT32_MAX
2362 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
2363 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
2365 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
2370 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
2373 struct nx_action_write_metadata *nawm;
2375 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
2376 nawm->metadata = om->metadata;
2377 nawm->mask = om->mask;
2381 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
2383 size_t start_ofs = ofpbuf_size(out);
2384 struct nx_action_note *nan;
2385 unsigned int remainder;
2388 ofputil_put_NXAST_NOTE(out);
2389 ofpbuf_set_size(out, ofpbuf_size(out) - sizeof nan->note);
2391 ofpbuf_put(out, note->data, note->length);
2393 len = ofpbuf_size(out) - start_ofs;
2394 remainder = len % OFP_ACTION_ALIGN;
2396 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
2398 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
2399 nan->len = htons(ofpbuf_size(out) - start_ofs);
2403 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
2406 struct nx_action_controller *nac;
2408 nac = ofputil_put_NXAST_CONTROLLER(out);
2409 nac->max_len = htons(oc->max_len);
2410 nac->controller_id = htons(oc->controller_id);
2411 nac->reason = oc->reason;
2415 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
2418 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
2419 ofputil_put_NXAST_DEC_TTL(out);
2421 struct nx_action_cnt_ids *nac_ids =
2422 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
2423 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
2427 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
2428 nac_ids->n_controllers = htons(oc_ids->n_controllers);
2430 ids = ofpbuf_put_zeros(out, ids_len);
2431 for (i = 0; i < oc_ids->n_controllers; i++) {
2432 ids[i] = htons(oc_ids->cnt_ids[i]);
2438 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
2441 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
2442 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
2443 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
2447 ofpact_sample_to_nxast(const struct ofpact_sample *os,
2450 struct nx_action_sample *nas;
2452 nas = ofputil_put_NXAST_SAMPLE(out);
2453 nas->probability = htons(os->probability);
2454 nas->collector_set_id = htonl(os->collector_set_id);
2455 nas->obs_domain_id = htonl(os->obs_domain_id);
2456 nas->obs_point_id = htonl(os->obs_point_id);
2460 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
2463 case OFPACT_CONTROLLER:
2464 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
2467 case OFPACT_OUTPUT_REG:
2468 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
2472 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
2475 case OFPACT_REG_MOVE:
2476 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
2479 case OFPACT_REG_LOAD:
2480 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
2483 case OFPACT_STACK_PUSH:
2484 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
2487 case OFPACT_STACK_POP:
2488 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
2491 case OFPACT_DEC_TTL:
2492 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
2495 case OFPACT_SET_MPLS_LABEL:
2496 ofputil_put_NXAST_SET_MPLS_LABEL(out)->label
2497 = ofpact_get_SET_MPLS_LABEL(a)->label;
2500 case OFPACT_SET_MPLS_TC:
2501 ofputil_put_NXAST_SET_MPLS_TC(out)->tc
2502 = ofpact_get_SET_MPLS_TC(a)->tc;
2505 case OFPACT_SET_MPLS_TTL:
2506 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
2507 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2510 case OFPACT_DEC_MPLS_TTL:
2511 ofputil_put_NXAST_DEC_MPLS_TTL(out);
2514 case OFPACT_SET_TUNNEL:
2515 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
2518 case OFPACT_WRITE_METADATA:
2519 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
2522 case OFPACT_SET_QUEUE:
2523 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
2524 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2527 case OFPACT_POP_QUEUE:
2528 ofputil_put_NXAST_POP_QUEUE(out);
2531 case OFPACT_FIN_TIMEOUT:
2532 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
2535 case OFPACT_RESUBMIT:
2536 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
2540 learn_to_nxast(ofpact_get_LEARN(a), out);
2543 case OFPACT_MULTIPATH:
2544 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
2548 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
2552 ofputil_put_NXAST_EXIT(out);
2555 case OFPACT_PUSH_MPLS:
2556 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
2557 ofpact_get_PUSH_MPLS(a)->ethertype;
2560 case OFPACT_POP_MPLS:
2561 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2562 ofpact_get_POP_MPLS(a)->ethertype;
2566 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2571 case OFPACT_ENQUEUE:
2572 case OFPACT_SET_VLAN_VID:
2573 case OFPACT_SET_VLAN_PCP:
2574 case OFPACT_STRIP_VLAN:
2575 case OFPACT_PUSH_VLAN:
2576 case OFPACT_SET_ETH_SRC:
2577 case OFPACT_SET_ETH_DST:
2578 case OFPACT_SET_IPV4_SRC:
2579 case OFPACT_SET_IPV4_DST:
2580 case OFPACT_SET_IP_DSCP:
2581 case OFPACT_SET_IP_ECN:
2582 case OFPACT_SET_IP_TTL:
2583 case OFPACT_SET_L4_SRC_PORT:
2584 case OFPACT_SET_L4_DST_PORT:
2585 case OFPACT_WRITE_ACTIONS:
2586 case OFPACT_CLEAR_ACTIONS:
2587 case OFPACT_GOTO_TABLE:
2589 case OFPACT_SET_FIELD:
2594 /* Converting ofpacts to OpenFlow 1.0. */
2597 ofpact_output_to_openflow10(const struct ofpact_output *output,
2600 struct ofp10_action_output *oao;
2602 oao = ofputil_put_OFPAT10_OUTPUT(out);
2603 oao->port = htons(ofp_to_u16(output->port));
2604 oao->max_len = htons(output->max_len);
2608 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2611 struct ofp10_action_enqueue *oae;
2613 oae = ofputil_put_OFPAT10_ENQUEUE(out);
2614 oae->port = htons(ofp_to_u16(enqueue->port));
2615 oae->queue_id = htonl(enqueue->queue);
2619 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2623 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2626 case OFPACT_ENQUEUE:
2627 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2630 case OFPACT_SET_VLAN_VID:
2631 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2632 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2635 case OFPACT_SET_VLAN_PCP:
2636 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2637 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2640 case OFPACT_STRIP_VLAN:
2641 ofputil_put_OFPAT10_STRIP_VLAN(out);
2644 case OFPACT_SET_ETH_SRC:
2645 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2646 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2649 case OFPACT_SET_ETH_DST:
2650 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2651 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2654 case OFPACT_SET_IPV4_SRC:
2655 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2656 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2659 case OFPACT_SET_IPV4_DST:
2660 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2661 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2664 case OFPACT_SET_IP_DSCP:
2665 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2666 = ofpact_get_SET_IP_DSCP(a)->dscp;
2669 case OFPACT_SET_L4_SRC_PORT:
2670 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2671 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2674 case OFPACT_SET_L4_DST_PORT:
2675 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2676 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2679 case OFPACT_PUSH_VLAN:
2680 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
2681 * follow this action. */
2684 case OFPACT_SET_IP_ECN:
2685 case OFPACT_SET_IP_TTL:
2686 case OFPACT_CLEAR_ACTIONS:
2687 case OFPACT_WRITE_ACTIONS:
2688 case OFPACT_GOTO_TABLE:
2696 case OFPACT_SET_FIELD:
2697 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2700 case OFPACT_CONTROLLER:
2701 case OFPACT_OUTPUT_REG:
2703 case OFPACT_REG_MOVE:
2704 case OFPACT_REG_LOAD:
2705 case OFPACT_STACK_PUSH:
2706 case OFPACT_STACK_POP:
2707 case OFPACT_DEC_TTL:
2708 case OFPACT_SET_MPLS_LABEL:
2709 case OFPACT_SET_MPLS_TC:
2710 case OFPACT_SET_MPLS_TTL:
2711 case OFPACT_DEC_MPLS_TTL:
2712 case OFPACT_SET_TUNNEL:
2713 case OFPACT_WRITE_METADATA:
2714 case OFPACT_SET_QUEUE:
2715 case OFPACT_POP_QUEUE:
2716 case OFPACT_FIN_TIMEOUT:
2717 case OFPACT_RESUBMIT:
2719 case OFPACT_MULTIPATH:
2722 case OFPACT_PUSH_MPLS:
2723 case OFPACT_POP_MPLS:
2725 ofpact_to_nxast(a, out);
2730 /* Converting ofpacts to OpenFlow 1.1. */
2733 ofpact_output_to_openflow11(const struct ofpact_output *output,
2736 struct ofp11_action_output *oao;
2738 oao = ofputil_put_OFPAT11_OUTPUT(out);
2739 oao->port = ofputil_port_to_ofp11(output->port);
2740 oao->max_len = htons(output->max_len);
2744 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2747 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2748 && (!dec_ttl->ofpact.compat ||
2749 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2750 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2752 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2757 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2761 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2763 case OFPACT_ENQUEUE:
2767 case OFPACT_SET_VLAN_VID:
2768 /* Push a VLAN tag, if one was not seen at action validation time. */
2769 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan
2770 && ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2771 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2772 = htons(ETH_TYPE_VLAN_8021Q);
2774 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2775 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2778 case OFPACT_SET_VLAN_PCP:
2779 /* Push a VLAN tag, if one was not seen at action validation time. */
2780 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan
2781 && ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2782 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2783 = htons(ETH_TYPE_VLAN_8021Q);
2785 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2786 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2789 case OFPACT_STRIP_VLAN:
2790 ofputil_put_OFPAT11_POP_VLAN(out);
2793 case OFPACT_PUSH_VLAN:
2794 /* XXX ETH_TYPE_VLAN_8021AD case */
2795 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2796 htons(ETH_TYPE_VLAN_8021Q);
2799 case OFPACT_SET_QUEUE:
2800 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2801 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2804 case OFPACT_SET_ETH_SRC:
2805 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2806 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2809 case OFPACT_SET_ETH_DST:
2810 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2811 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2814 case OFPACT_SET_IPV4_SRC:
2815 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2816 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2819 case OFPACT_SET_IPV4_DST:
2820 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2821 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2824 case OFPACT_SET_IP_DSCP:
2825 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2826 = ofpact_get_SET_IP_DSCP(a)->dscp;
2829 case OFPACT_SET_IP_ECN:
2830 ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2831 = ofpact_get_SET_IP_ECN(a)->ecn;
2834 case OFPACT_SET_IP_TTL:
2835 ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2836 = ofpact_get_SET_IP_TTL(a)->ttl;
2839 case OFPACT_SET_L4_SRC_PORT:
2840 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2841 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2844 case OFPACT_SET_L4_DST_PORT:
2845 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2846 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2849 case OFPACT_DEC_TTL:
2850 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2853 case OFPACT_SET_MPLS_LABEL:
2854 ofputil_put_OFPAT11_SET_MPLS_LABEL(out)->mpls_label
2855 = ofpact_get_SET_MPLS_LABEL(a)->label;
2858 case OFPACT_SET_MPLS_TC:
2859 ofputil_put_OFPAT11_SET_MPLS_TC(out)->mpls_tc
2860 = ofpact_get_SET_MPLS_TC(a)->tc;
2863 case OFPACT_SET_MPLS_TTL:
2864 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2865 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2868 case OFPACT_DEC_MPLS_TTL:
2869 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2872 case OFPACT_WRITE_METADATA:
2873 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2876 case OFPACT_PUSH_MPLS:
2877 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2878 ofpact_get_PUSH_MPLS(a)->ethertype;
2881 case OFPACT_POP_MPLS:
2882 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2883 ofpact_get_POP_MPLS(a)->ethertype;
2887 case OFPACT_CLEAR_ACTIONS:
2888 case OFPACT_WRITE_ACTIONS:
2889 case OFPACT_GOTO_TABLE:
2894 ofputil_put_OFPAT11_GROUP(out)->group_id =
2895 htonl(ofpact_get_GROUP(a)->group_id);
2898 case OFPACT_SET_FIELD:
2899 set_field_to_openflow(ofpact_get_SET_FIELD(a), out);
2902 case OFPACT_CONTROLLER:
2903 case OFPACT_OUTPUT_REG:
2905 case OFPACT_REG_MOVE:
2906 case OFPACT_REG_LOAD:
2907 case OFPACT_STACK_PUSH:
2908 case OFPACT_STACK_POP:
2909 case OFPACT_SET_TUNNEL:
2910 case OFPACT_POP_QUEUE:
2911 case OFPACT_FIN_TIMEOUT:
2912 case OFPACT_RESUBMIT:
2914 case OFPACT_MULTIPATH:
2918 ofpact_to_nxast(a, out);
2923 /* Output deprecated set actions as set_field actions. */
2925 ofpact_to_openflow12(const struct ofpact *a, struct ofpbuf *out)
2927 enum mf_field_id field;
2928 union mf_value value;
2929 struct ofpact_l4_port *l4port;
2933 * Convert actions deprecated in OpenFlow 1.2 to Set Field actions,
2936 switch ((int)a->type) {
2937 case OFPACT_SET_VLAN_VID:
2938 case OFPACT_SET_VLAN_PCP:
2939 case OFPACT_SET_ETH_SRC:
2940 case OFPACT_SET_ETH_DST:
2941 case OFPACT_SET_IPV4_SRC:
2942 case OFPACT_SET_IPV4_DST:
2943 case OFPACT_SET_IP_DSCP:
2944 case OFPACT_SET_IP_ECN:
2945 case OFPACT_SET_L4_SRC_PORT:
2946 case OFPACT_SET_L4_DST_PORT:
2947 case OFPACT_SET_MPLS_LABEL:
2948 case OFPACT_SET_MPLS_TC:
2949 case OFPACT_SET_TUNNEL: /* Convert to a set_field, too. */
2951 switch ((int)a->type) {
2953 case OFPACT_SET_VLAN_VID:
2954 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan &&
2955 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2956 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2957 = htons(ETH_TYPE_VLAN_8021Q);
2959 field = MFF_VLAN_VID;
2960 /* Set-Field on OXM_OF_VLAN_VID must have OFPVID_PRESENT set. */
2961 value.be16 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid
2962 | OFPVID12_PRESENT);
2965 case OFPACT_SET_VLAN_PCP:
2966 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan &&
2967 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2968 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2969 = htons(ETH_TYPE_VLAN_8021Q);
2971 field = MFF_VLAN_PCP;
2972 value.u8 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2975 case OFPACT_SET_ETH_SRC:
2976 field = MFF_ETH_SRC;
2977 memcpy(value.mac, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2980 case OFPACT_SET_ETH_DST:
2981 field = MFF_ETH_DST;
2982 memcpy(value.mac, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2985 case OFPACT_SET_IPV4_SRC:
2986 field = MFF_IPV4_SRC;
2987 value.be32 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2990 case OFPACT_SET_IPV4_DST:
2991 field = MFF_IPV4_DST;
2992 value.be32 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2995 case OFPACT_SET_IP_DSCP:
2996 field = MFF_IP_DSCP_SHIFTED; /* OXM_OF_IP_DSCP */
2997 value.u8 = ofpact_get_SET_IP_DSCP(a)->dscp >> 2;
3000 case OFPACT_SET_IP_ECN:
3002 value.u8 = ofpact_get_SET_IP_ECN(a)->ecn;
3005 case OFPACT_SET_L4_SRC_PORT:
3006 /* We keep track of IP protocol while translating actions to be
3007 * able to translate to the proper OXM type.
3008 * If the IP protocol type is unknown, the translation cannot
3009 * be performed and we will send the action using the original
3011 l4port = ofpact_get_SET_L4_SRC_PORT(a);
3012 proto = l4port->flow_ip_proto;
3013 field = proto == IPPROTO_TCP ? MFF_TCP_SRC
3014 : proto == IPPROTO_UDP ? MFF_UDP_SRC
3015 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
3016 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
3017 value.be16 = htons(l4port->port);
3020 case OFPACT_SET_L4_DST_PORT:
3021 l4port = ofpact_get_SET_L4_DST_PORT(a);
3022 proto = l4port->flow_ip_proto;
3023 field = proto == IPPROTO_TCP ? MFF_TCP_DST
3024 : proto == IPPROTO_UDP ? MFF_UDP_DST
3025 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
3026 : MFF_N_IDS; /* RFC: Unknown IP proto, do not translate. */
3027 value.be16 = htons(l4port->port);
3030 case OFPACT_SET_MPLS_LABEL:
3031 field = MFF_MPLS_LABEL;
3032 value.be32 = ofpact_get_SET_MPLS_LABEL(a)->label;
3035 case OFPACT_SET_MPLS_TC:
3036 field = MFF_MPLS_TC;
3037 value.u8 = ofpact_get_SET_MPLS_TC(a)->tc;
3040 case OFPACT_SET_TUNNEL:
3042 value.be64 = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
3049 /* Put the action out as a set field action, if possible. */
3050 if (field < MFF_N_IDS) {
3051 uint64_t ofpacts_stub[128 / 8];
3052 struct ofpbuf sf_act;
3053 struct ofpact_set_field *sf;
3055 ofpbuf_use_stub(&sf_act, ofpacts_stub, sizeof ofpacts_stub);
3056 sf = ofpact_put_SET_FIELD(&sf_act);
3057 sf->field = mf_from_id(field);
3058 memcpy(&sf->value, &value, sf->field->n_bytes);
3059 set_field_to_openflow(sf, out);
3064 ofpact_to_openflow11(a, out);
3067 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
3068 * actions in 'openflow', appending the actions to any existing data in
3071 ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
3072 struct ofpbuf *openflow,
3073 enum ofp_version ofp_version)
3075 const struct ofpact *a;
3076 size_t start_size = ofpbuf_size(openflow);
3078 void (*translate)(const struct ofpact *a, struct ofpbuf *out) =
3079 (ofp_version == OFP10_VERSION) ? ofpact_to_openflow10 :
3080 (ofp_version == OFP11_VERSION) ? ofpact_to_openflow11 :
3081 ofpact_to_openflow12;
3083 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3084 translate(a, openflow);
3086 return ofpbuf_size(openflow) - start_size;
3090 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
3092 struct ofp11_instruction_actions *oia;
3094 /* Update the instruction's length (or, if it's empty, delete it). */
3095 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
3096 if (ofpbuf_size(openflow) > ofs + sizeof *oia) {
3097 oia->len = htons(ofpbuf_size(openflow) - ofs);
3099 ofpbuf_set_size(openflow, ofs);
3104 ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
3106 struct ofpbuf *openflow,
3107 enum ofp_version ofp_version)
3109 const struct ofpact *a;
3111 if (ofp_version == OFP10_VERSION) {
3112 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
3117 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3118 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
3119 case OVSINST_OFPIT11_CLEAR_ACTIONS:
3120 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
3123 case OVSINST_OFPIT11_GOTO_TABLE: {
3124 struct ofp11_instruction_goto_table *oigt;
3125 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
3126 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
3127 memset(oigt->pad, 0, sizeof oigt->pad);
3131 case OVSINST_OFPIT11_WRITE_METADATA: {
3132 const struct ofpact_metadata *om;
3133 struct ofp11_instruction_write_metadata *oiwm;
3135 om = ofpact_get_WRITE_METADATA(a);
3136 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
3137 oiwm->metadata = om->metadata;
3138 oiwm->metadata_mask = om->mask;
3142 case OVSINST_OFPIT13_METER:
3143 if (ofp_version >= OFP13_VERSION) {
3144 const struct ofpact_meter *om;
3145 struct ofp13_instruction_meter *oim;
3147 om = ofpact_get_METER(a);
3148 oim = instruction_put_OFPIT13_METER(openflow);
3149 oim->meter_id = htonl(om->meter_id);
3153 case OVSINST_OFPIT11_APPLY_ACTIONS: {
3154 const size_t ofs = ofpbuf_size(openflow);
3155 const size_t ofpacts_len_left =
3156 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
3157 const struct ofpact *action;
3158 const struct ofpact *processed = a;
3160 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
3161 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
3162 if (ovs_instruction_type_from_ofpact_type(action->type)
3163 != OVSINST_OFPIT11_APPLY_ACTIONS) {
3166 if (ofp_version == OFP11_VERSION) {
3167 ofpact_to_openflow11(action, openflow);
3169 ofpact_to_openflow12(action, openflow);
3173 ofpacts_update_instruction_actions(openflow, ofs);
3178 case OVSINST_OFPIT11_WRITE_ACTIONS: {
3179 const size_t ofs = ofpbuf_size(openflow);
3180 const struct ofpact_nest *on;
3182 on = ofpact_get_WRITE_ACTIONS(a);
3183 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
3184 ofpacts_put_openflow_actions(on->actions,
3185 ofpact_nest_get_action_len(on),
3186 openflow, ofp_version);
3187 ofpacts_update_instruction_actions(openflow, ofs);
3195 /* Sets of supported actions. */
3197 /* Two-way translation between OVS's internal "OFPACT_*" representation of
3198 * actions and the "OFPAT_*" representation used in some OpenFlow version.
3199 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
3200 * instance is specific to one OpenFlow version.) */
3202 enum ofpact_type ofpact; /* Internal name for action type. */
3203 int ofpat; /* OFPAT_* number from OpenFlow spec. */
3206 static const struct ofpact_map *
3207 get_ofpact_map(enum ofp_version version)
3209 /* OpenFlow 1.0 actions. */
3210 static const struct ofpact_map of10[] = {
3211 { OFPACT_OUTPUT, 0 },
3212 { OFPACT_SET_VLAN_VID, 1 },
3213 { OFPACT_SET_VLAN_PCP, 2 },
3214 { OFPACT_STRIP_VLAN, 3 },
3215 { OFPACT_SET_ETH_SRC, 4 },
3216 { OFPACT_SET_ETH_DST, 5 },
3217 { OFPACT_SET_IPV4_SRC, 6 },
3218 { OFPACT_SET_IPV4_DST, 7 },
3219 { OFPACT_SET_IP_DSCP, 8 },
3220 { OFPACT_SET_L4_SRC_PORT, 9 },
3221 { OFPACT_SET_L4_DST_PORT, 10 },
3222 { OFPACT_ENQUEUE, 11 },
3226 /* OpenFlow 1.1 actions. */
3227 static const struct ofpact_map of11[] = {
3228 { OFPACT_OUTPUT, 0 },
3229 { OFPACT_SET_VLAN_VID, 1 },
3230 { OFPACT_SET_VLAN_PCP, 2 },
3231 { OFPACT_SET_ETH_SRC, 3 },
3232 { OFPACT_SET_ETH_DST, 4 },
3233 { OFPACT_SET_IPV4_SRC, 5 },
3234 { OFPACT_SET_IPV4_DST, 6 },
3235 { OFPACT_SET_IP_DSCP, 7 },
3236 { OFPACT_SET_IP_ECN, 8 },
3237 { OFPACT_SET_L4_SRC_PORT, 9 },
3238 { OFPACT_SET_L4_DST_PORT, 10 },
3239 /* OFPAT_COPY_TTL_OUT (11) not supported. */
3240 /* OFPAT_COPY_TTL_IN (12) not supported. */
3241 { OFPACT_SET_MPLS_LABEL, 13 },
3242 { OFPACT_SET_MPLS_TC, 14 },
3243 { OFPACT_SET_MPLS_TTL, 15 },
3244 { OFPACT_DEC_MPLS_TTL, 16 },
3245 { OFPACT_PUSH_VLAN, 17 },
3246 { OFPACT_STRIP_VLAN, 18 },
3247 { OFPACT_PUSH_MPLS, 19 },
3248 { OFPACT_POP_MPLS, 20 },
3249 { OFPACT_SET_QUEUE, 21 },
3250 { OFPACT_GROUP, 22 },
3251 { OFPACT_SET_IP_TTL, 23 },
3252 { OFPACT_DEC_TTL, 24 },
3256 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
3257 static const struct ofpact_map of12[] = {
3258 { OFPACT_OUTPUT, 0 },
3259 /* OFPAT_COPY_TTL_OUT (11) not supported. */
3260 /* OFPAT_COPY_TTL_IN (12) not supported. */
3261 { OFPACT_SET_MPLS_TTL, 15 },
3262 { OFPACT_DEC_MPLS_TTL, 16 },
3263 { OFPACT_PUSH_VLAN, 17 },
3264 { OFPACT_STRIP_VLAN, 18 },
3265 { OFPACT_PUSH_MPLS, 19 },
3266 { OFPACT_POP_MPLS, 20 },
3267 { OFPACT_SET_QUEUE, 21 },
3268 { OFPACT_GROUP, 22 },
3269 { OFPACT_SET_IP_TTL, 23 },
3270 { OFPACT_DEC_TTL, 24 },
3271 { OFPACT_SET_FIELD, 25 },
3272 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
3273 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
3293 /* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
3294 * values, into a bitmap of actions suitable for OpenFlow 'version', and
3295 * returns the result. */
3297 ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
3299 uint32_t openflow_bitmap = 0;
3300 const struct ofpact_map *x;
3302 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
3303 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
3304 openflow_bitmap |= 1u << x->ofpat;
3307 return htonl(openflow_bitmap);
3310 /* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
3311 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
3312 * and returns the result. */
3314 ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
3316 uint64_t ofpact_bitmap = 0;
3317 const struct ofpact_map *x;
3319 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
3320 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
3321 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
3324 return ofpact_bitmap;
3327 /* Appends to 's' a string representation of the set of OFPACT_* represented
3328 * by 'ofpacts_bitmap'. */
3330 ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
3332 if (!ofpacts_bitmap) {
3333 ds_put_cstr(s, "<none>");
3335 while (ofpacts_bitmap) {
3336 ds_put_format(s, "%s ",
3337 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
3338 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
3344 /* Returns true if 'action' outputs to 'port', false otherwise. */
3346 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
3348 switch (ofpact->type) {
3350 return ofpact_get_OUTPUT(ofpact)->port == port;
3351 case OFPACT_ENQUEUE:
3352 return ofpact_get_ENQUEUE(ofpact)->port == port;
3353 case OFPACT_CONTROLLER:
3354 return port == OFPP_CONTROLLER;
3356 case OFPACT_OUTPUT_REG:
3358 case OFPACT_SET_VLAN_VID:
3359 case OFPACT_SET_VLAN_PCP:
3360 case OFPACT_STRIP_VLAN:
3361 case OFPACT_PUSH_VLAN:
3362 case OFPACT_SET_ETH_SRC:
3363 case OFPACT_SET_ETH_DST:
3364 case OFPACT_SET_IPV4_SRC:
3365 case OFPACT_SET_IPV4_DST:
3366 case OFPACT_SET_IP_DSCP:
3367 case OFPACT_SET_IP_ECN:
3368 case OFPACT_SET_IP_TTL:
3369 case OFPACT_SET_L4_SRC_PORT:
3370 case OFPACT_SET_L4_DST_PORT:
3371 case OFPACT_REG_MOVE:
3372 case OFPACT_REG_LOAD:
3373 case OFPACT_SET_FIELD:
3374 case OFPACT_STACK_PUSH:
3375 case OFPACT_STACK_POP:
3376 case OFPACT_DEC_TTL:
3377 case OFPACT_SET_MPLS_LABEL:
3378 case OFPACT_SET_MPLS_TC:
3379 case OFPACT_SET_MPLS_TTL:
3380 case OFPACT_DEC_MPLS_TTL:
3381 case OFPACT_SET_TUNNEL:
3382 case OFPACT_WRITE_METADATA:
3383 case OFPACT_SET_QUEUE:
3384 case OFPACT_POP_QUEUE:
3385 case OFPACT_FIN_TIMEOUT:
3386 case OFPACT_RESUBMIT:
3388 case OFPACT_MULTIPATH:
3391 case OFPACT_PUSH_MPLS:
3392 case OFPACT_POP_MPLS:
3394 case OFPACT_CLEAR_ACTIONS:
3395 case OFPACT_WRITE_ACTIONS:
3396 case OFPACT_GOTO_TABLE:
3404 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3405 * to 'port', false otherwise. */
3407 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
3410 const struct ofpact *a;
3412 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3413 if (ofpact_outputs_to_port(a, port)) {
3421 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
3422 * to 'group', false otherwise. */
3424 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
3427 const struct ofpact *a;
3429 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3430 if (a->type == OFPACT_GROUP
3431 && ofpact_get_GROUP(a)->group_id == group_id) {
3440 ofpacts_equal(const struct ofpact *a, size_t a_len,
3441 const struct ofpact *b, size_t b_len)
3443 return a_len == b_len && !memcmp(a, b, a_len);
3446 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
3447 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
3449 * This function relies on the order of 'ofpacts' being correct (as checked by
3450 * ofpacts_verify()). */
3452 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
3454 const struct ofpact *a;
3456 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3457 enum ovs_instruction_type inst;
3459 inst = ovs_instruction_type_from_ofpact_type(a->type);
3460 if (a->type == OFPACT_METER) {
3461 return ofpact_get_METER(a)->meter_id;
3462 } else if (inst > OVSINST_OFPIT13_METER) {
3470 /* Formatting ofpacts. */
3473 print_note(const struct ofpact_note *note, struct ds *string)
3477 ds_put_cstr(string, "note:");
3478 for (i = 0; i < note->length; i++) {
3480 ds_put_char(string, '.');
3482 ds_put_format(string, "%02"PRIx8, note->data[i]);
3487 print_dec_ttl(const struct ofpact_cnt_ids *ids,
3492 ds_put_cstr(s, "dec_ttl");
3493 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
3494 ds_put_cstr(s, "(");
3495 for (i = 0; i < ids->n_controllers; i++) {
3497 ds_put_cstr(s, ",");
3499 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
3501 ds_put_cstr(s, ")");
3506 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
3509 ds_put_cstr(s, "fin_timeout(");
3510 if (fin_timeout->fin_idle_timeout) {
3511 ds_put_format(s, "idle_timeout=%"PRIu16",",
3512 fin_timeout->fin_idle_timeout);
3514 if (fin_timeout->fin_hard_timeout) {
3515 ds_put_format(s, "hard_timeout=%"PRIu16",",
3516 fin_timeout->fin_hard_timeout);
3519 ds_put_char(s, ')');
3523 ofpact_format(const struct ofpact *a, struct ds *s)
3525 const struct ofpact_enqueue *enqueue;
3526 const struct ofpact_resubmit *resubmit;
3527 const struct ofpact_controller *controller;
3528 const struct ofpact_metadata *metadata;
3529 const struct ofpact_tunnel *tunnel;
3530 const struct ofpact_sample *sample;
3531 const struct ofpact_set_field *set_field;
3532 const struct mf_field *mf;
3537 port = ofpact_get_OUTPUT(a)->port;
3538 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
3539 ds_put_format(s, "output:%"PRIu16, port);
3541 ofputil_format_port(port, s);
3542 if (port == OFPP_CONTROLLER) {
3543 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
3548 case OFPACT_CONTROLLER:
3549 controller = ofpact_get_CONTROLLER(a);
3550 if (controller->reason == OFPR_ACTION &&
3551 controller->controller_id == 0) {
3552 ds_put_format(s, "CONTROLLER:%"PRIu16,
3553 ofpact_get_CONTROLLER(a)->max_len);
3555 enum ofp_packet_in_reason reason = controller->reason;
3557 ds_put_cstr(s, "controller(");
3558 if (reason != OFPR_ACTION) {
3559 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3561 ds_put_format(s, "reason=%s,",
3562 ofputil_packet_in_reason_to_string(
3563 reason, reasonbuf, sizeof reasonbuf));
3565 if (controller->max_len != UINT16_MAX) {
3566 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
3568 if (controller->controller_id != 0) {
3569 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
3572 ds_put_char(s, ')');
3576 case OFPACT_ENQUEUE:
3577 enqueue = ofpact_get_ENQUEUE(a);
3578 ds_put_format(s, "enqueue:");
3579 ofputil_format_port(enqueue->port, s);
3580 ds_put_format(s, ":%"PRIu32, enqueue->queue);
3583 case OFPACT_OUTPUT_REG:
3584 ds_put_cstr(s, "output:");
3585 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
3589 bundle_format(ofpact_get_BUNDLE(a), s);
3592 case OFPACT_SET_VLAN_VID:
3593 ds_put_format(s, "%s:%"PRIu16,
3594 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_VID
3597 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
3600 case OFPACT_SET_VLAN_PCP:
3601 ds_put_format(s, "%s:%"PRIu8,
3602 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_PCP
3605 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
3608 case OFPACT_STRIP_VLAN:
3609 ds_put_cstr(s, a->compat == OFPUTIL_OFPAT11_POP_VLAN
3610 ? "pop_vlan" : "strip_vlan");
3613 case OFPACT_PUSH_VLAN:
3614 /* XXX 802.1AD case*/
3615 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
3618 case OFPACT_SET_ETH_SRC:
3619 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
3620 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
3623 case OFPACT_SET_ETH_DST:
3624 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
3625 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
3628 case OFPACT_SET_IPV4_SRC:
3629 ds_put_format(s, "mod_nw_src:"IP_FMT,
3630 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
3633 case OFPACT_SET_IPV4_DST:
3634 ds_put_format(s, "mod_nw_dst:"IP_FMT,
3635 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
3638 case OFPACT_SET_IP_DSCP:
3639 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
3642 case OFPACT_SET_IP_ECN:
3643 ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
3646 case OFPACT_SET_IP_TTL:
3647 ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
3650 case OFPACT_SET_L4_SRC_PORT:
3651 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
3654 case OFPACT_SET_L4_DST_PORT:
3655 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
3658 case OFPACT_REG_MOVE:
3659 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
3662 case OFPACT_REG_LOAD:
3663 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
3666 case OFPACT_SET_FIELD:
3667 set_field = ofpact_get_SET_FIELD(a);
3668 mf = set_field->field;
3669 ds_put_format(s, "set_field:");
3670 mf_format(mf, &set_field->value, NULL, s);
3671 ds_put_format(s, "->%s", mf->name);
3674 case OFPACT_STACK_PUSH:
3675 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
3678 case OFPACT_STACK_POP:
3679 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
3682 case OFPACT_DEC_TTL:
3683 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
3686 case OFPACT_SET_MPLS_LABEL:
3687 ds_put_format(s, "set_mpls_label(%"PRIu32")",
3688 ntohl(ofpact_get_SET_MPLS_LABEL(a)->label));
3691 case OFPACT_SET_MPLS_TC:
3692 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3693 ofpact_get_SET_MPLS_TC(a)->tc);
3696 case OFPACT_SET_MPLS_TTL:
3697 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
3698 ofpact_get_SET_MPLS_TTL(a)->ttl);
3701 case OFPACT_DEC_MPLS_TTL:
3702 ds_put_cstr(s, "dec_mpls_ttl");
3705 case OFPACT_SET_TUNNEL:
3706 tunnel = ofpact_get_SET_TUNNEL(a);
3707 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
3708 (tunnel->tun_id > UINT32_MAX
3709 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
3713 case OFPACT_SET_QUEUE:
3714 ds_put_format(s, "set_queue:%"PRIu32,
3715 ofpact_get_SET_QUEUE(a)->queue_id);
3718 case OFPACT_POP_QUEUE:
3719 ds_put_cstr(s, "pop_queue");
3722 case OFPACT_FIN_TIMEOUT:
3723 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
3726 case OFPACT_RESUBMIT:
3727 resubmit = ofpact_get_RESUBMIT(a);
3728 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
3729 ds_put_cstr(s, "resubmit:");
3730 ofputil_format_port(resubmit->in_port, s);
3732 ds_put_format(s, "resubmit(");
3733 if (resubmit->in_port != OFPP_IN_PORT) {
3734 ofputil_format_port(resubmit->in_port, s);
3736 ds_put_char(s, ',');
3737 if (resubmit->table_id != 255) {
3738 ds_put_format(s, "%"PRIu8, resubmit->table_id);
3740 ds_put_char(s, ')');
3745 learn_format(ofpact_get_LEARN(a), s);
3748 case OFPACT_MULTIPATH:
3749 multipath_format(ofpact_get_MULTIPATH(a), s);
3753 print_note(ofpact_get_NOTE(a), s);
3756 case OFPACT_PUSH_MPLS:
3757 ds_put_format(s, "push_mpls:0x%04"PRIx16,
3758 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
3761 case OFPACT_POP_MPLS:
3762 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
3763 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
3767 ds_put_cstr(s, "exit");
3771 sample = ofpact_get_SAMPLE(a);
3773 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
3774 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
3775 sample->probability, sample->collector_set_id,
3776 sample->obs_domain_id, sample->obs_point_id);
3779 case OFPACT_WRITE_ACTIONS: {
3780 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3781 ds_put_format(s, "%s(",
3782 ovs_instruction_name_from_type(
3783 OVSINST_OFPIT11_WRITE_ACTIONS));
3784 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
3785 ds_put_char(s, ')');
3789 case OFPACT_CLEAR_ACTIONS:
3790 ds_put_format(s, "%s",
3791 ovs_instruction_name_from_type(
3792 OVSINST_OFPIT11_CLEAR_ACTIONS));
3795 case OFPACT_WRITE_METADATA:
3796 metadata = ofpact_get_WRITE_METADATA(a);
3797 ds_put_format(s, "%s:%#"PRIx64,
3798 ovs_instruction_name_from_type(
3799 OVSINST_OFPIT11_WRITE_METADATA),
3800 ntohll(metadata->metadata));
3801 if (metadata->mask != OVS_BE64_MAX) {
3802 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
3806 case OFPACT_GOTO_TABLE:
3807 ds_put_format(s, "%s:%"PRIu8,
3808 ovs_instruction_name_from_type(
3809 OVSINST_OFPIT11_GOTO_TABLE),
3810 ofpact_get_GOTO_TABLE(a)->table_id);
3814 ds_put_format(s, "%s:%"PRIu32,
3815 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
3816 ofpact_get_METER(a)->meter_id);
3820 ds_put_format(s, "group:%"PRIu32,
3821 ofpact_get_GROUP(a)->group_id);
3826 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
3827 * 'ofpacts' to 'string'. */
3829 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
3833 ds_put_cstr(string, "drop");
3835 const struct ofpact *a;
3837 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3839 ds_put_cstr(string, ",");
3842 /* XXX write-actions */
3843 ofpact_format(a, string);
3848 /* Internal use by helpers. */
3851 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
3853 struct ofpact *ofpact;
3855 ofpact_pad(ofpacts);
3856 ofpact = ofpacts->frame = ofpbuf_put_uninit(ofpacts, len);
3857 ofpact_init(ofpact, type, len);
3862 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
3864 memset(ofpact, 0, len);
3865 ofpact->type = type;
3866 ofpact->compat = OFPUTIL_ACTION_INVALID;
3870 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
3871 * starting at 'ofpact'.
3873 * This is the correct way to update a variable-length ofpact's length after
3874 * adding the variable-length part of the payload. (See the large comment
3875 * near the end of ofp-actions.h for more information.) */
3877 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
3879 ovs_assert(ofpact == ofpacts->frame);
3880 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
3883 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
3884 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
3885 * client must call this itself after adding the final ofpact to an array of
3888 * (The consequences of failing to call this function are probably not dire.
3889 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
3890 * not dereference it. That's undefined behavior, technically, but it will not
3891 * cause a real problem on common systems. Still, it seems better to call
3894 ofpact_pad(struct ofpbuf *ofpacts)
3896 unsigned int pad = PAD_SIZE(ofpbuf_size(ofpacts), OFPACT_ALIGNTO);
3898 ofpbuf_put_zeros(ofpacts, pad);
3903 ofpact_name(enum ofpact_type type)
3906 #define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;