2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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"
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 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 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
223 const struct nx_action_header *nah = (const struct nx_action_header *) a;
224 uint16_t len = ntohs(a->header.len);
226 if (len < sizeof(struct nx_action_header)) {
227 return OFPERR_OFPBAC_BAD_LEN;
228 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
229 return OFPERR_OFPBAC_BAD_VENDOR;
232 switch (nah->subtype) {
233 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
234 case CONSTANT_HTONS(ENUM): \
236 ? len >= sizeof(struct STRUCT) \
237 : len == sizeof(struct STRUCT)) { \
238 *code = OFPUTIL_##ENUM; \
241 return OFPERR_OFPBAC_BAD_LEN; \
244 #include "ofp-util.def"
246 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
247 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
249 return OFPERR_OFPBAC_BAD_TYPE;
253 /* Parses 'a' to determine its type. On success stores the correct type into
254 * '*code' and returns 0. On failure returns an OFPERR_* error code and
255 * '*code' is indeterminate.
257 * The caller must have already verified that 'a''s length is potentially
258 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
259 * ofp_action) and no longer than the amount of space allocated to 'a').
261 * This function verifies that 'a''s length is correct for the type of action
262 * that it represents. */
264 decode_openflow10_action(const union ofp_action *a,
265 enum ofputil_action_code *code)
268 case CONSTANT_HTONS(OFPAT10_VENDOR):
269 return decode_nxast_action(a, code);
271 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
272 case CONSTANT_HTONS(ENUM): \
273 if (a->header.len == htons(sizeof(struct STRUCT))) { \
274 *code = OFPUTIL_##ENUM; \
277 return OFPERR_OFPBAC_BAD_LEN; \
280 #include "ofp-util.def"
283 return OFPERR_OFPBAC_BAD_TYPE;
288 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
291 const struct nx_action_resubmit *nar;
292 const struct nx_action_set_tunnel *nast;
293 const struct nx_action_set_queue *nasq;
294 const struct nx_action_note *nan;
295 const struct nx_action_set_tunnel64 *nast64;
296 const struct nx_action_write_metadata *nawm;
297 struct ofpact_tunnel *tunnel;
298 enum ofperr error = 0;
301 case OFPUTIL_ACTION_INVALID:
302 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
303 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
304 #include "ofp-util.def"
307 case OFPUTIL_NXAST_RESUBMIT:
308 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
311 case OFPUTIL_NXAST_SET_TUNNEL:
312 nast = (const struct nx_action_set_tunnel *) a;
313 tunnel = ofpact_put_SET_TUNNEL(out);
314 tunnel->ofpact.compat = code;
315 tunnel->tun_id = ntohl(nast->tun_id);
318 case OFPUTIL_NXAST_WRITE_METADATA:
319 nawm = (const struct nx_action_write_metadata *) a;
320 error = metadata_from_nxast(nawm, out);
323 case OFPUTIL_NXAST_SET_QUEUE:
324 nasq = (const struct nx_action_set_queue *) a;
325 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
328 case OFPUTIL_NXAST_POP_QUEUE:
329 ofpact_put_POP_QUEUE(out);
332 case OFPUTIL_NXAST_REG_MOVE:
333 error = nxm_reg_move_from_openflow(
334 (const struct nx_action_reg_move *) a, out);
337 case OFPUTIL_NXAST_REG_LOAD:
338 error = nxm_reg_load_from_openflow(
339 (const struct nx_action_reg_load *) a, out);
342 case OFPUTIL_NXAST_NOTE:
343 nan = (const struct nx_action_note *) a;
344 note_from_openflow(nan, out);
347 case OFPUTIL_NXAST_SET_TUNNEL64:
348 nast64 = (const struct nx_action_set_tunnel64 *) a;
349 tunnel = ofpact_put_SET_TUNNEL(out);
350 tunnel->ofpact.compat = code;
351 tunnel->tun_id = ntohll(nast64->tun_id);
354 case OFPUTIL_NXAST_MULTIPATH:
355 error = multipath_from_openflow((const struct nx_action_multipath *) a,
356 ofpact_put_MULTIPATH(out));
359 case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
360 error = autopath_from_openflow((const struct nx_action_autopath *) a,
361 ofpact_put_AUTOPATH(out));
364 case OFPUTIL_NXAST_BUNDLE:
365 case OFPUTIL_NXAST_BUNDLE_LOAD:
366 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
369 case OFPUTIL_NXAST_OUTPUT_REG:
370 error = output_reg_from_openflow(
371 (const struct nx_action_output_reg *) a, out);
374 case OFPUTIL_NXAST_RESUBMIT_TABLE:
375 nar = (const struct nx_action_resubmit *) a;
376 error = resubmit_table_from_openflow(nar, out);
379 case OFPUTIL_NXAST_LEARN:
380 error = learn_from_openflow((const struct nx_action_learn *) a, out);
383 case OFPUTIL_NXAST_EXIT:
384 ofpact_put_EXIT(out);
387 case OFPUTIL_NXAST_DEC_TTL:
388 error = dec_ttl_from_openflow(out, code);
391 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
392 error = dec_ttl_cnt_ids_from_openflow(
393 (const struct nx_action_cnt_ids *) a, out);
396 case OFPUTIL_NXAST_FIN_TIMEOUT:
397 fin_timeout_from_openflow(
398 (const struct nx_action_fin_timeout *) a, out);
401 case OFPUTIL_NXAST_CONTROLLER:
402 controller_from_openflow((const struct nx_action_controller *) a, out);
410 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
412 enum ofputil_action_code code;
415 error = decode_openflow10_action(a, &code);
421 case OFPUTIL_ACTION_INVALID:
422 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
423 #include "ofp-util.def"
426 case OFPUTIL_OFPAT10_OUTPUT:
427 return output_from_openflow10(&a->output10, out);
429 case OFPUTIL_OFPAT10_SET_VLAN_VID:
430 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
431 return OFPERR_OFPBAC_BAD_ARGUMENT;
433 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
436 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
437 if (a->vlan_pcp.vlan_pcp & ~7) {
438 return OFPERR_OFPBAC_BAD_ARGUMENT;
440 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
443 case OFPUTIL_OFPAT10_STRIP_VLAN:
444 ofpact_put_STRIP_VLAN(out);
447 case OFPUTIL_OFPAT10_SET_DL_SRC:
448 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
449 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
452 case OFPUTIL_OFPAT10_SET_DL_DST:
453 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
454 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
457 case OFPUTIL_OFPAT10_SET_NW_SRC:
458 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
461 case OFPUTIL_OFPAT10_SET_NW_DST:
462 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
465 case OFPUTIL_OFPAT10_SET_NW_TOS:
466 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
467 return OFPERR_OFPBAC_BAD_ARGUMENT;
469 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
472 case OFPUTIL_OFPAT10_SET_TP_SRC:
473 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
476 case OFPUTIL_OFPAT10_SET_TP_DST:
477 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
481 case OFPUTIL_OFPAT10_ENQUEUE:
482 error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
486 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
487 #include "ofp-util.def"
488 return ofpact_from_nxast(a, code, out);
494 static inline union ofp_action *
495 action_next(const union ofp_action *a)
497 return ((union ofp_action *) (void *)
498 ((uint8_t *) a + ntohs(a->header.len)));
502 action_is_valid(const union ofp_action *a, size_t n_actions)
504 uint16_t len = ntohs(a->header.len);
505 return (!(len % OFP_ACTION_ALIGN)
507 && len / sizeof *a <= n_actions);
510 /* This macro is careful to check for actions with bad lengths. */
511 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
512 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
513 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
514 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
515 (ITER) = action_next(ITER)))
518 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
521 if (!VLOG_DROP_WARN(&rl)) {
525 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
526 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
527 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
533 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
535 enum ofperr (*ofpact_from_openflow)(
536 const union ofp_action *a, struct ofpbuf *out))
538 const union ofp_action *a;
541 ACTION_FOR_EACH (a, left, in, n_in) {
542 enum ofperr error = ofpact_from_openflow(a, out);
544 log_bad_action(in, n_in, a - in, error);
549 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
550 log_bad_action(in, n_in, n_in - left, error);
559 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
562 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
566 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
567 struct ofpbuf *ofpacts,
568 enum ofperr (*translate)(const union ofp_action *actions,
570 struct ofpbuf *ofpacts))
572 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
573 const union ofp_action *actions;
576 ofpbuf_clear(ofpacts);
578 if (actions_len % OFP_ACTION_ALIGN != 0) {
579 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
580 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
581 return OFPERR_OFPBRC_BAD_LEN;
584 actions = ofpbuf_try_pull(openflow, actions_len);
585 if (actions == NULL) {
586 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
587 "remaining message length (%zu)",
588 actions_len, openflow->size);
589 return OFPERR_OFPBRC_BAD_LEN;
592 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
594 ofpbuf_clear(ofpacts);
598 error = ofpacts_verify(ofpacts->data, ofpacts->size);
600 ofpbuf_clear(ofpacts);
605 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
606 * front of 'openflow' into ofpacts. On success, replaces any existing content
607 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
608 * Returns 0 if successful, otherwise an OpenFlow error.
610 * The parsed actions are valid generically, but they may not be valid in a
611 * specific context. For example, port numbers up to OFPP_MAX are valid
612 * generically, but specific datapaths may only support port numbers in a
613 * smaller range. Use ofpacts_check() to additional check whether actions are
614 * valid in a specific context. */
616 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
617 struct ofpbuf *ofpacts)
619 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
620 ofpacts_from_openflow10);
623 /* OpenFlow 1.1 actions. */
625 /* Parses 'a' to determine its type. On success stores the correct type into
626 * '*code' and returns 0. On failure returns an OFPERR_* error code and
627 * '*code' is indeterminate.
629 * The caller must have already verified that 'a''s length is potentially
630 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
631 * ofp_action) and no longer than the amount of space allocated to 'a').
633 * This function verifies that 'a''s length is correct for the type of action
634 * that it represents. */
636 decode_openflow11_action(const union ofp_action *a,
637 enum ofputil_action_code *code)
642 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
643 return decode_nxast_action(a, code);
645 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
646 case CONSTANT_HTONS(ENUM): \
647 len = ntohs(a->header.len); \
649 ? len >= sizeof(struct STRUCT) \
650 : len == sizeof(struct STRUCT)) { \
651 *code = OFPUTIL_##ENUM; \
654 return OFPERR_OFPBAC_BAD_LEN; \
657 #include "ofp-util.def"
660 return OFPERR_OFPBAC_BAD_TYPE;
665 output_from_openflow11(const struct ofp11_action_output *oao,
668 struct ofpact_output *output;
671 output = ofpact_put_OUTPUT(out);
672 output->max_len = ntohs(oao->max_len);
674 error = ofputil_port_from_ofp11(oao->port, &output->port);
679 return ofputil_check_output_port(output->port, OFPP_MAX);
683 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
685 enum ofputil_action_code code;
688 error = decode_openflow11_action(a, &code);
694 case OFPUTIL_ACTION_INVALID:
695 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
696 #include "ofp-util.def"
699 case OFPUTIL_OFPAT11_OUTPUT:
700 return output_from_openflow11((const struct ofp11_action_output *) a,
703 case OFPUTIL_OFPAT11_SET_VLAN_VID:
704 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
705 return OFPERR_OFPBAC_BAD_ARGUMENT;
707 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
710 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
711 if (a->vlan_pcp.vlan_pcp & ~7) {
712 return OFPERR_OFPBAC_BAD_ARGUMENT;
714 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
717 case OFPUTIL_OFPAT11_PUSH_VLAN:
718 if (((const struct ofp11_action_push *)a)->ethertype !=
719 htons(ETH_TYPE_VLAN_8021Q)) {
720 /* TODO:XXX 802.1AD(QinQ) isn't supported at the moment */
721 return OFPERR_OFPET_BAD_ACTION;
723 ofpact_put_PUSH_VLAN(out);
726 case OFPUTIL_OFPAT11_POP_VLAN:
727 ofpact_put_STRIP_VLAN(out);
730 case OFPUTIL_OFPAT11_SET_QUEUE:
731 ofpact_put_SET_QUEUE(out)->queue_id =
732 ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
735 case OFPUTIL_OFPAT11_SET_DL_SRC:
736 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
737 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
740 case OFPUTIL_OFPAT11_SET_DL_DST:
741 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
742 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
745 case OFPUTIL_OFPAT11_DEC_NW_TTL:
746 dec_ttl_from_openflow(out, code);
749 case OFPUTIL_OFPAT11_SET_NW_SRC:
750 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
753 case OFPUTIL_OFPAT11_SET_NW_DST:
754 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
757 case OFPUTIL_OFPAT11_SET_NW_TOS:
758 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
759 return OFPERR_OFPBAC_BAD_ARGUMENT;
761 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
764 case OFPUTIL_OFPAT11_SET_TP_SRC:
765 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
768 case OFPUTIL_OFPAT11_SET_TP_DST:
769 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
772 case OFPUTIL_OFPAT12_SET_FIELD:
773 return nxm_reg_load_from_openflow12_set_field(
774 (const struct ofp12_action_set_field *)a, out);
776 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
777 #include "ofp-util.def"
778 return ofpact_from_nxast(a, code, out);
785 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
788 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
791 /* OpenFlow 1.1 instructions. */
793 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
794 static inline const struct STRUCT * \
795 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
797 assert(inst->type == htons(ENUM)); \
798 return (struct STRUCT *)inst; \
802 instruction_init_##ENUM(struct STRUCT *s) \
804 memset(s, 0, sizeof *s); \
805 s->type = htons(ENUM); \
806 s->len = htons(sizeof *s); \
809 static inline struct STRUCT * \
810 instruction_put_##ENUM(struct ofpbuf *buf) \
812 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
813 instruction_init_##ENUM(s); \
819 struct instruction_type_info {
820 enum ovs_instruction_type type;
824 static const struct instruction_type_info inst_info[] = {
825 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
831 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
833 return inst_info[type].name;
837 ofpact_instruction_type_from_name(const char *name)
839 const struct instruction_type_info *p;
840 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
841 if (!strcasecmp(name, p->name)) {
848 static inline struct ofp11_instruction *
849 instruction_next(const struct ofp11_instruction *inst)
851 return ((struct ofp11_instruction *) (void *)
852 ((uint8_t *) inst + ntohs(inst->len)));
856 instruction_is_valid(const struct ofp11_instruction *inst,
857 size_t n_instructions)
859 uint16_t len = ntohs(inst->len);
860 return (!(len % OFP11_INSTRUCTION_ALIGN)
861 && len >= sizeof *inst
862 && len / sizeof *inst <= n_instructions);
865 /* This macro is careful to check for instructions with bad lengths. */
866 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
867 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
868 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
869 ((LEFT) -= (ntohs((ITER)->len) \
870 / sizeof(struct ofp11_instruction)), \
871 (ITER) = instruction_next(ITER)))
874 decode_openflow11_instruction(const struct ofp11_instruction *inst,
875 enum ovs_instruction_type *type)
877 uint16_t len = ntohs(inst->len);
879 switch (inst->type) {
880 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
881 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
883 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
884 case CONSTANT_HTONS(ENUM): \
886 ? len >= sizeof(struct STRUCT) \
887 : len == sizeof(struct STRUCT)) { \
888 *type = OVSINST_##ENUM; \
891 return OFPERR_OFPBIC_BAD_LEN; \
897 return OFPERR_OFPBIC_UNKNOWN_INST;
902 decode_openflow11_instructions(const struct ofp11_instruction insts[],
904 const struct ofp11_instruction *out[])
906 const struct ofp11_instruction *inst;
909 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
910 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
911 enum ovs_instruction_type type;
914 error = decode_openflow11_instruction(inst, &type);
920 return OFPERR_OFPIT_BAD_INSTRUCTION;
926 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
927 (n_insts - left) * sizeof *inst);
928 return OFPERR_OFPBIC_BAD_LEN;
934 get_actions_from_instruction(const struct ofp11_instruction *inst,
935 const union ofp_action **actions,
938 *actions = (const union ofp_action *) (inst + 1);
939 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
942 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
943 * front of 'openflow' into ofpacts. On success, replaces any existing content
944 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
945 * Returns 0 if successful, otherwise an OpenFlow error.
947 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
948 * instructions, so you should call ofpacts_pull_openflow11_instructions()
949 * instead of this function.
951 * The parsed actions are valid generically, but they may not be valid in a
952 * specific context. For example, port numbers up to OFPP_MAX are valid
953 * generically, but specific datapaths may only support port numbers in a
954 * smaller range. Use ofpacts_check() to additional check whether actions are
955 * valid in a specific context. */
957 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
958 unsigned int actions_len,
959 struct ofpbuf *ofpacts)
961 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
962 ofpacts_from_openflow11);
966 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
967 unsigned int instructions_len,
968 struct ofpbuf *ofpacts)
970 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
971 const struct ofp11_instruction *instructions;
972 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
975 ofpbuf_clear(ofpacts);
977 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
978 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
980 instructions_len, OFP11_INSTRUCTION_ALIGN);
981 error = OFPERR_OFPBIC_BAD_LEN;
985 instructions = ofpbuf_try_pull(openflow, instructions_len);
986 if (instructions == NULL) {
987 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
988 "remaining message length (%zu)",
989 instructions_len, openflow->size);
990 error = OFPERR_OFPBIC_BAD_LEN;
994 error = decode_openflow11_instructions(
995 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1001 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1002 const union ofp_action *actions;
1005 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1006 &actions, &n_actions);
1007 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1012 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1013 instruction_get_OFPIT11_CLEAR_ACTIONS(
1014 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1015 ofpact_put_CLEAR_ACTIONS(ofpacts);
1017 /* TODO:XXX Write-Actions */
1018 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1019 const struct ofp11_instruction_write_metadata *oiwm;
1020 struct ofpact_metadata *om;
1022 oiwm = (const struct ofp11_instruction_write_metadata *)
1023 insts[OVSINST_OFPIT11_WRITE_METADATA];
1025 om = ofpact_put_WRITE_METADATA(ofpacts);
1026 om->metadata = oiwm->metadata;
1027 om->mask = oiwm->metadata_mask;
1029 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1030 const struct ofp11_instruction_goto_table *oigt;
1031 struct ofpact_goto_table *ogt;
1033 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1034 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1035 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1036 ogt->table_id = oigt->table_id;
1039 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1040 error = OFPERR_OFPBIC_UNSUP_INST;
1044 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1047 ofpbuf_clear(ofpacts);
1053 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
1055 const struct ofpact_enqueue *enqueue;
1059 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1062 case OFPACT_CONTROLLER:
1065 case OFPACT_ENQUEUE:
1066 enqueue = ofpact_get_ENQUEUE(a);
1067 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
1068 && enqueue->port != OFPP_LOCAL) {
1069 return OFPERR_OFPBAC_BAD_OUT_PORT;
1073 case OFPACT_OUTPUT_REG:
1074 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1077 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1079 case OFPACT_SET_VLAN_VID:
1080 case OFPACT_SET_VLAN_PCP:
1081 case OFPACT_STRIP_VLAN:
1082 case OFPACT_PUSH_VLAN:
1083 case OFPACT_SET_ETH_SRC:
1084 case OFPACT_SET_ETH_DST:
1085 case OFPACT_SET_IPV4_SRC:
1086 case OFPACT_SET_IPV4_DST:
1087 case OFPACT_SET_IPV4_DSCP:
1088 case OFPACT_SET_L4_SRC_PORT:
1089 case OFPACT_SET_L4_DST_PORT:
1092 case OFPACT_REG_MOVE:
1093 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1095 case OFPACT_REG_LOAD:
1096 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1098 case OFPACT_DEC_TTL:
1099 case OFPACT_SET_TUNNEL:
1100 case OFPACT_SET_QUEUE:
1101 case OFPACT_POP_QUEUE:
1102 case OFPACT_FIN_TIMEOUT:
1103 case OFPACT_RESUBMIT:
1107 return learn_check(ofpact_get_LEARN(a), flow);
1109 case OFPACT_MULTIPATH:
1110 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1112 case OFPACT_AUTOPATH:
1113 return autopath_check(ofpact_get_AUTOPATH(a), flow);
1119 case OFPACT_CLEAR_ACTIONS:
1120 case OFPACT_WRITE_METADATA:
1121 case OFPACT_GOTO_TABLE:
1129 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1130 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1131 * switch with no more than 'max_ports' ports. */
1133 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1134 const struct flow *flow, int max_ports)
1136 const struct ofpact *a;
1138 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1139 enum ofperr error = ofpact_check__(a, flow, max_ports);
1148 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1149 * in the appropriate order as defined by the OpenFlow spec. */
1151 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1153 const struct ofpact *a;
1154 const struct ofpact_metadata *om = NULL;
1156 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1158 if (a->type == OFPACT_WRITE_METADATA) {
1159 VLOG_WARN("duplicate write_metadata instruction specified");
1160 /* should be OFPERR_OFPET_BAD_ACTION? */
1161 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1163 VLOG_WARN("write_metadata instruction must be specified after "
1164 "other instructions/actions");
1165 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1169 if (a->type == OFPACT_WRITE_METADATA) {
1170 om = (const struct ofpact_metadata *) a;
1177 /* Converting ofpacts to Nicira OpenFlow extensions. */
1180 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1183 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1185 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1186 output_reg->src.n_bits);
1187 naor->src = htonl(output_reg->src.field->nxm_header);
1188 naor->max_len = htons(output_reg->max_len);
1192 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1195 struct nx_action_resubmit *nar;
1197 if (resubmit->table_id == 0xff
1198 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1199 nar = ofputil_put_NXAST_RESUBMIT(out);
1201 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1202 nar->table = resubmit->table_id;
1204 nar->in_port = htons(resubmit->in_port);
1208 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1211 uint64_t tun_id = tunnel->tun_id;
1213 if (tun_id <= UINT32_MAX
1214 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1215 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1217 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1222 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1225 struct nx_action_write_metadata *nawm;
1227 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1228 nawm->metadata = om->metadata;
1229 nawm->mask = om->mask;
1233 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1235 size_t start_ofs = out->size;
1236 struct nx_action_note *nan;
1237 unsigned int remainder;
1240 nan = ofputil_put_NXAST_NOTE(out);
1241 out->size -= sizeof nan->note;
1243 ofpbuf_put(out, note->data, note->length);
1245 len = out->size - start_ofs;
1246 remainder = len % OFP_ACTION_ALIGN;
1248 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1250 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1251 nan->len = htons(out->size - start_ofs);
1255 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1258 struct nx_action_controller *nac;
1260 nac = ofputil_put_NXAST_CONTROLLER(out);
1261 nac->max_len = htons(oc->max_len);
1262 nac->controller_id = htons(oc->controller_id);
1263 nac->reason = oc->reason;
1267 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1270 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1271 ofputil_put_NXAST_DEC_TTL(out);
1273 struct nx_action_cnt_ids *nac_ids =
1274 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1275 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1279 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1280 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1282 ids = ofpbuf_put_zeros(out, ids_len);
1283 for (i = 0; i < oc_ids->n_controllers; i++) {
1284 ids[i] = htons(oc_ids->cnt_ids[i]);
1290 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1293 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1294 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1295 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1299 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1302 case OFPACT_CONTROLLER:
1303 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1306 case OFPACT_OUTPUT_REG:
1307 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1311 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1314 case OFPACT_REG_MOVE:
1315 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1318 case OFPACT_REG_LOAD:
1319 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1322 case OFPACT_DEC_TTL:
1323 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1326 case OFPACT_SET_TUNNEL:
1327 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1330 case OFPACT_WRITE_METADATA:
1331 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1334 case OFPACT_SET_QUEUE:
1335 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1336 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1339 case OFPACT_POP_QUEUE:
1340 ofputil_put_NXAST_POP_QUEUE(out);
1343 case OFPACT_FIN_TIMEOUT:
1344 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1347 case OFPACT_RESUBMIT:
1348 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1352 learn_to_nxast(ofpact_get_LEARN(a), out);
1355 case OFPACT_MULTIPATH:
1356 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1359 case OFPACT_AUTOPATH:
1360 autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1364 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1368 ofputil_put_NXAST_EXIT(out);
1372 case OFPACT_ENQUEUE:
1373 case OFPACT_SET_VLAN_VID:
1374 case OFPACT_SET_VLAN_PCP:
1375 case OFPACT_STRIP_VLAN:
1376 case OFPACT_PUSH_VLAN:
1377 case OFPACT_SET_ETH_SRC:
1378 case OFPACT_SET_ETH_DST:
1379 case OFPACT_SET_IPV4_SRC:
1380 case OFPACT_SET_IPV4_DST:
1381 case OFPACT_SET_IPV4_DSCP:
1382 case OFPACT_SET_L4_SRC_PORT:
1383 case OFPACT_SET_L4_DST_PORT:
1384 case OFPACT_CLEAR_ACTIONS:
1385 case OFPACT_GOTO_TABLE:
1390 /* Converting ofpacts to OpenFlow 1.0. */
1393 ofpact_output_to_openflow10(const struct ofpact_output *output,
1396 struct ofp10_action_output *oao;
1398 oao = ofputil_put_OFPAT10_OUTPUT(out);
1399 oao->port = htons(output->port);
1400 oao->max_len = htons(output->max_len);
1404 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1407 struct ofp10_action_enqueue *oae;
1409 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1410 oae->port = htons(enqueue->port);
1411 oae->queue_id = htonl(enqueue->queue);
1415 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1419 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1422 case OFPACT_ENQUEUE:
1423 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1426 case OFPACT_SET_VLAN_VID:
1427 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1428 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1431 case OFPACT_SET_VLAN_PCP:
1432 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1433 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1436 case OFPACT_STRIP_VLAN:
1437 ofputil_put_OFPAT10_STRIP_VLAN(out);
1440 case OFPACT_SET_ETH_SRC:
1441 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1442 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1445 case OFPACT_SET_ETH_DST:
1446 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1447 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1450 case OFPACT_SET_IPV4_SRC:
1451 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1452 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1455 case OFPACT_SET_IPV4_DST:
1456 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1457 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1460 case OFPACT_SET_IPV4_DSCP:
1461 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1462 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1465 case OFPACT_SET_L4_SRC_PORT:
1466 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1467 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1470 case OFPACT_SET_L4_DST_PORT:
1471 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1472 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1475 case OFPACT_PUSH_VLAN:
1476 case OFPACT_CLEAR_ACTIONS:
1477 case OFPACT_GOTO_TABLE:
1481 case OFPACT_CONTROLLER:
1482 case OFPACT_OUTPUT_REG:
1484 case OFPACT_REG_MOVE:
1485 case OFPACT_REG_LOAD:
1486 case OFPACT_DEC_TTL:
1487 case OFPACT_SET_TUNNEL:
1488 case OFPACT_WRITE_METADATA:
1489 case OFPACT_SET_QUEUE:
1490 case OFPACT_POP_QUEUE:
1491 case OFPACT_FIN_TIMEOUT:
1492 case OFPACT_RESUBMIT:
1494 case OFPACT_MULTIPATH:
1495 case OFPACT_AUTOPATH:
1498 ofpact_to_nxast(a, out);
1503 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1504 * actions in 'openflow', appending the actions to any existing data in
1507 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1508 struct ofpbuf *openflow)
1510 const struct ofpact *a;
1512 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1513 ofpact_to_openflow10(a, openflow);
1517 /* Converting ofpacts to OpenFlow 1.1. */
1520 ofpact_output_to_openflow11(const struct ofpact_output *output,
1523 struct ofp11_action_output *oao;
1525 oao = ofputil_put_OFPAT11_OUTPUT(out);
1526 oao->port = ofputil_port_to_ofp11(output->port);
1527 oao->max_len = htons(output->max_len);
1531 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
1534 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
1535 && (!dec_ttl->ofpact.compat ||
1536 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
1537 ofputil_put_OFPAT11_DEC_NW_TTL(out);
1539 ofpact_dec_ttl_to_nxast(dec_ttl, out);
1544 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1548 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1550 case OFPACT_ENQUEUE:
1554 case OFPACT_SET_VLAN_VID:
1555 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1556 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1559 case OFPACT_SET_VLAN_PCP:
1560 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1561 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1564 case OFPACT_STRIP_VLAN:
1565 ofputil_put_OFPAT11_POP_VLAN(out);
1568 case OFPACT_PUSH_VLAN:
1569 /* TODO:XXX ETH_TYPE_VLAN_8021AD case */
1570 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
1571 htons(ETH_TYPE_VLAN_8021Q);
1574 case OFPACT_SET_QUEUE:
1575 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
1576 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1579 case OFPACT_SET_ETH_SRC:
1580 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1581 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1584 case OFPACT_SET_ETH_DST:
1585 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1586 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1589 case OFPACT_SET_IPV4_SRC:
1590 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1591 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1594 case OFPACT_SET_IPV4_DST:
1595 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1596 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1599 case OFPACT_SET_IPV4_DSCP:
1600 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1601 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1604 case OFPACT_SET_L4_SRC_PORT:
1605 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1606 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1609 case OFPACT_SET_L4_DST_PORT:
1610 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1611 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1614 case OFPACT_DEC_TTL:
1615 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
1618 case OFPACT_WRITE_METADATA:
1619 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
1622 case OFPACT_CLEAR_ACTIONS:
1623 case OFPACT_GOTO_TABLE:
1626 case OFPACT_CONTROLLER:
1627 case OFPACT_OUTPUT_REG:
1629 case OFPACT_REG_MOVE:
1630 case OFPACT_REG_LOAD:
1631 case OFPACT_SET_TUNNEL:
1632 case OFPACT_POP_QUEUE:
1633 case OFPACT_FIN_TIMEOUT:
1634 case OFPACT_RESUBMIT:
1636 case OFPACT_MULTIPATH:
1637 case OFPACT_AUTOPATH:
1640 ofpact_to_nxast(a, out);
1645 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1646 * 1.1 actions in 'openflow', appending the actions to any existing data in
1649 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1650 size_t ofpacts_len, struct ofpbuf *openflow)
1652 const struct ofpact *a;
1653 size_t start_size = openflow->size;
1655 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1656 ofpact_to_openflow11(a, openflow);
1659 return openflow->size - start_size;
1663 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
1665 struct ofp11_instruction_actions *oia;
1667 /* Update the instruction's length (or, if it's empty, delete it). */
1668 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1669 if (openflow->size > ofs + sizeof *oia) {
1670 oia->len = htons(openflow->size - ofs);
1672 openflow->size = ofs;
1677 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1679 struct ofpbuf *openflow)
1681 const struct ofpact *a;
1683 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1684 /* TODO:XXX Write-Actions */
1686 if (a->type == OFPACT_CLEAR_ACTIONS) {
1687 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
1688 } else if (a->type == OFPACT_GOTO_TABLE) {
1689 struct ofp11_instruction_goto_table *oigt;
1691 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1692 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1693 memset(oigt->pad, 0, sizeof oigt->pad);
1694 } else if (a->type == OFPACT_WRITE_METADATA) {
1695 const struct ofpact_metadata *om;
1696 struct ofp11_instruction_write_metadata *oiwm;
1698 om = ofpact_get_WRITE_METADATA(a);
1699 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
1700 oiwm->metadata = om->metadata;
1701 oiwm->metadata_mask = om->mask;
1702 } else if (!ofpact_is_instruction(a)) {
1704 const size_t ofs = openflow->size;
1705 const size_t ofpacts_len_left =
1706 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1707 const struct ofpact *action;
1708 const struct ofpact *processed = a;
1710 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1711 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
1712 if (ofpact_is_instruction(action)) {
1715 ofpact_to_openflow11(action, openflow);
1718 ofpacts_update_instruction_actions(openflow, ofs);
1724 /* Returns true if 'action' outputs to 'port', false otherwise. */
1726 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1728 switch (ofpact->type) {
1730 return ofpact_get_OUTPUT(ofpact)->port == port;
1731 case OFPACT_ENQUEUE:
1732 return ofpact_get_ENQUEUE(ofpact)->port == port;
1733 case OFPACT_CONTROLLER:
1734 return port == OFPP_CONTROLLER;
1736 case OFPACT_OUTPUT_REG:
1738 case OFPACT_SET_VLAN_VID:
1739 case OFPACT_SET_VLAN_PCP:
1740 case OFPACT_STRIP_VLAN:
1741 case OFPACT_PUSH_VLAN:
1742 case OFPACT_SET_ETH_SRC:
1743 case OFPACT_SET_ETH_DST:
1744 case OFPACT_SET_IPV4_SRC:
1745 case OFPACT_SET_IPV4_DST:
1746 case OFPACT_SET_IPV4_DSCP:
1747 case OFPACT_SET_L4_SRC_PORT:
1748 case OFPACT_SET_L4_DST_PORT:
1749 case OFPACT_REG_MOVE:
1750 case OFPACT_REG_LOAD:
1751 case OFPACT_DEC_TTL:
1752 case OFPACT_SET_TUNNEL:
1753 case OFPACT_WRITE_METADATA:
1754 case OFPACT_SET_QUEUE:
1755 case OFPACT_POP_QUEUE:
1756 case OFPACT_FIN_TIMEOUT:
1757 case OFPACT_RESUBMIT:
1759 case OFPACT_MULTIPATH:
1760 case OFPACT_AUTOPATH:
1763 case OFPACT_CLEAR_ACTIONS:
1764 case OFPACT_GOTO_TABLE:
1770 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1771 * to 'port', false otherwise. */
1773 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1776 const struct ofpact *a;
1778 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1779 if (ofpact_outputs_to_port(a, port)) {
1788 ofpacts_equal(const struct ofpact *a, size_t a_len,
1789 const struct ofpact *b, size_t b_len)
1791 return a_len == b_len && !memcmp(a, b, a_len);
1794 /* Formatting ofpacts. */
1797 print_note(const struct ofpact_note *note, struct ds *string)
1801 ds_put_cstr(string, "note:");
1802 for (i = 0; i < note->length; i++) {
1804 ds_put_char(string, '.');
1806 ds_put_format(string, "%02"PRIx8, note->data[i]);
1811 print_dec_ttl(const struct ofpact_cnt_ids *ids,
1816 ds_put_cstr(s, "dec_ttl");
1817 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
1818 ds_put_cstr(s, "(");
1819 for (i = 0; i < ids->n_controllers; i++) {
1821 ds_put_cstr(s, ",");
1823 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
1825 ds_put_cstr(s, ")");
1830 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1833 ds_put_cstr(s, "fin_timeout(");
1834 if (fin_timeout->fin_idle_timeout) {
1835 ds_put_format(s, "idle_timeout=%"PRIu16",",
1836 fin_timeout->fin_idle_timeout);
1838 if (fin_timeout->fin_hard_timeout) {
1839 ds_put_format(s, "hard_timeout=%"PRIu16",",
1840 fin_timeout->fin_hard_timeout);
1843 ds_put_char(s, ')');
1847 ofpact_format(const struct ofpact *a, struct ds *s)
1849 const struct ofpact_enqueue *enqueue;
1850 const struct ofpact_resubmit *resubmit;
1851 const struct ofpact_autopath *autopath;
1852 const struct ofpact_controller *controller;
1853 const struct ofpact_metadata *metadata;
1854 const struct ofpact_tunnel *tunnel;
1859 port = ofpact_get_OUTPUT(a)->port;
1860 if (port < OFPP_MAX) {
1861 ds_put_format(s, "output:%"PRIu16, port);
1863 ofputil_format_port(port, s);
1864 if (port == OFPP_CONTROLLER) {
1865 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1870 case OFPACT_CONTROLLER:
1871 controller = ofpact_get_CONTROLLER(a);
1872 if (controller->reason == OFPR_ACTION &&
1873 controller->controller_id == 0) {
1874 ds_put_format(s, "CONTROLLER:%"PRIu16,
1875 ofpact_get_CONTROLLER(a)->max_len);
1877 enum ofp_packet_in_reason reason = controller->reason;
1879 ds_put_cstr(s, "controller(");
1880 if (reason != OFPR_ACTION) {
1881 ds_put_format(s, "reason=%s,",
1882 ofputil_packet_in_reason_to_string(reason));
1884 if (controller->max_len != UINT16_MAX) {
1885 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1887 if (controller->controller_id != 0) {
1888 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1891 ds_put_char(s, ')');
1895 case OFPACT_ENQUEUE:
1896 enqueue = ofpact_get_ENQUEUE(a);
1897 ds_put_format(s, "enqueue:");
1898 ofputil_format_port(enqueue->port, s);
1899 ds_put_format(s, "q%"PRIu32, enqueue->queue);
1902 case OFPACT_OUTPUT_REG:
1903 ds_put_cstr(s, "output:");
1904 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1908 bundle_format(ofpact_get_BUNDLE(a), s);
1911 case OFPACT_SET_VLAN_VID:
1912 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1913 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1916 case OFPACT_SET_VLAN_PCP:
1917 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1918 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1921 case OFPACT_STRIP_VLAN:
1922 ds_put_cstr(s, "strip_vlan");
1925 case OFPACT_PUSH_VLAN:
1926 /* TODO:XXX 802.1AD case*/
1927 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1930 case OFPACT_SET_ETH_SRC:
1931 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1932 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1935 case OFPACT_SET_ETH_DST:
1936 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1937 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1940 case OFPACT_SET_IPV4_SRC:
1941 ds_put_format(s, "mod_nw_src:"IP_FMT,
1942 IP_ARGS(&ofpact_get_SET_IPV4_SRC(a)->ipv4));
1945 case OFPACT_SET_IPV4_DST:
1946 ds_put_format(s, "mod_nw_dst:"IP_FMT,
1947 IP_ARGS(&ofpact_get_SET_IPV4_DST(a)->ipv4));
1950 case OFPACT_SET_IPV4_DSCP:
1951 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1954 case OFPACT_SET_L4_SRC_PORT:
1955 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1958 case OFPACT_SET_L4_DST_PORT:
1959 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1962 case OFPACT_REG_MOVE:
1963 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1966 case OFPACT_REG_LOAD:
1967 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1970 case OFPACT_DEC_TTL:
1971 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
1974 case OFPACT_SET_TUNNEL:
1975 tunnel = ofpact_get_SET_TUNNEL(a);
1976 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1977 (tunnel->tun_id > UINT32_MAX
1978 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1982 case OFPACT_SET_QUEUE:
1983 ds_put_format(s, "set_queue:%"PRIu32,
1984 ofpact_get_SET_QUEUE(a)->queue_id);
1987 case OFPACT_POP_QUEUE:
1988 ds_put_cstr(s, "pop_queue");
1991 case OFPACT_FIN_TIMEOUT:
1992 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
1995 case OFPACT_RESUBMIT:
1996 resubmit = ofpact_get_RESUBMIT(a);
1997 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
1998 ds_put_cstr(s, "resubmit:");
1999 ofputil_format_port(resubmit->in_port, s);
2001 ds_put_format(s, "resubmit(");
2002 if (resubmit->in_port != OFPP_IN_PORT) {
2003 ofputil_format_port(resubmit->in_port, s);
2005 ds_put_char(s, ',');
2006 if (resubmit->table_id != 255) {
2007 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2009 ds_put_char(s, ')');
2014 learn_format(ofpact_get_LEARN(a), s);
2017 case OFPACT_MULTIPATH:
2018 multipath_format(ofpact_get_MULTIPATH(a), s);
2021 case OFPACT_AUTOPATH:
2022 autopath = ofpact_get_AUTOPATH(a);
2023 ds_put_cstr(s, "autopath(");
2024 ofputil_format_port(autopath->port, s);
2025 ds_put_char(s, ',');
2026 mf_format_subfield(&autopath->dst, s);
2027 ds_put_char(s, ')');
2031 print_note(ofpact_get_NOTE(a), s);
2035 ds_put_cstr(s, "exit");
2038 case OFPACT_CLEAR_ACTIONS:
2039 ds_put_format(s, "%s",
2040 ofpact_instruction_name_from_type(
2041 OVSINST_OFPIT11_CLEAR_ACTIONS));
2044 case OFPACT_WRITE_METADATA:
2045 metadata = ofpact_get_WRITE_METADATA(a);
2046 ds_put_format(s, "%s:%#"PRIx64,
2047 ofpact_instruction_name_from_type(
2048 OVSINST_OFPIT11_WRITE_METADATA),
2049 ntohll(metadata->metadata));
2050 if (metadata->mask != htonll(UINT64_MAX)) {
2051 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2055 case OFPACT_GOTO_TABLE:
2056 ds_put_format(s, "%s:%"PRIu8,
2057 ofpact_instruction_name_from_type(
2058 OVSINST_OFPIT11_GOTO_TABLE),
2059 ofpact_get_GOTO_TABLE(a)->table_id);
2064 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2065 * 'ofpacts' to 'string'. */
2067 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2070 ds_put_cstr(string, "actions=");
2072 ds_put_cstr(string, "drop");
2074 const struct ofpact *a;
2076 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2078 ds_put_cstr(string, ",");
2081 /* TODO:XXX write-actions */
2082 ofpact_format(a, string);
2087 /* Internal use by helpers. */
2090 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2092 struct ofpact *ofpact;
2094 ofpact_pad(ofpacts);
2095 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2096 ofpact_init(ofpact, type, len);
2101 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2103 memset(ofpact, 0, len);
2104 ofpact->type = type;
2105 ofpact->compat = OFPUTIL_ACTION_INVALID;
2109 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2110 * starting at 'ofpact'.
2112 * This is the correct way to update a variable-length ofpact's length after
2113 * adding the variable-length part of the payload. (See the large comment
2114 * near the end of ofp-actions.h for more information.) */
2116 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2118 assert(ofpact == ofpacts->l2);
2119 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2122 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2123 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2124 * client must call this itself after adding the final ofpact to an array of
2127 * (The consequences of failing to call this function are probably not dire.
2128 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2129 * not dereference it. That's undefined behavior, technically, but it will not
2130 * cause a real problem on common systems. Still, it seems better to call
2133 ofpact_pad(struct ofpbuf *ofpacts)
2135 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2137 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2142 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2145 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2146 load->dst.field = mf;
2148 load->dst.n_bits = mf->n_bits;
2149 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2150 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);