2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
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"
21 #include "byte-order.h"
23 #include "dynamic-string.h"
25 #include "meta-flow.h"
26 #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 ofp_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 ofp_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 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
141 struct ofpact_note *note;
144 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
145 note = ofpact_put(out, OFPACT_NOTE,
146 offsetof(struct ofpact_note, data) + length);
147 note->length = length;
148 memcpy(note->data, nan->note, length);
152 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
154 const struct nx_action_header *nah = (const struct nx_action_header *) a;
155 uint16_t len = ntohs(a->header.len);
157 if (len < sizeof(struct nx_action_header)) {
158 return OFPERR_OFPBAC_BAD_LEN;
159 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
160 return OFPERR_OFPBAC_BAD_VENDOR;
163 switch (nah->subtype) {
164 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
165 case CONSTANT_HTONS(ENUM): \
167 ? len >= sizeof(struct STRUCT) \
168 : len == sizeof(struct STRUCT)) { \
169 *code = OFPUTIL_##ENUM; \
172 return OFPERR_OFPBAC_BAD_LEN; \
175 #include "ofp-util.def"
177 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
178 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
180 return OFPERR_OFPBAC_BAD_TYPE;
184 /* Parses 'a' to determine its type. On success stores the correct type into
185 * '*code' and returns 0. On failure returns an OFPERR_* error code and
186 * '*code' is indeterminate.
188 * The caller must have already verified that 'a''s length is potentially
189 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
190 * ofp_action) and no longer than the amount of space allocated to 'a').
192 * This function verifies that 'a''s length is correct for the type of action
193 * that it represents. */
195 decode_openflow10_action(const union ofp_action *a,
196 enum ofputil_action_code *code)
199 case CONSTANT_HTONS(OFPAT10_VENDOR):
200 return decode_nxast_action(a, code);
202 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
203 case CONSTANT_HTONS(ENUM): \
204 if (a->header.len == htons(sizeof(struct STRUCT))) { \
205 *code = OFPUTIL_##ENUM; \
208 return OFPERR_OFPBAC_BAD_LEN; \
211 #include "ofp-util.def"
214 return OFPERR_OFPBAC_BAD_TYPE;
219 ofpact_from_openflow10__(const union ofp_action *a, struct ofpbuf *out)
221 const struct nx_action_resubmit *nar;
222 const struct nx_action_set_tunnel *nast;
223 const struct nx_action_set_queue *nasq;
224 const struct nx_action_note *nan;
225 const struct nx_action_set_tunnel64 *nast64;
226 struct ofpact_tunnel *tunnel;
227 enum ofputil_action_code code;
230 error = decode_openflow10_action(a, &code);
236 case OFPUTIL_ACTION_INVALID:
239 case OFPUTIL_OFPAT10_OUTPUT:
240 return output_from_openflow10((const struct ofp_action_output *) a,
243 case OFPUTIL_OFPAT10_SET_VLAN_VID:
244 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
245 return OFPERR_OFPBAC_BAD_ARGUMENT;
247 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
250 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
251 if (a->vlan_pcp.vlan_pcp & ~7) {
252 return OFPERR_OFPBAC_BAD_ARGUMENT;
254 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
257 case OFPUTIL_OFPAT10_STRIP_VLAN:
258 ofpact_put_STRIP_VLAN(out);
261 case OFPUTIL_OFPAT10_SET_DL_SRC:
262 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
263 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
266 case OFPUTIL_OFPAT10_SET_DL_DST:
267 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
268 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
271 case OFPUTIL_OFPAT10_SET_NW_SRC:
272 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
275 case OFPUTIL_OFPAT10_SET_NW_DST:
276 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
279 case OFPUTIL_OFPAT10_SET_NW_TOS:
280 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
281 return OFPERR_OFPBAC_BAD_ARGUMENT;
283 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
286 case OFPUTIL_OFPAT10_SET_TP_SRC:
287 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
290 case OFPUTIL_OFPAT10_SET_TP_DST:
291 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
295 case OFPUTIL_OFPAT10_ENQUEUE:
296 error = enqueue_from_openflow10((const struct ofp_action_enqueue *) a,
300 case OFPUTIL_NXAST_RESUBMIT:
301 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
304 case OFPUTIL_NXAST_SET_TUNNEL:
305 nast = (const struct nx_action_set_tunnel *) a;
306 tunnel = ofpact_put_SET_TUNNEL(out);
307 tunnel->ofpact.compat = code;
308 tunnel->tun_id = ntohl(nast->tun_id);
311 case OFPUTIL_NXAST_SET_QUEUE:
312 nasq = (const struct nx_action_set_queue *) a;
313 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
316 case OFPUTIL_NXAST_POP_QUEUE:
317 ofpact_put_POP_QUEUE(out);
320 case OFPUTIL_NXAST_REG_MOVE:
321 error = nxm_reg_move_from_openflow(
322 (const struct nx_action_reg_move *) a, out);
325 case OFPUTIL_NXAST_REG_LOAD:
326 error = nxm_reg_load_from_openflow(
327 (const struct nx_action_reg_load *) a, out);
330 case OFPUTIL_NXAST_NOTE:
331 nan = (const struct nx_action_note *) a;
332 note_from_openflow(nan, out);
335 case OFPUTIL_NXAST_SET_TUNNEL64:
336 nast64 = (const struct nx_action_set_tunnel64 *) a;
337 tunnel = ofpact_put_SET_TUNNEL(out);
338 tunnel->ofpact.compat = code;
339 tunnel->tun_id = ntohll(nast64->tun_id);
342 case OFPUTIL_NXAST_MULTIPATH:
343 error = multipath_from_openflow((const struct nx_action_multipath *) a,
344 ofpact_put_MULTIPATH(out));
347 case OFPUTIL_NXAST_AUTOPATH:
348 error = autopath_from_openflow((const struct nx_action_autopath *) a,
349 ofpact_put_AUTOPATH(out));
352 case OFPUTIL_NXAST_BUNDLE:
353 case OFPUTIL_NXAST_BUNDLE_LOAD:
354 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
357 case OFPUTIL_NXAST_OUTPUT_REG:
358 error = output_reg_from_openflow(
359 (const struct nx_action_output_reg *) a, out);
362 case OFPUTIL_NXAST_RESUBMIT_TABLE:
363 nar = (const struct nx_action_resubmit *) a;
364 error = resubmit_table_from_openflow(nar, out);
367 case OFPUTIL_NXAST_LEARN:
368 error = learn_from_openflow((const struct nx_action_learn *) a, out);
371 case OFPUTIL_NXAST_EXIT:
372 ofpact_put_EXIT(out);
375 case OFPUTIL_NXAST_DEC_TTL:
376 ofpact_put_DEC_TTL(out);
379 case OFPUTIL_NXAST_FIN_TIMEOUT:
380 fin_timeout_from_openflow(
381 (const struct nx_action_fin_timeout *) a, out);
384 case OFPUTIL_NXAST_CONTROLLER:
385 controller_from_openflow((const struct nx_action_controller *) a, out);
392 static inline union ofp_action *
393 action_next(const union ofp_action *a)
395 return ((union ofp_action *) (void *)
396 ((uint8_t *) a + ntohs(a->header.len)));
400 action_is_valid(const union ofp_action *a, size_t n_actions)
402 uint16_t len = ntohs(a->header.len);
403 return (!(len % OFP_ACTION_ALIGN)
405 && len / sizeof *a <= n_actions);
408 /* This macro is careful to check for actions with bad lengths. */
409 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
410 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
411 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
412 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
413 (ITER) = action_next(ITER)))
416 ofpact_from_openflow10(const union ofp_action *in, size_t n_in,
419 const union ofp_action *a;
422 ACTION_FOR_EACH (a, left, in, n_in) {
423 enum ofperr error = ofpact_from_openflow10__(a, out);
425 VLOG_WARN_RL(&rl, "bad action at offset %td (%s)",
426 (a - in) * sizeof *a, ofperr_get_name(error));
431 VLOG_WARN_RL(&rl, "bad action format at offset %zu",
432 (n_in - left) * sizeof *a);
433 return OFPERR_OFPBAC_BAD_LEN;
440 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
441 * of 'openflow' into ofpacts. On success, replaces any existing content in
442 * 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'. Returns 0
443 * if successful, otherwise an OpenFlow error.
445 * This function does not check that the actions are valid in a given context.
446 * The caller should do so, with ofpacts_check(). */
448 ofpacts_pull_openflow(struct ofpbuf *openflow, unsigned int actions_len,
449 struct ofpbuf *ofpacts)
451 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
452 const union ofp_action *actions;
455 ofpbuf_clear(ofpacts);
457 if (actions_len % OFP_ACTION_ALIGN != 0) {
458 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
459 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
460 return OFPERR_OFPBRC_BAD_LEN;
463 actions = ofpbuf_try_pull(openflow, actions_len);
464 if (actions == NULL) {
465 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
466 "remaining message length (%zu)",
467 actions_len, openflow->size);
468 return OFPERR_OFPBRC_BAD_LEN;
471 error = ofpact_from_openflow10(actions, actions_len / OFP_ACTION_ALIGN,
474 ofpbuf_clear(ofpacts);
480 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
482 const struct ofpact_enqueue *enqueue;
486 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
489 case OFPACT_CONTROLLER:
493 enqueue = ofpact_get_ENQUEUE(a);
494 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
495 && enqueue->port != OFPP_LOCAL) {
496 return OFPERR_OFPBAC_BAD_OUT_PORT;
500 case OFPACT_OUTPUT_REG:
501 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
504 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
506 case OFPACT_SET_VLAN_VID:
507 case OFPACT_SET_VLAN_PCP:
508 case OFPACT_STRIP_VLAN:
509 case OFPACT_SET_ETH_SRC:
510 case OFPACT_SET_ETH_DST:
511 case OFPACT_SET_IPV4_SRC:
512 case OFPACT_SET_IPV4_DST:
513 case OFPACT_SET_IPV4_DSCP:
514 case OFPACT_SET_L4_SRC_PORT:
515 case OFPACT_SET_L4_DST_PORT:
518 case OFPACT_REG_MOVE:
519 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
521 case OFPACT_REG_LOAD:
522 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
525 case OFPACT_SET_TUNNEL:
526 case OFPACT_SET_QUEUE:
527 case OFPACT_POP_QUEUE:
528 case OFPACT_FIN_TIMEOUT:
529 case OFPACT_RESUBMIT:
533 return learn_check(ofpact_get_LEARN(a), flow);
535 case OFPACT_MULTIPATH:
536 return multipath_check(ofpact_get_MULTIPATH(a), flow);
538 case OFPACT_AUTOPATH:
539 return autopath_check(ofpact_get_AUTOPATH(a), flow);
550 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
551 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
552 * switch with no more than 'max_ports' ports. */
554 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
555 const struct flow *flow, int max_ports)
557 const struct ofpact *a;
559 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
560 enum ofperr error = ofpact_check__(a, flow, max_ports);
569 /* Converting ofpacts to Nicira OpenFlow extensions. */
572 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
575 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
577 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
578 output_reg->src.n_bits);
579 naor->src = htonl(output_reg->src.field->nxm_header);
580 naor->max_len = htons(output_reg->max_len);
584 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
587 struct nx_action_resubmit *nar;
589 if (resubmit->table_id == 0xff
590 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
591 nar = ofputil_put_NXAST_RESUBMIT(out);
593 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
594 nar->table = resubmit->table_id;
596 nar->in_port = htons(resubmit->in_port);
600 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
603 uint64_t tun_id = tunnel->tun_id;
605 if (tun_id <= UINT32_MAX
606 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
607 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
609 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
614 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
616 size_t start_ofs = out->size;
617 struct nx_action_note *nan;
618 unsigned int remainder;
621 nan = ofputil_put_NXAST_NOTE(out);
622 out->size -= sizeof nan->note;
624 ofpbuf_put(out, note->data, note->length);
626 len = out->size - start_ofs;
627 remainder = len % OFP_ACTION_ALIGN;
629 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
631 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
632 nan->len = htons(out->size - start_ofs);
636 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
639 struct nx_action_controller *nac;
641 nac = ofputil_put_NXAST_CONTROLLER(out);
642 nac->max_len = htons(oc->max_len);
643 nac->controller_id = htons(oc->controller_id);
644 nac->reason = oc->reason;
648 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
651 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
652 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
653 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
657 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
660 case OFPACT_CONTROLLER:
661 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
664 case OFPACT_OUTPUT_REG:
665 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
669 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
672 case OFPACT_REG_MOVE:
673 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
676 case OFPACT_REG_LOAD:
677 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
681 ofputil_put_NXAST_DEC_TTL(out);
684 case OFPACT_SET_TUNNEL:
685 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
688 case OFPACT_SET_QUEUE:
689 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
690 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
693 case OFPACT_POP_QUEUE:
694 ofputil_put_NXAST_POP_QUEUE(out);
697 case OFPACT_FIN_TIMEOUT:
698 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
701 case OFPACT_RESUBMIT:
702 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
706 learn_to_nxast(ofpact_get_LEARN(a), out);
709 case OFPACT_MULTIPATH:
710 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
713 case OFPACT_AUTOPATH:
714 autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
718 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
722 ofputil_put_NXAST_EXIT(out);
727 case OFPACT_SET_VLAN_VID:
728 case OFPACT_SET_VLAN_PCP:
729 case OFPACT_STRIP_VLAN:
730 case OFPACT_SET_ETH_SRC:
731 case OFPACT_SET_ETH_DST:
732 case OFPACT_SET_IPV4_SRC:
733 case OFPACT_SET_IPV4_DST:
734 case OFPACT_SET_IPV4_DSCP:
735 case OFPACT_SET_L4_SRC_PORT:
736 case OFPACT_SET_L4_DST_PORT:
741 /* Converting ofpacts to OpenFlow 1.0. */
744 ofpact_output_to_openflow10(const struct ofpact_output *output,
747 struct ofp_action_output *oao;
749 oao = ofputil_put_OFPAT10_OUTPUT(out);
750 oao->port = htons(output->port);
751 oao->max_len = htons(output->max_len);
755 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
758 struct ofp_action_enqueue *oae;
760 oae = ofputil_put_OFPAT10_ENQUEUE(out);
761 oae->port = htons(enqueue->port);
762 oae->queue_id = htonl(enqueue->queue);
766 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
770 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
774 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
777 case OFPACT_SET_VLAN_VID:
778 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
779 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
782 case OFPACT_SET_VLAN_PCP:
783 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
784 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
787 case OFPACT_STRIP_VLAN:
788 ofputil_put_OFPAT10_STRIP_VLAN(out);
791 case OFPACT_SET_ETH_SRC:
792 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
793 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
796 case OFPACT_SET_ETH_DST:
797 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
798 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
801 case OFPACT_SET_IPV4_SRC:
802 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
803 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
806 case OFPACT_SET_IPV4_DST:
807 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
808 = ofpact_get_SET_IPV4_DST(a)->ipv4;
811 case OFPACT_SET_IPV4_DSCP:
812 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
813 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
816 case OFPACT_SET_L4_SRC_PORT:
817 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
818 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
821 case OFPACT_SET_L4_DST_PORT:
822 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
823 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
826 case OFPACT_CONTROLLER:
827 case OFPACT_OUTPUT_REG:
829 case OFPACT_REG_MOVE:
830 case OFPACT_REG_LOAD:
832 case OFPACT_SET_TUNNEL:
833 case OFPACT_SET_QUEUE:
834 case OFPACT_POP_QUEUE:
835 case OFPACT_FIN_TIMEOUT:
836 case OFPACT_RESUBMIT:
838 case OFPACT_MULTIPATH:
839 case OFPACT_AUTOPATH:
842 ofpact_to_nxast(a, out);
847 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
848 * actions in 'openflow', appending the actions to any existing data in
851 ofpacts_to_openflow(const struct ofpact ofpacts[], size_t ofpacts_len,
852 struct ofpbuf *openflow)
854 const struct ofpact *a;
856 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
857 ofpact_to_openflow10(a, openflow);
861 /* Returns true if 'action' outputs to 'port', false otherwise. */
863 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
865 switch (ofpact->type) {
867 return ofpact_get_OUTPUT(ofpact)->port == port;
869 return ofpact_get_ENQUEUE(ofpact)->port == port;
870 case OFPACT_CONTROLLER:
871 return port == OFPP_CONTROLLER;
873 case OFPACT_OUTPUT_REG:
875 case OFPACT_SET_VLAN_VID:
876 case OFPACT_SET_VLAN_PCP:
877 case OFPACT_STRIP_VLAN:
878 case OFPACT_SET_ETH_SRC:
879 case OFPACT_SET_ETH_DST:
880 case OFPACT_SET_IPV4_SRC:
881 case OFPACT_SET_IPV4_DST:
882 case OFPACT_SET_IPV4_DSCP:
883 case OFPACT_SET_L4_SRC_PORT:
884 case OFPACT_SET_L4_DST_PORT:
885 case OFPACT_REG_MOVE:
886 case OFPACT_REG_LOAD:
888 case OFPACT_SET_TUNNEL:
889 case OFPACT_SET_QUEUE:
890 case OFPACT_POP_QUEUE:
891 case OFPACT_FIN_TIMEOUT:
892 case OFPACT_RESUBMIT:
894 case OFPACT_MULTIPATH:
895 case OFPACT_AUTOPATH:
903 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
904 * to 'port', false otherwise. */
906 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
909 const struct ofpact *a;
911 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
912 if (ofpact_outputs_to_port(a, port)) {
921 ofpacts_equal(const struct ofpact *a, size_t a_len,
922 const struct ofpact *b, size_t b_len)
924 return a_len == b_len && !memcmp(a, b, a_len);
927 /* Formatting ofpacts. */
930 print_note(const struct ofpact_note *note, struct ds *string)
934 ds_put_cstr(string, "note:");
935 for (i = 0; i < note->length; i++) {
937 ds_put_char(string, '.');
939 ds_put_format(string, "%02"PRIx8, note->data[i]);
944 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
947 ds_put_cstr(s, "fin_timeout(");
948 if (fin_timeout->fin_idle_timeout) {
949 ds_put_format(s, "idle_timeout=%"PRIu16",",
950 fin_timeout->fin_idle_timeout);
952 if (fin_timeout->fin_hard_timeout) {
953 ds_put_format(s, "hard_timeout=%"PRIu16",",
954 fin_timeout->fin_hard_timeout);
961 ofpact_format(const struct ofpact *a, struct ds *s)
963 const struct ofpact_enqueue *enqueue;
964 const struct ofpact_resubmit *resubmit;
965 const struct ofpact_autopath *autopath;
966 const struct ofpact_controller *controller;
967 const struct ofpact_tunnel *tunnel;
972 port = ofpact_get_OUTPUT(a)->port;
973 if (port < OFPP_MAX) {
974 ds_put_format(s, "output:%"PRIu16, port);
976 ofputil_format_port(port, s);
977 if (port == OFPP_CONTROLLER) {
978 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
983 case OFPACT_CONTROLLER:
984 controller = ofpact_get_CONTROLLER(a);
985 if (controller->reason == OFPR_ACTION &&
986 controller->controller_id == 0) {
987 ds_put_format(s, "CONTROLLER:%"PRIu16,
988 ofpact_get_CONTROLLER(a)->max_len);
990 enum ofp_packet_in_reason reason = controller->reason;
992 ds_put_cstr(s, "controller(");
993 if (reason != OFPR_ACTION) {
994 ds_put_format(s, "reason=%s,",
995 ofputil_packet_in_reason_to_string(reason));
997 if (controller->max_len != UINT16_MAX) {
998 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1000 if (controller->controller_id != 0) {
1001 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1004 ds_put_char(s, ')');
1008 case OFPACT_ENQUEUE:
1009 enqueue = ofpact_get_ENQUEUE(a);
1010 ds_put_format(s, "enqueue:");
1011 ofputil_format_port(enqueue->port, s);
1012 ds_put_format(s, "q%"PRIu32, enqueue->queue);
1015 case OFPACT_OUTPUT_REG:
1016 ds_put_cstr(s, "output:");
1017 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1021 bundle_format(ofpact_get_BUNDLE(a), s);
1024 case OFPACT_SET_VLAN_VID:
1025 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1026 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1029 case OFPACT_SET_VLAN_PCP:
1030 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1031 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1034 case OFPACT_STRIP_VLAN:
1035 ds_put_cstr(s, "strip_vlan");
1038 case OFPACT_SET_ETH_SRC:
1039 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1040 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1043 case OFPACT_SET_ETH_DST:
1044 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1045 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1048 case OFPACT_SET_IPV4_SRC:
1049 ds_put_format(s, "mod_nw_src:"IP_FMT,
1050 IP_ARGS(&ofpact_get_SET_IPV4_SRC(a)->ipv4));
1053 case OFPACT_SET_IPV4_DST:
1054 ds_put_format(s, "mod_nw_dst:"IP_FMT,
1055 IP_ARGS(&ofpact_get_SET_IPV4_DST(a)->ipv4));
1058 case OFPACT_SET_IPV4_DSCP:
1059 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1062 case OFPACT_SET_L4_SRC_PORT:
1063 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1066 case OFPACT_SET_L4_DST_PORT:
1067 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1070 case OFPACT_REG_MOVE:
1071 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1074 case OFPACT_REG_LOAD:
1075 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1078 case OFPACT_DEC_TTL:
1079 ds_put_cstr(s, "dec_ttl");
1082 case OFPACT_SET_TUNNEL:
1083 tunnel = ofpact_get_SET_TUNNEL(a);
1084 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1085 (tunnel->tun_id > UINT32_MAX
1086 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1090 case OFPACT_SET_QUEUE:
1091 ds_put_format(s, "set_queue:%"PRIu32,
1092 ofpact_get_SET_QUEUE(a)->queue_id);
1095 case OFPACT_POP_QUEUE:
1096 ds_put_cstr(s, "pop_queue");
1099 case OFPACT_FIN_TIMEOUT:
1100 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
1103 case OFPACT_RESUBMIT:
1104 resubmit = ofpact_get_RESUBMIT(a);
1105 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
1106 ds_put_format(s, "resubmit:%"PRIu16, resubmit->in_port);
1108 ds_put_format(s, "resubmit(");
1109 if (resubmit->in_port != OFPP_IN_PORT) {
1110 ofputil_format_port(resubmit->in_port, s);
1112 ds_put_char(s, ',');
1113 if (resubmit->table_id != 255) {
1114 ds_put_format(s, "%"PRIu8, resubmit->table_id);
1116 ds_put_char(s, ')');
1121 learn_format(ofpact_get_LEARN(a), s);
1124 case OFPACT_MULTIPATH:
1125 multipath_format(ofpact_get_MULTIPATH(a), s);
1128 case OFPACT_AUTOPATH:
1129 autopath = ofpact_get_AUTOPATH(a);
1130 ds_put_format(s, "autopath(%u,", autopath->port);
1131 mf_format_subfield(&autopath->dst, s);
1132 ds_put_char(s, ')');
1136 print_note(ofpact_get_NOTE(a), s);
1140 ds_put_cstr(s, "exit");
1145 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
1146 * 'ofpacts' to 'string'. */
1148 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
1151 ds_put_cstr(string, "actions=");
1153 ds_put_cstr(string, "drop");
1155 const struct ofpact *a;
1157 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1159 ds_put_cstr(string, ",");
1161 ofpact_format(a, string);
1166 /* Internal use by helpers. */
1169 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
1171 struct ofpact *ofpact;
1173 ofpact_pad(ofpacts);
1174 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
1175 ofpact_init(ofpact, type, len);
1180 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
1182 memset(ofpact, 0, len);
1183 ofpact->type = type;
1184 ofpact->compat = OFPUTIL_ACTION_INVALID;
1188 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
1189 * starting at 'ofpact'.
1191 * This is the correct way to update a variable-length ofpact's length after
1192 * adding the variable-length part of the payload. (See the large comment
1193 * near the end of ofp-actions.h for more information.) */
1195 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
1197 assert(ofpact == ofpacts->l2);
1198 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
1201 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
1202 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
1203 * client must call this itself after adding the final ofpact to an array of
1206 * (The consequences of failing to call this function are probably not dire.
1207 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
1208 * not dereference it. That's undefined behavior, technically, but it will not
1209 * cause a real problem on common systems. Still, it seems better to call
1212 ofpact_pad(struct ofpbuf *ofpacts)
1214 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
1216 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);