2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-actions.h"
20 #include "byte-order.h"
22 #include "dynamic-string.h"
24 #include "meta-flow.h"
25 #include "multipath.h"
32 VLOG_DEFINE_THIS_MODULE(ofp_actions);
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
36 /* Converting OpenFlow 1.0 to ofpacts. */
40 struct ofp_action_header header;
41 struct ofp_action_vendor_header vendor;
42 struct ofp10_action_output output10;
43 struct ofp_action_vlan_vid vlan_vid;
44 struct ofp_action_vlan_pcp vlan_pcp;
45 struct ofp_action_nw_addr nw_addr;
46 struct ofp_action_nw_tos nw_tos;
47 struct ofp_action_tp_port tp_port;
49 OFP_ASSERT(sizeof(union ofp_action) == 8);
52 output_from_openflow10(const struct ofp10_action_output *oao,
55 struct ofpact_output *output;
57 output = ofpact_put_OUTPUT(out);
58 output->port = u16_to_ofp(ntohs(oao->port));
59 output->max_len = ntohs(oao->max_len);
61 return ofputil_check_output_port(output->port, OFPP_MAX);
65 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
68 struct ofpact_enqueue *enqueue;
70 enqueue = ofpact_put_ENQUEUE(out);
71 enqueue->port = u16_to_ofp(ntohs(oae->port));
72 enqueue->queue = ntohl(oae->queue_id);
73 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
74 && enqueue->port != OFPP_IN_PORT
75 && enqueue->port != OFPP_LOCAL) {
76 return OFPERR_OFPBAC_BAD_OUT_PORT;
82 resubmit_from_openflow(const struct nx_action_resubmit *nar,
85 struct ofpact_resubmit *resubmit;
87 resubmit = ofpact_put_RESUBMIT(out);
88 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
89 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
90 resubmit->table_id = 0xff;
94 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
97 struct ofpact_resubmit *resubmit;
99 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
100 return OFPERR_OFPBAC_BAD_ARGUMENT;
103 resubmit = ofpact_put_RESUBMIT(out);
104 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
105 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
106 resubmit->table_id = nar->table;
111 output_reg_from_openflow(const struct nx_action_output_reg *naor,
114 struct ofpact_output_reg *output_reg;
116 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
117 return OFPERR_OFPBAC_BAD_ARGUMENT;
120 output_reg = ofpact_put_OUTPUT_REG(out);
121 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
122 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
123 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
124 output_reg->max_len = ntohs(naor->max_len);
126 return mf_check_src(&output_reg->src, NULL);
130 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
133 struct ofpact_fin_timeout *oft;
135 oft = ofpact_put_FIN_TIMEOUT(out);
136 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
137 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
141 controller_from_openflow(const struct nx_action_controller *nac,
144 struct ofpact_controller *oc;
146 oc = ofpact_put_CONTROLLER(out);
147 oc->max_len = ntohs(nac->max_len);
148 oc->controller_id = ntohs(nac->controller_id);
149 oc->reason = nac->reason;
153 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
156 struct ofpact_metadata *om;
158 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
159 return OFPERR_NXBRC_MUST_BE_ZERO;
162 om = ofpact_put_WRITE_METADATA(out);
163 om->metadata = nawm->metadata;
164 om->mask = nawm->mask;
170 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
172 struct ofpact_note *note;
175 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
176 note = ofpact_put(out, OFPACT_NOTE,
177 offsetof(struct ofpact_note, data) + length);
178 note->length = length;
179 memcpy(note->data, nan->note, length);
183 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
186 struct ofpact_cnt_ids *ids;
187 enum ofperr error = 0;
189 ids = ofpact_put_DEC_TTL(out);
190 ids->ofpact.compat = compat;
191 ids->n_controllers = 1;
192 ofpbuf_put(out, &id, sizeof id);
194 ofpact_update_len(out, &ids->ofpact);
199 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
202 struct ofpact_cnt_ids *ids;
206 ids = ofpact_put_DEC_TTL(out);
207 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
208 ids->n_controllers = ntohs(nac_ids->n_controllers);
209 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
211 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
212 return OFPERR_NXBRC_MUST_BE_ZERO;
215 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
216 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
217 "allocated for controller ids. %zu bytes are required for "
218 "%"PRIu16" controllers.", ids_size,
219 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
220 return OFPERR_OFPBAC_BAD_LEN;
223 for (i = 0; i < ids->n_controllers; i++) {
224 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
225 ofpbuf_put(out, &id, sizeof id);
229 ofpact_update_len(out, &ids->ofpact);
235 sample_from_openflow(const struct nx_action_sample *nas,
238 struct ofpact_sample *sample;
240 sample = ofpact_put_SAMPLE(out);
241 sample->probability = ntohs(nas->probability);
242 sample->collector_set_id = ntohl(nas->collector_set_id);
243 sample->obs_domain_id = ntohl(nas->obs_domain_id);
244 sample->obs_point_id = ntohl(nas->obs_point_id);
246 if (sample->probability == 0) {
247 return OFPERR_OFPBAC_BAD_ARGUMENT;
254 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
256 const struct nx_action_header *nah = (const struct nx_action_header *) a;
257 uint16_t len = ntohs(a->header.len);
259 if (len < sizeof(struct nx_action_header)) {
260 return OFPERR_OFPBAC_BAD_LEN;
261 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
262 return OFPERR_OFPBAC_BAD_VENDOR;
265 switch (nah->subtype) {
266 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
267 case CONSTANT_HTONS(ENUM): \
269 ? len >= sizeof(struct STRUCT) \
270 : len == sizeof(struct STRUCT)) { \
271 *code = OFPUTIL_##ENUM; \
274 return OFPERR_OFPBAC_BAD_LEN; \
277 #include "ofp-util.def"
279 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
280 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
282 return OFPERR_OFPBAC_BAD_TYPE;
286 /* Parses 'a' to determine its type. On success stores the correct type into
287 * '*code' and returns 0. On failure returns an OFPERR_* error code and
288 * '*code' is indeterminate.
290 * The caller must have already verified that 'a''s length is potentially
291 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
292 * ofp_action) and no longer than the amount of space allocated to 'a').
294 * This function verifies that 'a''s length is correct for the type of action
295 * that it represents. */
297 decode_openflow10_action(const union ofp_action *a,
298 enum ofputil_action_code *code)
301 case CONSTANT_HTONS(OFPAT10_VENDOR):
302 return decode_nxast_action(a, code);
304 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
305 case CONSTANT_HTONS(ENUM): \
306 if (a->header.len == htons(sizeof(struct STRUCT))) { \
307 *code = OFPUTIL_##ENUM; \
310 return OFPERR_OFPBAC_BAD_LEN; \
313 #include "ofp-util.def"
316 return OFPERR_OFPBAC_BAD_TYPE;
321 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
324 const struct nx_action_resubmit *nar;
325 const struct nx_action_set_tunnel *nast;
326 const struct nx_action_set_queue *nasq;
327 const struct nx_action_note *nan;
328 const struct nx_action_set_tunnel64 *nast64;
329 const struct nx_action_write_metadata *nawm;
330 struct ofpact_tunnel *tunnel;
331 enum ofperr error = 0;
334 case OFPUTIL_ACTION_INVALID:
335 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
336 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
337 #include "ofp-util.def"
340 case OFPUTIL_NXAST_RESUBMIT:
341 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
344 case OFPUTIL_NXAST_SET_TUNNEL:
345 nast = (const struct nx_action_set_tunnel *) a;
346 tunnel = ofpact_put_SET_TUNNEL(out);
347 tunnel->ofpact.compat = code;
348 tunnel->tun_id = ntohl(nast->tun_id);
351 case OFPUTIL_NXAST_WRITE_METADATA:
352 nawm = ALIGNED_CAST(const struct nx_action_write_metadata *, a);
353 error = metadata_from_nxast(nawm, out);
356 case OFPUTIL_NXAST_SET_QUEUE:
357 nasq = (const struct nx_action_set_queue *) a;
358 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
361 case OFPUTIL_NXAST_POP_QUEUE:
362 ofpact_put_POP_QUEUE(out);
365 case OFPUTIL_NXAST_REG_MOVE:
366 error = nxm_reg_move_from_openflow(
367 (const struct nx_action_reg_move *) a, out);
370 case OFPUTIL_NXAST_REG_LOAD:
371 error = nxm_reg_load_from_openflow(
372 ALIGNED_CAST(const struct nx_action_reg_load *, a), out);
375 case OFPUTIL_NXAST_STACK_PUSH:
376 error = nxm_stack_push_from_openflow(
377 (const struct nx_action_stack *) a, out);
380 case OFPUTIL_NXAST_STACK_POP:
381 error = nxm_stack_pop_from_openflow(
382 (const struct nx_action_stack *) a, out);
385 case OFPUTIL_NXAST_NOTE:
386 nan = (const struct nx_action_note *) a;
387 note_from_openflow(nan, out);
390 case OFPUTIL_NXAST_SET_TUNNEL64:
391 nast64 = ALIGNED_CAST(const struct nx_action_set_tunnel64 *, a);
392 tunnel = ofpact_put_SET_TUNNEL(out);
393 tunnel->ofpact.compat = code;
394 tunnel->tun_id = ntohll(nast64->tun_id);
397 case OFPUTIL_NXAST_MULTIPATH:
398 error = multipath_from_openflow((const struct nx_action_multipath *) a,
399 ofpact_put_MULTIPATH(out));
402 case OFPUTIL_NXAST_BUNDLE:
403 case OFPUTIL_NXAST_BUNDLE_LOAD:
404 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
407 case OFPUTIL_NXAST_OUTPUT_REG:
408 error = output_reg_from_openflow(
409 (const struct nx_action_output_reg *) a, out);
412 case OFPUTIL_NXAST_RESUBMIT_TABLE:
413 nar = (const struct nx_action_resubmit *) a;
414 error = resubmit_table_from_openflow(nar, out);
417 case OFPUTIL_NXAST_LEARN:
418 error = learn_from_openflow(
419 ALIGNED_CAST(const struct nx_action_learn *, a), out);
422 case OFPUTIL_NXAST_EXIT:
423 ofpact_put_EXIT(out);
426 case OFPUTIL_NXAST_DEC_TTL:
427 error = dec_ttl_from_openflow(out, code);
430 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
431 error = dec_ttl_cnt_ids_from_openflow(
432 (const struct nx_action_cnt_ids *) a, out);
435 case OFPUTIL_NXAST_FIN_TIMEOUT:
436 fin_timeout_from_openflow(
437 (const struct nx_action_fin_timeout *) a, out);
440 case OFPUTIL_NXAST_CONTROLLER:
441 controller_from_openflow((const struct nx_action_controller *) a, out);
444 case OFPUTIL_NXAST_PUSH_MPLS: {
445 struct nx_action_push_mpls *nxapm = (struct nx_action_push_mpls *)a;
446 if (!eth_type_mpls(nxapm->ethertype)) {
447 return OFPERR_OFPBAC_BAD_ARGUMENT;
449 ofpact_put_PUSH_MPLS(out)->ethertype = nxapm->ethertype;
453 case OFPUTIL_NXAST_SET_MPLS_TTL: {
454 struct nx_action_mpls_ttl *nxamt = (struct nx_action_mpls_ttl *)a;
455 ofpact_put_SET_MPLS_TTL(out)->ttl = nxamt->ttl;
459 case OFPUTIL_NXAST_DEC_MPLS_TTL:
460 ofpact_put_DEC_MPLS_TTL(out);
463 case OFPUTIL_NXAST_POP_MPLS: {
464 struct nx_action_pop_mpls *nxapm = (struct nx_action_pop_mpls *)a;
465 if (eth_type_mpls(nxapm->ethertype)) {
466 return OFPERR_OFPBAC_BAD_ARGUMENT;
468 ofpact_put_POP_MPLS(out)->ethertype = nxapm->ethertype;
472 case OFPUTIL_NXAST_SAMPLE:
473 error = sample_from_openflow(
474 (const struct nx_action_sample *) a, out);
482 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
484 enum ofputil_action_code code;
487 error = decode_openflow10_action(a, &code);
493 case OFPUTIL_ACTION_INVALID:
494 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
495 #include "ofp-util.def"
498 case OFPUTIL_OFPAT10_OUTPUT:
499 return output_from_openflow10(&a->output10, out);
501 case OFPUTIL_OFPAT10_SET_VLAN_VID:
502 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
503 return OFPERR_OFPBAC_BAD_ARGUMENT;
505 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
508 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
509 if (a->vlan_pcp.vlan_pcp & ~7) {
510 return OFPERR_OFPBAC_BAD_ARGUMENT;
512 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
515 case OFPUTIL_OFPAT10_STRIP_VLAN:
516 ofpact_put_STRIP_VLAN(out);
519 case OFPUTIL_OFPAT10_SET_DL_SRC:
520 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
521 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
524 case OFPUTIL_OFPAT10_SET_DL_DST:
525 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
526 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
529 case OFPUTIL_OFPAT10_SET_NW_SRC:
530 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
533 case OFPUTIL_OFPAT10_SET_NW_DST:
534 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
537 case OFPUTIL_OFPAT10_SET_NW_TOS:
538 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
539 return OFPERR_OFPBAC_BAD_ARGUMENT;
541 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
544 case OFPUTIL_OFPAT10_SET_TP_SRC:
545 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
548 case OFPUTIL_OFPAT10_SET_TP_DST:
549 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
553 case OFPUTIL_OFPAT10_ENQUEUE:
554 error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
558 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
559 #include "ofp-util.def"
560 return ofpact_from_nxast(a, code, out);
566 static inline union ofp_action *
567 action_next(const union ofp_action *a)
569 return ((union ofp_action *) (void *)
570 ((uint8_t *) a + ntohs(a->header.len)));
574 action_is_valid(const union ofp_action *a, size_t n_actions)
576 uint16_t len = ntohs(a->header.len);
577 return (!(len % OFP_ACTION_ALIGN)
579 && len / sizeof *a <= n_actions);
582 /* This macro is careful to check for actions with bad lengths. */
583 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
584 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
585 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
586 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
587 (ITER) = action_next(ITER)))
590 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
593 if (!VLOG_DROP_WARN(&rl)) {
597 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
598 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
599 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
605 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
607 enum ofperr (*ofpact_from_openflow)(
608 const union ofp_action *a, struct ofpbuf *out))
610 const union ofp_action *a;
613 ACTION_FOR_EACH (a, left, in, n_in) {
614 enum ofperr error = ofpact_from_openflow(a, out);
616 log_bad_action(in, n_in, a - in, error);
621 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
622 log_bad_action(in, n_in, n_in - left, error);
631 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
634 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
638 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
639 struct ofpbuf *ofpacts,
640 enum ofperr (*translate)(const union ofp_action *actions,
642 struct ofpbuf *ofpacts))
644 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
645 const union ofp_action *actions;
648 ofpbuf_clear(ofpacts);
650 if (actions_len % OFP_ACTION_ALIGN != 0) {
651 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
652 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
653 return OFPERR_OFPBRC_BAD_LEN;
656 actions = ofpbuf_try_pull(openflow, actions_len);
657 if (actions == NULL) {
658 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
659 "remaining message length (%zu)",
660 actions_len, openflow->size);
661 return OFPERR_OFPBRC_BAD_LEN;
664 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
666 ofpbuf_clear(ofpacts);
670 error = ofpacts_verify(ofpacts->data, ofpacts->size);
672 ofpbuf_clear(ofpacts);
677 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
678 * front of 'openflow' into ofpacts. On success, replaces any existing content
679 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
680 * Returns 0 if successful, otherwise an OpenFlow error.
682 * The parsed actions are valid generically, but they may not be valid in a
683 * specific context. For example, port numbers up to OFPP_MAX are valid
684 * generically, but specific datapaths may only support port numbers in a
685 * smaller range. Use ofpacts_check() to additional check whether actions are
686 * valid in a specific context. */
688 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
689 struct ofpbuf *ofpacts)
691 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
692 ofpacts_from_openflow10);
695 /* OpenFlow 1.1 actions. */
697 /* Parses 'a' to determine its type. On success stores the correct type into
698 * '*code' and returns 0. On failure returns an OFPERR_* error code and
699 * '*code' is indeterminate.
701 * The caller must have already verified that 'a''s length is potentially
702 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
703 * ofp_action) and no longer than the amount of space allocated to 'a').
705 * This function verifies that 'a''s length is correct for the type of action
706 * that it represents. */
708 decode_openflow11_action(const union ofp_action *a,
709 enum ofputil_action_code *code)
714 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
715 return decode_nxast_action(a, code);
717 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
718 case CONSTANT_HTONS(ENUM): \
719 len = ntohs(a->header.len); \
721 ? len >= sizeof(struct STRUCT) \
722 : len == sizeof(struct STRUCT)) { \
723 *code = OFPUTIL_##ENUM; \
726 return OFPERR_OFPBAC_BAD_LEN; \
729 #include "ofp-util.def"
732 return OFPERR_OFPBAC_BAD_TYPE;
737 output_from_openflow11(const struct ofp11_action_output *oao,
740 struct ofpact_output *output;
743 output = ofpact_put_OUTPUT(out);
744 output->max_len = ntohs(oao->max_len);
746 error = ofputil_port_from_ofp11(oao->port, &output->port);
751 return ofputil_check_output_port(output->port, OFPP_MAX);
755 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
757 enum ofputil_action_code code;
760 error = decode_openflow11_action(a, &code);
766 case OFPUTIL_ACTION_INVALID:
767 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
768 #include "ofp-util.def"
771 case OFPUTIL_OFPAT11_OUTPUT:
772 return output_from_openflow11((const struct ofp11_action_output *) a,
775 case OFPUTIL_OFPAT11_SET_VLAN_VID:
776 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
777 return OFPERR_OFPBAC_BAD_ARGUMENT;
779 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
782 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
783 if (a->vlan_pcp.vlan_pcp & ~7) {
784 return OFPERR_OFPBAC_BAD_ARGUMENT;
786 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
789 case OFPUTIL_OFPAT11_PUSH_VLAN:
790 if (((const struct ofp11_action_push *)a)->ethertype !=
791 htons(ETH_TYPE_VLAN_8021Q)) {
792 /* XXX 802.1AD(QinQ) isn't supported at the moment */
793 return OFPERR_OFPBAC_BAD_ARGUMENT;
795 ofpact_put_PUSH_VLAN(out);
798 case OFPUTIL_OFPAT11_POP_VLAN:
799 ofpact_put_STRIP_VLAN(out);
802 case OFPUTIL_OFPAT11_SET_QUEUE:
803 ofpact_put_SET_QUEUE(out)->queue_id =
804 ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
807 case OFPUTIL_OFPAT11_SET_DL_SRC:
808 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
809 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
812 case OFPUTIL_OFPAT11_SET_DL_DST:
813 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
814 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
817 case OFPUTIL_OFPAT11_DEC_NW_TTL:
818 dec_ttl_from_openflow(out, code);
821 case OFPUTIL_OFPAT11_SET_NW_SRC:
822 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
825 case OFPUTIL_OFPAT11_SET_NW_DST:
826 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
829 case OFPUTIL_OFPAT11_SET_NW_TOS:
830 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
831 return OFPERR_OFPBAC_BAD_ARGUMENT;
833 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
836 case OFPUTIL_OFPAT11_SET_TP_SRC:
837 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
840 case OFPUTIL_OFPAT11_SET_TP_DST:
841 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
844 case OFPUTIL_OFPAT12_SET_FIELD:
845 return nxm_reg_load_from_openflow12_set_field(
846 (const struct ofp12_action_set_field *)a, out);
848 case OFPUTIL_OFPAT11_SET_MPLS_TTL: {
849 struct ofp11_action_mpls_ttl *oamt = (struct ofp11_action_mpls_ttl *)a;
850 ofpact_put_SET_MPLS_TTL(out)->ttl = oamt->mpls_ttl;
854 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
855 ofpact_put_DEC_MPLS_TTL(out);
858 case OFPUTIL_OFPAT11_PUSH_MPLS: {
859 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
860 if (!eth_type_mpls(oap->ethertype)) {
861 return OFPERR_OFPBAC_BAD_ARGUMENT;
863 ofpact_put_PUSH_MPLS(out)->ethertype = oap->ethertype;
867 case OFPUTIL_OFPAT11_POP_MPLS: {
868 struct ofp11_action_pop_mpls *oapm = (struct ofp11_action_pop_mpls *)a;
869 if (eth_type_mpls(oapm->ethertype)) {
870 return OFPERR_OFPBAC_BAD_ARGUMENT;
872 ofpact_put_POP_MPLS(out)->ethertype = oapm->ethertype;
876 case OFPUTIL_OFPAT11_GROUP: {
877 struct ofp11_action_group *oag = (struct ofp11_action_group *)a;
878 ofpact_put_GROUP(out)->group_id = ntohl(oag->group_id);
882 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
883 #include "ofp-util.def"
884 return ofpact_from_nxast(a, code, out);
891 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
894 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
897 /* True if an action sets the value of a field
898 * in a way that is compatibile with the action set.
899 * False otherwise. */
901 ofpact_is_set_action(const struct ofpact *a)
904 case OFPACT_REG_LOAD:
905 case OFPACT_SET_ETH_DST:
906 case OFPACT_SET_ETH_SRC:
907 case OFPACT_SET_IPV4_DSCP:
908 case OFPACT_SET_IPV4_DST:
909 case OFPACT_SET_IPV4_SRC:
910 case OFPACT_SET_L4_DST_PORT:
911 case OFPACT_SET_L4_SRC_PORT:
912 case OFPACT_SET_MPLS_TTL:
913 case OFPACT_SET_QUEUE:
914 case OFPACT_SET_TUNNEL:
915 case OFPACT_SET_VLAN_PCP:
916 case OFPACT_SET_VLAN_VID:
919 case OFPACT_CLEAR_ACTIONS:
920 case OFPACT_CONTROLLER:
921 case OFPACT_DEC_MPLS_TTL:
925 case OFPACT_FIN_TIMEOUT:
926 case OFPACT_GOTO_TABLE:
930 case OFPACT_MULTIPATH:
933 case OFPACT_OUTPUT_REG:
934 case OFPACT_POP_MPLS:
935 case OFPACT_POP_QUEUE:
936 case OFPACT_PUSH_MPLS:
937 case OFPACT_PUSH_VLAN:
938 case OFPACT_REG_MOVE:
939 case OFPACT_RESUBMIT:
941 case OFPACT_STACK_POP:
942 case OFPACT_STACK_PUSH:
943 case OFPACT_STRIP_VLAN:
944 case OFPACT_WRITE_ACTIONS:
945 case OFPACT_WRITE_METADATA:
952 /* True if an action is allowed in the action set.
953 * False otherwise. */
955 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
958 case OFPACT_DEC_MPLS_TTL:
962 case OFPACT_POP_MPLS:
963 case OFPACT_PUSH_MPLS:
964 case OFPACT_PUSH_VLAN:
965 case OFPACT_REG_LOAD:
966 case OFPACT_SET_ETH_DST:
967 case OFPACT_SET_ETH_SRC:
968 case OFPACT_SET_IPV4_DSCP:
969 case OFPACT_SET_IPV4_DST:
970 case OFPACT_SET_IPV4_SRC:
971 case OFPACT_SET_L4_DST_PORT:
972 case OFPACT_SET_L4_SRC_PORT:
973 case OFPACT_SET_MPLS_TTL:
974 case OFPACT_SET_QUEUE:
975 case OFPACT_SET_TUNNEL:
976 case OFPACT_SET_VLAN_PCP:
977 case OFPACT_SET_VLAN_VID:
978 case OFPACT_STRIP_VLAN:
981 /* In general these actions are excluded because they are not part of
982 * the OpenFlow specification nor map to actions that are defined in
983 * the specification. Thus the order in which they should be applied
984 * in the action set is undefined. */
986 case OFPACT_CONTROLLER:
989 case OFPACT_FIN_TIMEOUT:
991 case OFPACT_MULTIPATH:
993 case OFPACT_OUTPUT_REG:
994 case OFPACT_POP_QUEUE:
995 case OFPACT_REG_MOVE:
996 case OFPACT_RESUBMIT:
998 case OFPACT_STACK_POP:
999 case OFPACT_STACK_PUSH:
1001 /* The action set may only include actions and thus
1002 * may not include any instructions */
1003 case OFPACT_CLEAR_ACTIONS:
1004 case OFPACT_GOTO_TABLE:
1006 case OFPACT_WRITE_ACTIONS:
1007 case OFPACT_WRITE_METADATA:
1014 /* Append ofpact 'a' onto the tail of 'out' */
1016 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1018 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1021 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1023 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1024 enum ofpact_type filter)
1026 const struct ofpact *target;
1027 const struct ofpact *a;
1030 OFPACT_FOR_EACH (a, in->data, in->size) {
1031 if (a->type == filter) {
1036 ofpact_copy(out, target);
1038 return target != NULL;
1041 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1042 * The order of appended ofpacts is preserved between 'in' and 'out' */
1044 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1045 bool (*filter)(const struct ofpact *))
1047 const struct ofpact *a;
1049 OFPACT_FOR_EACH (a, in->data, in->size) {
1051 ofpact_copy(out, a);
1056 /* Reads 'action_set', which contains ofpacts accumulated by
1057 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1058 * executed directly into 'action_list'. (These names correspond to the
1059 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1061 * In general this involves appending the last instance of each action that is
1062 * adimissible in the action set in the order described in the OpenFlow
1066 * + output action is only appended if no group action was present in 'in'.
1067 * + As a simplification all set actions are copied in the order the are
1068 * provided in 'in' as many set actions applied to a field has the same
1069 * affect as only applying the last action that sets a field and
1070 * duplicates are removed by do_xlate_actions().
1071 * This has an unwanted side-effect of compsoting multiple
1072 * LOAD_REG actions that touch different regions of the same field. */
1074 ofpacts_execute_action_set(struct ofpbuf *action_list,
1075 const struct ofpbuf *action_set)
1077 /* The OpenFlow spec "Action Set" section specifies this order. */
1078 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1079 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1080 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1081 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1082 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1083 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1084 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1085 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1087 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1088 * we should execute only OFPACT_GROUP.
1090 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1091 * all the actions because there's no point in modifying a packet that will
1092 * not be sent anywhere. */
1093 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1094 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1095 ofpbuf_clear(action_list);
1101 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1102 size_t n_in, struct ofpbuf *out)
1106 size_t start = out->size;
1108 error = ofpacts_from_openflow11(in, n_in, out);
1113 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1114 if (!ofpact_is_allowed_in_actions_set(a)) {
1115 VLOG_WARN_RL(&rl, "disallowed action in action set");
1116 return OFPERR_OFPBAC_BAD_TYPE;
1124 /* OpenFlow 1.1 instructions. */
1126 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1127 static inline const struct STRUCT * OVS_UNUSED \
1128 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1130 ovs_assert(inst->type == htons(ENUM)); \
1131 return ALIGNED_CAST(struct STRUCT *, inst); \
1134 static inline void OVS_UNUSED \
1135 instruction_init_##ENUM(struct STRUCT *s) \
1137 memset(s, 0, sizeof *s); \
1138 s->type = htons(ENUM); \
1139 s->len = htons(sizeof *s); \
1142 static inline struct STRUCT * OVS_UNUSED \
1143 instruction_put_##ENUM(struct ofpbuf *buf) \
1145 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1146 instruction_init_##ENUM(s); \
1152 struct instruction_type_info {
1153 enum ovs_instruction_type type;
1157 static const struct instruction_type_info inst_info[] = {
1158 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1164 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1166 return inst_info[type].name;
1170 ovs_instruction_type_from_name(const char *name)
1172 const struct instruction_type_info *p;
1173 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1174 if (!strcasecmp(name, p->name)) {
1181 enum ovs_instruction_type
1182 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1186 return OVSINST_OFPIT13_METER;
1187 case OFPACT_CLEAR_ACTIONS:
1188 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1189 case OFPACT_WRITE_ACTIONS:
1190 return OVSINST_OFPIT11_WRITE_ACTIONS;
1191 case OFPACT_WRITE_METADATA:
1192 return OVSINST_OFPIT11_WRITE_METADATA;
1193 case OFPACT_GOTO_TABLE:
1194 return OVSINST_OFPIT11_GOTO_TABLE;
1197 case OFPACT_CONTROLLER:
1198 case OFPACT_ENQUEUE:
1199 case OFPACT_OUTPUT_REG:
1201 case OFPACT_SET_VLAN_VID:
1202 case OFPACT_SET_VLAN_PCP:
1203 case OFPACT_STRIP_VLAN:
1204 case OFPACT_PUSH_VLAN:
1205 case OFPACT_SET_ETH_SRC:
1206 case OFPACT_SET_ETH_DST:
1207 case OFPACT_SET_IPV4_SRC:
1208 case OFPACT_SET_IPV4_DST:
1209 case OFPACT_SET_IPV4_DSCP:
1210 case OFPACT_SET_L4_SRC_PORT:
1211 case OFPACT_SET_L4_DST_PORT:
1212 case OFPACT_REG_MOVE:
1213 case OFPACT_REG_LOAD:
1214 case OFPACT_STACK_PUSH:
1215 case OFPACT_STACK_POP:
1216 case OFPACT_DEC_TTL:
1217 case OFPACT_SET_MPLS_TTL:
1218 case OFPACT_DEC_MPLS_TTL:
1219 case OFPACT_PUSH_MPLS:
1220 case OFPACT_POP_MPLS:
1221 case OFPACT_SET_TUNNEL:
1222 case OFPACT_SET_QUEUE:
1223 case OFPACT_POP_QUEUE:
1224 case OFPACT_FIN_TIMEOUT:
1225 case OFPACT_RESUBMIT:
1227 case OFPACT_MULTIPATH:
1232 return OVSINST_OFPIT11_APPLY_ACTIONS;
1236 static inline struct ofp11_instruction *
1237 instruction_next(const struct ofp11_instruction *inst)
1239 return ((struct ofp11_instruction *) (void *)
1240 ((uint8_t *) inst + ntohs(inst->len)));
1244 instruction_is_valid(const struct ofp11_instruction *inst,
1245 size_t n_instructions)
1247 uint16_t len = ntohs(inst->len);
1248 return (!(len % OFP11_INSTRUCTION_ALIGN)
1249 && len >= sizeof *inst
1250 && len / sizeof *inst <= n_instructions);
1253 /* This macro is careful to check for instructions with bad lengths. */
1254 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1255 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1256 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1257 ((LEFT) -= (ntohs((ITER)->len) \
1258 / sizeof(struct ofp11_instruction)), \
1259 (ITER) = instruction_next(ITER)))
1262 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1263 enum ovs_instruction_type *type)
1265 uint16_t len = ntohs(inst->len);
1267 switch (inst->type) {
1268 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1269 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1271 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1272 case CONSTANT_HTONS(ENUM): \
1274 ? len >= sizeof(struct STRUCT) \
1275 : len == sizeof(struct STRUCT)) { \
1276 *type = OVSINST_##ENUM; \
1279 return OFPERR_OFPBIC_BAD_LEN; \
1285 return OFPERR_OFPBIC_UNKNOWN_INST;
1290 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1292 const struct ofp11_instruction *out[])
1294 const struct ofp11_instruction *inst;
1297 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1298 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1299 enum ovs_instruction_type type;
1302 error = decode_openflow11_instruction(inst, &type);
1308 return OFPERR_ONFBIC_DUP_INSTRUCTION;
1314 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1315 (n_insts - left) * sizeof *inst);
1316 return OFPERR_OFPBIC_BAD_LEN;
1322 get_actions_from_instruction(const struct ofp11_instruction *inst,
1323 const union ofp_action **actions,
1326 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1327 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1330 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
1331 * front of 'openflow' into ofpacts. On success, replaces any existing content
1332 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1333 * Returns 0 if successful, otherwise an OpenFlow error.
1335 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1336 * instructions, so you should call ofpacts_pull_openflow11_instructions()
1337 * instead of this function.
1339 * The parsed actions are valid generically, but they may not be valid in a
1340 * specific context. For example, port numbers up to OFPP_MAX are valid
1341 * generically, but specific datapaths may only support port numbers in a
1342 * smaller range. Use ofpacts_check() to additional check whether actions are
1343 * valid in a specific context. */
1345 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1346 unsigned int actions_len,
1347 struct ofpbuf *ofpacts)
1349 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1350 ofpacts_from_openflow11);
1354 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1355 unsigned int instructions_len,
1356 struct ofpbuf *ofpacts)
1358 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1359 const struct ofp11_instruction *instructions;
1360 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1363 ofpbuf_clear(ofpacts);
1365 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1366 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1368 instructions_len, OFP11_INSTRUCTION_ALIGN);
1369 error = OFPERR_OFPBIC_BAD_LEN;
1373 instructions = ofpbuf_try_pull(openflow, instructions_len);
1374 if (instructions == NULL) {
1375 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1376 "remaining message length (%zu)",
1377 instructions_len, openflow->size);
1378 error = OFPERR_OFPBIC_BAD_LEN;
1382 error = decode_openflow11_instructions(
1383 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1389 if (insts[OVSINST_OFPIT13_METER]) {
1390 const struct ofp13_instruction_meter *oim;
1391 struct ofpact_meter *om;
1393 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1394 insts[OVSINST_OFPIT13_METER]);
1396 om = ofpact_put_METER(ofpacts);
1397 om->meter_id = ntohl(oim->meter_id);
1399 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1400 const union ofp_action *actions;
1403 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1404 &actions, &n_actions);
1405 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1410 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1411 instruction_get_OFPIT11_CLEAR_ACTIONS(
1412 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1413 ofpact_put_CLEAR_ACTIONS(ofpacts);
1415 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1416 struct ofpact_nest *on;
1417 const union ofp_action *actions;
1421 ofpact_pad(ofpacts);
1422 start = ofpacts->size;
1423 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1424 offsetof(struct ofpact_nest, actions));
1425 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1426 &actions, &n_actions);
1427 error = ofpacts_from_openflow11_for_action_set(actions, n_actions,
1432 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1433 on->ofpact.len = ofpacts->size - start;
1435 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1436 const struct ofp11_instruction_write_metadata *oiwm;
1437 struct ofpact_metadata *om;
1439 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1440 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1442 om = ofpact_put_WRITE_METADATA(ofpacts);
1443 om->metadata = oiwm->metadata;
1444 om->mask = oiwm->metadata_mask;
1446 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1447 const struct ofp11_instruction_goto_table *oigt;
1448 struct ofpact_goto_table *ogt;
1450 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1451 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1452 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1453 ogt->table_id = oigt->table_id;
1456 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1459 ofpbuf_clear(ofpacts);
1464 /* May modify flow->dl_type, caller must restore it. */
1466 ofpact_check__(const struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
1469 const struct ofpact_enqueue *enqueue;
1473 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1476 case OFPACT_CONTROLLER:
1479 case OFPACT_ENQUEUE:
1480 enqueue = ofpact_get_ENQUEUE(a);
1481 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1482 && enqueue->port != OFPP_IN_PORT
1483 && enqueue->port != OFPP_LOCAL) {
1484 return OFPERR_OFPBAC_BAD_OUT_PORT;
1488 case OFPACT_OUTPUT_REG:
1489 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1492 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1494 case OFPACT_SET_VLAN_VID:
1495 case OFPACT_SET_VLAN_PCP:
1496 case OFPACT_STRIP_VLAN:
1497 case OFPACT_PUSH_VLAN:
1498 case OFPACT_SET_ETH_SRC:
1499 case OFPACT_SET_ETH_DST:
1500 case OFPACT_SET_IPV4_SRC:
1501 case OFPACT_SET_IPV4_DST:
1502 case OFPACT_SET_IPV4_DSCP:
1503 case OFPACT_SET_L4_SRC_PORT:
1504 case OFPACT_SET_L4_DST_PORT:
1507 case OFPACT_REG_MOVE:
1508 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1510 case OFPACT_REG_LOAD:
1511 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1513 case OFPACT_STACK_PUSH:
1514 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1516 case OFPACT_STACK_POP:
1517 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1519 case OFPACT_DEC_TTL:
1520 case OFPACT_SET_MPLS_TTL:
1521 case OFPACT_DEC_MPLS_TTL:
1522 case OFPACT_SET_TUNNEL:
1523 case OFPACT_SET_QUEUE:
1524 case OFPACT_POP_QUEUE:
1525 case OFPACT_FIN_TIMEOUT:
1526 case OFPACT_RESUBMIT:
1530 return learn_check(ofpact_get_LEARN(a), flow);
1532 case OFPACT_MULTIPATH:
1533 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1539 case OFPACT_PUSH_MPLS:
1540 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1543 case OFPACT_POP_MPLS:
1544 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1550 case OFPACT_CLEAR_ACTIONS:
1553 case OFPACT_WRITE_ACTIONS: {
1554 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
1555 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
1556 flow, max_ports, table_id);
1559 case OFPACT_WRITE_METADATA:
1562 case OFPACT_METER: {
1563 uint32_t mid = ofpact_get_METER(a)->meter_id;
1564 if (mid == 0 || mid > OFPM13_MAX) {
1565 return OFPERR_OFPMMFC_INVALID_METER;
1570 case OFPACT_GOTO_TABLE:
1571 if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
1572 return OFPERR_OFPBRC_BAD_TABLE_ID;
1584 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1585 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1586 * switch with no more than 'max_ports' ports.
1588 * May temporarily modify 'flow', but restores the changes before returning. */
1590 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1591 struct flow *flow, ofp_port_t max_ports, uint8_t table_id)
1593 const struct ofpact *a;
1594 ovs_be16 dl_type = flow->dl_type;
1595 enum ofperr error = 0;
1597 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1598 error = ofpact_check__(a, flow, max_ports, table_id);
1603 flow->dl_type = dl_type; /* Restore. */
1607 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1608 * in the appropriate order as defined by the OpenFlow spec. */
1610 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1612 const struct ofpact *a;
1613 enum ovs_instruction_type inst;
1615 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1616 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1617 enum ovs_instruction_type next;
1619 next = ovs_instruction_type_from_ofpact_type(a->type);
1620 if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
1623 const char *name = ovs_instruction_name_from_type(inst);
1624 const char *next_name = ovs_instruction_name_from_type(next);
1627 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1628 "1.1+ compatibility", name);
1630 VLOG_WARN("invalid instruction ordering: %s must appear "
1631 "before %s, for OpenFlow 1.1+ compatibility",
1634 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1643 /* Converting ofpacts to Nicira OpenFlow extensions. */
1646 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1649 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1651 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1652 output_reg->src.n_bits);
1653 naor->src = htonl(output_reg->src.field->nxm_header);
1654 naor->max_len = htons(output_reg->max_len);
1658 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1661 struct nx_action_resubmit *nar;
1663 if (resubmit->table_id == 0xff
1664 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1665 nar = ofputil_put_NXAST_RESUBMIT(out);
1667 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1668 nar->table = resubmit->table_id;
1670 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
1674 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1677 uint64_t tun_id = tunnel->tun_id;
1679 if (tun_id <= UINT32_MAX
1680 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1681 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1683 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1688 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1691 struct nx_action_write_metadata *nawm;
1693 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1694 nawm->metadata = om->metadata;
1695 nawm->mask = om->mask;
1699 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1701 size_t start_ofs = out->size;
1702 struct nx_action_note *nan;
1703 unsigned int remainder;
1706 nan = ofputil_put_NXAST_NOTE(out);
1707 out->size -= sizeof nan->note;
1709 ofpbuf_put(out, note->data, note->length);
1711 len = out->size - start_ofs;
1712 remainder = len % OFP_ACTION_ALIGN;
1714 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1716 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
1717 nan->len = htons(out->size - start_ofs);
1721 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1724 struct nx_action_controller *nac;
1726 nac = ofputil_put_NXAST_CONTROLLER(out);
1727 nac->max_len = htons(oc->max_len);
1728 nac->controller_id = htons(oc->controller_id);
1729 nac->reason = oc->reason;
1733 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1736 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1737 ofputil_put_NXAST_DEC_TTL(out);
1739 struct nx_action_cnt_ids *nac_ids =
1740 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1741 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1745 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1746 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1748 ids = ofpbuf_put_zeros(out, ids_len);
1749 for (i = 0; i < oc_ids->n_controllers; i++) {
1750 ids[i] = htons(oc_ids->cnt_ids[i]);
1756 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1759 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1760 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1761 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1765 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1768 struct nx_action_sample *nas;
1770 nas = ofputil_put_NXAST_SAMPLE(out);
1771 nas->probability = htons(os->probability);
1772 nas->collector_set_id = htonl(os->collector_set_id);
1773 nas->obs_domain_id = htonl(os->obs_domain_id);
1774 nas->obs_point_id = htonl(os->obs_point_id);
1778 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1781 case OFPACT_CONTROLLER:
1782 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1785 case OFPACT_OUTPUT_REG:
1786 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1790 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1793 case OFPACT_REG_MOVE:
1794 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1797 case OFPACT_REG_LOAD:
1798 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1801 case OFPACT_STACK_PUSH:
1802 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1805 case OFPACT_STACK_POP:
1806 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1809 case OFPACT_DEC_TTL:
1810 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1813 case OFPACT_SET_MPLS_TTL:
1814 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1815 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1818 case OFPACT_DEC_MPLS_TTL:
1819 ofputil_put_NXAST_DEC_MPLS_TTL(out);
1822 case OFPACT_SET_TUNNEL:
1823 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1826 case OFPACT_WRITE_METADATA:
1827 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1830 case OFPACT_SET_QUEUE:
1831 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1832 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1835 case OFPACT_POP_QUEUE:
1836 ofputil_put_NXAST_POP_QUEUE(out);
1839 case OFPACT_FIN_TIMEOUT:
1840 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1843 case OFPACT_RESUBMIT:
1844 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1848 learn_to_nxast(ofpact_get_LEARN(a), out);
1851 case OFPACT_MULTIPATH:
1852 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1856 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1860 ofputil_put_NXAST_EXIT(out);
1863 case OFPACT_PUSH_MPLS:
1864 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1865 ofpact_get_PUSH_MPLS(a)->ethertype;
1868 case OFPACT_POP_MPLS:
1869 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1870 ofpact_get_POP_MPLS(a)->ethertype;
1874 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
1879 case OFPACT_ENQUEUE:
1880 case OFPACT_SET_VLAN_VID:
1881 case OFPACT_SET_VLAN_PCP:
1882 case OFPACT_STRIP_VLAN:
1883 case OFPACT_PUSH_VLAN:
1884 case OFPACT_SET_ETH_SRC:
1885 case OFPACT_SET_ETH_DST:
1886 case OFPACT_SET_IPV4_SRC:
1887 case OFPACT_SET_IPV4_DST:
1888 case OFPACT_SET_IPV4_DSCP:
1889 case OFPACT_SET_L4_SRC_PORT:
1890 case OFPACT_SET_L4_DST_PORT:
1891 case OFPACT_WRITE_ACTIONS:
1892 case OFPACT_CLEAR_ACTIONS:
1893 case OFPACT_GOTO_TABLE:
1899 /* Converting ofpacts to OpenFlow 1.0. */
1902 ofpact_output_to_openflow10(const struct ofpact_output *output,
1905 struct ofp10_action_output *oao;
1907 oao = ofputil_put_OFPAT10_OUTPUT(out);
1908 oao->port = htons(ofp_to_u16(output->port));
1909 oao->max_len = htons(output->max_len);
1913 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1916 struct ofp10_action_enqueue *oae;
1918 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1919 oae->port = htons(ofp_to_u16(enqueue->port));
1920 oae->queue_id = htonl(enqueue->queue);
1924 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1928 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1931 case OFPACT_ENQUEUE:
1932 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1935 case OFPACT_SET_VLAN_VID:
1936 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1937 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1940 case OFPACT_SET_VLAN_PCP:
1941 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1942 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1945 case OFPACT_STRIP_VLAN:
1946 ofputil_put_OFPAT10_STRIP_VLAN(out);
1949 case OFPACT_SET_ETH_SRC:
1950 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1951 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1954 case OFPACT_SET_ETH_DST:
1955 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1956 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1959 case OFPACT_SET_IPV4_SRC:
1960 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1961 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1964 case OFPACT_SET_IPV4_DST:
1965 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1966 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1969 case OFPACT_SET_IPV4_DSCP:
1970 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1971 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1974 case OFPACT_SET_L4_SRC_PORT:
1975 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1976 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1979 case OFPACT_SET_L4_DST_PORT:
1980 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1981 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1984 case OFPACT_PUSH_VLAN:
1985 case OFPACT_CLEAR_ACTIONS:
1986 case OFPACT_WRITE_ACTIONS:
1987 case OFPACT_GOTO_TABLE:
1995 case OFPACT_CONTROLLER:
1996 case OFPACT_OUTPUT_REG:
1998 case OFPACT_REG_MOVE:
1999 case OFPACT_REG_LOAD:
2000 case OFPACT_STACK_PUSH:
2001 case OFPACT_STACK_POP:
2002 case OFPACT_DEC_TTL:
2003 case OFPACT_SET_MPLS_TTL:
2004 case OFPACT_DEC_MPLS_TTL:
2005 case OFPACT_SET_TUNNEL:
2006 case OFPACT_WRITE_METADATA:
2007 case OFPACT_SET_QUEUE:
2008 case OFPACT_POP_QUEUE:
2009 case OFPACT_FIN_TIMEOUT:
2010 case OFPACT_RESUBMIT:
2012 case OFPACT_MULTIPATH:
2015 case OFPACT_PUSH_MPLS:
2016 case OFPACT_POP_MPLS:
2018 ofpact_to_nxast(a, out);
2023 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
2024 * actions in 'openflow', appending the actions to any existing data in
2027 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
2028 struct ofpbuf *openflow)
2030 const struct ofpact *a;
2032 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2033 ofpact_to_openflow10(a, openflow);
2037 /* Converting ofpacts to OpenFlow 1.1. */
2040 ofpact_output_to_openflow11(const struct ofpact_output *output,
2043 struct ofp11_action_output *oao;
2045 oao = ofputil_put_OFPAT11_OUTPUT(out);
2046 oao->port = ofputil_port_to_ofp11(output->port);
2047 oao->max_len = htons(output->max_len);
2051 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2054 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2055 && (!dec_ttl->ofpact.compat ||
2056 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2057 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2059 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2064 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2068 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2070 case OFPACT_ENQUEUE:
2074 case OFPACT_SET_VLAN_VID:
2075 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2076 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2079 case OFPACT_SET_VLAN_PCP:
2080 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2081 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2084 case OFPACT_STRIP_VLAN:
2085 ofputil_put_OFPAT11_POP_VLAN(out);
2088 case OFPACT_PUSH_VLAN:
2089 /* XXX ETH_TYPE_VLAN_8021AD case */
2090 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2091 htons(ETH_TYPE_VLAN_8021Q);
2094 case OFPACT_SET_QUEUE:
2095 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2096 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2099 case OFPACT_SET_ETH_SRC:
2100 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2101 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2104 case OFPACT_SET_ETH_DST:
2105 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2106 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2109 case OFPACT_SET_IPV4_SRC:
2110 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2111 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2114 case OFPACT_SET_IPV4_DST:
2115 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2116 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2119 case OFPACT_SET_IPV4_DSCP:
2120 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2121 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
2124 case OFPACT_SET_L4_SRC_PORT:
2125 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2126 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2129 case OFPACT_SET_L4_DST_PORT:
2130 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2131 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2134 case OFPACT_DEC_TTL:
2135 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2138 case OFPACT_SET_MPLS_TTL:
2139 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2140 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2143 case OFPACT_DEC_MPLS_TTL:
2144 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2147 case OFPACT_WRITE_METADATA:
2148 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2151 case OFPACT_PUSH_MPLS:
2152 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2153 ofpact_get_PUSH_MPLS(a)->ethertype;
2156 case OFPACT_POP_MPLS:
2157 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2158 ofpact_get_POP_MPLS(a)->ethertype;
2162 case OFPACT_CLEAR_ACTIONS:
2163 case OFPACT_WRITE_ACTIONS:
2164 case OFPACT_GOTO_TABLE:
2169 ofputil_put_OFPAT11_GROUP(out)->group_id =
2170 htonl(ofpact_get_GROUP(a)->group_id);
2173 case OFPACT_CONTROLLER:
2174 case OFPACT_OUTPUT_REG:
2176 case OFPACT_REG_MOVE:
2177 case OFPACT_REG_LOAD:
2178 case OFPACT_STACK_PUSH:
2179 case OFPACT_STACK_POP:
2180 case OFPACT_SET_TUNNEL:
2181 case OFPACT_POP_QUEUE:
2182 case OFPACT_FIN_TIMEOUT:
2183 case OFPACT_RESUBMIT:
2185 case OFPACT_MULTIPATH:
2189 ofpact_to_nxast(a, out);
2194 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
2195 * 1.1 actions in 'openflow', appending the actions to any existing data in
2198 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
2199 size_t ofpacts_len, struct ofpbuf *openflow)
2201 const struct ofpact *a;
2202 size_t start_size = openflow->size;
2204 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2205 ofpact_to_openflow11(a, openflow);
2208 return openflow->size - start_size;
2212 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2214 struct ofp11_instruction_actions *oia;
2216 /* Update the instruction's length (or, if it's empty, delete it). */
2217 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2218 if (openflow->size > ofs + sizeof *oia) {
2219 oia->len = htons(openflow->size - ofs);
2221 openflow->size = ofs;
2226 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
2228 struct ofpbuf *openflow)
2230 const struct ofpact *a;
2232 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2233 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2234 case OVSINST_OFPIT11_CLEAR_ACTIONS:
2235 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2238 case OVSINST_OFPIT11_GOTO_TABLE: {
2239 struct ofp11_instruction_goto_table *oigt;
2240 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2241 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2242 memset(oigt->pad, 0, sizeof oigt->pad);
2246 case OVSINST_OFPIT11_WRITE_METADATA: {
2247 const struct ofpact_metadata *om;
2248 struct ofp11_instruction_write_metadata *oiwm;
2250 om = ofpact_get_WRITE_METADATA(a);
2251 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2252 oiwm->metadata = om->metadata;
2253 oiwm->metadata_mask = om->mask;
2257 case OVSINST_OFPIT13_METER: {
2258 const struct ofpact_meter *om;
2259 struct ofp13_instruction_meter *oim;
2261 om = ofpact_get_METER(a);
2262 oim = instruction_put_OFPIT13_METER(openflow);
2263 oim->meter_id = htonl(om->meter_id);
2267 case OVSINST_OFPIT11_APPLY_ACTIONS: {
2268 const size_t ofs = openflow->size;
2269 const size_t ofpacts_len_left =
2270 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2271 const struct ofpact *action;
2272 const struct ofpact *processed = a;
2274 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2275 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
2276 if (ovs_instruction_type_from_ofpact_type(action->type)
2277 != OVSINST_OFPIT11_APPLY_ACTIONS) {
2280 ofpact_to_openflow11(action, openflow);
2283 ofpacts_update_instruction_actions(openflow, ofs);
2288 case OVSINST_OFPIT11_WRITE_ACTIONS: {
2289 const size_t ofs = openflow->size;
2290 const struct ofpact_nest *on;
2292 on = ofpact_get_WRITE_ACTIONS(a);
2293 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2294 ofpacts_put_openflow11_actions(on->actions,
2295 ofpact_nest_get_action_len(on),
2297 ofpacts_update_instruction_actions(openflow, ofs);
2305 /* Returns true if 'action' outputs to 'port', false otherwise. */
2307 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
2309 switch (ofpact->type) {
2311 return ofpact_get_OUTPUT(ofpact)->port == port;
2312 case OFPACT_ENQUEUE:
2313 return ofpact_get_ENQUEUE(ofpact)->port == port;
2314 case OFPACT_CONTROLLER:
2315 return port == OFPP_CONTROLLER;
2317 case OFPACT_OUTPUT_REG:
2319 case OFPACT_SET_VLAN_VID:
2320 case OFPACT_SET_VLAN_PCP:
2321 case OFPACT_STRIP_VLAN:
2322 case OFPACT_PUSH_VLAN:
2323 case OFPACT_SET_ETH_SRC:
2324 case OFPACT_SET_ETH_DST:
2325 case OFPACT_SET_IPV4_SRC:
2326 case OFPACT_SET_IPV4_DST:
2327 case OFPACT_SET_IPV4_DSCP:
2328 case OFPACT_SET_L4_SRC_PORT:
2329 case OFPACT_SET_L4_DST_PORT:
2330 case OFPACT_REG_MOVE:
2331 case OFPACT_REG_LOAD:
2332 case OFPACT_STACK_PUSH:
2333 case OFPACT_STACK_POP:
2334 case OFPACT_DEC_TTL:
2335 case OFPACT_SET_MPLS_TTL:
2336 case OFPACT_DEC_MPLS_TTL:
2337 case OFPACT_SET_TUNNEL:
2338 case OFPACT_WRITE_METADATA:
2339 case OFPACT_SET_QUEUE:
2340 case OFPACT_POP_QUEUE:
2341 case OFPACT_FIN_TIMEOUT:
2342 case OFPACT_RESUBMIT:
2344 case OFPACT_MULTIPATH:
2347 case OFPACT_PUSH_MPLS:
2348 case OFPACT_POP_MPLS:
2350 case OFPACT_CLEAR_ACTIONS:
2351 case OFPACT_WRITE_ACTIONS:
2352 case OFPACT_GOTO_TABLE:
2360 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2361 * to 'port', false otherwise. */
2363 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
2366 const struct ofpact *a;
2368 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2369 if (ofpact_outputs_to_port(a, port)) {
2377 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2378 * to 'group', false otherwise. */
2380 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
2383 const struct ofpact *a;
2385 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2386 if (a->type == OFPACT_GROUP
2387 && ofpact_get_GROUP(a)->group_id == group_id) {
2396 ofpacts_equal(const struct ofpact *a, size_t a_len,
2397 const struct ofpact *b, size_t b_len)
2399 return a_len == b_len && !memcmp(a, b, a_len);
2402 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2403 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2405 * This function relies on the order of 'ofpacts' being correct (as checked by
2406 * ofpacts_verify()). */
2408 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2410 const struct ofpact *a;
2412 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2413 enum ovs_instruction_type inst;
2415 inst = ovs_instruction_type_from_ofpact_type(a->type);
2416 if (a->type == OFPACT_METER) {
2417 return ofpact_get_METER(a)->meter_id;
2418 } else if (inst > OVSINST_OFPIT13_METER) {
2426 /* Formatting ofpacts. */
2429 print_note(const struct ofpact_note *note, struct ds *string)
2433 ds_put_cstr(string, "note:");
2434 for (i = 0; i < note->length; i++) {
2436 ds_put_char(string, '.');
2438 ds_put_format(string, "%02"PRIx8, note->data[i]);
2443 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2448 ds_put_cstr(s, "dec_ttl");
2449 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2450 ds_put_cstr(s, "(");
2451 for (i = 0; i < ids->n_controllers; i++) {
2453 ds_put_cstr(s, ",");
2455 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2457 ds_put_cstr(s, ")");
2462 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2465 ds_put_cstr(s, "fin_timeout(");
2466 if (fin_timeout->fin_idle_timeout) {
2467 ds_put_format(s, "idle_timeout=%"PRIu16",",
2468 fin_timeout->fin_idle_timeout);
2470 if (fin_timeout->fin_hard_timeout) {
2471 ds_put_format(s, "hard_timeout=%"PRIu16",",
2472 fin_timeout->fin_hard_timeout);
2475 ds_put_char(s, ')');
2479 ofpact_format(const struct ofpact *a, struct ds *s)
2481 const struct ofpact_enqueue *enqueue;
2482 const struct ofpact_resubmit *resubmit;
2483 const struct ofpact_controller *controller;
2484 const struct ofpact_metadata *metadata;
2485 const struct ofpact_tunnel *tunnel;
2486 const struct ofpact_sample *sample;
2491 port = ofpact_get_OUTPUT(a)->port;
2492 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
2493 ds_put_format(s, "output:%"PRIu16, port);
2495 ofputil_format_port(port, s);
2496 if (port == OFPP_CONTROLLER) {
2497 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2502 case OFPACT_CONTROLLER:
2503 controller = ofpact_get_CONTROLLER(a);
2504 if (controller->reason == OFPR_ACTION &&
2505 controller->controller_id == 0) {
2506 ds_put_format(s, "CONTROLLER:%"PRIu16,
2507 ofpact_get_CONTROLLER(a)->max_len);
2509 enum ofp_packet_in_reason reason = controller->reason;
2511 ds_put_cstr(s, "controller(");
2512 if (reason != OFPR_ACTION) {
2513 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2515 ds_put_format(s, "reason=%s,",
2516 ofputil_packet_in_reason_to_string(
2517 reason, reasonbuf, sizeof reasonbuf));
2519 if (controller->max_len != UINT16_MAX) {
2520 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2522 if (controller->controller_id != 0) {
2523 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2526 ds_put_char(s, ')');
2530 case OFPACT_ENQUEUE:
2531 enqueue = ofpact_get_ENQUEUE(a);
2532 ds_put_format(s, "enqueue:");
2533 ofputil_format_port(enqueue->port, s);
2534 ds_put_format(s, "q%"PRIu32, enqueue->queue);
2537 case OFPACT_OUTPUT_REG:
2538 ds_put_cstr(s, "output:");
2539 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2543 bundle_format(ofpact_get_BUNDLE(a), s);
2546 case OFPACT_SET_VLAN_VID:
2547 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2548 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2551 case OFPACT_SET_VLAN_PCP:
2552 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2553 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2556 case OFPACT_STRIP_VLAN:
2557 ds_put_cstr(s, "strip_vlan");
2560 case OFPACT_PUSH_VLAN:
2561 /* XXX 802.1AD case*/
2562 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2565 case OFPACT_SET_ETH_SRC:
2566 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2567 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2570 case OFPACT_SET_ETH_DST:
2571 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2572 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2575 case OFPACT_SET_IPV4_SRC:
2576 ds_put_format(s, "mod_nw_src:"IP_FMT,
2577 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2580 case OFPACT_SET_IPV4_DST:
2581 ds_put_format(s, "mod_nw_dst:"IP_FMT,
2582 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2585 case OFPACT_SET_IPV4_DSCP:
2586 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
2589 case OFPACT_SET_L4_SRC_PORT:
2590 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2593 case OFPACT_SET_L4_DST_PORT:
2594 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2597 case OFPACT_REG_MOVE:
2598 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2601 case OFPACT_REG_LOAD:
2602 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2605 case OFPACT_STACK_PUSH:
2606 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2609 case OFPACT_STACK_POP:
2610 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2613 case OFPACT_DEC_TTL:
2614 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2617 case OFPACT_SET_MPLS_TTL:
2618 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2619 ofpact_get_SET_MPLS_TTL(a)->ttl);
2622 case OFPACT_DEC_MPLS_TTL:
2623 ds_put_cstr(s, "dec_mpls_ttl");
2626 case OFPACT_SET_TUNNEL:
2627 tunnel = ofpact_get_SET_TUNNEL(a);
2628 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2629 (tunnel->tun_id > UINT32_MAX
2630 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2634 case OFPACT_SET_QUEUE:
2635 ds_put_format(s, "set_queue:%"PRIu32,
2636 ofpact_get_SET_QUEUE(a)->queue_id);
2639 case OFPACT_POP_QUEUE:
2640 ds_put_cstr(s, "pop_queue");
2643 case OFPACT_FIN_TIMEOUT:
2644 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2647 case OFPACT_RESUBMIT:
2648 resubmit = ofpact_get_RESUBMIT(a);
2649 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2650 ds_put_cstr(s, "resubmit:");
2651 ofputil_format_port(resubmit->in_port, s);
2653 ds_put_format(s, "resubmit(");
2654 if (resubmit->in_port != OFPP_IN_PORT) {
2655 ofputil_format_port(resubmit->in_port, s);
2657 ds_put_char(s, ',');
2658 if (resubmit->table_id != 255) {
2659 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2661 ds_put_char(s, ')');
2666 learn_format(ofpact_get_LEARN(a), s);
2669 case OFPACT_MULTIPATH:
2670 multipath_format(ofpact_get_MULTIPATH(a), s);
2674 print_note(ofpact_get_NOTE(a), s);
2677 case OFPACT_PUSH_MPLS:
2678 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2679 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2682 case OFPACT_POP_MPLS:
2683 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2684 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2688 ds_put_cstr(s, "exit");
2692 sample = ofpact_get_SAMPLE(a);
2694 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2695 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2696 sample->probability, sample->collector_set_id,
2697 sample->obs_domain_id, sample->obs_point_id);
2700 case OFPACT_WRITE_ACTIONS: {
2701 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2702 ds_put_format(s, "%s(",
2703 ovs_instruction_name_from_type(
2704 OVSINST_OFPIT11_WRITE_ACTIONS));
2705 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
2706 ds_put_char(s, ')');
2710 case OFPACT_CLEAR_ACTIONS:
2711 ds_put_format(s, "%s",
2712 ovs_instruction_name_from_type(
2713 OVSINST_OFPIT11_CLEAR_ACTIONS));
2716 case OFPACT_WRITE_METADATA:
2717 metadata = ofpact_get_WRITE_METADATA(a);
2718 ds_put_format(s, "%s:%#"PRIx64,
2719 ovs_instruction_name_from_type(
2720 OVSINST_OFPIT11_WRITE_METADATA),
2721 ntohll(metadata->metadata));
2722 if (metadata->mask != OVS_BE64_MAX) {
2723 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2727 case OFPACT_GOTO_TABLE:
2728 ds_put_format(s, "%s:%"PRIu8,
2729 ovs_instruction_name_from_type(
2730 OVSINST_OFPIT11_GOTO_TABLE),
2731 ofpact_get_GOTO_TABLE(a)->table_id);
2735 ds_put_format(s, "%s:%"PRIu32,
2736 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
2737 ofpact_get_METER(a)->meter_id);
2741 ds_put_format(s, "group:%"PRIu32,
2742 ofpact_get_GROUP(a)->group_id);
2747 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2748 * 'ofpacts' to 'string'. */
2750 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2754 ds_put_cstr(string, "drop");
2756 const struct ofpact *a;
2758 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2760 ds_put_cstr(string, ",");
2763 /* XXX write-actions */
2764 ofpact_format(a, string);
2769 /* Internal use by helpers. */
2772 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2774 struct ofpact *ofpact;
2776 ofpact_pad(ofpacts);
2777 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2778 ofpact_init(ofpact, type, len);
2783 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2785 memset(ofpact, 0, len);
2786 ofpact->type = type;
2787 ofpact->compat = OFPUTIL_ACTION_INVALID;
2791 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2792 * starting at 'ofpact'.
2794 * This is the correct way to update a variable-length ofpact's length after
2795 * adding the variable-length part of the payload. (See the large comment
2796 * near the end of ofp-actions.h for more information.) */
2798 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2800 ovs_assert(ofpact == ofpacts->l2);
2801 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2804 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2805 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2806 * client must call this itself after adding the final ofpact to an array of
2809 * (The consequences of failing to call this function are probably not dire.
2810 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2811 * not dereference it. That's undefined behavior, technically, but it will not
2812 * cause a real problem on common systems. Still, it seems better to call
2815 ofpact_pad(struct ofpbuf *ofpacts)
2817 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2819 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2824 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2827 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2828 load->dst.field = mf;
2830 load->dst.n_bits = mf->n_bits;
2831 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2832 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);