2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-actions.h"
20 #include "byte-order.h"
22 #include "dynamic-string.h"
24 #include "meta-flow.h"
25 #include "multipath.h"
32 VLOG_DEFINE_THIS_MODULE(ofp_actions);
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
36 /* Converting OpenFlow 1.0 to ofpacts. */
39 output_from_openflow10(const struct ofp10_action_output *oao,
42 struct ofpact_output *output;
44 output = ofpact_put_OUTPUT(out);
45 output->port = u16_to_ofp(ntohs(oao->port));
46 output->max_len = ntohs(oao->max_len);
48 return ofputil_check_output_port(output->port, OFPP_MAX);
52 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
55 struct ofpact_enqueue *enqueue;
57 enqueue = ofpact_put_ENQUEUE(out);
58 enqueue->port = u16_to_ofp(ntohs(oae->port));
59 enqueue->queue = ntohl(oae->queue_id);
60 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
61 && enqueue->port != OFPP_IN_PORT
62 && enqueue->port != OFPP_LOCAL) {
63 return OFPERR_OFPBAC_BAD_OUT_PORT;
69 resubmit_from_openflow(const struct nx_action_resubmit *nar,
72 struct ofpact_resubmit *resubmit;
74 resubmit = ofpact_put_RESUBMIT(out);
75 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
76 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
77 resubmit->table_id = 0xff;
81 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
84 struct ofpact_resubmit *resubmit;
86 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
87 return OFPERR_OFPBAC_BAD_ARGUMENT;
90 resubmit = ofpact_put_RESUBMIT(out);
91 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
92 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
93 resubmit->table_id = nar->table;
98 output_reg_from_openflow(const struct nx_action_output_reg *naor,
101 struct ofpact_output_reg *output_reg;
103 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
104 return OFPERR_OFPBAC_BAD_ARGUMENT;
107 output_reg = ofpact_put_OUTPUT_REG(out);
108 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
109 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
110 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
111 output_reg->max_len = ntohs(naor->max_len);
113 return mf_check_src(&output_reg->src, NULL);
117 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
120 struct ofpact_fin_timeout *oft;
122 oft = ofpact_put_FIN_TIMEOUT(out);
123 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
124 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
128 controller_from_openflow(const struct nx_action_controller *nac,
131 struct ofpact_controller *oc;
133 oc = ofpact_put_CONTROLLER(out);
134 oc->max_len = ntohs(nac->max_len);
135 oc->controller_id = ntohs(nac->controller_id);
136 oc->reason = nac->reason;
140 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
143 struct ofpact_metadata *om;
145 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
146 return OFPERR_NXBRC_MUST_BE_ZERO;
149 om = ofpact_put_WRITE_METADATA(out);
150 om->metadata = nawm->metadata;
151 om->mask = nawm->mask;
157 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
159 struct ofpact_note *note;
162 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
163 note = ofpact_put(out, OFPACT_NOTE,
164 offsetof(struct ofpact_note, data) + length);
165 note->length = length;
166 memcpy(note->data, nan->note, length);
170 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
173 struct ofpact_cnt_ids *ids;
174 enum ofperr error = 0;
176 ids = ofpact_put_DEC_TTL(out);
177 ids->ofpact.compat = compat;
178 ids->n_controllers = 1;
179 ofpbuf_put(out, &id, sizeof id);
181 ofpact_update_len(out, &ids->ofpact);
186 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
189 struct ofpact_cnt_ids *ids;
193 ids = ofpact_put_DEC_TTL(out);
194 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
195 ids->n_controllers = ntohs(nac_ids->n_controllers);
196 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
198 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
199 return OFPERR_NXBRC_MUST_BE_ZERO;
202 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
203 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
204 "allocated for controller ids. %zu bytes are required for "
205 "%"PRIu16" controllers.", ids_size,
206 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
207 return OFPERR_OFPBAC_BAD_LEN;
210 for (i = 0; i < ids->n_controllers; i++) {
211 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
212 ofpbuf_put(out, &id, sizeof id);
216 ofpact_update_len(out, &ids->ofpact);
222 sample_from_openflow(const struct nx_action_sample *nas,
225 struct ofpact_sample *sample;
227 sample = ofpact_put_SAMPLE(out);
228 sample->probability = ntohs(nas->probability);
229 sample->collector_set_id = ntohl(nas->collector_set_id);
230 sample->obs_domain_id = ntohl(nas->obs_domain_id);
231 sample->obs_point_id = ntohl(nas->obs_point_id);
233 if (sample->probability == 0) {
234 return OFPERR_OFPBAC_BAD_ARGUMENT;
241 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
243 const struct nx_action_header *nah = (const struct nx_action_header *) a;
244 uint16_t len = ntohs(a->header.len);
246 if (len < sizeof(struct nx_action_header)) {
247 return OFPERR_OFPBAC_BAD_LEN;
248 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
249 return OFPERR_OFPBAC_BAD_VENDOR;
252 switch (nah->subtype) {
253 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
254 case CONSTANT_HTONS(ENUM): \
256 ? len >= sizeof(struct STRUCT) \
257 : len == sizeof(struct STRUCT)) { \
258 *code = OFPUTIL_##ENUM; \
261 return OFPERR_OFPBAC_BAD_LEN; \
264 #include "ofp-util.def"
266 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
267 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
269 return OFPERR_OFPBAC_BAD_TYPE;
273 /* Parses 'a' to determine its type. On success stores the correct type into
274 * '*code' and returns 0. On failure returns an OFPERR_* error code and
275 * '*code' is indeterminate.
277 * The caller must have already verified that 'a''s length is potentially
278 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
279 * ofp_action) and no longer than the amount of space allocated to 'a').
281 * This function verifies that 'a''s length is correct for the type of action
282 * that it represents. */
284 decode_openflow10_action(const union ofp_action *a,
285 enum ofputil_action_code *code)
288 case CONSTANT_HTONS(OFPAT10_VENDOR):
289 return decode_nxast_action(a, code);
291 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
292 case CONSTANT_HTONS(ENUM): \
293 if (a->header.len == htons(sizeof(struct STRUCT))) { \
294 *code = OFPUTIL_##ENUM; \
297 return OFPERR_OFPBAC_BAD_LEN; \
300 #include "ofp-util.def"
303 return OFPERR_OFPBAC_BAD_TYPE;
308 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
311 const struct nx_action_resubmit *nar;
312 const struct nx_action_set_tunnel *nast;
313 const struct nx_action_set_queue *nasq;
314 const struct nx_action_note *nan;
315 const struct nx_action_set_tunnel64 *nast64;
316 const struct nx_action_write_metadata *nawm;
317 struct ofpact_tunnel *tunnel;
318 enum ofperr error = 0;
321 case OFPUTIL_ACTION_INVALID:
322 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
323 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
324 #include "ofp-util.def"
327 case OFPUTIL_NXAST_RESUBMIT:
328 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
331 case OFPUTIL_NXAST_SET_TUNNEL:
332 nast = (const struct nx_action_set_tunnel *) a;
333 tunnel = ofpact_put_SET_TUNNEL(out);
334 tunnel->ofpact.compat = code;
335 tunnel->tun_id = ntohl(nast->tun_id);
338 case OFPUTIL_NXAST_WRITE_METADATA:
339 nawm = (const struct nx_action_write_metadata *) a;
340 error = metadata_from_nxast(nawm, out);
343 case OFPUTIL_NXAST_SET_QUEUE:
344 nasq = (const struct nx_action_set_queue *) a;
345 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
348 case OFPUTIL_NXAST_POP_QUEUE:
349 ofpact_put_POP_QUEUE(out);
352 case OFPUTIL_NXAST_REG_MOVE:
353 error = nxm_reg_move_from_openflow(
354 (const struct nx_action_reg_move *) a, out);
357 case OFPUTIL_NXAST_REG_LOAD:
358 error = nxm_reg_load_from_openflow(
359 (const struct nx_action_reg_load *) a, out);
362 case OFPUTIL_NXAST_STACK_PUSH:
363 error = nxm_stack_push_from_openflow(
364 (const struct nx_action_stack *) a, out);
367 case OFPUTIL_NXAST_STACK_POP:
368 error = nxm_stack_pop_from_openflow(
369 (const struct nx_action_stack *) a, out);
372 case OFPUTIL_NXAST_NOTE:
373 nan = (const struct nx_action_note *) a;
374 note_from_openflow(nan, out);
377 case OFPUTIL_NXAST_SET_TUNNEL64:
378 nast64 = (const struct nx_action_set_tunnel64 *) a;
379 tunnel = ofpact_put_SET_TUNNEL(out);
380 tunnel->ofpact.compat = code;
381 tunnel->tun_id = ntohll(nast64->tun_id);
384 case OFPUTIL_NXAST_MULTIPATH:
385 error = multipath_from_openflow((const struct nx_action_multipath *) a,
386 ofpact_put_MULTIPATH(out));
389 case OFPUTIL_NXAST_BUNDLE:
390 case OFPUTIL_NXAST_BUNDLE_LOAD:
391 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
394 case OFPUTIL_NXAST_OUTPUT_REG:
395 error = output_reg_from_openflow(
396 (const struct nx_action_output_reg *) a, out);
399 case OFPUTIL_NXAST_RESUBMIT_TABLE:
400 nar = (const struct nx_action_resubmit *) a;
401 error = resubmit_table_from_openflow(nar, out);
404 case OFPUTIL_NXAST_LEARN:
405 error = learn_from_openflow((const struct nx_action_learn *) a, out);
408 case OFPUTIL_NXAST_EXIT:
409 ofpact_put_EXIT(out);
412 case OFPUTIL_NXAST_DEC_TTL:
413 error = dec_ttl_from_openflow(out, code);
416 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
417 error = dec_ttl_cnt_ids_from_openflow(
418 (const struct nx_action_cnt_ids *) a, out);
421 case OFPUTIL_NXAST_FIN_TIMEOUT:
422 fin_timeout_from_openflow(
423 (const struct nx_action_fin_timeout *) a, out);
426 case OFPUTIL_NXAST_CONTROLLER:
427 controller_from_openflow((const struct nx_action_controller *) a, out);
430 case OFPUTIL_NXAST_PUSH_MPLS: {
431 struct nx_action_push_mpls *nxapm = (struct nx_action_push_mpls *)a;
432 if (!eth_type_mpls(nxapm->ethertype)) {
433 return OFPERR_OFPBAC_BAD_ARGUMENT;
435 ofpact_put_PUSH_MPLS(out)->ethertype = nxapm->ethertype;
439 case OFPUTIL_NXAST_SET_MPLS_TTL: {
440 struct nx_action_mpls_ttl *nxamt = (struct nx_action_mpls_ttl *)a;
441 ofpact_put_SET_MPLS_TTL(out)->ttl = nxamt->ttl;
445 case OFPUTIL_NXAST_DEC_MPLS_TTL:
446 ofpact_put_DEC_MPLS_TTL(out);
449 case OFPUTIL_NXAST_POP_MPLS: {
450 struct nx_action_pop_mpls *nxapm = (struct nx_action_pop_mpls *)a;
451 if (eth_type_mpls(nxapm->ethertype)) {
452 return OFPERR_OFPBAC_BAD_ARGUMENT;
454 ofpact_put_POP_MPLS(out)->ethertype = nxapm->ethertype;
458 case OFPUTIL_NXAST_SAMPLE:
459 error = sample_from_openflow(
460 (const struct nx_action_sample *) a, out);
468 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
470 enum ofputil_action_code code;
473 error = decode_openflow10_action(a, &code);
479 case OFPUTIL_ACTION_INVALID:
480 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
481 #include "ofp-util.def"
484 case OFPUTIL_OFPAT10_OUTPUT:
485 return output_from_openflow10(&a->output10, out);
487 case OFPUTIL_OFPAT10_SET_VLAN_VID:
488 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
489 return OFPERR_OFPBAC_BAD_ARGUMENT;
491 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
494 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
495 if (a->vlan_pcp.vlan_pcp & ~7) {
496 return OFPERR_OFPBAC_BAD_ARGUMENT;
498 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
501 case OFPUTIL_OFPAT10_STRIP_VLAN:
502 ofpact_put_STRIP_VLAN(out);
505 case OFPUTIL_OFPAT10_SET_DL_SRC:
506 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
507 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
510 case OFPUTIL_OFPAT10_SET_DL_DST:
511 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
512 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
515 case OFPUTIL_OFPAT10_SET_NW_SRC:
516 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
519 case OFPUTIL_OFPAT10_SET_NW_DST:
520 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
523 case OFPUTIL_OFPAT10_SET_NW_TOS:
524 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
525 return OFPERR_OFPBAC_BAD_ARGUMENT;
527 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
530 case OFPUTIL_OFPAT10_SET_TP_SRC:
531 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
534 case OFPUTIL_OFPAT10_SET_TP_DST:
535 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
539 case OFPUTIL_OFPAT10_ENQUEUE:
540 error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
544 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
545 #include "ofp-util.def"
546 return ofpact_from_nxast(a, code, out);
552 static inline union ofp_action *
553 action_next(const union ofp_action *a)
555 return ((union ofp_action *) (void *)
556 ((uint8_t *) a + ntohs(a->header.len)));
560 action_is_valid(const union ofp_action *a, size_t n_actions)
562 uint16_t len = ntohs(a->header.len);
563 return (!(len % OFP_ACTION_ALIGN)
565 && len / sizeof *a <= n_actions);
568 /* This macro is careful to check for actions with bad lengths. */
569 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
570 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
571 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
572 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
573 (ITER) = action_next(ITER)))
576 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
579 if (!VLOG_DROP_WARN(&rl)) {
583 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
584 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
585 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
591 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
593 enum ofperr (*ofpact_from_openflow)(
594 const union ofp_action *a, struct ofpbuf *out))
596 const union ofp_action *a;
599 ACTION_FOR_EACH (a, left, in, n_in) {
600 enum ofperr error = ofpact_from_openflow(a, out);
602 log_bad_action(in, n_in, a - in, error);
607 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
608 log_bad_action(in, n_in, n_in - left, error);
617 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
620 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
624 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
625 struct ofpbuf *ofpacts,
626 enum ofperr (*translate)(const union ofp_action *actions,
628 struct ofpbuf *ofpacts))
630 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
631 const union ofp_action *actions;
634 ofpbuf_clear(ofpacts);
636 if (actions_len % OFP_ACTION_ALIGN != 0) {
637 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
638 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
639 return OFPERR_OFPBRC_BAD_LEN;
642 actions = ofpbuf_try_pull(openflow, actions_len);
643 if (actions == NULL) {
644 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
645 "remaining message length (%zu)",
646 actions_len, openflow->size);
647 return OFPERR_OFPBRC_BAD_LEN;
650 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
652 ofpbuf_clear(ofpacts);
656 error = ofpacts_verify(ofpacts->data, ofpacts->size);
658 ofpbuf_clear(ofpacts);
663 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
664 * front of 'openflow' into ofpacts. On success, replaces any existing content
665 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
666 * Returns 0 if successful, otherwise an OpenFlow error.
668 * The parsed actions are valid generically, but they may not be valid in a
669 * specific context. For example, port numbers up to OFPP_MAX are valid
670 * generically, but specific datapaths may only support port numbers in a
671 * smaller range. Use ofpacts_check() to additional check whether actions are
672 * valid in a specific context. */
674 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
675 struct ofpbuf *ofpacts)
677 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
678 ofpacts_from_openflow10);
681 /* OpenFlow 1.1 actions. */
683 /* Parses 'a' to determine its type. On success stores the correct type into
684 * '*code' and returns 0. On failure returns an OFPERR_* error code and
685 * '*code' is indeterminate.
687 * The caller must have already verified that 'a''s length is potentially
688 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
689 * ofp_action) and no longer than the amount of space allocated to 'a').
691 * This function verifies that 'a''s length is correct for the type of action
692 * that it represents. */
694 decode_openflow11_action(const union ofp_action *a,
695 enum ofputil_action_code *code)
700 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
701 return decode_nxast_action(a, code);
703 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
704 case CONSTANT_HTONS(ENUM): \
705 len = ntohs(a->header.len); \
707 ? len >= sizeof(struct STRUCT) \
708 : len == sizeof(struct STRUCT)) { \
709 *code = OFPUTIL_##ENUM; \
712 return OFPERR_OFPBAC_BAD_LEN; \
715 #include "ofp-util.def"
718 return OFPERR_OFPBAC_BAD_TYPE;
723 output_from_openflow11(const struct ofp11_action_output *oao,
726 struct ofpact_output *output;
729 output = ofpact_put_OUTPUT(out);
730 output->max_len = ntohs(oao->max_len);
732 error = ofputil_port_from_ofp11(oao->port, &output->port);
737 return ofputil_check_output_port(output->port, OFPP_MAX);
741 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
743 enum ofputil_action_code code;
746 error = decode_openflow11_action(a, &code);
752 case OFPUTIL_ACTION_INVALID:
753 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
754 #include "ofp-util.def"
757 case OFPUTIL_OFPAT11_OUTPUT:
758 return output_from_openflow11((const struct ofp11_action_output *) a,
761 case OFPUTIL_OFPAT11_SET_VLAN_VID:
762 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
763 return OFPERR_OFPBAC_BAD_ARGUMENT;
765 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
768 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
769 if (a->vlan_pcp.vlan_pcp & ~7) {
770 return OFPERR_OFPBAC_BAD_ARGUMENT;
772 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
775 case OFPUTIL_OFPAT11_PUSH_VLAN:
776 if (((const struct ofp11_action_push *)a)->ethertype !=
777 htons(ETH_TYPE_VLAN_8021Q)) {
778 /* XXX 802.1AD(QinQ) isn't supported at the moment */
779 return OFPERR_OFPBAC_BAD_ARGUMENT;
781 ofpact_put_PUSH_VLAN(out);
784 case OFPUTIL_OFPAT11_POP_VLAN:
785 ofpact_put_STRIP_VLAN(out);
788 case OFPUTIL_OFPAT11_SET_QUEUE:
789 ofpact_put_SET_QUEUE(out)->queue_id =
790 ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
793 case OFPUTIL_OFPAT11_SET_DL_SRC:
794 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
795 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
798 case OFPUTIL_OFPAT11_SET_DL_DST:
799 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
800 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
803 case OFPUTIL_OFPAT11_DEC_NW_TTL:
804 dec_ttl_from_openflow(out, code);
807 case OFPUTIL_OFPAT11_SET_NW_SRC:
808 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
811 case OFPUTIL_OFPAT11_SET_NW_DST:
812 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
815 case OFPUTIL_OFPAT11_SET_NW_TOS:
816 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
817 return OFPERR_OFPBAC_BAD_ARGUMENT;
819 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
822 case OFPUTIL_OFPAT11_SET_TP_SRC:
823 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
826 case OFPUTIL_OFPAT11_SET_TP_DST:
827 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
830 case OFPUTIL_OFPAT12_SET_FIELD:
831 return nxm_reg_load_from_openflow12_set_field(
832 (const struct ofp12_action_set_field *)a, out);
834 case OFPUTIL_OFPAT11_SET_MPLS_TTL: {
835 struct ofp11_action_mpls_ttl *oamt = (struct ofp11_action_mpls_ttl *)a;
836 ofpact_put_SET_MPLS_TTL(out)->ttl = oamt->mpls_ttl;
840 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
841 ofpact_put_DEC_MPLS_TTL(out);
844 case OFPUTIL_OFPAT11_PUSH_MPLS: {
845 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
846 if (!eth_type_mpls(oap->ethertype)) {
847 return OFPERR_OFPBAC_BAD_ARGUMENT;
849 ofpact_put_PUSH_MPLS(out)->ethertype = oap->ethertype;
853 case OFPUTIL_OFPAT11_POP_MPLS: {
854 struct ofp11_action_pop_mpls *oapm = (struct ofp11_action_pop_mpls *)a;
855 if (eth_type_mpls(oapm->ethertype)) {
856 return OFPERR_OFPBAC_BAD_ARGUMENT;
858 ofpact_put_POP_MPLS(out)->ethertype = oapm->ethertype;
862 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
863 #include "ofp-util.def"
864 return ofpact_from_nxast(a, code, out);
871 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
874 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
877 /* OpenFlow 1.1 instructions. */
879 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
880 static inline const struct STRUCT * \
881 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
883 ovs_assert(inst->type == htons(ENUM)); \
884 return (struct STRUCT *)inst; \
888 instruction_init_##ENUM(struct STRUCT *s) \
890 memset(s, 0, sizeof *s); \
891 s->type = htons(ENUM); \
892 s->len = htons(sizeof *s); \
895 static inline struct STRUCT * \
896 instruction_put_##ENUM(struct ofpbuf *buf) \
898 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
899 instruction_init_##ENUM(s); \
905 struct instruction_type_info {
906 enum ovs_instruction_type type;
910 static const struct instruction_type_info inst_info[] = {
911 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
917 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
919 return inst_info[type].name;
923 ofpact_instruction_type_from_name(const char *name)
925 const struct instruction_type_info *p;
926 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
927 if (!strcasecmp(name, p->name)) {
934 static inline struct ofp11_instruction *
935 instruction_next(const struct ofp11_instruction *inst)
937 return ((struct ofp11_instruction *) (void *)
938 ((uint8_t *) inst + ntohs(inst->len)));
942 instruction_is_valid(const struct ofp11_instruction *inst,
943 size_t n_instructions)
945 uint16_t len = ntohs(inst->len);
946 return (!(len % OFP11_INSTRUCTION_ALIGN)
947 && len >= sizeof *inst
948 && len / sizeof *inst <= n_instructions);
951 /* This macro is careful to check for instructions with bad lengths. */
952 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
953 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
954 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
955 ((LEFT) -= (ntohs((ITER)->len) \
956 / sizeof(struct ofp11_instruction)), \
957 (ITER) = instruction_next(ITER)))
960 decode_openflow11_instruction(const struct ofp11_instruction *inst,
961 enum ovs_instruction_type *type)
963 uint16_t len = ntohs(inst->len);
965 switch (inst->type) {
966 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
967 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
969 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
970 case CONSTANT_HTONS(ENUM): \
972 ? len >= sizeof(struct STRUCT) \
973 : len == sizeof(struct STRUCT)) { \
974 *type = OVSINST_##ENUM; \
977 return OFPERR_OFPBIC_BAD_LEN; \
983 return OFPERR_OFPBIC_UNKNOWN_INST;
988 decode_openflow11_instructions(const struct ofp11_instruction insts[],
990 const struct ofp11_instruction *out[])
992 const struct ofp11_instruction *inst;
995 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
996 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
997 enum ovs_instruction_type type;
1000 error = decode_openflow11_instruction(inst, &type);
1006 return OFPERR_OFPBAC_UNSUPPORTED_ORDER; /* No specific code for
1007 * a duplicate instruction
1014 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1015 (n_insts - left) * sizeof *inst);
1016 return OFPERR_OFPBIC_BAD_LEN;
1022 get_actions_from_instruction(const struct ofp11_instruction *inst,
1023 const union ofp_action **actions,
1026 *actions = (const union ofp_action *) (inst + 1);
1027 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1030 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
1031 * front of 'openflow' into ofpacts. On success, replaces any existing content
1032 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1033 * Returns 0 if successful, otherwise an OpenFlow error.
1035 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1036 * instructions, so you should call ofpacts_pull_openflow11_instructions()
1037 * instead of this function.
1039 * The parsed actions are valid generically, but they may not be valid in a
1040 * specific context. For example, port numbers up to OFPP_MAX are valid
1041 * generically, but specific datapaths may only support port numbers in a
1042 * smaller range. Use ofpacts_check() to additional check whether actions are
1043 * valid in a specific context. */
1045 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1046 unsigned int actions_len,
1047 struct ofpbuf *ofpacts)
1049 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1050 ofpacts_from_openflow11);
1054 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1055 unsigned int instructions_len,
1057 struct ofpbuf *ofpacts)
1059 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1060 const struct ofp11_instruction *instructions;
1061 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1064 ofpbuf_clear(ofpacts);
1066 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1067 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1069 instructions_len, OFP11_INSTRUCTION_ALIGN);
1070 error = OFPERR_OFPBIC_BAD_LEN;
1074 instructions = ofpbuf_try_pull(openflow, instructions_len);
1075 if (instructions == NULL) {
1076 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1077 "remaining message length (%zu)",
1078 instructions_len, openflow->size);
1079 error = OFPERR_OFPBIC_BAD_LEN;
1083 error = decode_openflow11_instructions(
1084 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1090 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1091 const union ofp_action *actions;
1094 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1095 &actions, &n_actions);
1096 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1101 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1102 instruction_get_OFPIT11_CLEAR_ACTIONS(
1103 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1104 ofpact_put_CLEAR_ACTIONS(ofpacts);
1106 /* XXX Write-Actions */
1107 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1108 const struct ofp11_instruction_write_metadata *oiwm;
1109 struct ofpact_metadata *om;
1111 oiwm = (const struct ofp11_instruction_write_metadata *)
1112 insts[OVSINST_OFPIT11_WRITE_METADATA];
1114 om = ofpact_put_WRITE_METADATA(ofpacts);
1115 om->metadata = oiwm->metadata;
1116 om->mask = oiwm->metadata_mask;
1118 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1119 const struct ofp11_instruction_goto_table *oigt;
1120 struct ofpact_goto_table *ogt;
1122 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1123 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1124 if (table_id >= oigt->table_id) {
1125 error = OFPERR_OFPBRC_BAD_TABLE_ID;
1128 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1129 ogt->table_id = oigt->table_id;
1132 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1133 error = OFPERR_OFPBIC_UNSUP_INST;
1137 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1140 ofpbuf_clear(ofpacts);
1146 ofpact_check__(const struct ofpact *a, const struct flow *flow,
1147 ofp_port_t max_ports, ovs_be16 *dl_type)
1149 const struct ofpact_enqueue *enqueue;
1153 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1156 case OFPACT_CONTROLLER:
1159 case OFPACT_ENQUEUE:
1160 enqueue = ofpact_get_ENQUEUE(a);
1161 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1162 && enqueue->port != OFPP_IN_PORT
1163 && enqueue->port != OFPP_LOCAL) {
1164 return OFPERR_OFPBAC_BAD_OUT_PORT;
1168 case OFPACT_OUTPUT_REG:
1169 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1172 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1174 case OFPACT_SET_VLAN_VID:
1175 case OFPACT_SET_VLAN_PCP:
1176 case OFPACT_STRIP_VLAN:
1177 case OFPACT_PUSH_VLAN:
1178 case OFPACT_SET_ETH_SRC:
1179 case OFPACT_SET_ETH_DST:
1180 case OFPACT_SET_IPV4_SRC:
1181 case OFPACT_SET_IPV4_DST:
1182 case OFPACT_SET_IPV4_DSCP:
1183 case OFPACT_SET_L4_SRC_PORT:
1184 case OFPACT_SET_L4_DST_PORT:
1187 case OFPACT_REG_MOVE:
1188 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1190 case OFPACT_REG_LOAD:
1191 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1193 case OFPACT_STACK_PUSH:
1194 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1196 case OFPACT_STACK_POP:
1197 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1199 case OFPACT_DEC_TTL:
1200 case OFPACT_SET_MPLS_TTL:
1201 case OFPACT_DEC_MPLS_TTL:
1202 case OFPACT_SET_TUNNEL:
1203 case OFPACT_SET_QUEUE:
1204 case OFPACT_POP_QUEUE:
1205 case OFPACT_FIN_TIMEOUT:
1206 case OFPACT_RESUBMIT:
1210 return learn_check(ofpact_get_LEARN(a), flow);
1212 case OFPACT_MULTIPATH:
1213 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1219 case OFPACT_PUSH_MPLS:
1220 *dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1223 case OFPACT_POP_MPLS:
1224 *dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1230 case OFPACT_CLEAR_ACTIONS:
1231 case OFPACT_WRITE_METADATA:
1232 case OFPACT_GOTO_TABLE:
1240 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1241 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1242 * switch with no more than 'max_ports' ports. */
1244 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1245 const struct flow *flow, ofp_port_t max_ports)
1247 const struct ofpact *a;
1248 ovs_be16 dl_type = flow->dl_type;
1249 struct flow updated_flow;
1251 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1254 /* If the dl_type was changed by an action then its new value
1255 * should be present in the flow passed to ofpact_check__(). */
1256 if (flow->dl_type != dl_type) {
1257 /* Only copy flow at most once */
1258 if (flow != &updated_flow) {
1259 updated_flow = *flow;
1260 flow = &updated_flow;
1262 updated_flow.dl_type = dl_type;
1265 error = ofpact_check__(a, flow, max_ports, &dl_type);
1274 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1275 * in the appropriate order as defined by the OpenFlow spec. */
1277 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1279 const struct ofpact *a;
1280 enum ovs_instruction_type inst;
1282 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1283 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1284 enum ovs_instruction_type next;
1286 if (a->type == OFPACT_CLEAR_ACTIONS) {
1287 next = OVSINST_OFPIT11_CLEAR_ACTIONS;
1288 } else if (a->type == OFPACT_WRITE_METADATA) {
1289 next = OVSINST_OFPIT11_WRITE_METADATA;
1290 } else if (a->type == OFPACT_GOTO_TABLE) {
1291 next = OVSINST_OFPIT11_GOTO_TABLE;
1293 next = OVSINST_OFPIT11_APPLY_ACTIONS;
1296 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS && next <= inst) {
1297 const char *name = ofpact_instruction_name_from_type(inst);
1298 const char *next_name = ofpact_instruction_name_from_type(next);
1301 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1302 "1.1+ compatibility", name);
1304 VLOG_WARN("invalid instruction ordering: %s must appear "
1305 "before %s, for OpenFlow 1.1+ compatibility",
1308 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1317 /* Converting ofpacts to Nicira OpenFlow extensions. */
1320 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1323 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1325 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1326 output_reg->src.n_bits);
1327 naor->src = htonl(output_reg->src.field->nxm_header);
1328 naor->max_len = htons(output_reg->max_len);
1332 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1335 struct nx_action_resubmit *nar;
1337 if (resubmit->table_id == 0xff
1338 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1339 nar = ofputil_put_NXAST_RESUBMIT(out);
1341 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1342 nar->table = resubmit->table_id;
1344 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
1348 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1351 uint64_t tun_id = tunnel->tun_id;
1353 if (tun_id <= UINT32_MAX
1354 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1355 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1357 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1362 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1365 struct nx_action_write_metadata *nawm;
1367 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1368 nawm->metadata = om->metadata;
1369 nawm->mask = om->mask;
1373 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1375 size_t start_ofs = out->size;
1376 struct nx_action_note *nan;
1377 unsigned int remainder;
1380 nan = ofputil_put_NXAST_NOTE(out);
1381 out->size -= sizeof nan->note;
1383 ofpbuf_put(out, note->data, note->length);
1385 len = out->size - start_ofs;
1386 remainder = len % OFP_ACTION_ALIGN;
1388 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1390 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1391 nan->len = htons(out->size - start_ofs);
1395 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1398 struct nx_action_controller *nac;
1400 nac = ofputil_put_NXAST_CONTROLLER(out);
1401 nac->max_len = htons(oc->max_len);
1402 nac->controller_id = htons(oc->controller_id);
1403 nac->reason = oc->reason;
1407 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1410 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1411 ofputil_put_NXAST_DEC_TTL(out);
1413 struct nx_action_cnt_ids *nac_ids =
1414 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1415 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1419 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1420 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1422 ids = ofpbuf_put_zeros(out, ids_len);
1423 for (i = 0; i < oc_ids->n_controllers; i++) {
1424 ids[i] = htons(oc_ids->cnt_ids[i]);
1430 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1433 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1434 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1435 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1439 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1442 struct nx_action_sample *nas;
1444 nas = ofputil_put_NXAST_SAMPLE(out);
1445 nas->probability = htons(os->probability);
1446 nas->collector_set_id = htonl(os->collector_set_id);
1447 nas->obs_domain_id = htonl(os->obs_domain_id);
1448 nas->obs_point_id = htonl(os->obs_point_id);
1452 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1455 case OFPACT_CONTROLLER:
1456 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1459 case OFPACT_OUTPUT_REG:
1460 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1464 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1467 case OFPACT_REG_MOVE:
1468 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1471 case OFPACT_REG_LOAD:
1472 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1475 case OFPACT_STACK_PUSH:
1476 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1479 case OFPACT_STACK_POP:
1480 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1483 case OFPACT_DEC_TTL:
1484 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1487 case OFPACT_SET_MPLS_TTL:
1488 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1489 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1492 case OFPACT_DEC_MPLS_TTL:
1493 ofputil_put_NXAST_DEC_MPLS_TTL(out);
1496 case OFPACT_SET_TUNNEL:
1497 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1500 case OFPACT_WRITE_METADATA:
1501 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1504 case OFPACT_SET_QUEUE:
1505 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1506 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1509 case OFPACT_POP_QUEUE:
1510 ofputil_put_NXAST_POP_QUEUE(out);
1513 case OFPACT_FIN_TIMEOUT:
1514 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1517 case OFPACT_RESUBMIT:
1518 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1522 learn_to_nxast(ofpact_get_LEARN(a), out);
1525 case OFPACT_MULTIPATH:
1526 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1530 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1534 ofputil_put_NXAST_EXIT(out);
1537 case OFPACT_PUSH_MPLS:
1538 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1539 ofpact_get_PUSH_MPLS(a)->ethertype;
1542 case OFPACT_POP_MPLS:
1543 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1544 ofpact_get_POP_MPLS(a)->ethertype;
1548 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
1552 case OFPACT_ENQUEUE:
1553 case OFPACT_SET_VLAN_VID:
1554 case OFPACT_SET_VLAN_PCP:
1555 case OFPACT_STRIP_VLAN:
1556 case OFPACT_PUSH_VLAN:
1557 case OFPACT_SET_ETH_SRC:
1558 case OFPACT_SET_ETH_DST:
1559 case OFPACT_SET_IPV4_SRC:
1560 case OFPACT_SET_IPV4_DST:
1561 case OFPACT_SET_IPV4_DSCP:
1562 case OFPACT_SET_L4_SRC_PORT:
1563 case OFPACT_SET_L4_DST_PORT:
1564 case OFPACT_CLEAR_ACTIONS:
1565 case OFPACT_GOTO_TABLE:
1570 /* Converting ofpacts to OpenFlow 1.0. */
1573 ofpact_output_to_openflow10(const struct ofpact_output *output,
1576 struct ofp10_action_output *oao;
1578 oao = ofputil_put_OFPAT10_OUTPUT(out);
1579 oao->port = htons(ofp_to_u16(output->port));
1580 oao->max_len = htons(output->max_len);
1584 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1587 struct ofp10_action_enqueue *oae;
1589 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1590 oae->port = htons(ofp_to_u16(enqueue->port));
1591 oae->queue_id = htonl(enqueue->queue);
1595 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1599 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1602 case OFPACT_ENQUEUE:
1603 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1606 case OFPACT_SET_VLAN_VID:
1607 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1608 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1611 case OFPACT_SET_VLAN_PCP:
1612 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1613 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1616 case OFPACT_STRIP_VLAN:
1617 ofputil_put_OFPAT10_STRIP_VLAN(out);
1620 case OFPACT_SET_ETH_SRC:
1621 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1622 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1625 case OFPACT_SET_ETH_DST:
1626 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1627 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1630 case OFPACT_SET_IPV4_SRC:
1631 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1632 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1635 case OFPACT_SET_IPV4_DST:
1636 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1637 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1640 case OFPACT_SET_IPV4_DSCP:
1641 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1642 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1645 case OFPACT_SET_L4_SRC_PORT:
1646 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1647 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1650 case OFPACT_SET_L4_DST_PORT:
1651 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1652 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1655 case OFPACT_PUSH_VLAN:
1656 case OFPACT_CLEAR_ACTIONS:
1657 case OFPACT_GOTO_TABLE:
1661 case OFPACT_CONTROLLER:
1662 case OFPACT_OUTPUT_REG:
1664 case OFPACT_REG_MOVE:
1665 case OFPACT_REG_LOAD:
1666 case OFPACT_STACK_PUSH:
1667 case OFPACT_STACK_POP:
1668 case OFPACT_DEC_TTL:
1669 case OFPACT_SET_MPLS_TTL:
1670 case OFPACT_DEC_MPLS_TTL:
1671 case OFPACT_SET_TUNNEL:
1672 case OFPACT_WRITE_METADATA:
1673 case OFPACT_SET_QUEUE:
1674 case OFPACT_POP_QUEUE:
1675 case OFPACT_FIN_TIMEOUT:
1676 case OFPACT_RESUBMIT:
1678 case OFPACT_MULTIPATH:
1681 case OFPACT_PUSH_MPLS:
1682 case OFPACT_POP_MPLS:
1684 ofpact_to_nxast(a, out);
1689 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1690 * actions in 'openflow', appending the actions to any existing data in
1693 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1694 struct ofpbuf *openflow)
1696 const struct ofpact *a;
1698 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1699 ofpact_to_openflow10(a, openflow);
1703 /* Converting ofpacts to OpenFlow 1.1. */
1706 ofpact_output_to_openflow11(const struct ofpact_output *output,
1709 struct ofp11_action_output *oao;
1711 oao = ofputil_put_OFPAT11_OUTPUT(out);
1712 oao->port = ofputil_port_to_ofp11(output->port);
1713 oao->max_len = htons(output->max_len);
1717 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
1720 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
1721 && (!dec_ttl->ofpact.compat ||
1722 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
1723 ofputil_put_OFPAT11_DEC_NW_TTL(out);
1725 ofpact_dec_ttl_to_nxast(dec_ttl, out);
1730 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1734 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1736 case OFPACT_ENQUEUE:
1740 case OFPACT_SET_VLAN_VID:
1741 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1742 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1745 case OFPACT_SET_VLAN_PCP:
1746 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1747 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1750 case OFPACT_STRIP_VLAN:
1751 ofputil_put_OFPAT11_POP_VLAN(out);
1754 case OFPACT_PUSH_VLAN:
1755 /* XXX ETH_TYPE_VLAN_8021AD case */
1756 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
1757 htons(ETH_TYPE_VLAN_8021Q);
1760 case OFPACT_SET_QUEUE:
1761 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
1762 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1765 case OFPACT_SET_ETH_SRC:
1766 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1767 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1770 case OFPACT_SET_ETH_DST:
1771 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1772 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1775 case OFPACT_SET_IPV4_SRC:
1776 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1777 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1780 case OFPACT_SET_IPV4_DST:
1781 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1782 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1785 case OFPACT_SET_IPV4_DSCP:
1786 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1787 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1790 case OFPACT_SET_L4_SRC_PORT:
1791 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1792 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1795 case OFPACT_SET_L4_DST_PORT:
1796 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1797 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1800 case OFPACT_DEC_TTL:
1801 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
1804 case OFPACT_SET_MPLS_TTL:
1805 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
1806 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1809 case OFPACT_DEC_MPLS_TTL:
1810 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
1813 case OFPACT_WRITE_METADATA:
1814 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
1817 case OFPACT_PUSH_MPLS:
1818 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
1819 ofpact_get_PUSH_MPLS(a)->ethertype;
1822 case OFPACT_POP_MPLS:
1823 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
1824 ofpact_get_POP_MPLS(a)->ethertype;
1828 case OFPACT_CLEAR_ACTIONS:
1829 case OFPACT_GOTO_TABLE:
1832 case OFPACT_CONTROLLER:
1833 case OFPACT_OUTPUT_REG:
1835 case OFPACT_REG_MOVE:
1836 case OFPACT_REG_LOAD:
1837 case OFPACT_STACK_PUSH:
1838 case OFPACT_STACK_POP:
1839 case OFPACT_SET_TUNNEL:
1840 case OFPACT_POP_QUEUE:
1841 case OFPACT_FIN_TIMEOUT:
1842 case OFPACT_RESUBMIT:
1844 case OFPACT_MULTIPATH:
1848 ofpact_to_nxast(a, out);
1853 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1854 * 1.1 actions in 'openflow', appending the actions to any existing data in
1857 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1858 size_t ofpacts_len, struct ofpbuf *openflow)
1860 const struct ofpact *a;
1861 size_t start_size = openflow->size;
1863 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1864 ofpact_to_openflow11(a, openflow);
1867 return openflow->size - start_size;
1871 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
1873 struct ofp11_instruction_actions *oia;
1875 /* Update the instruction's length (or, if it's empty, delete it). */
1876 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1877 if (openflow->size > ofs + sizeof *oia) {
1878 oia->len = htons(openflow->size - ofs);
1880 openflow->size = ofs;
1885 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1887 struct ofpbuf *openflow)
1889 const struct ofpact *a;
1891 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1892 /* XXX Write-Actions */
1894 if (a->type == OFPACT_CLEAR_ACTIONS) {
1895 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
1896 } else if (a->type == OFPACT_GOTO_TABLE) {
1897 struct ofp11_instruction_goto_table *oigt;
1899 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1900 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1901 memset(oigt->pad, 0, sizeof oigt->pad);
1902 } else if (a->type == OFPACT_WRITE_METADATA) {
1903 const struct ofpact_metadata *om;
1904 struct ofp11_instruction_write_metadata *oiwm;
1906 om = ofpact_get_WRITE_METADATA(a);
1907 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
1908 oiwm->metadata = om->metadata;
1909 oiwm->metadata_mask = om->mask;
1910 } else if (!ofpact_is_instruction(a)) {
1912 const size_t ofs = openflow->size;
1913 const size_t ofpacts_len_left =
1914 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1915 const struct ofpact *action;
1916 const struct ofpact *processed = a;
1918 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1919 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
1920 if (ofpact_is_instruction(action)) {
1923 ofpact_to_openflow11(action, openflow);
1926 ofpacts_update_instruction_actions(openflow, ofs);
1932 /* Returns true if 'action' outputs to 'port', false otherwise. */
1934 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
1936 switch (ofpact->type) {
1938 return ofpact_get_OUTPUT(ofpact)->port == port;
1939 case OFPACT_ENQUEUE:
1940 return ofpact_get_ENQUEUE(ofpact)->port == port;
1941 case OFPACT_CONTROLLER:
1942 return port == OFPP_CONTROLLER;
1944 case OFPACT_OUTPUT_REG:
1946 case OFPACT_SET_VLAN_VID:
1947 case OFPACT_SET_VLAN_PCP:
1948 case OFPACT_STRIP_VLAN:
1949 case OFPACT_PUSH_VLAN:
1950 case OFPACT_SET_ETH_SRC:
1951 case OFPACT_SET_ETH_DST:
1952 case OFPACT_SET_IPV4_SRC:
1953 case OFPACT_SET_IPV4_DST:
1954 case OFPACT_SET_IPV4_DSCP:
1955 case OFPACT_SET_L4_SRC_PORT:
1956 case OFPACT_SET_L4_DST_PORT:
1957 case OFPACT_REG_MOVE:
1958 case OFPACT_REG_LOAD:
1959 case OFPACT_STACK_PUSH:
1960 case OFPACT_STACK_POP:
1961 case OFPACT_DEC_TTL:
1962 case OFPACT_SET_MPLS_TTL:
1963 case OFPACT_DEC_MPLS_TTL:
1964 case OFPACT_SET_TUNNEL:
1965 case OFPACT_WRITE_METADATA:
1966 case OFPACT_SET_QUEUE:
1967 case OFPACT_POP_QUEUE:
1968 case OFPACT_FIN_TIMEOUT:
1969 case OFPACT_RESUBMIT:
1971 case OFPACT_MULTIPATH:
1974 case OFPACT_PUSH_MPLS:
1975 case OFPACT_POP_MPLS:
1977 case OFPACT_CLEAR_ACTIONS:
1978 case OFPACT_GOTO_TABLE:
1984 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1985 * to 'port', false otherwise. */
1987 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1990 const struct ofpact *a;
1992 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1993 if (ofpact_outputs_to_port(a, port)) {
2002 ofpacts_equal(const struct ofpact *a, size_t a_len,
2003 const struct ofpact *b, size_t b_len)
2005 return a_len == b_len && !memcmp(a, b, a_len);
2008 /* Formatting ofpacts. */
2011 print_note(const struct ofpact_note *note, struct ds *string)
2015 ds_put_cstr(string, "note:");
2016 for (i = 0; i < note->length; i++) {
2018 ds_put_char(string, '.');
2020 ds_put_format(string, "%02"PRIx8, note->data[i]);
2025 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2030 ds_put_cstr(s, "dec_ttl");
2031 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2032 ds_put_cstr(s, "(");
2033 for (i = 0; i < ids->n_controllers; i++) {
2035 ds_put_cstr(s, ",");
2037 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2039 ds_put_cstr(s, ")");
2044 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2047 ds_put_cstr(s, "fin_timeout(");
2048 if (fin_timeout->fin_idle_timeout) {
2049 ds_put_format(s, "idle_timeout=%"PRIu16",",
2050 fin_timeout->fin_idle_timeout);
2052 if (fin_timeout->fin_hard_timeout) {
2053 ds_put_format(s, "hard_timeout=%"PRIu16",",
2054 fin_timeout->fin_hard_timeout);
2057 ds_put_char(s, ')');
2061 ofpact_format(const struct ofpact *a, struct ds *s)
2063 const struct ofpact_enqueue *enqueue;
2064 const struct ofpact_resubmit *resubmit;
2065 const struct ofpact_controller *controller;
2066 const struct ofpact_metadata *metadata;
2067 const struct ofpact_tunnel *tunnel;
2068 const struct ofpact_sample *sample;
2073 port = ofpact_get_OUTPUT(a)->port;
2074 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
2075 ds_put_format(s, "output:%"PRIu16, port);
2077 ofputil_format_port(port, s);
2078 if (port == OFPP_CONTROLLER) {
2079 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2084 case OFPACT_CONTROLLER:
2085 controller = ofpact_get_CONTROLLER(a);
2086 if (controller->reason == OFPR_ACTION &&
2087 controller->controller_id == 0) {
2088 ds_put_format(s, "CONTROLLER:%"PRIu16,
2089 ofpact_get_CONTROLLER(a)->max_len);
2091 enum ofp_packet_in_reason reason = controller->reason;
2093 ds_put_cstr(s, "controller(");
2094 if (reason != OFPR_ACTION) {
2095 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2097 ds_put_format(s, "reason=%s,",
2098 ofputil_packet_in_reason_to_string(
2099 reason, reasonbuf, sizeof reasonbuf));
2101 if (controller->max_len != UINT16_MAX) {
2102 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2104 if (controller->controller_id != 0) {
2105 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2108 ds_put_char(s, ')');
2112 case OFPACT_ENQUEUE:
2113 enqueue = ofpact_get_ENQUEUE(a);
2114 ds_put_format(s, "enqueue:");
2115 ofputil_format_port(enqueue->port, s);
2116 ds_put_format(s, "q%"PRIu32, enqueue->queue);
2119 case OFPACT_OUTPUT_REG:
2120 ds_put_cstr(s, "output:");
2121 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2125 bundle_format(ofpact_get_BUNDLE(a), s);
2128 case OFPACT_SET_VLAN_VID:
2129 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2130 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2133 case OFPACT_SET_VLAN_PCP:
2134 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2135 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2138 case OFPACT_STRIP_VLAN:
2139 ds_put_cstr(s, "strip_vlan");
2142 case OFPACT_PUSH_VLAN:
2143 /* XXX 802.1AD case*/
2144 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2147 case OFPACT_SET_ETH_SRC:
2148 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2149 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2152 case OFPACT_SET_ETH_DST:
2153 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2154 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2157 case OFPACT_SET_IPV4_SRC:
2158 ds_put_format(s, "mod_nw_src:"IP_FMT,
2159 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2162 case OFPACT_SET_IPV4_DST:
2163 ds_put_format(s, "mod_nw_dst:"IP_FMT,
2164 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2167 case OFPACT_SET_IPV4_DSCP:
2168 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
2171 case OFPACT_SET_L4_SRC_PORT:
2172 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2175 case OFPACT_SET_L4_DST_PORT:
2176 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2179 case OFPACT_REG_MOVE:
2180 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2183 case OFPACT_REG_LOAD:
2184 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2187 case OFPACT_STACK_PUSH:
2188 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2191 case OFPACT_STACK_POP:
2192 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2195 case OFPACT_DEC_TTL:
2196 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2199 case OFPACT_SET_MPLS_TTL:
2200 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2201 ofpact_get_SET_MPLS_TTL(a)->ttl);
2204 case OFPACT_DEC_MPLS_TTL:
2205 ds_put_cstr(s, "dec_mpls_ttl");
2208 case OFPACT_SET_TUNNEL:
2209 tunnel = ofpact_get_SET_TUNNEL(a);
2210 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2211 (tunnel->tun_id > UINT32_MAX
2212 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2216 case OFPACT_SET_QUEUE:
2217 ds_put_format(s, "set_queue:%"PRIu32,
2218 ofpact_get_SET_QUEUE(a)->queue_id);
2221 case OFPACT_POP_QUEUE:
2222 ds_put_cstr(s, "pop_queue");
2225 case OFPACT_FIN_TIMEOUT:
2226 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2229 case OFPACT_RESUBMIT:
2230 resubmit = ofpact_get_RESUBMIT(a);
2231 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2232 ds_put_cstr(s, "resubmit:");
2233 ofputil_format_port(resubmit->in_port, s);
2235 ds_put_format(s, "resubmit(");
2236 if (resubmit->in_port != OFPP_IN_PORT) {
2237 ofputil_format_port(resubmit->in_port, s);
2239 ds_put_char(s, ',');
2240 if (resubmit->table_id != 255) {
2241 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2243 ds_put_char(s, ')');
2248 learn_format(ofpact_get_LEARN(a), s);
2251 case OFPACT_MULTIPATH:
2252 multipath_format(ofpact_get_MULTIPATH(a), s);
2256 print_note(ofpact_get_NOTE(a), s);
2259 case OFPACT_PUSH_MPLS:
2260 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2261 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2264 case OFPACT_POP_MPLS:
2265 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2266 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2270 ds_put_cstr(s, "exit");
2274 sample = ofpact_get_SAMPLE(a);
2276 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2277 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2278 sample->probability, sample->collector_set_id,
2279 sample->obs_domain_id, sample->obs_point_id);
2282 case OFPACT_CLEAR_ACTIONS:
2283 ds_put_format(s, "%s",
2284 ofpact_instruction_name_from_type(
2285 OVSINST_OFPIT11_CLEAR_ACTIONS));
2288 case OFPACT_WRITE_METADATA:
2289 metadata = ofpact_get_WRITE_METADATA(a);
2290 ds_put_format(s, "%s:%#"PRIx64,
2291 ofpact_instruction_name_from_type(
2292 OVSINST_OFPIT11_WRITE_METADATA),
2293 ntohll(metadata->metadata));
2294 if (metadata->mask != htonll(UINT64_MAX)) {
2295 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2299 case OFPACT_GOTO_TABLE:
2300 ds_put_format(s, "%s:%"PRIu8,
2301 ofpact_instruction_name_from_type(
2302 OVSINST_OFPIT11_GOTO_TABLE),
2303 ofpact_get_GOTO_TABLE(a)->table_id);
2308 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2309 * 'ofpacts' to 'string'. */
2311 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2314 ds_put_cstr(string, "actions=");
2316 ds_put_cstr(string, "drop");
2318 const struct ofpact *a;
2320 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2322 ds_put_cstr(string, ",");
2325 /* XXX write-actions */
2326 ofpact_format(a, string);
2331 /* Internal use by helpers. */
2334 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2336 struct ofpact *ofpact;
2338 ofpact_pad(ofpacts);
2339 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2340 ofpact_init(ofpact, type, len);
2345 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2347 memset(ofpact, 0, len);
2348 ofpact->type = type;
2349 ofpact->compat = OFPUTIL_ACTION_INVALID;
2353 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2354 * starting at 'ofpact'.
2356 * This is the correct way to update a variable-length ofpact's length after
2357 * adding the variable-length part of the payload. (See the large comment
2358 * near the end of ofp-actions.h for more information.) */
2360 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2362 ovs_assert(ofpact == ofpacts->l2);
2363 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2366 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2367 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2368 * client must call this itself after adding the final ofpact to an array of
2371 * (The consequences of failing to call this function are probably not dire.
2372 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2373 * not dereference it. That's undefined behavior, technically, but it will not
2374 * cause a real problem on common systems. Still, it seems better to call
2377 ofpact_pad(struct ofpbuf *ofpacts)
2379 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2381 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2386 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2389 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2390 load->dst.field = mf;
2392 load->dst.n_bits = mf->n_bits;
2393 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2394 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);