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,
1055 struct ofpbuf *ofpacts)
1057 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1058 const struct ofp11_instruction *instructions;
1059 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1062 ofpbuf_clear(ofpacts);
1064 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1065 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1067 instructions_len, OFP11_INSTRUCTION_ALIGN);
1068 error = OFPERR_OFPBIC_BAD_LEN;
1072 instructions = ofpbuf_try_pull(openflow, instructions_len);
1073 if (instructions == NULL) {
1074 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1075 "remaining message length (%zu)",
1076 instructions_len, openflow->size);
1077 error = OFPERR_OFPBIC_BAD_LEN;
1081 error = decode_openflow11_instructions(
1082 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1088 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1089 const union ofp_action *actions;
1092 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1093 &actions, &n_actions);
1094 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1099 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1100 instruction_get_OFPIT11_CLEAR_ACTIONS(
1101 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1102 ofpact_put_CLEAR_ACTIONS(ofpacts);
1104 /* XXX Write-Actions */
1105 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1106 const struct ofp11_instruction_write_metadata *oiwm;
1107 struct ofpact_metadata *om;
1109 oiwm = (const struct ofp11_instruction_write_metadata *)
1110 insts[OVSINST_OFPIT11_WRITE_METADATA];
1112 om = ofpact_put_WRITE_METADATA(ofpacts);
1113 om->metadata = oiwm->metadata;
1114 om->mask = oiwm->metadata_mask;
1116 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1117 const struct ofp11_instruction_goto_table *oigt;
1118 struct ofpact_goto_table *ogt;
1120 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1121 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1122 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1123 ogt->table_id = oigt->table_id;
1126 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1127 error = OFPERR_OFPBIC_UNSUP_INST;
1131 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1134 ofpbuf_clear(ofpacts);
1140 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports,
1143 const struct ofpact_enqueue *enqueue;
1147 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1150 case OFPACT_CONTROLLER:
1153 case OFPACT_ENQUEUE:
1154 enqueue = ofpact_get_ENQUEUE(a);
1155 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
1156 && enqueue->port != OFPP_LOCAL) {
1157 return OFPERR_OFPBAC_BAD_OUT_PORT;
1161 case OFPACT_OUTPUT_REG:
1162 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1165 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1167 case OFPACT_SET_VLAN_VID:
1168 case OFPACT_SET_VLAN_PCP:
1169 case OFPACT_STRIP_VLAN:
1170 case OFPACT_PUSH_VLAN:
1171 case OFPACT_SET_ETH_SRC:
1172 case OFPACT_SET_ETH_DST:
1173 case OFPACT_SET_IPV4_SRC:
1174 case OFPACT_SET_IPV4_DST:
1175 case OFPACT_SET_IPV4_DSCP:
1176 case OFPACT_SET_L4_SRC_PORT:
1177 case OFPACT_SET_L4_DST_PORT:
1180 case OFPACT_REG_MOVE:
1181 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1183 case OFPACT_REG_LOAD:
1184 if (*dl_type != flow->dl_type) {
1185 struct flow updated_flow = *flow;
1186 updated_flow.dl_type = *dl_type;
1187 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), &updated_flow);
1189 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1192 case OFPACT_STACK_PUSH:
1193 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1195 case OFPACT_STACK_POP:
1196 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1198 case OFPACT_DEC_TTL:
1199 case OFPACT_SET_MPLS_TTL:
1200 case OFPACT_DEC_MPLS_TTL:
1201 case OFPACT_SET_TUNNEL:
1202 case OFPACT_SET_QUEUE:
1203 case OFPACT_POP_QUEUE:
1204 case OFPACT_FIN_TIMEOUT:
1205 case OFPACT_RESUBMIT:
1209 return learn_check(ofpact_get_LEARN(a), flow);
1211 case OFPACT_MULTIPATH:
1212 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1218 case OFPACT_PUSH_MPLS:
1219 *dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1222 case OFPACT_POP_MPLS:
1223 *dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1229 case OFPACT_CLEAR_ACTIONS:
1230 case OFPACT_WRITE_METADATA:
1231 case OFPACT_GOTO_TABLE:
1239 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1240 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1241 * switch with no more than 'max_ports' ports. */
1243 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1244 const struct flow *flow, int max_ports)
1246 const struct ofpact *a;
1247 ovs_be16 dl_type = flow->dl_type;
1249 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1250 enum ofperr error = ofpact_check__(a, flow, max_ports, &dl_type);
1259 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1260 * in the appropriate order as defined by the OpenFlow spec. */
1262 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1264 const struct ofpact *a;
1265 enum ovs_instruction_type inst;
1267 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1268 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1269 enum ovs_instruction_type next;
1271 if (a->type == OFPACT_CLEAR_ACTIONS) {
1272 next = OVSINST_OFPIT11_CLEAR_ACTIONS;
1273 } else if (a->type == OFPACT_WRITE_METADATA) {
1274 next = OVSINST_OFPIT11_WRITE_METADATA;
1275 } else if (a->type == OFPACT_GOTO_TABLE) {
1276 next = OVSINST_OFPIT11_GOTO_TABLE;
1278 next = OVSINST_OFPIT11_APPLY_ACTIONS;
1281 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS && next <= inst) {
1282 const char *name = ofpact_instruction_name_from_type(inst);
1283 const char *next_name = ofpact_instruction_name_from_type(next);
1286 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1287 "1.1+ compatibility", name);
1289 VLOG_WARN("invalid instruction ordering: %s must appear "
1290 "before %s, for OpenFlow 1.1+ compatibility",
1293 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1302 /* Converting ofpacts to Nicira OpenFlow extensions. */
1305 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1308 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1310 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1311 output_reg->src.n_bits);
1312 naor->src = htonl(output_reg->src.field->nxm_header);
1313 naor->max_len = htons(output_reg->max_len);
1317 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1320 struct nx_action_resubmit *nar;
1322 if (resubmit->table_id == 0xff
1323 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1324 nar = ofputil_put_NXAST_RESUBMIT(out);
1326 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1327 nar->table = resubmit->table_id;
1329 nar->in_port = htons(resubmit->in_port);
1333 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1336 uint64_t tun_id = tunnel->tun_id;
1338 if (tun_id <= UINT32_MAX
1339 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1340 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1342 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1347 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1350 struct nx_action_write_metadata *nawm;
1352 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1353 nawm->metadata = om->metadata;
1354 nawm->mask = om->mask;
1358 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1360 size_t start_ofs = out->size;
1361 struct nx_action_note *nan;
1362 unsigned int remainder;
1365 nan = ofputil_put_NXAST_NOTE(out);
1366 out->size -= sizeof nan->note;
1368 ofpbuf_put(out, note->data, note->length);
1370 len = out->size - start_ofs;
1371 remainder = len % OFP_ACTION_ALIGN;
1373 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1375 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1376 nan->len = htons(out->size - start_ofs);
1380 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1383 struct nx_action_controller *nac;
1385 nac = ofputil_put_NXAST_CONTROLLER(out);
1386 nac->max_len = htons(oc->max_len);
1387 nac->controller_id = htons(oc->controller_id);
1388 nac->reason = oc->reason;
1392 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1395 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1396 ofputil_put_NXAST_DEC_TTL(out);
1398 struct nx_action_cnt_ids *nac_ids =
1399 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1400 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1404 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1405 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1407 ids = ofpbuf_put_zeros(out, ids_len);
1408 for (i = 0; i < oc_ids->n_controllers; i++) {
1409 ids[i] = htons(oc_ids->cnt_ids[i]);
1415 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1418 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1419 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1420 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1424 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1427 struct nx_action_sample *nas;
1429 nas = ofputil_put_NXAST_SAMPLE(out);
1430 nas->probability = htons(os->probability);
1431 nas->collector_set_id = htonl(os->collector_set_id);
1432 nas->obs_domain_id = htonl(os->obs_domain_id);
1433 nas->obs_point_id = htonl(os->obs_point_id);
1437 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1440 case OFPACT_CONTROLLER:
1441 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1444 case OFPACT_OUTPUT_REG:
1445 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1449 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1452 case OFPACT_REG_MOVE:
1453 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1456 case OFPACT_REG_LOAD:
1457 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1460 case OFPACT_STACK_PUSH:
1461 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1464 case OFPACT_STACK_POP:
1465 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1468 case OFPACT_DEC_TTL:
1469 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1472 case OFPACT_SET_MPLS_TTL:
1473 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1474 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1477 case OFPACT_DEC_MPLS_TTL:
1478 ofputil_put_NXAST_DEC_MPLS_TTL(out);
1481 case OFPACT_SET_TUNNEL:
1482 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1485 case OFPACT_WRITE_METADATA:
1486 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1489 case OFPACT_SET_QUEUE:
1490 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1491 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1494 case OFPACT_POP_QUEUE:
1495 ofputil_put_NXAST_POP_QUEUE(out);
1498 case OFPACT_FIN_TIMEOUT:
1499 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1502 case OFPACT_RESUBMIT:
1503 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1507 learn_to_nxast(ofpact_get_LEARN(a), out);
1510 case OFPACT_MULTIPATH:
1511 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1515 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1519 ofputil_put_NXAST_EXIT(out);
1522 case OFPACT_PUSH_MPLS:
1523 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1524 ofpact_get_PUSH_MPLS(a)->ethertype;
1527 case OFPACT_POP_MPLS:
1528 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1529 ofpact_get_POP_MPLS(a)->ethertype;
1533 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
1537 case OFPACT_ENQUEUE:
1538 case OFPACT_SET_VLAN_VID:
1539 case OFPACT_SET_VLAN_PCP:
1540 case OFPACT_STRIP_VLAN:
1541 case OFPACT_PUSH_VLAN:
1542 case OFPACT_SET_ETH_SRC:
1543 case OFPACT_SET_ETH_DST:
1544 case OFPACT_SET_IPV4_SRC:
1545 case OFPACT_SET_IPV4_DST:
1546 case OFPACT_SET_IPV4_DSCP:
1547 case OFPACT_SET_L4_SRC_PORT:
1548 case OFPACT_SET_L4_DST_PORT:
1549 case OFPACT_CLEAR_ACTIONS:
1550 case OFPACT_GOTO_TABLE:
1555 /* Converting ofpacts to OpenFlow 1.0. */
1558 ofpact_output_to_openflow10(const struct ofpact_output *output,
1561 struct ofp10_action_output *oao;
1563 oao = ofputil_put_OFPAT10_OUTPUT(out);
1564 oao->port = htons(output->port);
1565 oao->max_len = htons(output->max_len);
1569 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1572 struct ofp10_action_enqueue *oae;
1574 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1575 oae->port = htons(enqueue->port);
1576 oae->queue_id = htonl(enqueue->queue);
1580 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1584 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1587 case OFPACT_ENQUEUE:
1588 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1591 case OFPACT_SET_VLAN_VID:
1592 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1593 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1596 case OFPACT_SET_VLAN_PCP:
1597 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1598 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1601 case OFPACT_STRIP_VLAN:
1602 ofputil_put_OFPAT10_STRIP_VLAN(out);
1605 case OFPACT_SET_ETH_SRC:
1606 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1607 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1610 case OFPACT_SET_ETH_DST:
1611 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1612 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1615 case OFPACT_SET_IPV4_SRC:
1616 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1617 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1620 case OFPACT_SET_IPV4_DST:
1621 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1622 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1625 case OFPACT_SET_IPV4_DSCP:
1626 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1627 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1630 case OFPACT_SET_L4_SRC_PORT:
1631 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1632 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1635 case OFPACT_SET_L4_DST_PORT:
1636 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1637 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1640 case OFPACT_PUSH_VLAN:
1641 case OFPACT_CLEAR_ACTIONS:
1642 case OFPACT_GOTO_TABLE:
1646 case OFPACT_CONTROLLER:
1647 case OFPACT_OUTPUT_REG:
1649 case OFPACT_REG_MOVE:
1650 case OFPACT_REG_LOAD:
1651 case OFPACT_STACK_PUSH:
1652 case OFPACT_STACK_POP:
1653 case OFPACT_DEC_TTL:
1654 case OFPACT_SET_MPLS_TTL:
1655 case OFPACT_DEC_MPLS_TTL:
1656 case OFPACT_SET_TUNNEL:
1657 case OFPACT_WRITE_METADATA:
1658 case OFPACT_SET_QUEUE:
1659 case OFPACT_POP_QUEUE:
1660 case OFPACT_FIN_TIMEOUT:
1661 case OFPACT_RESUBMIT:
1663 case OFPACT_MULTIPATH:
1666 case OFPACT_PUSH_MPLS:
1667 case OFPACT_POP_MPLS:
1669 ofpact_to_nxast(a, out);
1674 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1675 * actions in 'openflow', appending the actions to any existing data in
1678 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1679 struct ofpbuf *openflow)
1681 const struct ofpact *a;
1683 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1684 ofpact_to_openflow10(a, openflow);
1688 /* Converting ofpacts to OpenFlow 1.1. */
1691 ofpact_output_to_openflow11(const struct ofpact_output *output,
1694 struct ofp11_action_output *oao;
1696 oao = ofputil_put_OFPAT11_OUTPUT(out);
1697 oao->port = ofputil_port_to_ofp11(output->port);
1698 oao->max_len = htons(output->max_len);
1702 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
1705 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
1706 && (!dec_ttl->ofpact.compat ||
1707 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
1708 ofputil_put_OFPAT11_DEC_NW_TTL(out);
1710 ofpact_dec_ttl_to_nxast(dec_ttl, out);
1715 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1719 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1721 case OFPACT_ENQUEUE:
1725 case OFPACT_SET_VLAN_VID:
1726 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1727 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1730 case OFPACT_SET_VLAN_PCP:
1731 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1732 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1735 case OFPACT_STRIP_VLAN:
1736 ofputil_put_OFPAT11_POP_VLAN(out);
1739 case OFPACT_PUSH_VLAN:
1740 /* XXX ETH_TYPE_VLAN_8021AD case */
1741 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
1742 htons(ETH_TYPE_VLAN_8021Q);
1745 case OFPACT_SET_QUEUE:
1746 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
1747 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1750 case OFPACT_SET_ETH_SRC:
1751 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1752 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1755 case OFPACT_SET_ETH_DST:
1756 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1757 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1760 case OFPACT_SET_IPV4_SRC:
1761 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1762 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1765 case OFPACT_SET_IPV4_DST:
1766 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1767 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1770 case OFPACT_SET_IPV4_DSCP:
1771 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1772 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1775 case OFPACT_SET_L4_SRC_PORT:
1776 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1777 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1780 case OFPACT_SET_L4_DST_PORT:
1781 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1782 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1785 case OFPACT_DEC_TTL:
1786 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
1789 case OFPACT_SET_MPLS_TTL:
1790 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
1791 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1794 case OFPACT_DEC_MPLS_TTL:
1795 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
1798 case OFPACT_WRITE_METADATA:
1799 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
1802 case OFPACT_PUSH_MPLS:
1803 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
1804 ofpact_get_PUSH_MPLS(a)->ethertype;
1807 case OFPACT_POP_MPLS:
1808 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
1809 ofpact_get_POP_MPLS(a)->ethertype;
1813 case OFPACT_CLEAR_ACTIONS:
1814 case OFPACT_GOTO_TABLE:
1817 case OFPACT_CONTROLLER:
1818 case OFPACT_OUTPUT_REG:
1820 case OFPACT_REG_MOVE:
1821 case OFPACT_REG_LOAD:
1822 case OFPACT_STACK_PUSH:
1823 case OFPACT_STACK_POP:
1824 case OFPACT_SET_TUNNEL:
1825 case OFPACT_POP_QUEUE:
1826 case OFPACT_FIN_TIMEOUT:
1827 case OFPACT_RESUBMIT:
1829 case OFPACT_MULTIPATH:
1833 ofpact_to_nxast(a, out);
1838 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1839 * 1.1 actions in 'openflow', appending the actions to any existing data in
1842 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1843 size_t ofpacts_len, struct ofpbuf *openflow)
1845 const struct ofpact *a;
1846 size_t start_size = openflow->size;
1848 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1849 ofpact_to_openflow11(a, openflow);
1852 return openflow->size - start_size;
1856 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
1858 struct ofp11_instruction_actions *oia;
1860 /* Update the instruction's length (or, if it's empty, delete it). */
1861 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1862 if (openflow->size > ofs + sizeof *oia) {
1863 oia->len = htons(openflow->size - ofs);
1865 openflow->size = ofs;
1870 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1872 struct ofpbuf *openflow)
1874 const struct ofpact *a;
1876 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1877 /* XXX Write-Actions */
1879 if (a->type == OFPACT_CLEAR_ACTIONS) {
1880 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
1881 } else if (a->type == OFPACT_GOTO_TABLE) {
1882 struct ofp11_instruction_goto_table *oigt;
1884 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1885 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1886 memset(oigt->pad, 0, sizeof oigt->pad);
1887 } else if (a->type == OFPACT_WRITE_METADATA) {
1888 const struct ofpact_metadata *om;
1889 struct ofp11_instruction_write_metadata *oiwm;
1891 om = ofpact_get_WRITE_METADATA(a);
1892 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
1893 oiwm->metadata = om->metadata;
1894 oiwm->metadata_mask = om->mask;
1895 } else if (!ofpact_is_instruction(a)) {
1897 const size_t ofs = openflow->size;
1898 const size_t ofpacts_len_left =
1899 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1900 const struct ofpact *action;
1901 const struct ofpact *processed = a;
1903 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1904 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
1905 if (ofpact_is_instruction(action)) {
1908 ofpact_to_openflow11(action, openflow);
1911 ofpacts_update_instruction_actions(openflow, ofs);
1917 /* Returns true if 'action' outputs to 'port', false otherwise. */
1919 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1921 switch (ofpact->type) {
1923 return ofpact_get_OUTPUT(ofpact)->port == port;
1924 case OFPACT_ENQUEUE:
1925 return ofpact_get_ENQUEUE(ofpact)->port == port;
1926 case OFPACT_CONTROLLER:
1927 return port == OFPP_CONTROLLER;
1929 case OFPACT_OUTPUT_REG:
1931 case OFPACT_SET_VLAN_VID:
1932 case OFPACT_SET_VLAN_PCP:
1933 case OFPACT_STRIP_VLAN:
1934 case OFPACT_PUSH_VLAN:
1935 case OFPACT_SET_ETH_SRC:
1936 case OFPACT_SET_ETH_DST:
1937 case OFPACT_SET_IPV4_SRC:
1938 case OFPACT_SET_IPV4_DST:
1939 case OFPACT_SET_IPV4_DSCP:
1940 case OFPACT_SET_L4_SRC_PORT:
1941 case OFPACT_SET_L4_DST_PORT:
1942 case OFPACT_REG_MOVE:
1943 case OFPACT_REG_LOAD:
1944 case OFPACT_STACK_PUSH:
1945 case OFPACT_STACK_POP:
1946 case OFPACT_DEC_TTL:
1947 case OFPACT_SET_MPLS_TTL:
1948 case OFPACT_DEC_MPLS_TTL:
1949 case OFPACT_SET_TUNNEL:
1950 case OFPACT_WRITE_METADATA:
1951 case OFPACT_SET_QUEUE:
1952 case OFPACT_POP_QUEUE:
1953 case OFPACT_FIN_TIMEOUT:
1954 case OFPACT_RESUBMIT:
1956 case OFPACT_MULTIPATH:
1959 case OFPACT_PUSH_MPLS:
1960 case OFPACT_POP_MPLS:
1962 case OFPACT_CLEAR_ACTIONS:
1963 case OFPACT_GOTO_TABLE:
1969 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1970 * to 'port', false otherwise. */
1972 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1975 const struct ofpact *a;
1977 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1978 if (ofpact_outputs_to_port(a, port)) {
1987 ofpacts_equal(const struct ofpact *a, size_t a_len,
1988 const struct ofpact *b, size_t b_len)
1990 return a_len == b_len && !memcmp(a, b, a_len);
1993 /* Formatting ofpacts. */
1996 print_note(const struct ofpact_note *note, struct ds *string)
2000 ds_put_cstr(string, "note:");
2001 for (i = 0; i < note->length; i++) {
2003 ds_put_char(string, '.');
2005 ds_put_format(string, "%02"PRIx8, note->data[i]);
2010 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2015 ds_put_cstr(s, "dec_ttl");
2016 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2017 ds_put_cstr(s, "(");
2018 for (i = 0; i < ids->n_controllers; i++) {
2020 ds_put_cstr(s, ",");
2022 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2024 ds_put_cstr(s, ")");
2029 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2032 ds_put_cstr(s, "fin_timeout(");
2033 if (fin_timeout->fin_idle_timeout) {
2034 ds_put_format(s, "idle_timeout=%"PRIu16",",
2035 fin_timeout->fin_idle_timeout);
2037 if (fin_timeout->fin_hard_timeout) {
2038 ds_put_format(s, "hard_timeout=%"PRIu16",",
2039 fin_timeout->fin_hard_timeout);
2042 ds_put_char(s, ')');
2046 ofpact_format(const struct ofpact *a, struct ds *s)
2048 const struct ofpact_enqueue *enqueue;
2049 const struct ofpact_resubmit *resubmit;
2050 const struct ofpact_controller *controller;
2051 const struct ofpact_metadata *metadata;
2052 const struct ofpact_tunnel *tunnel;
2053 const struct ofpact_sample *sample;
2058 port = ofpact_get_OUTPUT(a)->port;
2059 if (port < OFPP_MAX) {
2060 ds_put_format(s, "output:%"PRIu16, port);
2062 ofputil_format_port(port, s);
2063 if (port == OFPP_CONTROLLER) {
2064 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2069 case OFPACT_CONTROLLER:
2070 controller = ofpact_get_CONTROLLER(a);
2071 if (controller->reason == OFPR_ACTION &&
2072 controller->controller_id == 0) {
2073 ds_put_format(s, "CONTROLLER:%"PRIu16,
2074 ofpact_get_CONTROLLER(a)->max_len);
2076 enum ofp_packet_in_reason reason = controller->reason;
2078 ds_put_cstr(s, "controller(");
2079 if (reason != OFPR_ACTION) {
2080 ds_put_format(s, "reason=%s,",
2081 ofputil_packet_in_reason_to_string(reason));
2083 if (controller->max_len != UINT16_MAX) {
2084 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2086 if (controller->controller_id != 0) {
2087 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2090 ds_put_char(s, ')');
2094 case OFPACT_ENQUEUE:
2095 enqueue = ofpact_get_ENQUEUE(a);
2096 ds_put_format(s, "enqueue:");
2097 ofputil_format_port(enqueue->port, s);
2098 ds_put_format(s, "q%"PRIu32, enqueue->queue);
2101 case OFPACT_OUTPUT_REG:
2102 ds_put_cstr(s, "output:");
2103 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2107 bundle_format(ofpact_get_BUNDLE(a), s);
2110 case OFPACT_SET_VLAN_VID:
2111 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2112 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2115 case OFPACT_SET_VLAN_PCP:
2116 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2117 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2120 case OFPACT_STRIP_VLAN:
2121 ds_put_cstr(s, "strip_vlan");
2124 case OFPACT_PUSH_VLAN:
2125 /* XXX 802.1AD case*/
2126 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2129 case OFPACT_SET_ETH_SRC:
2130 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2131 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2134 case OFPACT_SET_ETH_DST:
2135 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2136 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2139 case OFPACT_SET_IPV4_SRC:
2140 ds_put_format(s, "mod_nw_src:"IP_FMT,
2141 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2144 case OFPACT_SET_IPV4_DST:
2145 ds_put_format(s, "mod_nw_dst:"IP_FMT,
2146 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2149 case OFPACT_SET_IPV4_DSCP:
2150 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
2153 case OFPACT_SET_L4_SRC_PORT:
2154 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2157 case OFPACT_SET_L4_DST_PORT:
2158 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2161 case OFPACT_REG_MOVE:
2162 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2165 case OFPACT_REG_LOAD:
2166 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2169 case OFPACT_STACK_PUSH:
2170 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2173 case OFPACT_STACK_POP:
2174 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2177 case OFPACT_DEC_TTL:
2178 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2181 case OFPACT_SET_MPLS_TTL:
2182 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2183 ofpact_get_SET_MPLS_TTL(a)->ttl);
2186 case OFPACT_DEC_MPLS_TTL:
2187 ds_put_cstr(s, "dec_mpls_ttl");
2190 case OFPACT_SET_TUNNEL:
2191 tunnel = ofpact_get_SET_TUNNEL(a);
2192 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2193 (tunnel->tun_id > UINT32_MAX
2194 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2198 case OFPACT_SET_QUEUE:
2199 ds_put_format(s, "set_queue:%"PRIu32,
2200 ofpact_get_SET_QUEUE(a)->queue_id);
2203 case OFPACT_POP_QUEUE:
2204 ds_put_cstr(s, "pop_queue");
2207 case OFPACT_FIN_TIMEOUT:
2208 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2211 case OFPACT_RESUBMIT:
2212 resubmit = ofpact_get_RESUBMIT(a);
2213 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2214 ds_put_cstr(s, "resubmit:");
2215 ofputil_format_port(resubmit->in_port, s);
2217 ds_put_format(s, "resubmit(");
2218 if (resubmit->in_port != OFPP_IN_PORT) {
2219 ofputil_format_port(resubmit->in_port, s);
2221 ds_put_char(s, ',');
2222 if (resubmit->table_id != 255) {
2223 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2225 ds_put_char(s, ')');
2230 learn_format(ofpact_get_LEARN(a), s);
2233 case OFPACT_MULTIPATH:
2234 multipath_format(ofpact_get_MULTIPATH(a), s);
2238 print_note(ofpact_get_NOTE(a), s);
2241 case OFPACT_PUSH_MPLS:
2242 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2243 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2246 case OFPACT_POP_MPLS:
2247 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2248 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2252 ds_put_cstr(s, "exit");
2256 sample = ofpact_get_SAMPLE(a);
2258 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2259 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2260 sample->probability, sample->collector_set_id,
2261 sample->obs_domain_id, sample->obs_point_id);
2264 case OFPACT_CLEAR_ACTIONS:
2265 ds_put_format(s, "%s",
2266 ofpact_instruction_name_from_type(
2267 OVSINST_OFPIT11_CLEAR_ACTIONS));
2270 case OFPACT_WRITE_METADATA:
2271 metadata = ofpact_get_WRITE_METADATA(a);
2272 ds_put_format(s, "%s:%#"PRIx64,
2273 ofpact_instruction_name_from_type(
2274 OVSINST_OFPIT11_WRITE_METADATA),
2275 ntohll(metadata->metadata));
2276 if (metadata->mask != htonll(UINT64_MAX)) {
2277 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2281 case OFPACT_GOTO_TABLE:
2282 ds_put_format(s, "%s:%"PRIu8,
2283 ofpact_instruction_name_from_type(
2284 OVSINST_OFPIT11_GOTO_TABLE),
2285 ofpact_get_GOTO_TABLE(a)->table_id);
2290 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2291 * 'ofpacts' to 'string'. */
2293 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2296 ds_put_cstr(string, "actions=");
2298 ds_put_cstr(string, "drop");
2300 const struct ofpact *a;
2302 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2304 ds_put_cstr(string, ",");
2307 /* XXX write-actions */
2308 ofpact_format(a, string);
2313 /* Internal use by helpers. */
2316 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2318 struct ofpact *ofpact;
2320 ofpact_pad(ofpacts);
2321 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2322 ofpact_init(ofpact, type, len);
2327 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2329 memset(ofpact, 0, len);
2330 ofpact->type = type;
2331 ofpact->compat = OFPUTIL_ACTION_INVALID;
2335 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2336 * starting at 'ofpact'.
2338 * This is the correct way to update a variable-length ofpact's length after
2339 * adding the variable-length part of the payload. (See the large comment
2340 * near the end of ofp-actions.h for more information.) */
2342 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2344 ovs_assert(ofpact == ofpacts->l2);
2345 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2348 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2349 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2350 * client must call this itself after adding the final ofpact to an array of
2353 * (The consequences of failing to call this function are probably not dire.
2354 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2355 * not dereference it. That's undefined behavior, technically, but it will not
2356 * cause a real problem on common systems. Still, it seems better to call
2359 ofpact_pad(struct ofpbuf *ofpacts)
2361 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2363 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2368 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2371 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2372 load->dst.field = mf;
2374 load->dst.n_bits = mf->n_bits;
2375 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2376 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);