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. */
39 output_from_openflow10(const struct ofp10_action_output *oao,
42 struct ofpact_output *output;
44 output = ofpact_put_OUTPUT(out);
45 output->port = u16_to_ofp(ntohs(oao->port));
46 output->max_len = ntohs(oao->max_len);
48 return ofputil_check_output_port(output->port, OFPP_MAX);
52 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
55 struct ofpact_enqueue *enqueue;
57 enqueue = ofpact_put_ENQUEUE(out);
58 enqueue->port = u16_to_ofp(ntohs(oae->port));
59 enqueue->queue = ntohl(oae->queue_id);
60 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
61 && enqueue->port != OFPP_IN_PORT
62 && enqueue->port != OFPP_LOCAL) {
63 return OFPERR_OFPBAC_BAD_OUT_PORT;
69 resubmit_from_openflow(const struct nx_action_resubmit *nar,
72 struct ofpact_resubmit *resubmit;
74 resubmit = ofpact_put_RESUBMIT(out);
75 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
76 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
77 resubmit->table_id = 0xff;
81 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
84 struct ofpact_resubmit *resubmit;
86 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
87 return OFPERR_OFPBAC_BAD_ARGUMENT;
90 resubmit = ofpact_put_RESUBMIT(out);
91 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
92 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
93 resubmit->table_id = nar->table;
98 output_reg_from_openflow(const struct nx_action_output_reg *naor,
101 struct ofpact_output_reg *output_reg;
103 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
104 return OFPERR_OFPBAC_BAD_ARGUMENT;
107 output_reg = ofpact_put_OUTPUT_REG(out);
108 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
109 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
110 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
111 output_reg->max_len = ntohs(naor->max_len);
113 return mf_check_src(&output_reg->src, NULL);
117 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
120 struct ofpact_fin_timeout *oft;
122 oft = ofpact_put_FIN_TIMEOUT(out);
123 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
124 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
128 controller_from_openflow(const struct nx_action_controller *nac,
131 struct ofpact_controller *oc;
133 oc = ofpact_put_CONTROLLER(out);
134 oc->max_len = ntohs(nac->max_len);
135 oc->controller_id = ntohs(nac->controller_id);
136 oc->reason = nac->reason;
140 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
143 struct ofpact_metadata *om;
145 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
146 return OFPERR_NXBRC_MUST_BE_ZERO;
149 om = ofpact_put_WRITE_METADATA(out);
150 om->metadata = nawm->metadata;
151 om->mask = nawm->mask;
157 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
159 struct ofpact_note *note;
162 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
163 note = ofpact_put(out, OFPACT_NOTE,
164 offsetof(struct ofpact_note, data) + length);
165 note->length = length;
166 memcpy(note->data, nan->note, length);
170 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
173 struct ofpact_cnt_ids *ids;
174 enum ofperr error = 0;
176 ids = ofpact_put_DEC_TTL(out);
177 ids->ofpact.compat = compat;
178 ids->n_controllers = 1;
179 ofpbuf_put(out, &id, sizeof id);
181 ofpact_update_len(out, &ids->ofpact);
186 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
189 struct ofpact_cnt_ids *ids;
193 ids = ofpact_put_DEC_TTL(out);
194 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
195 ids->n_controllers = ntohs(nac_ids->n_controllers);
196 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
198 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
199 return OFPERR_NXBRC_MUST_BE_ZERO;
202 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
203 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
204 "allocated for controller ids. %zu bytes are required for "
205 "%"PRIu16" controllers.", ids_size,
206 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
207 return OFPERR_OFPBAC_BAD_LEN;
210 for (i = 0; i < ids->n_controllers; i++) {
211 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
212 ofpbuf_put(out, &id, sizeof id);
216 ofpact_update_len(out, &ids->ofpact);
222 sample_from_openflow(const struct nx_action_sample *nas,
225 struct ofpact_sample *sample;
227 sample = ofpact_put_SAMPLE(out);
228 sample->probability = ntohs(nas->probability);
229 sample->collector_set_id = ntohl(nas->collector_set_id);
230 sample->obs_domain_id = ntohl(nas->obs_domain_id);
231 sample->obs_point_id = ntohl(nas->obs_point_id);
233 if (sample->probability == 0) {
234 return OFPERR_OFPBAC_BAD_ARGUMENT;
241 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
243 const struct nx_action_header *nah = (const struct nx_action_header *) a;
244 uint16_t len = ntohs(a->header.len);
246 if (len < sizeof(struct nx_action_header)) {
247 return OFPERR_OFPBAC_BAD_LEN;
248 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
249 return OFPERR_OFPBAC_BAD_VENDOR;
252 switch (nah->subtype) {
253 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
254 case CONSTANT_HTONS(ENUM): \
256 ? len >= sizeof(struct STRUCT) \
257 : len == sizeof(struct STRUCT)) { \
258 *code = OFPUTIL_##ENUM; \
261 return OFPERR_OFPBAC_BAD_LEN; \
264 #include "ofp-util.def"
266 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
267 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
269 return OFPERR_OFPBAC_BAD_TYPE;
273 /* Parses 'a' to determine its type. On success stores the correct type into
274 * '*code' and returns 0. On failure returns an OFPERR_* error code and
275 * '*code' is indeterminate.
277 * The caller must have already verified that 'a''s length is potentially
278 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
279 * ofp_action) and no longer than the amount of space allocated to 'a').
281 * This function verifies that 'a''s length is correct for the type of action
282 * that it represents. */
284 decode_openflow10_action(const union ofp_action *a,
285 enum ofputil_action_code *code)
288 case CONSTANT_HTONS(OFPAT10_VENDOR):
289 return decode_nxast_action(a, code);
291 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
292 case CONSTANT_HTONS(ENUM): \
293 if (a->header.len == htons(sizeof(struct STRUCT))) { \
294 *code = OFPUTIL_##ENUM; \
297 return OFPERR_OFPBAC_BAD_LEN; \
300 #include "ofp-util.def"
303 return OFPERR_OFPBAC_BAD_TYPE;
308 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
311 const struct nx_action_resubmit *nar;
312 const struct nx_action_set_tunnel *nast;
313 const struct nx_action_set_queue *nasq;
314 const struct nx_action_note *nan;
315 const struct nx_action_set_tunnel64 *nast64;
316 const struct nx_action_write_metadata *nawm;
317 struct ofpact_tunnel *tunnel;
318 enum ofperr error = 0;
321 case OFPUTIL_ACTION_INVALID:
322 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
323 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
324 #include "ofp-util.def"
327 case OFPUTIL_NXAST_RESUBMIT:
328 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
331 case OFPUTIL_NXAST_SET_TUNNEL:
332 nast = (const struct nx_action_set_tunnel *) a;
333 tunnel = ofpact_put_SET_TUNNEL(out);
334 tunnel->ofpact.compat = code;
335 tunnel->tun_id = ntohl(nast->tun_id);
338 case OFPUTIL_NXAST_WRITE_METADATA:
339 nawm = ALIGNED_CAST(const struct nx_action_write_metadata *, a);
340 error = metadata_from_nxast(nawm, out);
343 case OFPUTIL_NXAST_SET_QUEUE:
344 nasq = (const struct nx_action_set_queue *) a;
345 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
348 case OFPUTIL_NXAST_POP_QUEUE:
349 ofpact_put_POP_QUEUE(out);
352 case OFPUTIL_NXAST_REG_MOVE:
353 error = nxm_reg_move_from_openflow(
354 (const struct nx_action_reg_move *) a, out);
357 case OFPUTIL_NXAST_REG_LOAD:
358 error = nxm_reg_load_from_openflow(
359 ALIGNED_CAST(const struct nx_action_reg_load *, a), out);
362 case OFPUTIL_NXAST_STACK_PUSH:
363 error = nxm_stack_push_from_openflow(
364 (const struct nx_action_stack *) a, out);
367 case OFPUTIL_NXAST_STACK_POP:
368 error = nxm_stack_pop_from_openflow(
369 (const struct nx_action_stack *) a, out);
372 case OFPUTIL_NXAST_NOTE:
373 nan = (const struct nx_action_note *) a;
374 note_from_openflow(nan, out);
377 case OFPUTIL_NXAST_SET_TUNNEL64:
378 nast64 = ALIGNED_CAST(const struct nx_action_set_tunnel64 *, a);
379 tunnel = ofpact_put_SET_TUNNEL(out);
380 tunnel->ofpact.compat = code;
381 tunnel->tun_id = ntohll(nast64->tun_id);
384 case OFPUTIL_NXAST_MULTIPATH:
385 error = multipath_from_openflow((const struct nx_action_multipath *) a,
386 ofpact_put_MULTIPATH(out));
389 case OFPUTIL_NXAST_BUNDLE:
390 case OFPUTIL_NXAST_BUNDLE_LOAD:
391 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
394 case OFPUTIL_NXAST_OUTPUT_REG:
395 error = output_reg_from_openflow(
396 (const struct nx_action_output_reg *) a, out);
399 case OFPUTIL_NXAST_RESUBMIT_TABLE:
400 nar = (const struct nx_action_resubmit *) a;
401 error = resubmit_table_from_openflow(nar, out);
404 case OFPUTIL_NXAST_LEARN:
405 error = learn_from_openflow(
406 ALIGNED_CAST(const struct nx_action_learn *, a), out);
409 case OFPUTIL_NXAST_EXIT:
410 ofpact_put_EXIT(out);
413 case OFPUTIL_NXAST_DEC_TTL:
414 error = dec_ttl_from_openflow(out, code);
417 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
418 error = dec_ttl_cnt_ids_from_openflow(
419 (const struct nx_action_cnt_ids *) a, out);
422 case OFPUTIL_NXAST_FIN_TIMEOUT:
423 fin_timeout_from_openflow(
424 (const struct nx_action_fin_timeout *) a, out);
427 case OFPUTIL_NXAST_CONTROLLER:
428 controller_from_openflow((const struct nx_action_controller *) a, out);
431 case OFPUTIL_NXAST_PUSH_MPLS: {
432 struct nx_action_push_mpls *nxapm = (struct nx_action_push_mpls *)a;
433 if (!eth_type_mpls(nxapm->ethertype)) {
434 return OFPERR_OFPBAC_BAD_ARGUMENT;
436 ofpact_put_PUSH_MPLS(out)->ethertype = nxapm->ethertype;
440 case OFPUTIL_NXAST_SET_MPLS_TTL: {
441 struct nx_action_mpls_ttl *nxamt = (struct nx_action_mpls_ttl *)a;
442 ofpact_put_SET_MPLS_TTL(out)->ttl = nxamt->ttl;
446 case OFPUTIL_NXAST_DEC_MPLS_TTL:
447 ofpact_put_DEC_MPLS_TTL(out);
450 case OFPUTIL_NXAST_POP_MPLS: {
451 struct nx_action_pop_mpls *nxapm = (struct nx_action_pop_mpls *)a;
452 if (eth_type_mpls(nxapm->ethertype)) {
453 return OFPERR_OFPBAC_BAD_ARGUMENT;
455 ofpact_put_POP_MPLS(out)->ethertype = nxapm->ethertype;
459 case OFPUTIL_NXAST_SAMPLE:
460 error = sample_from_openflow(
461 (const struct nx_action_sample *) a, out);
469 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
471 enum ofputil_action_code code;
474 error = decode_openflow10_action(a, &code);
480 case OFPUTIL_ACTION_INVALID:
481 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
482 #include "ofp-util.def"
485 case OFPUTIL_OFPAT10_OUTPUT:
486 return output_from_openflow10(&a->output10, out);
488 case OFPUTIL_OFPAT10_SET_VLAN_VID:
489 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
490 return OFPERR_OFPBAC_BAD_ARGUMENT;
492 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
495 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
496 if (a->vlan_pcp.vlan_pcp & ~7) {
497 return OFPERR_OFPBAC_BAD_ARGUMENT;
499 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
502 case OFPUTIL_OFPAT10_STRIP_VLAN:
503 ofpact_put_STRIP_VLAN(out);
506 case OFPUTIL_OFPAT10_SET_DL_SRC:
507 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
508 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
511 case OFPUTIL_OFPAT10_SET_DL_DST:
512 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
513 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
516 case OFPUTIL_OFPAT10_SET_NW_SRC:
517 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
520 case OFPUTIL_OFPAT10_SET_NW_DST:
521 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
524 case OFPUTIL_OFPAT10_SET_NW_TOS:
525 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
526 return OFPERR_OFPBAC_BAD_ARGUMENT;
528 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
531 case OFPUTIL_OFPAT10_SET_TP_SRC:
532 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
535 case OFPUTIL_OFPAT10_SET_TP_DST:
536 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
540 case OFPUTIL_OFPAT10_ENQUEUE:
541 error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
545 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
546 #include "ofp-util.def"
547 return ofpact_from_nxast(a, code, out);
553 static inline union ofp_action *
554 action_next(const union ofp_action *a)
556 return ((union ofp_action *) (void *)
557 ((uint8_t *) a + ntohs(a->header.len)));
561 action_is_valid(const union ofp_action *a, size_t n_actions)
563 uint16_t len = ntohs(a->header.len);
564 return (!(len % OFP_ACTION_ALIGN)
566 && len / sizeof *a <= n_actions);
569 /* This macro is careful to check for actions with bad lengths. */
570 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
571 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
572 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
573 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
574 (ITER) = action_next(ITER)))
577 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
580 if (!VLOG_DROP_WARN(&rl)) {
584 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
585 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
586 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
592 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
594 enum ofperr (*ofpact_from_openflow)(
595 const union ofp_action *a, struct ofpbuf *out))
597 const union ofp_action *a;
600 ACTION_FOR_EACH (a, left, in, n_in) {
601 enum ofperr error = ofpact_from_openflow(a, out);
603 log_bad_action(in, n_in, a - in, error);
608 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
609 log_bad_action(in, n_in, n_in - left, error);
618 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
621 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
625 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
626 struct ofpbuf *ofpacts,
627 enum ofperr (*translate)(const union ofp_action *actions,
629 struct ofpbuf *ofpacts))
631 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
632 const union ofp_action *actions;
635 ofpbuf_clear(ofpacts);
637 if (actions_len % OFP_ACTION_ALIGN != 0) {
638 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
639 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
640 return OFPERR_OFPBRC_BAD_LEN;
643 actions = ofpbuf_try_pull(openflow, actions_len);
644 if (actions == NULL) {
645 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
646 "remaining message length (%zu)",
647 actions_len, openflow->size);
648 return OFPERR_OFPBRC_BAD_LEN;
651 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
653 ofpbuf_clear(ofpacts);
657 error = ofpacts_verify(ofpacts->data, ofpacts->size);
659 ofpbuf_clear(ofpacts);
664 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
665 * front of 'openflow' into ofpacts. On success, replaces any existing content
666 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
667 * Returns 0 if successful, otherwise an OpenFlow error.
669 * The parsed actions are valid generically, but they may not be valid in a
670 * specific context. For example, port numbers up to OFPP_MAX are valid
671 * generically, but specific datapaths may only support port numbers in a
672 * smaller range. Use ofpacts_check() to additional check whether actions are
673 * valid in a specific context. */
675 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
676 struct ofpbuf *ofpacts)
678 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
679 ofpacts_from_openflow10);
682 /* OpenFlow 1.1 actions. */
684 /* Parses 'a' to determine its type. On success stores the correct type into
685 * '*code' and returns 0. On failure returns an OFPERR_* error code and
686 * '*code' is indeterminate.
688 * The caller must have already verified that 'a''s length is potentially
689 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
690 * ofp_action) and no longer than the amount of space allocated to 'a').
692 * This function verifies that 'a''s length is correct for the type of action
693 * that it represents. */
695 decode_openflow11_action(const union ofp_action *a,
696 enum ofputil_action_code *code)
701 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
702 return decode_nxast_action(a, code);
704 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
705 case CONSTANT_HTONS(ENUM): \
706 len = ntohs(a->header.len); \
708 ? len >= sizeof(struct STRUCT) \
709 : len == sizeof(struct STRUCT)) { \
710 *code = OFPUTIL_##ENUM; \
713 return OFPERR_OFPBAC_BAD_LEN; \
716 #include "ofp-util.def"
719 return OFPERR_OFPBAC_BAD_TYPE;
724 output_from_openflow11(const struct ofp11_action_output *oao,
727 struct ofpact_output *output;
730 output = ofpact_put_OUTPUT(out);
731 output->max_len = ntohs(oao->max_len);
733 error = ofputil_port_from_ofp11(oao->port, &output->port);
738 return ofputil_check_output_port(output->port, OFPP_MAX);
742 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
744 enum ofputil_action_code code;
747 error = decode_openflow11_action(a, &code);
753 case OFPUTIL_ACTION_INVALID:
754 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
755 #include "ofp-util.def"
758 case OFPUTIL_OFPAT11_OUTPUT:
759 return output_from_openflow11((const struct ofp11_action_output *) a,
762 case OFPUTIL_OFPAT11_SET_VLAN_VID:
763 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
764 return OFPERR_OFPBAC_BAD_ARGUMENT;
766 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
769 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
770 if (a->vlan_pcp.vlan_pcp & ~7) {
771 return OFPERR_OFPBAC_BAD_ARGUMENT;
773 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
776 case OFPUTIL_OFPAT11_PUSH_VLAN:
777 if (((const struct ofp11_action_push *)a)->ethertype !=
778 htons(ETH_TYPE_VLAN_8021Q)) {
779 /* XXX 802.1AD(QinQ) isn't supported at the moment */
780 return OFPERR_OFPBAC_BAD_ARGUMENT;
782 ofpact_put_PUSH_VLAN(out);
785 case OFPUTIL_OFPAT11_POP_VLAN:
786 ofpact_put_STRIP_VLAN(out);
789 case OFPUTIL_OFPAT11_SET_QUEUE:
790 ofpact_put_SET_QUEUE(out)->queue_id =
791 ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
794 case OFPUTIL_OFPAT11_SET_DL_SRC:
795 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
796 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
799 case OFPUTIL_OFPAT11_SET_DL_DST:
800 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
801 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
804 case OFPUTIL_OFPAT11_DEC_NW_TTL:
805 dec_ttl_from_openflow(out, code);
808 case OFPUTIL_OFPAT11_SET_NW_SRC:
809 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
812 case OFPUTIL_OFPAT11_SET_NW_DST:
813 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
816 case OFPUTIL_OFPAT11_SET_NW_TOS:
817 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
818 return OFPERR_OFPBAC_BAD_ARGUMENT;
820 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
823 case OFPUTIL_OFPAT11_SET_TP_SRC:
824 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
827 case OFPUTIL_OFPAT11_SET_TP_DST:
828 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
831 case OFPUTIL_OFPAT12_SET_FIELD:
832 return nxm_reg_load_from_openflow12_set_field(
833 (const struct ofp12_action_set_field *)a, out);
835 case OFPUTIL_OFPAT11_SET_MPLS_TTL: {
836 struct ofp11_action_mpls_ttl *oamt = (struct ofp11_action_mpls_ttl *)a;
837 ofpact_put_SET_MPLS_TTL(out)->ttl = oamt->mpls_ttl;
841 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
842 ofpact_put_DEC_MPLS_TTL(out);
845 case OFPUTIL_OFPAT11_PUSH_MPLS: {
846 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
847 if (!eth_type_mpls(oap->ethertype)) {
848 return OFPERR_OFPBAC_BAD_ARGUMENT;
850 ofpact_put_PUSH_MPLS(out)->ethertype = oap->ethertype;
854 case OFPUTIL_OFPAT11_POP_MPLS: {
855 struct ofp11_action_pop_mpls *oapm = (struct ofp11_action_pop_mpls *)a;
856 if (eth_type_mpls(oapm->ethertype)) {
857 return OFPERR_OFPBAC_BAD_ARGUMENT;
859 ofpact_put_POP_MPLS(out)->ethertype = oapm->ethertype;
863 case OFPUTIL_OFPAT11_GROUP: {
864 struct ofp11_action_group *oag = (struct ofp11_action_group *)a;
865 ofpact_put_GROUP(out)->group_id = ntohl(oag->group_id);
869 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
870 #include "ofp-util.def"
871 return ofpact_from_nxast(a, code, out);
878 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
881 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
884 /* True if an action sets the value of a field
885 * in a way that is compatibile with the action set.
886 * False otherwise. */
888 ofpact_is_set_action(const struct ofpact *a)
891 case OFPACT_REG_LOAD:
892 case OFPACT_SET_ETH_DST:
893 case OFPACT_SET_ETH_SRC:
894 case OFPACT_SET_IPV4_DSCP:
895 case OFPACT_SET_IPV4_DST:
896 case OFPACT_SET_IPV4_SRC:
897 case OFPACT_SET_L4_DST_PORT:
898 case OFPACT_SET_L4_SRC_PORT:
899 case OFPACT_SET_MPLS_TTL:
900 case OFPACT_SET_QUEUE:
901 case OFPACT_SET_TUNNEL:
902 case OFPACT_SET_VLAN_PCP:
903 case OFPACT_SET_VLAN_VID:
906 case OFPACT_CLEAR_ACTIONS:
907 case OFPACT_CONTROLLER:
908 case OFPACT_DEC_MPLS_TTL:
912 case OFPACT_FIN_TIMEOUT:
913 case OFPACT_GOTO_TABLE:
917 case OFPACT_MULTIPATH:
920 case OFPACT_OUTPUT_REG:
921 case OFPACT_POP_MPLS:
922 case OFPACT_POP_QUEUE:
923 case OFPACT_PUSH_MPLS:
924 case OFPACT_PUSH_VLAN:
925 case OFPACT_REG_MOVE:
926 case OFPACT_RESUBMIT:
928 case OFPACT_STACK_POP:
929 case OFPACT_STACK_PUSH:
930 case OFPACT_STRIP_VLAN:
931 case OFPACT_WRITE_ACTIONS:
932 case OFPACT_WRITE_METADATA:
939 /* True if an action is allowed in the action set.
940 * False otherwise. */
942 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
945 case OFPACT_DEC_MPLS_TTL:
949 case OFPACT_POP_MPLS:
950 case OFPACT_PUSH_MPLS:
951 case OFPACT_PUSH_VLAN:
952 case OFPACT_REG_LOAD:
953 case OFPACT_SET_ETH_DST:
954 case OFPACT_SET_ETH_SRC:
955 case OFPACT_SET_IPV4_DSCP:
956 case OFPACT_SET_IPV4_DST:
957 case OFPACT_SET_IPV4_SRC:
958 case OFPACT_SET_L4_DST_PORT:
959 case OFPACT_SET_L4_SRC_PORT:
960 case OFPACT_SET_MPLS_TTL:
961 case OFPACT_SET_QUEUE:
962 case OFPACT_SET_TUNNEL:
963 case OFPACT_SET_VLAN_PCP:
964 case OFPACT_SET_VLAN_VID:
965 case OFPACT_STRIP_VLAN:
968 /* In general these actions are excluded because they are not part of
969 * the OpenFlow specification nor map to actions that are defined in
970 * the specification. Thus the order in which they should be applied
971 * in the action set is undefined. */
973 case OFPACT_CONTROLLER:
976 case OFPACT_FIN_TIMEOUT:
978 case OFPACT_MULTIPATH:
980 case OFPACT_OUTPUT_REG:
981 case OFPACT_POP_QUEUE:
982 case OFPACT_REG_MOVE:
983 case OFPACT_RESUBMIT:
985 case OFPACT_STACK_POP:
986 case OFPACT_STACK_PUSH:
988 /* The action set may only include actions and thus
989 * may not include any instructions */
990 case OFPACT_CLEAR_ACTIONS:
991 case OFPACT_GOTO_TABLE:
993 case OFPACT_WRITE_ACTIONS:
994 case OFPACT_WRITE_METADATA:
1001 /* Append ofpact 'a' onto the tail of 'out' */
1003 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1005 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1008 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1010 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1011 enum ofpact_type filter)
1013 const struct ofpact *target;
1014 const struct ofpact *a;
1017 OFPACT_FOR_EACH (a, in->data, in->size) {
1018 if (a->type == filter) {
1023 ofpact_copy(out, target);
1025 return target != NULL;
1028 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1029 * The order of appended ofpacts is preserved between 'in' and 'out' */
1031 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1032 bool (*filter)(const struct ofpact *))
1034 const struct ofpact *a;
1036 OFPACT_FOR_EACH (a, in->data, in->size) {
1038 ofpact_copy(out, a);
1043 /* Reads 'action_set', which contains ofpacts accumulated by
1044 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1045 * executed directly into 'action_list'. (These names correspond to the
1046 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1048 * In general this involves appending the last instance of each action that is
1049 * adimissible in the action set in the order described in the OpenFlow
1053 * + output action is only appended if no group action was present in 'in'.
1054 * + As a simplification all set actions are copied in the order the are
1055 * provided in 'in' as many set actions applied to a field has the same
1056 * affect as only applying the last action that sets a field and
1057 * duplicates are removed by do_xlate_actions().
1058 * This has an unwanted side-effect of compsoting multiple
1059 * LOAD_REG actions that touch different regions of the same field. */
1061 ofpacts_execute_action_set(struct ofpbuf *action_list,
1062 const struct ofpbuf *action_set)
1064 /* The OpenFlow spec "Action Set" section specifies this order. */
1065 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1066 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1067 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1068 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1069 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1070 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1071 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1072 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1074 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1075 * we should execute only OFPACT_GROUP.
1077 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1078 * all the actions because there's no point in modifying a packet that will
1079 * not be sent anywhere. */
1080 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1081 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1082 ofpbuf_clear(action_list);
1088 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1089 size_t n_in, struct ofpbuf *out)
1093 size_t start = out->size;
1095 error = ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
1100 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1101 if (!ofpact_is_allowed_in_actions_set(a)) {
1102 VLOG_WARN_RL(&rl, "disallowed action in action set");
1103 return OFPERR_OFPBAC_BAD_TYPE;
1111 /* OpenFlow 1.1 instructions. */
1113 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1114 static inline const struct STRUCT * OVS_UNUSED \
1115 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1117 ovs_assert(inst->type == htons(ENUM)); \
1118 return ALIGNED_CAST(struct STRUCT *, inst); \
1121 static inline void OVS_UNUSED \
1122 instruction_init_##ENUM(struct STRUCT *s) \
1124 memset(s, 0, sizeof *s); \
1125 s->type = htons(ENUM); \
1126 s->len = htons(sizeof *s); \
1129 static inline struct STRUCT * OVS_UNUSED \
1130 instruction_put_##ENUM(struct ofpbuf *buf) \
1132 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1133 instruction_init_##ENUM(s); \
1139 struct instruction_type_info {
1140 enum ovs_instruction_type type;
1144 static const struct instruction_type_info inst_info[] = {
1145 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1151 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1153 return inst_info[type].name;
1157 ovs_instruction_type_from_name(const char *name)
1159 const struct instruction_type_info *p;
1160 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1161 if (!strcasecmp(name, p->name)) {
1168 enum ovs_instruction_type
1169 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1173 return OVSINST_OFPIT13_METER;
1174 case OFPACT_CLEAR_ACTIONS:
1175 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1176 case OFPACT_WRITE_ACTIONS:
1177 return OVSINST_OFPIT11_WRITE_ACTIONS;
1178 case OFPACT_WRITE_METADATA:
1179 return OVSINST_OFPIT11_WRITE_METADATA;
1180 case OFPACT_GOTO_TABLE:
1181 return OVSINST_OFPIT11_GOTO_TABLE;
1184 case OFPACT_CONTROLLER:
1185 case OFPACT_ENQUEUE:
1186 case OFPACT_OUTPUT_REG:
1188 case OFPACT_SET_VLAN_VID:
1189 case OFPACT_SET_VLAN_PCP:
1190 case OFPACT_STRIP_VLAN:
1191 case OFPACT_PUSH_VLAN:
1192 case OFPACT_SET_ETH_SRC:
1193 case OFPACT_SET_ETH_DST:
1194 case OFPACT_SET_IPV4_SRC:
1195 case OFPACT_SET_IPV4_DST:
1196 case OFPACT_SET_IPV4_DSCP:
1197 case OFPACT_SET_L4_SRC_PORT:
1198 case OFPACT_SET_L4_DST_PORT:
1199 case OFPACT_REG_MOVE:
1200 case OFPACT_REG_LOAD:
1201 case OFPACT_STACK_PUSH:
1202 case OFPACT_STACK_POP:
1203 case OFPACT_DEC_TTL:
1204 case OFPACT_SET_MPLS_TTL:
1205 case OFPACT_DEC_MPLS_TTL:
1206 case OFPACT_PUSH_MPLS:
1207 case OFPACT_POP_MPLS:
1208 case OFPACT_SET_TUNNEL:
1209 case OFPACT_SET_QUEUE:
1210 case OFPACT_POP_QUEUE:
1211 case OFPACT_FIN_TIMEOUT:
1212 case OFPACT_RESUBMIT:
1214 case OFPACT_MULTIPATH:
1219 return OVSINST_OFPIT11_APPLY_ACTIONS;
1223 static inline struct ofp11_instruction *
1224 instruction_next(const struct ofp11_instruction *inst)
1226 return ((struct ofp11_instruction *) (void *)
1227 ((uint8_t *) inst + ntohs(inst->len)));
1231 instruction_is_valid(const struct ofp11_instruction *inst,
1232 size_t n_instructions)
1234 uint16_t len = ntohs(inst->len);
1235 return (!(len % OFP11_INSTRUCTION_ALIGN)
1236 && len >= sizeof *inst
1237 && len / sizeof *inst <= n_instructions);
1240 /* This macro is careful to check for instructions with bad lengths. */
1241 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1242 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1243 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1244 ((LEFT) -= (ntohs((ITER)->len) \
1245 / sizeof(struct ofp11_instruction)), \
1246 (ITER) = instruction_next(ITER)))
1249 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1250 enum ovs_instruction_type *type)
1252 uint16_t len = ntohs(inst->len);
1254 switch (inst->type) {
1255 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1256 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1258 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1259 case CONSTANT_HTONS(ENUM): \
1261 ? len >= sizeof(struct STRUCT) \
1262 : len == sizeof(struct STRUCT)) { \
1263 *type = OVSINST_##ENUM; \
1266 return OFPERR_OFPBIC_BAD_LEN; \
1272 return OFPERR_OFPBIC_UNKNOWN_INST;
1277 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1279 const struct ofp11_instruction *out[])
1281 const struct ofp11_instruction *inst;
1284 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1285 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1286 enum ovs_instruction_type type;
1289 error = decode_openflow11_instruction(inst, &type);
1295 return OFPERR_ONFBIC_DUP_INSTRUCTION;
1301 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1302 (n_insts - left) * sizeof *inst);
1303 return OFPERR_OFPBIC_BAD_LEN;
1309 get_actions_from_instruction(const struct ofp11_instruction *inst,
1310 const union ofp_action **actions,
1313 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1314 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1317 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
1318 * front of 'openflow' into ofpacts. On success, replaces any existing content
1319 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1320 * Returns 0 if successful, otherwise an OpenFlow error.
1322 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1323 * instructions, so you should call ofpacts_pull_openflow11_instructions()
1324 * instead of this function.
1326 * The parsed actions are valid generically, but they may not be valid in a
1327 * specific context. For example, port numbers up to OFPP_MAX are valid
1328 * generically, but specific datapaths may only support port numbers in a
1329 * smaller range. Use ofpacts_check() to additional check whether actions are
1330 * valid in a specific context. */
1332 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1333 unsigned int actions_len,
1334 struct ofpbuf *ofpacts)
1336 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1337 ofpacts_from_openflow11);
1341 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1342 unsigned int instructions_len,
1343 struct ofpbuf *ofpacts)
1345 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1346 const struct ofp11_instruction *instructions;
1347 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1350 ofpbuf_clear(ofpacts);
1352 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1353 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1355 instructions_len, OFP11_INSTRUCTION_ALIGN);
1356 error = OFPERR_OFPBIC_BAD_LEN;
1360 instructions = ofpbuf_try_pull(openflow, instructions_len);
1361 if (instructions == NULL) {
1362 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1363 "remaining message length (%zu)",
1364 instructions_len, openflow->size);
1365 error = OFPERR_OFPBIC_BAD_LEN;
1369 error = decode_openflow11_instructions(
1370 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1376 if (insts[OVSINST_OFPIT13_METER]) {
1377 const struct ofp13_instruction_meter *oim;
1378 struct ofpact_meter *om;
1380 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1381 insts[OVSINST_OFPIT13_METER]);
1383 om = ofpact_put_METER(ofpacts);
1384 om->meter_id = ntohl(oim->meter_id);
1386 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1387 const union ofp_action *actions;
1390 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1391 &actions, &n_actions);
1392 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1397 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1398 instruction_get_OFPIT11_CLEAR_ACTIONS(
1399 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1400 ofpact_put_CLEAR_ACTIONS(ofpacts);
1402 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1403 struct ofpact_nest *on;
1404 const union ofp_action *actions;
1406 size_t start = ofpacts->size;
1408 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1409 offsetof(struct ofpact_nest, actions));
1410 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1411 &actions, &n_actions);
1412 error = ofpacts_from_openflow11_for_action_set(actions, n_actions,
1417 on->ofpact.len = ofpacts->size - start;
1419 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1420 const struct ofp11_instruction_write_metadata *oiwm;
1421 struct ofpact_metadata *om;
1423 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1424 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1426 om = ofpact_put_WRITE_METADATA(ofpacts);
1427 om->metadata = oiwm->metadata;
1428 om->mask = oiwm->metadata_mask;
1430 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1431 const struct ofp11_instruction_goto_table *oigt;
1432 struct ofpact_goto_table *ogt;
1434 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1435 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1436 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1437 ogt->table_id = oigt->table_id;
1440 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1443 ofpbuf_clear(ofpacts);
1448 /* May modify flow->dl_type, caller must restore it. */
1450 ofpact_check__(const struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
1453 const struct ofpact_enqueue *enqueue;
1457 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1460 case OFPACT_CONTROLLER:
1463 case OFPACT_ENQUEUE:
1464 enqueue = ofpact_get_ENQUEUE(a);
1465 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1466 && enqueue->port != OFPP_IN_PORT
1467 && enqueue->port != OFPP_LOCAL) {
1468 return OFPERR_OFPBAC_BAD_OUT_PORT;
1472 case OFPACT_OUTPUT_REG:
1473 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1476 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1478 case OFPACT_SET_VLAN_VID:
1479 case OFPACT_SET_VLAN_PCP:
1480 case OFPACT_STRIP_VLAN:
1481 case OFPACT_PUSH_VLAN:
1482 case OFPACT_SET_ETH_SRC:
1483 case OFPACT_SET_ETH_DST:
1484 case OFPACT_SET_IPV4_SRC:
1485 case OFPACT_SET_IPV4_DST:
1486 case OFPACT_SET_IPV4_DSCP:
1487 case OFPACT_SET_L4_SRC_PORT:
1488 case OFPACT_SET_L4_DST_PORT:
1491 case OFPACT_REG_MOVE:
1492 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1494 case OFPACT_REG_LOAD:
1495 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1497 case OFPACT_STACK_PUSH:
1498 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1500 case OFPACT_STACK_POP:
1501 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1503 case OFPACT_DEC_TTL:
1504 case OFPACT_SET_MPLS_TTL:
1505 case OFPACT_DEC_MPLS_TTL:
1506 case OFPACT_SET_TUNNEL:
1507 case OFPACT_SET_QUEUE:
1508 case OFPACT_POP_QUEUE:
1509 case OFPACT_FIN_TIMEOUT:
1510 case OFPACT_RESUBMIT:
1514 return learn_check(ofpact_get_LEARN(a), flow);
1516 case OFPACT_MULTIPATH:
1517 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1523 case OFPACT_PUSH_MPLS:
1524 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1527 case OFPACT_POP_MPLS:
1528 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1534 case OFPACT_CLEAR_ACTIONS:
1537 case OFPACT_WRITE_ACTIONS: {
1538 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
1539 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
1540 flow, max_ports, table_id);
1543 case OFPACT_WRITE_METADATA:
1546 case OFPACT_METER: {
1547 uint32_t mid = ofpact_get_METER(a)->meter_id;
1548 if (mid == 0 || mid > OFPM13_MAX) {
1549 return OFPERR_OFPMMFC_INVALID_METER;
1554 case OFPACT_GOTO_TABLE:
1555 if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
1556 return OFPERR_OFPBRC_BAD_TABLE_ID;
1568 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1569 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1570 * switch with no more than 'max_ports' ports.
1572 * May temporarily modify 'flow', but restores the changes before returning. */
1574 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1575 struct flow *flow, ofp_port_t max_ports, uint8_t table_id)
1577 const struct ofpact *a;
1578 ovs_be16 dl_type = flow->dl_type;
1579 enum ofperr error = 0;
1581 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1582 error = ofpact_check__(a, flow, max_ports, table_id);
1587 flow->dl_type = dl_type; /* Restore. */
1591 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1592 * in the appropriate order as defined by the OpenFlow spec. */
1594 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1596 const struct ofpact *a;
1597 enum ovs_instruction_type inst;
1599 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1600 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1601 enum ovs_instruction_type next;
1603 next = ovs_instruction_type_from_ofpact_type(a->type);
1604 if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
1607 const char *name = ovs_instruction_name_from_type(inst);
1608 const char *next_name = ovs_instruction_name_from_type(next);
1611 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1612 "1.1+ compatibility", name);
1614 VLOG_WARN("invalid instruction ordering: %s must appear "
1615 "before %s, for OpenFlow 1.1+ compatibility",
1618 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1627 /* Converting ofpacts to Nicira OpenFlow extensions. */
1630 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1633 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1635 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1636 output_reg->src.n_bits);
1637 naor->src = htonl(output_reg->src.field->nxm_header);
1638 naor->max_len = htons(output_reg->max_len);
1642 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1645 struct nx_action_resubmit *nar;
1647 if (resubmit->table_id == 0xff
1648 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1649 nar = ofputil_put_NXAST_RESUBMIT(out);
1651 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1652 nar->table = resubmit->table_id;
1654 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
1658 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1661 uint64_t tun_id = tunnel->tun_id;
1663 if (tun_id <= UINT32_MAX
1664 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1665 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1667 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1672 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1675 struct nx_action_write_metadata *nawm;
1677 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1678 nawm->metadata = om->metadata;
1679 nawm->mask = om->mask;
1683 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1685 size_t start_ofs = out->size;
1686 struct nx_action_note *nan;
1687 unsigned int remainder;
1690 nan = ofputil_put_NXAST_NOTE(out);
1691 out->size -= sizeof nan->note;
1693 ofpbuf_put(out, note->data, note->length);
1695 len = out->size - start_ofs;
1696 remainder = len % OFP_ACTION_ALIGN;
1698 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1700 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
1701 nan->len = htons(out->size - start_ofs);
1705 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1708 struct nx_action_controller *nac;
1710 nac = ofputil_put_NXAST_CONTROLLER(out);
1711 nac->max_len = htons(oc->max_len);
1712 nac->controller_id = htons(oc->controller_id);
1713 nac->reason = oc->reason;
1717 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1720 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1721 ofputil_put_NXAST_DEC_TTL(out);
1723 struct nx_action_cnt_ids *nac_ids =
1724 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1725 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1729 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1730 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1732 ids = ofpbuf_put_zeros(out, ids_len);
1733 for (i = 0; i < oc_ids->n_controllers; i++) {
1734 ids[i] = htons(oc_ids->cnt_ids[i]);
1740 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1743 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1744 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1745 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1749 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1752 struct nx_action_sample *nas;
1754 nas = ofputil_put_NXAST_SAMPLE(out);
1755 nas->probability = htons(os->probability);
1756 nas->collector_set_id = htonl(os->collector_set_id);
1757 nas->obs_domain_id = htonl(os->obs_domain_id);
1758 nas->obs_point_id = htonl(os->obs_point_id);
1762 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1765 case OFPACT_CONTROLLER:
1766 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1769 case OFPACT_OUTPUT_REG:
1770 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1774 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1777 case OFPACT_REG_MOVE:
1778 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1781 case OFPACT_REG_LOAD:
1782 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1785 case OFPACT_STACK_PUSH:
1786 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1789 case OFPACT_STACK_POP:
1790 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1793 case OFPACT_DEC_TTL:
1794 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1797 case OFPACT_SET_MPLS_TTL:
1798 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1799 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1802 case OFPACT_DEC_MPLS_TTL:
1803 ofputil_put_NXAST_DEC_MPLS_TTL(out);
1806 case OFPACT_SET_TUNNEL:
1807 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1810 case OFPACT_WRITE_METADATA:
1811 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1814 case OFPACT_SET_QUEUE:
1815 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1816 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1819 case OFPACT_POP_QUEUE:
1820 ofputil_put_NXAST_POP_QUEUE(out);
1823 case OFPACT_FIN_TIMEOUT:
1824 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1827 case OFPACT_RESUBMIT:
1828 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1832 learn_to_nxast(ofpact_get_LEARN(a), out);
1835 case OFPACT_MULTIPATH:
1836 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1840 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1844 ofputil_put_NXAST_EXIT(out);
1847 case OFPACT_PUSH_MPLS:
1848 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1849 ofpact_get_PUSH_MPLS(a)->ethertype;
1852 case OFPACT_POP_MPLS:
1853 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1854 ofpact_get_POP_MPLS(a)->ethertype;
1858 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
1863 case OFPACT_ENQUEUE:
1864 case OFPACT_SET_VLAN_VID:
1865 case OFPACT_SET_VLAN_PCP:
1866 case OFPACT_STRIP_VLAN:
1867 case OFPACT_PUSH_VLAN:
1868 case OFPACT_SET_ETH_SRC:
1869 case OFPACT_SET_ETH_DST:
1870 case OFPACT_SET_IPV4_SRC:
1871 case OFPACT_SET_IPV4_DST:
1872 case OFPACT_SET_IPV4_DSCP:
1873 case OFPACT_SET_L4_SRC_PORT:
1874 case OFPACT_SET_L4_DST_PORT:
1875 case OFPACT_WRITE_ACTIONS:
1876 case OFPACT_CLEAR_ACTIONS:
1877 case OFPACT_GOTO_TABLE:
1883 /* Converting ofpacts to OpenFlow 1.0. */
1886 ofpact_output_to_openflow10(const struct ofpact_output *output,
1889 struct ofp10_action_output *oao;
1891 oao = ofputil_put_OFPAT10_OUTPUT(out);
1892 oao->port = htons(ofp_to_u16(output->port));
1893 oao->max_len = htons(output->max_len);
1897 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1900 struct ofp10_action_enqueue *oae;
1902 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1903 oae->port = htons(ofp_to_u16(enqueue->port));
1904 oae->queue_id = htonl(enqueue->queue);
1908 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1912 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1915 case OFPACT_ENQUEUE:
1916 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1919 case OFPACT_SET_VLAN_VID:
1920 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1921 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1924 case OFPACT_SET_VLAN_PCP:
1925 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1926 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1929 case OFPACT_STRIP_VLAN:
1930 ofputil_put_OFPAT10_STRIP_VLAN(out);
1933 case OFPACT_SET_ETH_SRC:
1934 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1935 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1938 case OFPACT_SET_ETH_DST:
1939 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1940 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1943 case OFPACT_SET_IPV4_SRC:
1944 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1945 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1948 case OFPACT_SET_IPV4_DST:
1949 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1950 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1953 case OFPACT_SET_IPV4_DSCP:
1954 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1955 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1958 case OFPACT_SET_L4_SRC_PORT:
1959 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1960 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1963 case OFPACT_SET_L4_DST_PORT:
1964 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1965 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1968 case OFPACT_PUSH_VLAN:
1969 case OFPACT_CLEAR_ACTIONS:
1970 case OFPACT_WRITE_ACTIONS:
1971 case OFPACT_GOTO_TABLE:
1979 case OFPACT_CONTROLLER:
1980 case OFPACT_OUTPUT_REG:
1982 case OFPACT_REG_MOVE:
1983 case OFPACT_REG_LOAD:
1984 case OFPACT_STACK_PUSH:
1985 case OFPACT_STACK_POP:
1986 case OFPACT_DEC_TTL:
1987 case OFPACT_SET_MPLS_TTL:
1988 case OFPACT_DEC_MPLS_TTL:
1989 case OFPACT_SET_TUNNEL:
1990 case OFPACT_WRITE_METADATA:
1991 case OFPACT_SET_QUEUE:
1992 case OFPACT_POP_QUEUE:
1993 case OFPACT_FIN_TIMEOUT:
1994 case OFPACT_RESUBMIT:
1996 case OFPACT_MULTIPATH:
1999 case OFPACT_PUSH_MPLS:
2000 case OFPACT_POP_MPLS:
2002 ofpact_to_nxast(a, out);
2007 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
2008 * actions in 'openflow', appending the actions to any existing data in
2011 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
2012 struct ofpbuf *openflow)
2014 const struct ofpact *a;
2016 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2017 ofpact_to_openflow10(a, openflow);
2021 /* Converting ofpacts to OpenFlow 1.1. */
2024 ofpact_output_to_openflow11(const struct ofpact_output *output,
2027 struct ofp11_action_output *oao;
2029 oao = ofputil_put_OFPAT11_OUTPUT(out);
2030 oao->port = ofputil_port_to_ofp11(output->port);
2031 oao->max_len = htons(output->max_len);
2035 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2038 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2039 && (!dec_ttl->ofpact.compat ||
2040 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2041 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2043 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2048 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2052 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2054 case OFPACT_ENQUEUE:
2058 case OFPACT_SET_VLAN_VID:
2059 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2060 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2063 case OFPACT_SET_VLAN_PCP:
2064 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2065 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2068 case OFPACT_STRIP_VLAN:
2069 ofputil_put_OFPAT11_POP_VLAN(out);
2072 case OFPACT_PUSH_VLAN:
2073 /* XXX ETH_TYPE_VLAN_8021AD case */
2074 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2075 htons(ETH_TYPE_VLAN_8021Q);
2078 case OFPACT_SET_QUEUE:
2079 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2080 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2083 case OFPACT_SET_ETH_SRC:
2084 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2085 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2088 case OFPACT_SET_ETH_DST:
2089 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2090 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2093 case OFPACT_SET_IPV4_SRC:
2094 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2095 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2098 case OFPACT_SET_IPV4_DST:
2099 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2100 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2103 case OFPACT_SET_IPV4_DSCP:
2104 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2105 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
2108 case OFPACT_SET_L4_SRC_PORT:
2109 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2110 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2113 case OFPACT_SET_L4_DST_PORT:
2114 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2115 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2118 case OFPACT_DEC_TTL:
2119 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2122 case OFPACT_SET_MPLS_TTL:
2123 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2124 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2127 case OFPACT_DEC_MPLS_TTL:
2128 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2131 case OFPACT_WRITE_METADATA:
2132 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2135 case OFPACT_PUSH_MPLS:
2136 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2137 ofpact_get_PUSH_MPLS(a)->ethertype;
2140 case OFPACT_POP_MPLS:
2141 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2142 ofpact_get_POP_MPLS(a)->ethertype;
2146 case OFPACT_CLEAR_ACTIONS:
2147 case OFPACT_WRITE_ACTIONS:
2148 case OFPACT_GOTO_TABLE:
2153 ofputil_put_OFPAT11_GROUP(out)->group_id =
2154 htonl(ofpact_get_GROUP(a)->group_id);
2157 case OFPACT_CONTROLLER:
2158 case OFPACT_OUTPUT_REG:
2160 case OFPACT_REG_MOVE:
2161 case OFPACT_REG_LOAD:
2162 case OFPACT_STACK_PUSH:
2163 case OFPACT_STACK_POP:
2164 case OFPACT_SET_TUNNEL:
2165 case OFPACT_POP_QUEUE:
2166 case OFPACT_FIN_TIMEOUT:
2167 case OFPACT_RESUBMIT:
2169 case OFPACT_MULTIPATH:
2173 ofpact_to_nxast(a, out);
2178 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
2179 * 1.1 actions in 'openflow', appending the actions to any existing data in
2182 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
2183 size_t ofpacts_len, struct ofpbuf *openflow)
2185 const struct ofpact *a;
2186 size_t start_size = openflow->size;
2188 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2189 ofpact_to_openflow11(a, openflow);
2192 return openflow->size - start_size;
2196 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2198 struct ofp11_instruction_actions *oia;
2200 /* Update the instruction's length (or, if it's empty, delete it). */
2201 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2202 if (openflow->size > ofs + sizeof *oia) {
2203 oia->len = htons(openflow->size - ofs);
2205 openflow->size = ofs;
2210 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
2212 struct ofpbuf *openflow)
2214 const struct ofpact *a;
2216 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2217 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2218 case OVSINST_OFPIT11_CLEAR_ACTIONS:
2219 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2222 case OVSINST_OFPIT11_GOTO_TABLE: {
2223 struct ofp11_instruction_goto_table *oigt;
2224 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2225 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2226 memset(oigt->pad, 0, sizeof oigt->pad);
2230 case OVSINST_OFPIT11_WRITE_METADATA: {
2231 const struct ofpact_metadata *om;
2232 struct ofp11_instruction_write_metadata *oiwm;
2234 om = ofpact_get_WRITE_METADATA(a);
2235 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2236 oiwm->metadata = om->metadata;
2237 oiwm->metadata_mask = om->mask;
2241 case OVSINST_OFPIT13_METER: {
2242 const struct ofpact_meter *om;
2243 struct ofp13_instruction_meter *oim;
2245 om = ofpact_get_METER(a);
2246 oim = instruction_put_OFPIT13_METER(openflow);
2247 oim->meter_id = htonl(om->meter_id);
2251 case OVSINST_OFPIT11_APPLY_ACTIONS: {
2252 const size_t ofs = openflow->size;
2253 const size_t ofpacts_len_left =
2254 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2255 const struct ofpact *action;
2256 const struct ofpact *processed = a;
2258 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2259 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
2260 if (ovs_instruction_type_from_ofpact_type(action->type)
2261 != OVSINST_OFPIT11_APPLY_ACTIONS) {
2264 ofpact_to_openflow11(action, openflow);
2267 ofpacts_update_instruction_actions(openflow, ofs);
2272 case OVSINST_OFPIT11_WRITE_ACTIONS: {
2273 const size_t ofs = openflow->size;
2274 const struct ofpact_nest *on;
2276 on = ofpact_get_WRITE_ACTIONS(a);
2277 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2278 ofpacts_put_openflow11_actions(on->actions,
2279 ofpact_nest_get_action_len(on),
2281 ofpacts_update_instruction_actions(openflow, ofs);
2289 /* Returns true if 'action' outputs to 'port', false otherwise. */
2291 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
2293 switch (ofpact->type) {
2295 return ofpact_get_OUTPUT(ofpact)->port == port;
2296 case OFPACT_ENQUEUE:
2297 return ofpact_get_ENQUEUE(ofpact)->port == port;
2298 case OFPACT_CONTROLLER:
2299 return port == OFPP_CONTROLLER;
2301 case OFPACT_OUTPUT_REG:
2303 case OFPACT_SET_VLAN_VID:
2304 case OFPACT_SET_VLAN_PCP:
2305 case OFPACT_STRIP_VLAN:
2306 case OFPACT_PUSH_VLAN:
2307 case OFPACT_SET_ETH_SRC:
2308 case OFPACT_SET_ETH_DST:
2309 case OFPACT_SET_IPV4_SRC:
2310 case OFPACT_SET_IPV4_DST:
2311 case OFPACT_SET_IPV4_DSCP:
2312 case OFPACT_SET_L4_SRC_PORT:
2313 case OFPACT_SET_L4_DST_PORT:
2314 case OFPACT_REG_MOVE:
2315 case OFPACT_REG_LOAD:
2316 case OFPACT_STACK_PUSH:
2317 case OFPACT_STACK_POP:
2318 case OFPACT_DEC_TTL:
2319 case OFPACT_SET_MPLS_TTL:
2320 case OFPACT_DEC_MPLS_TTL:
2321 case OFPACT_SET_TUNNEL:
2322 case OFPACT_WRITE_METADATA:
2323 case OFPACT_SET_QUEUE:
2324 case OFPACT_POP_QUEUE:
2325 case OFPACT_FIN_TIMEOUT:
2326 case OFPACT_RESUBMIT:
2328 case OFPACT_MULTIPATH:
2331 case OFPACT_PUSH_MPLS:
2332 case OFPACT_POP_MPLS:
2334 case OFPACT_CLEAR_ACTIONS:
2335 case OFPACT_WRITE_ACTIONS:
2336 case OFPACT_GOTO_TABLE:
2344 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2345 * to 'port', false otherwise. */
2347 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
2350 const struct ofpact *a;
2352 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2353 if (ofpact_outputs_to_port(a, port)) {
2361 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2362 * to 'group', false otherwise. */
2364 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
2367 const struct ofpact *a;
2369 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2370 if (a->type == OFPACT_GROUP
2371 && ofpact_get_GROUP(a)->group_id == group_id) {
2380 ofpacts_equal(const struct ofpact *a, size_t a_len,
2381 const struct ofpact *b, size_t b_len)
2383 return a_len == b_len && !memcmp(a, b, a_len);
2386 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2387 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2389 * This function relies on the order of 'ofpacts' being correct (as checked by
2390 * ofpacts_verify()). */
2392 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2394 const struct ofpact *a;
2396 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2397 enum ovs_instruction_type inst;
2399 inst = ovs_instruction_type_from_ofpact_type(a->type);
2400 if (a->type == OFPACT_METER) {
2401 return ofpact_get_METER(a)->meter_id;
2402 } else if (inst > OVSINST_OFPIT13_METER) {
2410 /* Formatting ofpacts. */
2413 print_note(const struct ofpact_note *note, struct ds *string)
2417 ds_put_cstr(string, "note:");
2418 for (i = 0; i < note->length; i++) {
2420 ds_put_char(string, '.');
2422 ds_put_format(string, "%02"PRIx8, note->data[i]);
2427 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2432 ds_put_cstr(s, "dec_ttl");
2433 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2434 ds_put_cstr(s, "(");
2435 for (i = 0; i < ids->n_controllers; i++) {
2437 ds_put_cstr(s, ",");
2439 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2441 ds_put_cstr(s, ")");
2446 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2449 ds_put_cstr(s, "fin_timeout(");
2450 if (fin_timeout->fin_idle_timeout) {
2451 ds_put_format(s, "idle_timeout=%"PRIu16",",
2452 fin_timeout->fin_idle_timeout);
2454 if (fin_timeout->fin_hard_timeout) {
2455 ds_put_format(s, "hard_timeout=%"PRIu16",",
2456 fin_timeout->fin_hard_timeout);
2459 ds_put_char(s, ')');
2463 ofpact_format(const struct ofpact *a, struct ds *s)
2465 const struct ofpact_enqueue *enqueue;
2466 const struct ofpact_resubmit *resubmit;
2467 const struct ofpact_controller *controller;
2468 const struct ofpact_metadata *metadata;
2469 const struct ofpact_tunnel *tunnel;
2470 const struct ofpact_sample *sample;
2475 port = ofpact_get_OUTPUT(a)->port;
2476 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
2477 ds_put_format(s, "output:%"PRIu16, port);
2479 ofputil_format_port(port, s);
2480 if (port == OFPP_CONTROLLER) {
2481 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2486 case OFPACT_CONTROLLER:
2487 controller = ofpact_get_CONTROLLER(a);
2488 if (controller->reason == OFPR_ACTION &&
2489 controller->controller_id == 0) {
2490 ds_put_format(s, "CONTROLLER:%"PRIu16,
2491 ofpact_get_CONTROLLER(a)->max_len);
2493 enum ofp_packet_in_reason reason = controller->reason;
2495 ds_put_cstr(s, "controller(");
2496 if (reason != OFPR_ACTION) {
2497 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2499 ds_put_format(s, "reason=%s,",
2500 ofputil_packet_in_reason_to_string(
2501 reason, reasonbuf, sizeof reasonbuf));
2503 if (controller->max_len != UINT16_MAX) {
2504 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2506 if (controller->controller_id != 0) {
2507 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2510 ds_put_char(s, ')');
2514 case OFPACT_ENQUEUE:
2515 enqueue = ofpact_get_ENQUEUE(a);
2516 ds_put_format(s, "enqueue:");
2517 ofputil_format_port(enqueue->port, s);
2518 ds_put_format(s, "q%"PRIu32, enqueue->queue);
2521 case OFPACT_OUTPUT_REG:
2522 ds_put_cstr(s, "output:");
2523 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2527 bundle_format(ofpact_get_BUNDLE(a), s);
2530 case OFPACT_SET_VLAN_VID:
2531 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2532 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2535 case OFPACT_SET_VLAN_PCP:
2536 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2537 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2540 case OFPACT_STRIP_VLAN:
2541 ds_put_cstr(s, "strip_vlan");
2544 case OFPACT_PUSH_VLAN:
2545 /* XXX 802.1AD case*/
2546 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2549 case OFPACT_SET_ETH_SRC:
2550 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2551 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2554 case OFPACT_SET_ETH_DST:
2555 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2556 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2559 case OFPACT_SET_IPV4_SRC:
2560 ds_put_format(s, "mod_nw_src:"IP_FMT,
2561 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2564 case OFPACT_SET_IPV4_DST:
2565 ds_put_format(s, "mod_nw_dst:"IP_FMT,
2566 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2569 case OFPACT_SET_IPV4_DSCP:
2570 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
2573 case OFPACT_SET_L4_SRC_PORT:
2574 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2577 case OFPACT_SET_L4_DST_PORT:
2578 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2581 case OFPACT_REG_MOVE:
2582 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2585 case OFPACT_REG_LOAD:
2586 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2589 case OFPACT_STACK_PUSH:
2590 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2593 case OFPACT_STACK_POP:
2594 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2597 case OFPACT_DEC_TTL:
2598 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2601 case OFPACT_SET_MPLS_TTL:
2602 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2603 ofpact_get_SET_MPLS_TTL(a)->ttl);
2606 case OFPACT_DEC_MPLS_TTL:
2607 ds_put_cstr(s, "dec_mpls_ttl");
2610 case OFPACT_SET_TUNNEL:
2611 tunnel = ofpact_get_SET_TUNNEL(a);
2612 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2613 (tunnel->tun_id > UINT32_MAX
2614 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2618 case OFPACT_SET_QUEUE:
2619 ds_put_format(s, "set_queue:%"PRIu32,
2620 ofpact_get_SET_QUEUE(a)->queue_id);
2623 case OFPACT_POP_QUEUE:
2624 ds_put_cstr(s, "pop_queue");
2627 case OFPACT_FIN_TIMEOUT:
2628 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2631 case OFPACT_RESUBMIT:
2632 resubmit = ofpact_get_RESUBMIT(a);
2633 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2634 ds_put_cstr(s, "resubmit:");
2635 ofputil_format_port(resubmit->in_port, s);
2637 ds_put_format(s, "resubmit(");
2638 if (resubmit->in_port != OFPP_IN_PORT) {
2639 ofputil_format_port(resubmit->in_port, s);
2641 ds_put_char(s, ',');
2642 if (resubmit->table_id != 255) {
2643 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2645 ds_put_char(s, ')');
2650 learn_format(ofpact_get_LEARN(a), s);
2653 case OFPACT_MULTIPATH:
2654 multipath_format(ofpact_get_MULTIPATH(a), s);
2658 print_note(ofpact_get_NOTE(a), s);
2661 case OFPACT_PUSH_MPLS:
2662 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2663 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2666 case OFPACT_POP_MPLS:
2667 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2668 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2672 ds_put_cstr(s, "exit");
2676 sample = ofpact_get_SAMPLE(a);
2678 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2679 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2680 sample->probability, sample->collector_set_id,
2681 sample->obs_domain_id, sample->obs_point_id);
2684 case OFPACT_WRITE_ACTIONS: {
2685 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2686 ds_put_format(s, "%s(",
2687 ovs_instruction_name_from_type(
2688 OVSINST_OFPIT11_WRITE_ACTIONS));
2689 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
2690 ds_put_char(s, ')');
2694 case OFPACT_CLEAR_ACTIONS:
2695 ds_put_format(s, "%s",
2696 ovs_instruction_name_from_type(
2697 OVSINST_OFPIT11_CLEAR_ACTIONS));
2700 case OFPACT_WRITE_METADATA:
2701 metadata = ofpact_get_WRITE_METADATA(a);
2702 ds_put_format(s, "%s:%#"PRIx64,
2703 ovs_instruction_name_from_type(
2704 OVSINST_OFPIT11_WRITE_METADATA),
2705 ntohll(metadata->metadata));
2706 if (metadata->mask != OVS_BE64_MAX) {
2707 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2711 case OFPACT_GOTO_TABLE:
2712 ds_put_format(s, "%s:%"PRIu8,
2713 ovs_instruction_name_from_type(
2714 OVSINST_OFPIT11_GOTO_TABLE),
2715 ofpact_get_GOTO_TABLE(a)->table_id);
2719 ds_put_format(s, "%s:%"PRIu32,
2720 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
2721 ofpact_get_METER(a)->meter_id);
2725 ds_put_format(s, "group:%"PRIu32,
2726 ofpact_get_GROUP(a)->group_id);
2731 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2732 * 'ofpacts' to 'string'. */
2734 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2738 ds_put_cstr(string, "drop");
2740 const struct ofpact *a;
2742 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2744 ds_put_cstr(string, ",");
2747 /* XXX write-actions */
2748 ofpact_format(a, string);
2753 /* Internal use by helpers. */
2756 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2758 struct ofpact *ofpact;
2760 ofpact_pad(ofpacts);
2761 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2762 ofpact_init(ofpact, type, len);
2767 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2769 memset(ofpact, 0, len);
2770 ofpact->type = type;
2771 ofpact->compat = OFPUTIL_ACTION_INVALID;
2775 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2776 * starting at 'ofpact'.
2778 * This is the correct way to update a variable-length ofpact's length after
2779 * adding the variable-length part of the payload. (See the large comment
2780 * near the end of ofp-actions.h for more information.) */
2782 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2784 ovs_assert(ofpact == ofpacts->l2);
2785 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2788 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2789 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2790 * client must call this itself after adding the final ofpact to an array of
2793 * (The consequences of failing to call this function are probably not dire.
2794 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2795 * not dereference it. That's undefined behavior, technically, but it will not
2796 * cause a real problem on common systems. Still, it seems better to call
2799 ofpact_pad(struct ofpbuf *ofpacts)
2801 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2803 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2808 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2811 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2812 load->dst.field = mf;
2814 load->dst.n_bits = mf->n_bits;
2815 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2816 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);