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 = 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 = ntohs(oae->port);
59 enqueue->queue = ntohl(oae->queue_id);
60 if (enqueue->port >= OFPP_MAX && enqueue->port != OFPP_IN_PORT
61 && enqueue->port != OFPP_LOCAL) {
62 return OFPERR_OFPBAC_BAD_OUT_PORT;
68 resubmit_from_openflow(const struct nx_action_resubmit *nar,
71 struct ofpact_resubmit *resubmit;
73 resubmit = ofpact_put_RESUBMIT(out);
74 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
75 resubmit->in_port = ntohs(nar->in_port);
76 resubmit->table_id = 0xff;
80 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
83 struct ofpact_resubmit *resubmit;
85 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
86 return OFPERR_OFPBAC_BAD_ARGUMENT;
89 resubmit = ofpact_put_RESUBMIT(out);
90 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
91 resubmit->in_port = ntohs(nar->in_port);
92 resubmit->table_id = nar->table;
97 output_reg_from_openflow(const struct nx_action_output_reg *naor,
100 struct ofpact_output_reg *output_reg;
102 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
103 return OFPERR_OFPBAC_BAD_ARGUMENT;
106 output_reg = ofpact_put_OUTPUT_REG(out);
107 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
108 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
109 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
110 output_reg->max_len = ntohs(naor->max_len);
112 return mf_check_src(&output_reg->src, NULL);
116 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
119 struct ofpact_fin_timeout *oft;
121 oft = ofpact_put_FIN_TIMEOUT(out);
122 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
123 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
127 controller_from_openflow(const struct nx_action_controller *nac,
130 struct ofpact_controller *oc;
132 oc = ofpact_put_CONTROLLER(out);
133 oc->max_len = ntohs(nac->max_len);
134 oc->controller_id = ntohs(nac->controller_id);
135 oc->reason = nac->reason;
139 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
142 struct ofpact_metadata *om;
144 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
145 return OFPERR_NXBRC_MUST_BE_ZERO;
148 om = ofpact_put_WRITE_METADATA(out);
149 om->metadata = nawm->metadata;
150 om->mask = nawm->mask;
156 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
158 struct ofpact_note *note;
161 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
162 note = ofpact_put(out, OFPACT_NOTE,
163 offsetof(struct ofpact_note, data) + length);
164 note->length = length;
165 memcpy(note->data, nan->note, length);
169 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
172 struct ofpact_cnt_ids *ids;
173 enum ofperr error = 0;
175 ids = ofpact_put_DEC_TTL(out);
176 ids->ofpact.compat = compat;
177 ids->n_controllers = 1;
178 ofpbuf_put(out, &id, sizeof id);
180 ofpact_update_len(out, &ids->ofpact);
185 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
188 struct ofpact_cnt_ids *ids;
192 ids = ofpact_put_DEC_TTL(out);
193 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
194 ids->n_controllers = ntohs(nac_ids->n_controllers);
195 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
197 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
198 return OFPERR_NXBRC_MUST_BE_ZERO;
201 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
202 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
203 "allocated for controller ids. %zu bytes are required for "
204 "%"PRIu16" controllers.", ids_size,
205 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
206 return OFPERR_OFPBAC_BAD_LEN;
209 for (i = 0; i < ids->n_controllers; i++) {
210 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
211 ofpbuf_put(out, &id, sizeof id);
215 ofpact_update_len(out, &ids->ofpact);
221 sample_from_openflow(const struct nx_action_sample *nas,
224 struct ofpact_sample *sample;
226 sample = ofpact_put_SAMPLE(out);
227 sample->probability = ntohs(nas->probability);
228 sample->collector_set_id = ntohl(nas->collector_set_id);
229 sample->obs_domain_id = ntohl(nas->obs_domain_id);
230 sample->obs_point_id = ntohl(nas->obs_point_id);
232 if (sample->probability == 0) {
233 return OFPERR_OFPBAC_BAD_ARGUMENT;
240 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
242 const struct nx_action_header *nah = (const struct nx_action_header *) a;
243 uint16_t len = ntohs(a->header.len);
245 if (len < sizeof(struct nx_action_header)) {
246 return OFPERR_OFPBAC_BAD_LEN;
247 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
248 return OFPERR_OFPBAC_BAD_VENDOR;
251 switch (nah->subtype) {
252 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
253 case CONSTANT_HTONS(ENUM): \
255 ? len >= sizeof(struct STRUCT) \
256 : len == sizeof(struct STRUCT)) { \
257 *code = OFPUTIL_##ENUM; \
260 return OFPERR_OFPBAC_BAD_LEN; \
263 #include "ofp-util.def"
265 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
266 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
268 return OFPERR_OFPBAC_BAD_TYPE;
272 /* Parses 'a' to determine its type. On success stores the correct type into
273 * '*code' and returns 0. On failure returns an OFPERR_* error code and
274 * '*code' is indeterminate.
276 * The caller must have already verified that 'a''s length is potentially
277 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
278 * ofp_action) and no longer than the amount of space allocated to 'a').
280 * This function verifies that 'a''s length is correct for the type of action
281 * that it represents. */
283 decode_openflow10_action(const union ofp_action *a,
284 enum ofputil_action_code *code)
287 case CONSTANT_HTONS(OFPAT10_VENDOR):
288 return decode_nxast_action(a, code);
290 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
291 case CONSTANT_HTONS(ENUM): \
292 if (a->header.len == htons(sizeof(struct STRUCT))) { \
293 *code = OFPUTIL_##ENUM; \
296 return OFPERR_OFPBAC_BAD_LEN; \
299 #include "ofp-util.def"
302 return OFPERR_OFPBAC_BAD_TYPE;
307 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
310 const struct nx_action_resubmit *nar;
311 const struct nx_action_set_tunnel *nast;
312 const struct nx_action_set_queue *nasq;
313 const struct nx_action_note *nan;
314 const struct nx_action_set_tunnel64 *nast64;
315 const struct nx_action_write_metadata *nawm;
316 struct ofpact_tunnel *tunnel;
317 enum ofperr error = 0;
320 case OFPUTIL_ACTION_INVALID:
321 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
322 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
323 #include "ofp-util.def"
326 case OFPUTIL_NXAST_RESUBMIT:
327 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
330 case OFPUTIL_NXAST_SET_TUNNEL:
331 nast = (const struct nx_action_set_tunnel *) a;
332 tunnel = ofpact_put_SET_TUNNEL(out);
333 tunnel->ofpact.compat = code;
334 tunnel->tun_id = ntohl(nast->tun_id);
337 case OFPUTIL_NXAST_WRITE_METADATA:
338 nawm = (const struct nx_action_write_metadata *) a;
339 error = metadata_from_nxast(nawm, out);
342 case OFPUTIL_NXAST_SET_QUEUE:
343 nasq = (const struct nx_action_set_queue *) a;
344 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
347 case OFPUTIL_NXAST_POP_QUEUE:
348 ofpact_put_POP_QUEUE(out);
351 case OFPUTIL_NXAST_REG_MOVE:
352 error = nxm_reg_move_from_openflow(
353 (const struct nx_action_reg_move *) a, out);
356 case OFPUTIL_NXAST_REG_LOAD:
357 error = nxm_reg_load_from_openflow(
358 (const struct nx_action_reg_load *) a, out);
361 case OFPUTIL_NXAST_STACK_PUSH:
362 error = nxm_stack_push_from_openflow(
363 (const struct nx_action_stack *) a, out);
366 case OFPUTIL_NXAST_STACK_POP:
367 error = nxm_stack_pop_from_openflow(
368 (const struct nx_action_stack *) a, out);
371 case OFPUTIL_NXAST_NOTE:
372 nan = (const struct nx_action_note *) a;
373 note_from_openflow(nan, out);
376 case OFPUTIL_NXAST_SET_TUNNEL64:
377 nast64 = (const struct nx_action_set_tunnel64 *) a;
378 tunnel = ofpact_put_SET_TUNNEL(out);
379 tunnel->ofpact.compat = code;
380 tunnel->tun_id = ntohll(nast64->tun_id);
383 case OFPUTIL_NXAST_MULTIPATH:
384 error = multipath_from_openflow((const struct nx_action_multipath *) a,
385 ofpact_put_MULTIPATH(out));
388 case OFPUTIL_NXAST_BUNDLE:
389 case OFPUTIL_NXAST_BUNDLE_LOAD:
390 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
393 case OFPUTIL_NXAST_OUTPUT_REG:
394 error = output_reg_from_openflow(
395 (const struct nx_action_output_reg *) a, out);
398 case OFPUTIL_NXAST_RESUBMIT_TABLE:
399 nar = (const struct nx_action_resubmit *) a;
400 error = resubmit_table_from_openflow(nar, out);
403 case OFPUTIL_NXAST_LEARN:
404 error = learn_from_openflow((const struct nx_action_learn *) a, out);
407 case OFPUTIL_NXAST_EXIT:
408 ofpact_put_EXIT(out);
411 case OFPUTIL_NXAST_DEC_TTL:
412 error = dec_ttl_from_openflow(out, code);
415 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
416 error = dec_ttl_cnt_ids_from_openflow(
417 (const struct nx_action_cnt_ids *) a, out);
420 case OFPUTIL_NXAST_FIN_TIMEOUT:
421 fin_timeout_from_openflow(
422 (const struct nx_action_fin_timeout *) a, out);
425 case OFPUTIL_NXAST_CONTROLLER:
426 controller_from_openflow((const struct nx_action_controller *) a, out);
429 case OFPUTIL_NXAST_PUSH_MPLS: {
430 struct nx_action_push_mpls *nxapm = (struct nx_action_push_mpls *)a;
431 if (!eth_type_mpls(nxapm->ethertype)) {
432 return OFPERR_OFPBAC_BAD_ARGUMENT;
434 ofpact_put_PUSH_MPLS(out)->ethertype = nxapm->ethertype;
438 case OFPUTIL_NXAST_SET_MPLS_TTL: {
439 struct nx_action_mpls_ttl *nxamt = (struct nx_action_mpls_ttl *)a;
440 ofpact_put_SET_MPLS_TTL(out)->ttl = nxamt->ttl;
444 case OFPUTIL_NXAST_DEC_MPLS_TTL:
445 ofpact_put_DEC_MPLS_TTL(out);
448 case OFPUTIL_NXAST_POP_MPLS: {
449 struct nx_action_pop_mpls *nxapm = (struct nx_action_pop_mpls *)a;
450 if (eth_type_mpls(nxapm->ethertype)) {
451 return OFPERR_OFPBAC_BAD_ARGUMENT;
453 ofpact_put_POP_MPLS(out)->ethertype = nxapm->ethertype;
457 case OFPUTIL_NXAST_SAMPLE:
458 error = sample_from_openflow(
459 (const struct nx_action_sample *) a, out);
467 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
469 enum ofputil_action_code code;
472 error = decode_openflow10_action(a, &code);
478 case OFPUTIL_ACTION_INVALID:
479 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
480 #include "ofp-util.def"
483 case OFPUTIL_OFPAT10_OUTPUT:
484 return output_from_openflow10(&a->output10, out);
486 case OFPUTIL_OFPAT10_SET_VLAN_VID:
487 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
488 return OFPERR_OFPBAC_BAD_ARGUMENT;
490 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
493 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
494 if (a->vlan_pcp.vlan_pcp & ~7) {
495 return OFPERR_OFPBAC_BAD_ARGUMENT;
497 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
500 case OFPUTIL_OFPAT10_STRIP_VLAN:
501 ofpact_put_STRIP_VLAN(out);
504 case OFPUTIL_OFPAT10_SET_DL_SRC:
505 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
506 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
509 case OFPUTIL_OFPAT10_SET_DL_DST:
510 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
511 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
514 case OFPUTIL_OFPAT10_SET_NW_SRC:
515 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
518 case OFPUTIL_OFPAT10_SET_NW_DST:
519 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
522 case OFPUTIL_OFPAT10_SET_NW_TOS:
523 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
524 return OFPERR_OFPBAC_BAD_ARGUMENT;
526 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
529 case OFPUTIL_OFPAT10_SET_TP_SRC:
530 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
533 case OFPUTIL_OFPAT10_SET_TP_DST:
534 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
538 case OFPUTIL_OFPAT10_ENQUEUE:
539 error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
543 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
544 #include "ofp-util.def"
545 return ofpact_from_nxast(a, code, out);
551 static inline union ofp_action *
552 action_next(const union ofp_action *a)
554 return ((union ofp_action *) (void *)
555 ((uint8_t *) a + ntohs(a->header.len)));
559 action_is_valid(const union ofp_action *a, size_t n_actions)
561 uint16_t len = ntohs(a->header.len);
562 return (!(len % OFP_ACTION_ALIGN)
564 && len / sizeof *a <= n_actions);
567 /* This macro is careful to check for actions with bad lengths. */
568 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
569 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
570 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
571 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
572 (ITER) = action_next(ITER)))
575 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
578 if (!VLOG_DROP_WARN(&rl)) {
582 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
583 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
584 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
590 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
592 enum ofperr (*ofpact_from_openflow)(
593 const union ofp_action *a, struct ofpbuf *out))
595 const union ofp_action *a;
598 ACTION_FOR_EACH (a, left, in, n_in) {
599 enum ofperr error = ofpact_from_openflow(a, out);
601 log_bad_action(in, n_in, a - in, error);
606 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
607 log_bad_action(in, n_in, n_in - left, error);
616 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
619 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
623 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
624 struct ofpbuf *ofpacts,
625 enum ofperr (*translate)(const union ofp_action *actions,
627 struct ofpbuf *ofpacts))
629 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
630 const union ofp_action *actions;
633 ofpbuf_clear(ofpacts);
635 if (actions_len % OFP_ACTION_ALIGN != 0) {
636 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
637 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
638 return OFPERR_OFPBRC_BAD_LEN;
641 actions = ofpbuf_try_pull(openflow, actions_len);
642 if (actions == NULL) {
643 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
644 "remaining message length (%zu)",
645 actions_len, openflow->size);
646 return OFPERR_OFPBRC_BAD_LEN;
649 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
651 ofpbuf_clear(ofpacts);
655 error = ofpacts_verify(ofpacts->data, ofpacts->size);
657 ofpbuf_clear(ofpacts);
662 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
663 * front of 'openflow' into ofpacts. On success, replaces any existing content
664 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
665 * Returns 0 if successful, otherwise an OpenFlow error.
667 * The parsed actions are valid generically, but they may not be valid in a
668 * specific context. For example, port numbers up to OFPP_MAX are valid
669 * generically, but specific datapaths may only support port numbers in a
670 * smaller range. Use ofpacts_check() to additional check whether actions are
671 * valid in a specific context. */
673 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
674 struct ofpbuf *ofpacts)
676 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
677 ofpacts_from_openflow10);
680 /* OpenFlow 1.1 actions. */
682 /* Parses 'a' to determine its type. On success stores the correct type into
683 * '*code' and returns 0. On failure returns an OFPERR_* error code and
684 * '*code' is indeterminate.
686 * The caller must have already verified that 'a''s length is potentially
687 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
688 * ofp_action) and no longer than the amount of space allocated to 'a').
690 * This function verifies that 'a''s length is correct for the type of action
691 * that it represents. */
693 decode_openflow11_action(const union ofp_action *a,
694 enum ofputil_action_code *code)
699 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
700 return decode_nxast_action(a, code);
702 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
703 case CONSTANT_HTONS(ENUM): \
704 len = ntohs(a->header.len); \
706 ? len >= sizeof(struct STRUCT) \
707 : len == sizeof(struct STRUCT)) { \
708 *code = OFPUTIL_##ENUM; \
711 return OFPERR_OFPBAC_BAD_LEN; \
714 #include "ofp-util.def"
717 return OFPERR_OFPBAC_BAD_TYPE;
722 output_from_openflow11(const struct ofp11_action_output *oao,
725 struct ofpact_output *output;
728 output = ofpact_put_OUTPUT(out);
729 output->max_len = ntohs(oao->max_len);
731 error = ofputil_port_from_ofp11(oao->port, &output->port);
736 return ofputil_check_output_port(output->port, OFPP_MAX);
740 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
742 enum ofputil_action_code code;
745 error = decode_openflow11_action(a, &code);
751 case OFPUTIL_ACTION_INVALID:
752 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
753 #include "ofp-util.def"
756 case OFPUTIL_OFPAT11_OUTPUT:
757 return output_from_openflow11((const struct ofp11_action_output *) a,
760 case OFPUTIL_OFPAT11_SET_VLAN_VID:
761 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
762 return OFPERR_OFPBAC_BAD_ARGUMENT;
764 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
767 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
768 if (a->vlan_pcp.vlan_pcp & ~7) {
769 return OFPERR_OFPBAC_BAD_ARGUMENT;
771 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
774 case OFPUTIL_OFPAT11_PUSH_VLAN:
775 if (((const struct ofp11_action_push *)a)->ethertype !=
776 htons(ETH_TYPE_VLAN_8021Q)) {
777 /* XXX 802.1AD(QinQ) isn't supported at the moment */
778 return OFPERR_OFPBAC_BAD_ARGUMENT;
780 ofpact_put_PUSH_VLAN(out);
783 case OFPUTIL_OFPAT11_POP_VLAN:
784 ofpact_put_STRIP_VLAN(out);
787 case OFPUTIL_OFPAT11_SET_QUEUE:
788 ofpact_put_SET_QUEUE(out)->queue_id =
789 ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
792 case OFPUTIL_OFPAT11_SET_DL_SRC:
793 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
794 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
797 case OFPUTIL_OFPAT11_SET_DL_DST:
798 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
799 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
802 case OFPUTIL_OFPAT11_DEC_NW_TTL:
803 dec_ttl_from_openflow(out, code);
806 case OFPUTIL_OFPAT11_SET_NW_SRC:
807 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
810 case OFPUTIL_OFPAT11_SET_NW_DST:
811 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
814 case OFPUTIL_OFPAT11_SET_NW_TOS:
815 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
816 return OFPERR_OFPBAC_BAD_ARGUMENT;
818 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
821 case OFPUTIL_OFPAT11_SET_TP_SRC:
822 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
825 case OFPUTIL_OFPAT11_SET_TP_DST:
826 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
829 case OFPUTIL_OFPAT12_SET_FIELD:
830 return nxm_reg_load_from_openflow12_set_field(
831 (const struct ofp12_action_set_field *)a, out);
833 case OFPUTIL_OFPAT11_SET_MPLS_TTL: {
834 struct ofp11_action_mpls_ttl *oamt = (struct ofp11_action_mpls_ttl *)a;
835 ofpact_put_SET_MPLS_TTL(out)->ttl = oamt->mpls_ttl;
839 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
840 ofpact_put_DEC_MPLS_TTL(out);
843 case OFPUTIL_OFPAT11_PUSH_MPLS: {
844 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
845 if (!eth_type_mpls(oap->ethertype)) {
846 return OFPERR_OFPBAC_BAD_ARGUMENT;
848 ofpact_put_PUSH_MPLS(out)->ethertype = oap->ethertype;
852 case OFPUTIL_OFPAT11_POP_MPLS: {
853 struct ofp11_action_pop_mpls *oapm = (struct ofp11_action_pop_mpls *)a;
854 if (eth_type_mpls(oapm->ethertype)) {
855 return OFPERR_OFPBAC_BAD_ARGUMENT;
857 ofpact_put_POP_MPLS(out)->ethertype = oapm->ethertype;
861 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
862 #include "ofp-util.def"
863 return ofpact_from_nxast(a, code, out);
870 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
873 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
876 /* OpenFlow 1.1 instructions. */
878 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
879 static inline const struct STRUCT * \
880 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
882 ovs_assert(inst->type == htons(ENUM)); \
883 return (struct STRUCT *)inst; \
887 instruction_init_##ENUM(struct STRUCT *s) \
889 memset(s, 0, sizeof *s); \
890 s->type = htons(ENUM); \
891 s->len = htons(sizeof *s); \
894 static inline struct STRUCT * \
895 instruction_put_##ENUM(struct ofpbuf *buf) \
897 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
898 instruction_init_##ENUM(s); \
904 struct instruction_type_info {
905 enum ovs_instruction_type type;
909 static const struct instruction_type_info inst_info[] = {
910 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
916 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
918 return inst_info[type].name;
922 ofpact_instruction_type_from_name(const char *name)
924 const struct instruction_type_info *p;
925 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
926 if (!strcasecmp(name, p->name)) {
933 static inline struct ofp11_instruction *
934 instruction_next(const struct ofp11_instruction *inst)
936 return ((struct ofp11_instruction *) (void *)
937 ((uint8_t *) inst + ntohs(inst->len)));
941 instruction_is_valid(const struct ofp11_instruction *inst,
942 size_t n_instructions)
944 uint16_t len = ntohs(inst->len);
945 return (!(len % OFP11_INSTRUCTION_ALIGN)
946 && len >= sizeof *inst
947 && len / sizeof *inst <= n_instructions);
950 /* This macro is careful to check for instructions with bad lengths. */
951 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
952 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
953 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
954 ((LEFT) -= (ntohs((ITER)->len) \
955 / sizeof(struct ofp11_instruction)), \
956 (ITER) = instruction_next(ITER)))
959 decode_openflow11_instruction(const struct ofp11_instruction *inst,
960 enum ovs_instruction_type *type)
962 uint16_t len = ntohs(inst->len);
964 switch (inst->type) {
965 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
966 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
968 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
969 case CONSTANT_HTONS(ENUM): \
971 ? len >= sizeof(struct STRUCT) \
972 : len == sizeof(struct STRUCT)) { \
973 *type = OVSINST_##ENUM; \
976 return OFPERR_OFPBIC_BAD_LEN; \
982 return OFPERR_OFPBIC_UNKNOWN_INST;
987 decode_openflow11_instructions(const struct ofp11_instruction insts[],
989 const struct ofp11_instruction *out[])
991 const struct ofp11_instruction *inst;
994 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
995 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
996 enum ovs_instruction_type type;
999 error = decode_openflow11_instruction(inst, &type);
1005 return OFPERR_OFPBAC_UNSUPPORTED_ORDER; /* No specific code for
1006 * a duplicate instruction
1013 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1014 (n_insts - left) * sizeof *inst);
1015 return OFPERR_OFPBIC_BAD_LEN;
1021 get_actions_from_instruction(const struct ofp11_instruction *inst,
1022 const union ofp_action **actions,
1025 *actions = (const union ofp_action *) (inst + 1);
1026 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1029 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
1030 * front of 'openflow' into ofpacts. On success, replaces any existing content
1031 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1032 * Returns 0 if successful, otherwise an OpenFlow error.
1034 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1035 * instructions, so you should call ofpacts_pull_openflow11_instructions()
1036 * instead of this function.
1038 * The parsed actions are valid generically, but they may not be valid in a
1039 * specific context. For example, port numbers up to OFPP_MAX are valid
1040 * generically, but specific datapaths may only support port numbers in a
1041 * smaller range. Use ofpacts_check() to additional check whether actions are
1042 * valid in a specific context. */
1044 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1045 unsigned int actions_len,
1046 struct ofpbuf *ofpacts)
1048 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1049 ofpacts_from_openflow11);
1053 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1054 unsigned int instructions_len,
1056 struct ofpbuf *ofpacts)
1058 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1059 const struct ofp11_instruction *instructions;
1060 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1063 ofpbuf_clear(ofpacts);
1065 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1066 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1068 instructions_len, OFP11_INSTRUCTION_ALIGN);
1069 error = OFPERR_OFPBIC_BAD_LEN;
1073 instructions = ofpbuf_try_pull(openflow, instructions_len);
1074 if (instructions == NULL) {
1075 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1076 "remaining message length (%zu)",
1077 instructions_len, openflow->size);
1078 error = OFPERR_OFPBIC_BAD_LEN;
1082 error = decode_openflow11_instructions(
1083 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1089 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1090 const union ofp_action *actions;
1093 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1094 &actions, &n_actions);
1095 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1100 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1101 instruction_get_OFPIT11_CLEAR_ACTIONS(
1102 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1103 ofpact_put_CLEAR_ACTIONS(ofpacts);
1105 /* XXX Write-Actions */
1106 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1107 const struct ofp11_instruction_write_metadata *oiwm;
1108 struct ofpact_metadata *om;
1110 oiwm = (const struct ofp11_instruction_write_metadata *)
1111 insts[OVSINST_OFPIT11_WRITE_METADATA];
1113 om = ofpact_put_WRITE_METADATA(ofpacts);
1114 om->metadata = oiwm->metadata;
1115 om->mask = oiwm->metadata_mask;
1117 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1118 const struct ofp11_instruction_goto_table *oigt;
1119 struct ofpact_goto_table *ogt;
1121 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1122 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1123 if (table_id >= oigt->table_id) {
1124 error = OFPERR_OFPBRC_BAD_TABLE_ID;
1127 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1128 ogt->table_id = oigt->table_id;
1131 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1132 error = OFPERR_OFPBIC_UNSUP_INST;
1136 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1139 ofpbuf_clear(ofpacts);
1145 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports,
1148 const struct ofpact_enqueue *enqueue;
1152 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1155 case OFPACT_CONTROLLER:
1158 case OFPACT_ENQUEUE:
1159 enqueue = ofpact_get_ENQUEUE(a);
1160 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
1161 && enqueue->port != OFPP_LOCAL) {
1162 return OFPERR_OFPBAC_BAD_OUT_PORT;
1166 case OFPACT_OUTPUT_REG:
1167 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1170 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1172 case OFPACT_SET_VLAN_VID:
1173 case OFPACT_SET_VLAN_PCP:
1174 case OFPACT_STRIP_VLAN:
1175 case OFPACT_PUSH_VLAN:
1176 case OFPACT_SET_ETH_SRC:
1177 case OFPACT_SET_ETH_DST:
1178 case OFPACT_SET_IPV4_SRC:
1179 case OFPACT_SET_IPV4_DST:
1180 case OFPACT_SET_IPV4_DSCP:
1181 case OFPACT_SET_L4_SRC_PORT:
1182 case OFPACT_SET_L4_DST_PORT:
1185 case OFPACT_REG_MOVE:
1186 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1188 case OFPACT_REG_LOAD:
1189 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1191 case OFPACT_STACK_PUSH:
1192 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1194 case OFPACT_STACK_POP:
1195 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1197 case OFPACT_DEC_TTL:
1198 case OFPACT_SET_MPLS_TTL:
1199 case OFPACT_DEC_MPLS_TTL:
1200 case OFPACT_SET_TUNNEL:
1201 case OFPACT_SET_QUEUE:
1202 case OFPACT_POP_QUEUE:
1203 case OFPACT_FIN_TIMEOUT:
1204 case OFPACT_RESUBMIT:
1208 return learn_check(ofpact_get_LEARN(a), flow);
1210 case OFPACT_MULTIPATH:
1211 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1217 case OFPACT_PUSH_MPLS:
1218 *dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1221 case OFPACT_POP_MPLS:
1222 *dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1228 case OFPACT_CLEAR_ACTIONS:
1229 case OFPACT_WRITE_METADATA:
1230 case OFPACT_GOTO_TABLE:
1238 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1239 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1240 * switch with no more than 'max_ports' ports. */
1242 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1243 const struct flow *flow, int max_ports)
1245 const struct ofpact *a;
1246 ovs_be16 dl_type = flow->dl_type;
1247 struct flow updated_flow;
1249 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1252 /* If the dl_type was changed by an action then its new value
1253 * should be present in the flow passed to ofpact_check__(). */
1254 if (flow->dl_type != dl_type) {
1255 /* Only copy flow at most once */
1256 if (flow != &updated_flow) {
1257 updated_flow = *flow;
1258 flow = &updated_flow;
1260 updated_flow.dl_type = dl_type;
1263 error = ofpact_check__(a, flow, max_ports, &dl_type);
1272 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1273 * in the appropriate order as defined by the OpenFlow spec. */
1275 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1277 const struct ofpact *a;
1278 enum ovs_instruction_type inst;
1280 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1281 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1282 enum ovs_instruction_type next;
1284 if (a->type == OFPACT_CLEAR_ACTIONS) {
1285 next = OVSINST_OFPIT11_CLEAR_ACTIONS;
1286 } else if (a->type == OFPACT_WRITE_METADATA) {
1287 next = OVSINST_OFPIT11_WRITE_METADATA;
1288 } else if (a->type == OFPACT_GOTO_TABLE) {
1289 next = OVSINST_OFPIT11_GOTO_TABLE;
1291 next = OVSINST_OFPIT11_APPLY_ACTIONS;
1294 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS && next <= inst) {
1295 const char *name = ofpact_instruction_name_from_type(inst);
1296 const char *next_name = ofpact_instruction_name_from_type(next);
1299 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1300 "1.1+ compatibility", name);
1302 VLOG_WARN("invalid instruction ordering: %s must appear "
1303 "before %s, for OpenFlow 1.1+ compatibility",
1306 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1315 /* Converting ofpacts to Nicira OpenFlow extensions. */
1318 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1321 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1323 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1324 output_reg->src.n_bits);
1325 naor->src = htonl(output_reg->src.field->nxm_header);
1326 naor->max_len = htons(output_reg->max_len);
1330 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1333 struct nx_action_resubmit *nar;
1335 if (resubmit->table_id == 0xff
1336 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1337 nar = ofputil_put_NXAST_RESUBMIT(out);
1339 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1340 nar->table = resubmit->table_id;
1342 nar->in_port = htons(resubmit->in_port);
1346 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1349 uint64_t tun_id = tunnel->tun_id;
1351 if (tun_id <= UINT32_MAX
1352 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1353 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1355 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1360 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1363 struct nx_action_write_metadata *nawm;
1365 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1366 nawm->metadata = om->metadata;
1367 nawm->mask = om->mask;
1371 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1373 size_t start_ofs = out->size;
1374 struct nx_action_note *nan;
1375 unsigned int remainder;
1378 nan = ofputil_put_NXAST_NOTE(out);
1379 out->size -= sizeof nan->note;
1381 ofpbuf_put(out, note->data, note->length);
1383 len = out->size - start_ofs;
1384 remainder = len % OFP_ACTION_ALIGN;
1386 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1388 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1389 nan->len = htons(out->size - start_ofs);
1393 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1396 struct nx_action_controller *nac;
1398 nac = ofputil_put_NXAST_CONTROLLER(out);
1399 nac->max_len = htons(oc->max_len);
1400 nac->controller_id = htons(oc->controller_id);
1401 nac->reason = oc->reason;
1405 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1408 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1409 ofputil_put_NXAST_DEC_TTL(out);
1411 struct nx_action_cnt_ids *nac_ids =
1412 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1413 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1417 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1418 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1420 ids = ofpbuf_put_zeros(out, ids_len);
1421 for (i = 0; i < oc_ids->n_controllers; i++) {
1422 ids[i] = htons(oc_ids->cnt_ids[i]);
1428 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1431 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1432 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1433 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1437 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1440 struct nx_action_sample *nas;
1442 nas = ofputil_put_NXAST_SAMPLE(out);
1443 nas->probability = htons(os->probability);
1444 nas->collector_set_id = htonl(os->collector_set_id);
1445 nas->obs_domain_id = htonl(os->obs_domain_id);
1446 nas->obs_point_id = htonl(os->obs_point_id);
1450 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1453 case OFPACT_CONTROLLER:
1454 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1457 case OFPACT_OUTPUT_REG:
1458 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1462 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1465 case OFPACT_REG_MOVE:
1466 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1469 case OFPACT_REG_LOAD:
1470 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1473 case OFPACT_STACK_PUSH:
1474 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1477 case OFPACT_STACK_POP:
1478 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1481 case OFPACT_DEC_TTL:
1482 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1485 case OFPACT_SET_MPLS_TTL:
1486 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1487 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1490 case OFPACT_DEC_MPLS_TTL:
1491 ofputil_put_NXAST_DEC_MPLS_TTL(out);
1494 case OFPACT_SET_TUNNEL:
1495 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1498 case OFPACT_WRITE_METADATA:
1499 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1502 case OFPACT_SET_QUEUE:
1503 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1504 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1507 case OFPACT_POP_QUEUE:
1508 ofputil_put_NXAST_POP_QUEUE(out);
1511 case OFPACT_FIN_TIMEOUT:
1512 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1515 case OFPACT_RESUBMIT:
1516 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1520 learn_to_nxast(ofpact_get_LEARN(a), out);
1523 case OFPACT_MULTIPATH:
1524 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1528 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1532 ofputil_put_NXAST_EXIT(out);
1535 case OFPACT_PUSH_MPLS:
1536 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1537 ofpact_get_PUSH_MPLS(a)->ethertype;
1540 case OFPACT_POP_MPLS:
1541 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1542 ofpact_get_POP_MPLS(a)->ethertype;
1546 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
1550 case OFPACT_ENQUEUE:
1551 case OFPACT_SET_VLAN_VID:
1552 case OFPACT_SET_VLAN_PCP:
1553 case OFPACT_STRIP_VLAN:
1554 case OFPACT_PUSH_VLAN:
1555 case OFPACT_SET_ETH_SRC:
1556 case OFPACT_SET_ETH_DST:
1557 case OFPACT_SET_IPV4_SRC:
1558 case OFPACT_SET_IPV4_DST:
1559 case OFPACT_SET_IPV4_DSCP:
1560 case OFPACT_SET_L4_SRC_PORT:
1561 case OFPACT_SET_L4_DST_PORT:
1562 case OFPACT_CLEAR_ACTIONS:
1563 case OFPACT_GOTO_TABLE:
1568 /* Converting ofpacts to OpenFlow 1.0. */
1571 ofpact_output_to_openflow10(const struct ofpact_output *output,
1574 struct ofp10_action_output *oao;
1576 oao = ofputil_put_OFPAT10_OUTPUT(out);
1577 oao->port = htons(output->port);
1578 oao->max_len = htons(output->max_len);
1582 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1585 struct ofp10_action_enqueue *oae;
1587 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1588 oae->port = htons(enqueue->port);
1589 oae->queue_id = htonl(enqueue->queue);
1593 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1597 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1600 case OFPACT_ENQUEUE:
1601 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1604 case OFPACT_SET_VLAN_VID:
1605 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1606 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1609 case OFPACT_SET_VLAN_PCP:
1610 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1611 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1614 case OFPACT_STRIP_VLAN:
1615 ofputil_put_OFPAT10_STRIP_VLAN(out);
1618 case OFPACT_SET_ETH_SRC:
1619 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1620 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1623 case OFPACT_SET_ETH_DST:
1624 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1625 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1628 case OFPACT_SET_IPV4_SRC:
1629 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1630 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1633 case OFPACT_SET_IPV4_DST:
1634 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1635 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1638 case OFPACT_SET_IPV4_DSCP:
1639 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1640 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1643 case OFPACT_SET_L4_SRC_PORT:
1644 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1645 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1648 case OFPACT_SET_L4_DST_PORT:
1649 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1650 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1653 case OFPACT_PUSH_VLAN:
1654 case OFPACT_CLEAR_ACTIONS:
1655 case OFPACT_GOTO_TABLE:
1659 case OFPACT_CONTROLLER:
1660 case OFPACT_OUTPUT_REG:
1662 case OFPACT_REG_MOVE:
1663 case OFPACT_REG_LOAD:
1664 case OFPACT_STACK_PUSH:
1665 case OFPACT_STACK_POP:
1666 case OFPACT_DEC_TTL:
1667 case OFPACT_SET_MPLS_TTL:
1668 case OFPACT_DEC_MPLS_TTL:
1669 case OFPACT_SET_TUNNEL:
1670 case OFPACT_WRITE_METADATA:
1671 case OFPACT_SET_QUEUE:
1672 case OFPACT_POP_QUEUE:
1673 case OFPACT_FIN_TIMEOUT:
1674 case OFPACT_RESUBMIT:
1676 case OFPACT_MULTIPATH:
1679 case OFPACT_PUSH_MPLS:
1680 case OFPACT_POP_MPLS:
1682 ofpact_to_nxast(a, out);
1687 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1688 * actions in 'openflow', appending the actions to any existing data in
1691 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1692 struct ofpbuf *openflow)
1694 const struct ofpact *a;
1696 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1697 ofpact_to_openflow10(a, openflow);
1701 /* Converting ofpacts to OpenFlow 1.1. */
1704 ofpact_output_to_openflow11(const struct ofpact_output *output,
1707 struct ofp11_action_output *oao;
1709 oao = ofputil_put_OFPAT11_OUTPUT(out);
1710 oao->port = ofputil_port_to_ofp11(output->port);
1711 oao->max_len = htons(output->max_len);
1715 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
1718 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
1719 && (!dec_ttl->ofpact.compat ||
1720 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
1721 ofputil_put_OFPAT11_DEC_NW_TTL(out);
1723 ofpact_dec_ttl_to_nxast(dec_ttl, out);
1728 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1732 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1734 case OFPACT_ENQUEUE:
1738 case OFPACT_SET_VLAN_VID:
1739 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1740 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1743 case OFPACT_SET_VLAN_PCP:
1744 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1745 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1748 case OFPACT_STRIP_VLAN:
1749 ofputil_put_OFPAT11_POP_VLAN(out);
1752 case OFPACT_PUSH_VLAN:
1753 /* XXX ETH_TYPE_VLAN_8021AD case */
1754 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
1755 htons(ETH_TYPE_VLAN_8021Q);
1758 case OFPACT_SET_QUEUE:
1759 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
1760 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1763 case OFPACT_SET_ETH_SRC:
1764 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1765 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1768 case OFPACT_SET_ETH_DST:
1769 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1770 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1773 case OFPACT_SET_IPV4_SRC:
1774 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1775 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1778 case OFPACT_SET_IPV4_DST:
1779 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1780 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1783 case OFPACT_SET_IPV4_DSCP:
1784 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1785 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1788 case OFPACT_SET_L4_SRC_PORT:
1789 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1790 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1793 case OFPACT_SET_L4_DST_PORT:
1794 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1795 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1798 case OFPACT_DEC_TTL:
1799 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
1802 case OFPACT_SET_MPLS_TTL:
1803 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
1804 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1807 case OFPACT_DEC_MPLS_TTL:
1808 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
1811 case OFPACT_WRITE_METADATA:
1812 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
1815 case OFPACT_PUSH_MPLS:
1816 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
1817 ofpact_get_PUSH_MPLS(a)->ethertype;
1820 case OFPACT_POP_MPLS:
1821 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
1822 ofpact_get_POP_MPLS(a)->ethertype;
1826 case OFPACT_CLEAR_ACTIONS:
1827 case OFPACT_GOTO_TABLE:
1830 case OFPACT_CONTROLLER:
1831 case OFPACT_OUTPUT_REG:
1833 case OFPACT_REG_MOVE:
1834 case OFPACT_REG_LOAD:
1835 case OFPACT_STACK_PUSH:
1836 case OFPACT_STACK_POP:
1837 case OFPACT_SET_TUNNEL:
1838 case OFPACT_POP_QUEUE:
1839 case OFPACT_FIN_TIMEOUT:
1840 case OFPACT_RESUBMIT:
1842 case OFPACT_MULTIPATH:
1846 ofpact_to_nxast(a, out);
1851 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1852 * 1.1 actions in 'openflow', appending the actions to any existing data in
1855 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1856 size_t ofpacts_len, struct ofpbuf *openflow)
1858 const struct ofpact *a;
1859 size_t start_size = openflow->size;
1861 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1862 ofpact_to_openflow11(a, openflow);
1865 return openflow->size - start_size;
1869 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
1871 struct ofp11_instruction_actions *oia;
1873 /* Update the instruction's length (or, if it's empty, delete it). */
1874 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1875 if (openflow->size > ofs + sizeof *oia) {
1876 oia->len = htons(openflow->size - ofs);
1878 openflow->size = ofs;
1883 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1885 struct ofpbuf *openflow)
1887 const struct ofpact *a;
1889 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1890 /* XXX Write-Actions */
1892 if (a->type == OFPACT_CLEAR_ACTIONS) {
1893 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
1894 } else if (a->type == OFPACT_GOTO_TABLE) {
1895 struct ofp11_instruction_goto_table *oigt;
1897 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1898 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1899 memset(oigt->pad, 0, sizeof oigt->pad);
1900 } else if (a->type == OFPACT_WRITE_METADATA) {
1901 const struct ofpact_metadata *om;
1902 struct ofp11_instruction_write_metadata *oiwm;
1904 om = ofpact_get_WRITE_METADATA(a);
1905 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
1906 oiwm->metadata = om->metadata;
1907 oiwm->metadata_mask = om->mask;
1908 } else if (!ofpact_is_instruction(a)) {
1910 const size_t ofs = openflow->size;
1911 const size_t ofpacts_len_left =
1912 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1913 const struct ofpact *action;
1914 const struct ofpact *processed = a;
1916 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1917 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
1918 if (ofpact_is_instruction(action)) {
1921 ofpact_to_openflow11(action, openflow);
1924 ofpacts_update_instruction_actions(openflow, ofs);
1930 /* Returns true if 'action' outputs to 'port', false otherwise. */
1932 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1934 switch (ofpact->type) {
1936 return ofpact_get_OUTPUT(ofpact)->port == port;
1937 case OFPACT_ENQUEUE:
1938 return ofpact_get_ENQUEUE(ofpact)->port == port;
1939 case OFPACT_CONTROLLER:
1940 return port == OFPP_CONTROLLER;
1942 case OFPACT_OUTPUT_REG:
1944 case OFPACT_SET_VLAN_VID:
1945 case OFPACT_SET_VLAN_PCP:
1946 case OFPACT_STRIP_VLAN:
1947 case OFPACT_PUSH_VLAN:
1948 case OFPACT_SET_ETH_SRC:
1949 case OFPACT_SET_ETH_DST:
1950 case OFPACT_SET_IPV4_SRC:
1951 case OFPACT_SET_IPV4_DST:
1952 case OFPACT_SET_IPV4_DSCP:
1953 case OFPACT_SET_L4_SRC_PORT:
1954 case OFPACT_SET_L4_DST_PORT:
1955 case OFPACT_REG_MOVE:
1956 case OFPACT_REG_LOAD:
1957 case OFPACT_STACK_PUSH:
1958 case OFPACT_STACK_POP:
1959 case OFPACT_DEC_TTL:
1960 case OFPACT_SET_MPLS_TTL:
1961 case OFPACT_DEC_MPLS_TTL:
1962 case OFPACT_SET_TUNNEL:
1963 case OFPACT_WRITE_METADATA:
1964 case OFPACT_SET_QUEUE:
1965 case OFPACT_POP_QUEUE:
1966 case OFPACT_FIN_TIMEOUT:
1967 case OFPACT_RESUBMIT:
1969 case OFPACT_MULTIPATH:
1972 case OFPACT_PUSH_MPLS:
1973 case OFPACT_POP_MPLS:
1975 case OFPACT_CLEAR_ACTIONS:
1976 case OFPACT_GOTO_TABLE:
1982 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1983 * to 'port', false otherwise. */
1985 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1988 const struct ofpact *a;
1990 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1991 if (ofpact_outputs_to_port(a, port)) {
2000 ofpacts_equal(const struct ofpact *a, size_t a_len,
2001 const struct ofpact *b, size_t b_len)
2003 return a_len == b_len && !memcmp(a, b, a_len);
2006 /* Formatting ofpacts. */
2009 print_note(const struct ofpact_note *note, struct ds *string)
2013 ds_put_cstr(string, "note:");
2014 for (i = 0; i < note->length; i++) {
2016 ds_put_char(string, '.');
2018 ds_put_format(string, "%02"PRIx8, note->data[i]);
2023 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2028 ds_put_cstr(s, "dec_ttl");
2029 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2030 ds_put_cstr(s, "(");
2031 for (i = 0; i < ids->n_controllers; i++) {
2033 ds_put_cstr(s, ",");
2035 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2037 ds_put_cstr(s, ")");
2042 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2045 ds_put_cstr(s, "fin_timeout(");
2046 if (fin_timeout->fin_idle_timeout) {
2047 ds_put_format(s, "idle_timeout=%"PRIu16",",
2048 fin_timeout->fin_idle_timeout);
2050 if (fin_timeout->fin_hard_timeout) {
2051 ds_put_format(s, "hard_timeout=%"PRIu16",",
2052 fin_timeout->fin_hard_timeout);
2055 ds_put_char(s, ')');
2059 ofpact_format(const struct ofpact *a, struct ds *s)
2061 const struct ofpact_enqueue *enqueue;
2062 const struct ofpact_resubmit *resubmit;
2063 const struct ofpact_controller *controller;
2064 const struct ofpact_metadata *metadata;
2065 const struct ofpact_tunnel *tunnel;
2066 const struct ofpact_sample *sample;
2071 port = ofpact_get_OUTPUT(a)->port;
2072 if (port < OFPP_MAX) {
2073 ds_put_format(s, "output:%"PRIu16, port);
2075 ofputil_format_port(port, s);
2076 if (port == OFPP_CONTROLLER) {
2077 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2082 case OFPACT_CONTROLLER:
2083 controller = ofpact_get_CONTROLLER(a);
2084 if (controller->reason == OFPR_ACTION &&
2085 controller->controller_id == 0) {
2086 ds_put_format(s, "CONTROLLER:%"PRIu16,
2087 ofpact_get_CONTROLLER(a)->max_len);
2089 enum ofp_packet_in_reason reason = controller->reason;
2091 ds_put_cstr(s, "controller(");
2092 if (reason != OFPR_ACTION) {
2093 ds_put_format(s, "reason=%s,",
2094 ofputil_packet_in_reason_to_string(reason));
2096 if (controller->max_len != UINT16_MAX) {
2097 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2099 if (controller->controller_id != 0) {
2100 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2103 ds_put_char(s, ')');
2107 case OFPACT_ENQUEUE:
2108 enqueue = ofpact_get_ENQUEUE(a);
2109 ds_put_format(s, "enqueue:");
2110 ofputil_format_port(enqueue->port, s);
2111 ds_put_format(s, "q%"PRIu32, enqueue->queue);
2114 case OFPACT_OUTPUT_REG:
2115 ds_put_cstr(s, "output:");
2116 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2120 bundle_format(ofpact_get_BUNDLE(a), s);
2123 case OFPACT_SET_VLAN_VID:
2124 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2125 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2128 case OFPACT_SET_VLAN_PCP:
2129 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2130 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2133 case OFPACT_STRIP_VLAN:
2134 ds_put_cstr(s, "strip_vlan");
2137 case OFPACT_PUSH_VLAN:
2138 /* XXX 802.1AD case*/
2139 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2142 case OFPACT_SET_ETH_SRC:
2143 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2144 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2147 case OFPACT_SET_ETH_DST:
2148 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2149 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2152 case OFPACT_SET_IPV4_SRC:
2153 ds_put_format(s, "mod_nw_src:"IP_FMT,
2154 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2157 case OFPACT_SET_IPV4_DST:
2158 ds_put_format(s, "mod_nw_dst:"IP_FMT,
2159 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2162 case OFPACT_SET_IPV4_DSCP:
2163 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
2166 case OFPACT_SET_L4_SRC_PORT:
2167 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2170 case OFPACT_SET_L4_DST_PORT:
2171 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2174 case OFPACT_REG_MOVE:
2175 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2178 case OFPACT_REG_LOAD:
2179 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2182 case OFPACT_STACK_PUSH:
2183 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2186 case OFPACT_STACK_POP:
2187 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2190 case OFPACT_DEC_TTL:
2191 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2194 case OFPACT_SET_MPLS_TTL:
2195 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2196 ofpact_get_SET_MPLS_TTL(a)->ttl);
2199 case OFPACT_DEC_MPLS_TTL:
2200 ds_put_cstr(s, "dec_mpls_ttl");
2203 case OFPACT_SET_TUNNEL:
2204 tunnel = ofpact_get_SET_TUNNEL(a);
2205 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2206 (tunnel->tun_id > UINT32_MAX
2207 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2211 case OFPACT_SET_QUEUE:
2212 ds_put_format(s, "set_queue:%"PRIu32,
2213 ofpact_get_SET_QUEUE(a)->queue_id);
2216 case OFPACT_POP_QUEUE:
2217 ds_put_cstr(s, "pop_queue");
2220 case OFPACT_FIN_TIMEOUT:
2221 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2224 case OFPACT_RESUBMIT:
2225 resubmit = ofpact_get_RESUBMIT(a);
2226 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2227 ds_put_cstr(s, "resubmit:");
2228 ofputil_format_port(resubmit->in_port, s);
2230 ds_put_format(s, "resubmit(");
2231 if (resubmit->in_port != OFPP_IN_PORT) {
2232 ofputil_format_port(resubmit->in_port, s);
2234 ds_put_char(s, ',');
2235 if (resubmit->table_id != 255) {
2236 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2238 ds_put_char(s, ')');
2243 learn_format(ofpact_get_LEARN(a), s);
2246 case OFPACT_MULTIPATH:
2247 multipath_format(ofpact_get_MULTIPATH(a), s);
2251 print_note(ofpact_get_NOTE(a), s);
2254 case OFPACT_PUSH_MPLS:
2255 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2256 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2259 case OFPACT_POP_MPLS:
2260 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2261 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2265 ds_put_cstr(s, "exit");
2269 sample = ofpact_get_SAMPLE(a);
2271 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2272 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2273 sample->probability, sample->collector_set_id,
2274 sample->obs_domain_id, sample->obs_point_id);
2277 case OFPACT_CLEAR_ACTIONS:
2278 ds_put_format(s, "%s",
2279 ofpact_instruction_name_from_type(
2280 OVSINST_OFPIT11_CLEAR_ACTIONS));
2283 case OFPACT_WRITE_METADATA:
2284 metadata = ofpact_get_WRITE_METADATA(a);
2285 ds_put_format(s, "%s:%#"PRIx64,
2286 ofpact_instruction_name_from_type(
2287 OVSINST_OFPIT11_WRITE_METADATA),
2288 ntohll(metadata->metadata));
2289 if (metadata->mask != htonll(UINT64_MAX)) {
2290 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2294 case OFPACT_GOTO_TABLE:
2295 ds_put_format(s, "%s:%"PRIu8,
2296 ofpact_instruction_name_from_type(
2297 OVSINST_OFPIT11_GOTO_TABLE),
2298 ofpact_get_GOTO_TABLE(a)->table_id);
2303 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2304 * 'ofpacts' to 'string'. */
2306 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2309 ds_put_cstr(string, "actions=");
2311 ds_put_cstr(string, "drop");
2313 const struct ofpact *a;
2315 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2317 ds_put_cstr(string, ",");
2320 /* XXX write-actions */
2321 ofpact_format(a, string);
2326 /* Internal use by helpers. */
2329 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2331 struct ofpact *ofpact;
2333 ofpact_pad(ofpacts);
2334 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2335 ofpact_init(ofpact, type, len);
2340 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2342 memset(ofpact, 0, len);
2343 ofpact->type = type;
2344 ofpact->compat = OFPUTIL_ACTION_INVALID;
2348 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2349 * starting at 'ofpact'.
2351 * This is the correct way to update a variable-length ofpact's length after
2352 * adding the variable-length part of the payload. (See the large comment
2353 * near the end of ofp-actions.h for more information.) */
2355 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2357 ovs_assert(ofpact == ofpacts->l2);
2358 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2361 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2362 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2363 * client must call this itself after adding the final ofpact to an array of
2366 * (The consequences of failing to call this function are probably not dire.
2367 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2368 * not dereference it. That's undefined behavior, technically, but it will not
2369 * cause a real problem on common systems. Still, it seems better to call
2372 ofpact_pad(struct ofpbuf *ofpacts)
2374 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2376 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2381 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2384 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2385 load->dst.field = mf;
2387 load->dst.n_bits = mf->n_bits;
2388 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2389 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);