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"
21 #include "byte-order.h"
23 #include "dynamic-string.h"
25 #include "meta-flow.h"
26 #include "multipath.h"
33 VLOG_DEFINE_THIS_MODULE(ofp_actions);
35 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
37 /* Converting OpenFlow 1.0 to ofpacts. */
40 output_from_openflow10(const struct ofp10_action_output *oao,
43 struct ofpact_output *output;
45 output = ofpact_put_OUTPUT(out);
46 output->port = ntohs(oao->port);
47 output->max_len = ntohs(oao->max_len);
49 return ofputil_check_output_port(output->port, OFPP_MAX);
53 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
56 struct ofpact_enqueue *enqueue;
58 enqueue = ofpact_put_ENQUEUE(out);
59 enqueue->port = ntohs(oae->port);
60 enqueue->queue = ntohl(oae->queue_id);
61 if (enqueue->port >= OFPP_MAX && 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 = 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 = 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 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
224 const struct nx_action_header *nah = (const struct nx_action_header *) a;
225 uint16_t len = ntohs(a->header.len);
227 if (len < sizeof(struct nx_action_header)) {
228 return OFPERR_OFPBAC_BAD_LEN;
229 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
230 return OFPERR_OFPBAC_BAD_VENDOR;
233 switch (nah->subtype) {
234 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
235 case CONSTANT_HTONS(ENUM): \
237 ? len >= sizeof(struct STRUCT) \
238 : len == sizeof(struct STRUCT)) { \
239 *code = OFPUTIL_##ENUM; \
242 return OFPERR_OFPBAC_BAD_LEN; \
245 #include "ofp-util.def"
247 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
248 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
250 return OFPERR_OFPBAC_BAD_TYPE;
254 /* Parses 'a' to determine its type. On success stores the correct type into
255 * '*code' and returns 0. On failure returns an OFPERR_* error code and
256 * '*code' is indeterminate.
258 * The caller must have already verified that 'a''s length is potentially
259 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
260 * ofp_action) and no longer than the amount of space allocated to 'a').
262 * This function verifies that 'a''s length is correct for the type of action
263 * that it represents. */
265 decode_openflow10_action(const union ofp_action *a,
266 enum ofputil_action_code *code)
269 case CONSTANT_HTONS(OFPAT10_VENDOR):
270 return decode_nxast_action(a, code);
272 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
273 case CONSTANT_HTONS(ENUM): \
274 if (a->header.len == htons(sizeof(struct STRUCT))) { \
275 *code = OFPUTIL_##ENUM; \
278 return OFPERR_OFPBAC_BAD_LEN; \
281 #include "ofp-util.def"
284 return OFPERR_OFPBAC_BAD_TYPE;
289 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
292 const struct nx_action_resubmit *nar;
293 const struct nx_action_set_tunnel *nast;
294 const struct nx_action_set_queue *nasq;
295 const struct nx_action_note *nan;
296 const struct nx_action_set_tunnel64 *nast64;
297 const struct nx_action_write_metadata *nawm;
298 struct ofpact_tunnel *tunnel;
299 enum ofperr error = 0;
302 case OFPUTIL_ACTION_INVALID:
303 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
304 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
305 #include "ofp-util.def"
308 case OFPUTIL_NXAST_RESUBMIT:
309 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
312 case OFPUTIL_NXAST_SET_TUNNEL:
313 nast = (const struct nx_action_set_tunnel *) a;
314 tunnel = ofpact_put_SET_TUNNEL(out);
315 tunnel->ofpact.compat = code;
316 tunnel->tun_id = ntohl(nast->tun_id);
319 case OFPUTIL_NXAST_WRITE_METADATA:
320 nawm = (const struct nx_action_write_metadata *) a;
321 error = metadata_from_nxast(nawm, out);
324 case OFPUTIL_NXAST_SET_QUEUE:
325 nasq = (const struct nx_action_set_queue *) a;
326 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
329 case OFPUTIL_NXAST_POP_QUEUE:
330 ofpact_put_POP_QUEUE(out);
333 case OFPUTIL_NXAST_REG_MOVE:
334 error = nxm_reg_move_from_openflow(
335 (const struct nx_action_reg_move *) a, out);
338 case OFPUTIL_NXAST_REG_LOAD:
339 error = nxm_reg_load_from_openflow(
340 (const struct nx_action_reg_load *) a, out);
343 case OFPUTIL_NXAST_NOTE:
344 nan = (const struct nx_action_note *) a;
345 note_from_openflow(nan, out);
348 case OFPUTIL_NXAST_SET_TUNNEL64:
349 nast64 = (const struct nx_action_set_tunnel64 *) a;
350 tunnel = ofpact_put_SET_TUNNEL(out);
351 tunnel->ofpact.compat = code;
352 tunnel->tun_id = ntohll(nast64->tun_id);
355 case OFPUTIL_NXAST_MULTIPATH:
356 error = multipath_from_openflow((const struct nx_action_multipath *) a,
357 ofpact_put_MULTIPATH(out));
360 case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
361 error = autopath_from_openflow((const struct nx_action_autopath *) a,
362 ofpact_put_AUTOPATH(out));
365 case OFPUTIL_NXAST_BUNDLE:
366 case OFPUTIL_NXAST_BUNDLE_LOAD:
367 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
370 case OFPUTIL_NXAST_OUTPUT_REG:
371 error = output_reg_from_openflow(
372 (const struct nx_action_output_reg *) a, out);
375 case OFPUTIL_NXAST_RESUBMIT_TABLE:
376 nar = (const struct nx_action_resubmit *) a;
377 error = resubmit_table_from_openflow(nar, out);
380 case OFPUTIL_NXAST_LEARN:
381 error = learn_from_openflow((const struct nx_action_learn *) a, out);
384 case OFPUTIL_NXAST_EXIT:
385 ofpact_put_EXIT(out);
388 case OFPUTIL_NXAST_DEC_TTL:
389 error = dec_ttl_from_openflow(out, code);
392 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
393 error = dec_ttl_cnt_ids_from_openflow(
394 (const struct nx_action_cnt_ids *) a, out);
397 case OFPUTIL_NXAST_FIN_TIMEOUT:
398 fin_timeout_from_openflow(
399 (const struct nx_action_fin_timeout *) a, out);
402 case OFPUTIL_NXAST_CONTROLLER:
403 controller_from_openflow((const struct nx_action_controller *) a, out);
411 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
413 enum ofputil_action_code code;
416 error = decode_openflow10_action(a, &code);
422 case OFPUTIL_ACTION_INVALID:
423 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
424 #include "ofp-util.def"
427 case OFPUTIL_OFPAT10_OUTPUT:
428 return output_from_openflow10(&a->output10, out);
430 case OFPUTIL_OFPAT10_SET_VLAN_VID:
431 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
432 return OFPERR_OFPBAC_BAD_ARGUMENT;
434 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
437 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
438 if (a->vlan_pcp.vlan_pcp & ~7) {
439 return OFPERR_OFPBAC_BAD_ARGUMENT;
441 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
444 case OFPUTIL_OFPAT10_STRIP_VLAN:
445 ofpact_put_STRIP_VLAN(out);
448 case OFPUTIL_OFPAT10_SET_DL_SRC:
449 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
450 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
453 case OFPUTIL_OFPAT10_SET_DL_DST:
454 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
455 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
458 case OFPUTIL_OFPAT10_SET_NW_SRC:
459 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
462 case OFPUTIL_OFPAT10_SET_NW_DST:
463 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
466 case OFPUTIL_OFPAT10_SET_NW_TOS:
467 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
468 return OFPERR_OFPBAC_BAD_ARGUMENT;
470 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
473 case OFPUTIL_OFPAT10_SET_TP_SRC:
474 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
477 case OFPUTIL_OFPAT10_SET_TP_DST:
478 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
482 case OFPUTIL_OFPAT10_ENQUEUE:
483 error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
487 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
488 #include "ofp-util.def"
489 return ofpact_from_nxast(a, code, out);
495 static inline union ofp_action *
496 action_next(const union ofp_action *a)
498 return ((union ofp_action *) (void *)
499 ((uint8_t *) a + ntohs(a->header.len)));
503 action_is_valid(const union ofp_action *a, size_t n_actions)
505 uint16_t len = ntohs(a->header.len);
506 return (!(len % OFP_ACTION_ALIGN)
508 && len / sizeof *a <= n_actions);
511 /* This macro is careful to check for actions with bad lengths. */
512 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
513 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
514 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
515 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
516 (ITER) = action_next(ITER)))
519 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
522 if (!VLOG_DROP_WARN(&rl)) {
526 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
527 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
528 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
534 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
536 enum ofperr (*ofpact_from_openflow)(
537 const union ofp_action *a, struct ofpbuf *out))
539 const union ofp_action *a;
542 ACTION_FOR_EACH (a, left, in, n_in) {
543 enum ofperr error = ofpact_from_openflow(a, out);
545 log_bad_action(in, n_in, a - in, error);
550 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
551 log_bad_action(in, n_in, n_in - left, error);
560 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
563 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
567 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
568 struct ofpbuf *ofpacts,
569 enum ofperr (*translate)(const union ofp_action *actions,
571 struct ofpbuf *ofpacts))
573 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
574 const union ofp_action *actions;
577 ofpbuf_clear(ofpacts);
579 if (actions_len % OFP_ACTION_ALIGN != 0) {
580 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
581 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
582 return OFPERR_OFPBRC_BAD_LEN;
585 actions = ofpbuf_try_pull(openflow, actions_len);
586 if (actions == NULL) {
587 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
588 "remaining message length (%zu)",
589 actions_len, openflow->size);
590 return OFPERR_OFPBRC_BAD_LEN;
593 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
595 ofpbuf_clear(ofpacts);
599 error = ofpacts_verify(ofpacts->data, ofpacts->size);
601 ofpbuf_clear(ofpacts);
606 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
607 * front of 'openflow' into ofpacts. On success, replaces any existing content
608 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
609 * Returns 0 if successful, otherwise an OpenFlow error.
611 * The parsed actions are valid generically, but they may not be valid in a
612 * specific context. For example, port numbers up to OFPP_MAX are valid
613 * generically, but specific datapaths may only support port numbers in a
614 * smaller range. Use ofpacts_check() to additional check whether actions are
615 * valid in a specific context. */
617 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
618 struct ofpbuf *ofpacts)
620 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
621 ofpacts_from_openflow10);
624 /* OpenFlow 1.1 actions. */
626 /* Parses 'a' to determine its type. On success stores the correct type into
627 * '*code' and returns 0. On failure returns an OFPERR_* error code and
628 * '*code' is indeterminate.
630 * The caller must have already verified that 'a''s length is potentially
631 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
632 * ofp_action) and no longer than the amount of space allocated to 'a').
634 * This function verifies that 'a''s length is correct for the type of action
635 * that it represents. */
637 decode_openflow11_action(const union ofp_action *a,
638 enum ofputil_action_code *code)
643 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
644 return decode_nxast_action(a, code);
646 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
647 case CONSTANT_HTONS(ENUM): \
648 len = ntohs(a->header.len); \
650 ? len >= sizeof(struct STRUCT) \
651 : len == sizeof(struct STRUCT)) { \
652 *code = OFPUTIL_##ENUM; \
655 return OFPERR_OFPBAC_BAD_LEN; \
658 #include "ofp-util.def"
661 return OFPERR_OFPBAC_BAD_TYPE;
666 output_from_openflow11(const struct ofp11_action_output *oao,
669 struct ofpact_output *output;
672 output = ofpact_put_OUTPUT(out);
673 output->max_len = ntohs(oao->max_len);
675 error = ofputil_port_from_ofp11(oao->port, &output->port);
680 return ofputil_check_output_port(output->port, OFPP_MAX);
684 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
686 enum ofputil_action_code code;
689 error = decode_openflow11_action(a, &code);
695 case OFPUTIL_ACTION_INVALID:
696 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
697 #include "ofp-util.def"
700 case OFPUTIL_OFPAT11_OUTPUT:
701 return output_from_openflow11((const struct ofp11_action_output *) a,
704 case OFPUTIL_OFPAT11_SET_VLAN_VID:
705 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
706 return OFPERR_OFPBAC_BAD_ARGUMENT;
708 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
711 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
712 if (a->vlan_pcp.vlan_pcp & ~7) {
713 return OFPERR_OFPBAC_BAD_ARGUMENT;
715 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
718 case OFPUTIL_OFPAT11_PUSH_VLAN:
719 if (((const struct ofp11_action_push *)a)->ethertype !=
720 htons(ETH_TYPE_VLAN_8021Q)) {
721 /* XXX 802.1AD(QinQ) isn't supported at the moment */
722 return OFPERR_OFPBAC_BAD_ARGUMENT;
724 ofpact_put_PUSH_VLAN(out);
727 case OFPUTIL_OFPAT11_POP_VLAN:
728 ofpact_put_STRIP_VLAN(out);
731 case OFPUTIL_OFPAT11_SET_QUEUE:
732 ofpact_put_SET_QUEUE(out)->queue_id =
733 ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
736 case OFPUTIL_OFPAT11_SET_DL_SRC:
737 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
738 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
741 case OFPUTIL_OFPAT11_SET_DL_DST:
742 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
743 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
746 case OFPUTIL_OFPAT11_DEC_NW_TTL:
747 dec_ttl_from_openflow(out, code);
750 case OFPUTIL_OFPAT11_SET_NW_SRC:
751 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
754 case OFPUTIL_OFPAT11_SET_NW_DST:
755 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
758 case OFPUTIL_OFPAT11_SET_NW_TOS:
759 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
760 return OFPERR_OFPBAC_BAD_ARGUMENT;
762 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
765 case OFPUTIL_OFPAT11_SET_TP_SRC:
766 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
769 case OFPUTIL_OFPAT11_SET_TP_DST:
770 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
773 case OFPUTIL_OFPAT12_SET_FIELD:
774 return nxm_reg_load_from_openflow12_set_field(
775 (const struct ofp12_action_set_field *)a, out);
777 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
778 #include "ofp-util.def"
779 return ofpact_from_nxast(a, code, out);
786 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
789 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
792 /* OpenFlow 1.1 instructions. */
794 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
795 static inline const struct STRUCT * \
796 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
798 ovs_assert(inst->type == htons(ENUM)); \
799 return (struct STRUCT *)inst; \
803 instruction_init_##ENUM(struct STRUCT *s) \
805 memset(s, 0, sizeof *s); \
806 s->type = htons(ENUM); \
807 s->len = htons(sizeof *s); \
810 static inline struct STRUCT * \
811 instruction_put_##ENUM(struct ofpbuf *buf) \
813 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
814 instruction_init_##ENUM(s); \
820 struct instruction_type_info {
821 enum ovs_instruction_type type;
825 static const struct instruction_type_info inst_info[] = {
826 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
832 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
834 return inst_info[type].name;
838 ofpact_instruction_type_from_name(const char *name)
840 const struct instruction_type_info *p;
841 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
842 if (!strcasecmp(name, p->name)) {
849 static inline struct ofp11_instruction *
850 instruction_next(const struct ofp11_instruction *inst)
852 return ((struct ofp11_instruction *) (void *)
853 ((uint8_t *) inst + ntohs(inst->len)));
857 instruction_is_valid(const struct ofp11_instruction *inst,
858 size_t n_instructions)
860 uint16_t len = ntohs(inst->len);
861 return (!(len % OFP11_INSTRUCTION_ALIGN)
862 && len >= sizeof *inst
863 && len / sizeof *inst <= n_instructions);
866 /* This macro is careful to check for instructions with bad lengths. */
867 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
868 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
869 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
870 ((LEFT) -= (ntohs((ITER)->len) \
871 / sizeof(struct ofp11_instruction)), \
872 (ITER) = instruction_next(ITER)))
875 decode_openflow11_instruction(const struct ofp11_instruction *inst,
876 enum ovs_instruction_type *type)
878 uint16_t len = ntohs(inst->len);
880 switch (inst->type) {
881 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
882 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
884 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
885 case CONSTANT_HTONS(ENUM): \
887 ? len >= sizeof(struct STRUCT) \
888 : len == sizeof(struct STRUCT)) { \
889 *type = OVSINST_##ENUM; \
892 return OFPERR_OFPBIC_BAD_LEN; \
898 return OFPERR_OFPBIC_UNKNOWN_INST;
903 decode_openflow11_instructions(const struct ofp11_instruction insts[],
905 const struct ofp11_instruction *out[])
907 const struct ofp11_instruction *inst;
910 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
911 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
912 enum ovs_instruction_type type;
915 error = decode_openflow11_instruction(inst, &type);
921 return OFPERR_OFPBAC_UNSUPPORTED_ORDER; /* No specific code for
922 * a duplicate instruction
929 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
930 (n_insts - left) * sizeof *inst);
931 return OFPERR_OFPBIC_BAD_LEN;
937 get_actions_from_instruction(const struct ofp11_instruction *inst,
938 const union ofp_action **actions,
941 *actions = (const union ofp_action *) (inst + 1);
942 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
945 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
946 * front of 'openflow' into ofpacts. On success, replaces any existing content
947 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
948 * Returns 0 if successful, otherwise an OpenFlow error.
950 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
951 * instructions, so you should call ofpacts_pull_openflow11_instructions()
952 * instead of this function.
954 * The parsed actions are valid generically, but they may not be valid in a
955 * specific context. For example, port numbers up to OFPP_MAX are valid
956 * generically, but specific datapaths may only support port numbers in a
957 * smaller range. Use ofpacts_check() to additional check whether actions are
958 * valid in a specific context. */
960 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
961 unsigned int actions_len,
962 struct ofpbuf *ofpacts)
964 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
965 ofpacts_from_openflow11);
969 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
970 unsigned int instructions_len,
971 struct ofpbuf *ofpacts)
973 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
974 const struct ofp11_instruction *instructions;
975 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
978 ofpbuf_clear(ofpacts);
980 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
981 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
983 instructions_len, OFP11_INSTRUCTION_ALIGN);
984 error = OFPERR_OFPBIC_BAD_LEN;
988 instructions = ofpbuf_try_pull(openflow, instructions_len);
989 if (instructions == NULL) {
990 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
991 "remaining message length (%zu)",
992 instructions_len, openflow->size);
993 error = OFPERR_OFPBIC_BAD_LEN;
997 error = decode_openflow11_instructions(
998 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1004 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1005 const union ofp_action *actions;
1008 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1009 &actions, &n_actions);
1010 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1015 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1016 instruction_get_OFPIT11_CLEAR_ACTIONS(
1017 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1018 ofpact_put_CLEAR_ACTIONS(ofpacts);
1020 /* XXX Write-Actions */
1021 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1022 const struct ofp11_instruction_write_metadata *oiwm;
1023 struct ofpact_metadata *om;
1025 oiwm = (const struct ofp11_instruction_write_metadata *)
1026 insts[OVSINST_OFPIT11_WRITE_METADATA];
1028 om = ofpact_put_WRITE_METADATA(ofpacts);
1029 om->metadata = oiwm->metadata;
1030 om->mask = oiwm->metadata_mask;
1032 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1033 const struct ofp11_instruction_goto_table *oigt;
1034 struct ofpact_goto_table *ogt;
1036 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1037 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1038 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1039 ogt->table_id = oigt->table_id;
1042 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1043 error = OFPERR_OFPBIC_UNSUP_INST;
1047 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1050 ofpbuf_clear(ofpacts);
1056 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
1058 const struct ofpact_enqueue *enqueue;
1062 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1065 case OFPACT_CONTROLLER:
1068 case OFPACT_ENQUEUE:
1069 enqueue = ofpact_get_ENQUEUE(a);
1070 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
1071 && enqueue->port != OFPP_LOCAL) {
1072 return OFPERR_OFPBAC_BAD_OUT_PORT;
1076 case OFPACT_OUTPUT_REG:
1077 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1080 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1082 case OFPACT_SET_VLAN_VID:
1083 case OFPACT_SET_VLAN_PCP:
1084 case OFPACT_STRIP_VLAN:
1085 case OFPACT_PUSH_VLAN:
1086 case OFPACT_SET_ETH_SRC:
1087 case OFPACT_SET_ETH_DST:
1088 case OFPACT_SET_IPV4_SRC:
1089 case OFPACT_SET_IPV4_DST:
1090 case OFPACT_SET_IPV4_DSCP:
1091 case OFPACT_SET_L4_SRC_PORT:
1092 case OFPACT_SET_L4_DST_PORT:
1095 case OFPACT_REG_MOVE:
1096 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1098 case OFPACT_REG_LOAD:
1099 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1101 case OFPACT_DEC_TTL:
1102 case OFPACT_SET_TUNNEL:
1103 case OFPACT_SET_QUEUE:
1104 case OFPACT_POP_QUEUE:
1105 case OFPACT_FIN_TIMEOUT:
1106 case OFPACT_RESUBMIT:
1110 return learn_check(ofpact_get_LEARN(a), flow);
1112 case OFPACT_MULTIPATH:
1113 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1115 case OFPACT_AUTOPATH:
1116 return autopath_check(ofpact_get_AUTOPATH(a), flow);
1122 case OFPACT_CLEAR_ACTIONS:
1123 case OFPACT_WRITE_METADATA:
1124 case OFPACT_GOTO_TABLE:
1132 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1133 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1134 * switch with no more than 'max_ports' ports. */
1136 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1137 const struct flow *flow, int max_ports)
1139 const struct ofpact *a;
1141 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1142 enum ofperr error = ofpact_check__(a, flow, max_ports);
1151 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1152 * in the appropriate order as defined by the OpenFlow spec. */
1154 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1156 const struct ofpact *a;
1157 enum ovs_instruction_type inst;
1159 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1160 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1161 enum ovs_instruction_type next;
1163 if (a->type == OFPACT_CLEAR_ACTIONS) {
1164 next = OVSINST_OFPIT11_CLEAR_ACTIONS;
1165 } else if (a->type == OFPACT_WRITE_METADATA) {
1166 next = OVSINST_OFPIT11_WRITE_METADATA;
1167 } else if (a->type == OFPACT_GOTO_TABLE) {
1168 next = OVSINST_OFPIT11_GOTO_TABLE;
1170 next = OVSINST_OFPIT11_APPLY_ACTIONS;
1173 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS && next <= inst) {
1174 const char *name = ofpact_instruction_name_from_type(inst);
1175 const char *next_name = ofpact_instruction_name_from_type(next);
1178 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1179 "1.1+ compatibility", name);
1181 VLOG_WARN("invalid instruction ordering: %s must appear "
1182 "before %s, for OpenFlow 1.1+ compatibility",
1185 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1194 /* Converting ofpacts to Nicira OpenFlow extensions. */
1197 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1200 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1202 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1203 output_reg->src.n_bits);
1204 naor->src = htonl(output_reg->src.field->nxm_header);
1205 naor->max_len = htons(output_reg->max_len);
1209 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1212 struct nx_action_resubmit *nar;
1214 if (resubmit->table_id == 0xff
1215 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1216 nar = ofputil_put_NXAST_RESUBMIT(out);
1218 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1219 nar->table = resubmit->table_id;
1221 nar->in_port = htons(resubmit->in_port);
1225 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1228 uint64_t tun_id = tunnel->tun_id;
1230 if (tun_id <= UINT32_MAX
1231 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1232 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1234 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1239 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1242 struct nx_action_write_metadata *nawm;
1244 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1245 nawm->metadata = om->metadata;
1246 nawm->mask = om->mask;
1250 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1252 size_t start_ofs = out->size;
1253 struct nx_action_note *nan;
1254 unsigned int remainder;
1257 nan = ofputil_put_NXAST_NOTE(out);
1258 out->size -= sizeof nan->note;
1260 ofpbuf_put(out, note->data, note->length);
1262 len = out->size - start_ofs;
1263 remainder = len % OFP_ACTION_ALIGN;
1265 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1267 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1268 nan->len = htons(out->size - start_ofs);
1272 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1275 struct nx_action_controller *nac;
1277 nac = ofputil_put_NXAST_CONTROLLER(out);
1278 nac->max_len = htons(oc->max_len);
1279 nac->controller_id = htons(oc->controller_id);
1280 nac->reason = oc->reason;
1284 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1287 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1288 ofputil_put_NXAST_DEC_TTL(out);
1290 struct nx_action_cnt_ids *nac_ids =
1291 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1292 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1296 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1297 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1299 ids = ofpbuf_put_zeros(out, ids_len);
1300 for (i = 0; i < oc_ids->n_controllers; i++) {
1301 ids[i] = htons(oc_ids->cnt_ids[i]);
1307 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1310 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1311 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1312 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1316 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1319 case OFPACT_CONTROLLER:
1320 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1323 case OFPACT_OUTPUT_REG:
1324 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1328 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1331 case OFPACT_REG_MOVE:
1332 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1335 case OFPACT_REG_LOAD:
1336 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1339 case OFPACT_DEC_TTL:
1340 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1343 case OFPACT_SET_TUNNEL:
1344 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1347 case OFPACT_WRITE_METADATA:
1348 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1351 case OFPACT_SET_QUEUE:
1352 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1353 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1356 case OFPACT_POP_QUEUE:
1357 ofputil_put_NXAST_POP_QUEUE(out);
1360 case OFPACT_FIN_TIMEOUT:
1361 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1364 case OFPACT_RESUBMIT:
1365 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1369 learn_to_nxast(ofpact_get_LEARN(a), out);
1372 case OFPACT_MULTIPATH:
1373 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1376 case OFPACT_AUTOPATH:
1377 autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1381 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1385 ofputil_put_NXAST_EXIT(out);
1389 case OFPACT_ENQUEUE:
1390 case OFPACT_SET_VLAN_VID:
1391 case OFPACT_SET_VLAN_PCP:
1392 case OFPACT_STRIP_VLAN:
1393 case OFPACT_PUSH_VLAN:
1394 case OFPACT_SET_ETH_SRC:
1395 case OFPACT_SET_ETH_DST:
1396 case OFPACT_SET_IPV4_SRC:
1397 case OFPACT_SET_IPV4_DST:
1398 case OFPACT_SET_IPV4_DSCP:
1399 case OFPACT_SET_L4_SRC_PORT:
1400 case OFPACT_SET_L4_DST_PORT:
1401 case OFPACT_CLEAR_ACTIONS:
1402 case OFPACT_GOTO_TABLE:
1407 /* Converting ofpacts to OpenFlow 1.0. */
1410 ofpact_output_to_openflow10(const struct ofpact_output *output,
1413 struct ofp10_action_output *oao;
1415 oao = ofputil_put_OFPAT10_OUTPUT(out);
1416 oao->port = htons(output->port);
1417 oao->max_len = htons(output->max_len);
1421 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1424 struct ofp10_action_enqueue *oae;
1426 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1427 oae->port = htons(enqueue->port);
1428 oae->queue_id = htonl(enqueue->queue);
1432 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1436 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1439 case OFPACT_ENQUEUE:
1440 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1443 case OFPACT_SET_VLAN_VID:
1444 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1445 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1448 case OFPACT_SET_VLAN_PCP:
1449 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1450 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1453 case OFPACT_STRIP_VLAN:
1454 ofputil_put_OFPAT10_STRIP_VLAN(out);
1457 case OFPACT_SET_ETH_SRC:
1458 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1459 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1462 case OFPACT_SET_ETH_DST:
1463 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1464 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1467 case OFPACT_SET_IPV4_SRC:
1468 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1469 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1472 case OFPACT_SET_IPV4_DST:
1473 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1474 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1477 case OFPACT_SET_IPV4_DSCP:
1478 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1479 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1482 case OFPACT_SET_L4_SRC_PORT:
1483 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1484 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1487 case OFPACT_SET_L4_DST_PORT:
1488 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1489 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1492 case OFPACT_PUSH_VLAN:
1493 case OFPACT_CLEAR_ACTIONS:
1494 case OFPACT_GOTO_TABLE:
1498 case OFPACT_CONTROLLER:
1499 case OFPACT_OUTPUT_REG:
1501 case OFPACT_REG_MOVE:
1502 case OFPACT_REG_LOAD:
1503 case OFPACT_DEC_TTL:
1504 case OFPACT_SET_TUNNEL:
1505 case OFPACT_WRITE_METADATA:
1506 case OFPACT_SET_QUEUE:
1507 case OFPACT_POP_QUEUE:
1508 case OFPACT_FIN_TIMEOUT:
1509 case OFPACT_RESUBMIT:
1511 case OFPACT_MULTIPATH:
1512 case OFPACT_AUTOPATH:
1515 ofpact_to_nxast(a, out);
1520 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1521 * actions in 'openflow', appending the actions to any existing data in
1524 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1525 struct ofpbuf *openflow)
1527 const struct ofpact *a;
1529 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1530 ofpact_to_openflow10(a, openflow);
1534 /* Converting ofpacts to OpenFlow 1.1. */
1537 ofpact_output_to_openflow11(const struct ofpact_output *output,
1540 struct ofp11_action_output *oao;
1542 oao = ofputil_put_OFPAT11_OUTPUT(out);
1543 oao->port = ofputil_port_to_ofp11(output->port);
1544 oao->max_len = htons(output->max_len);
1548 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
1551 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
1552 && (!dec_ttl->ofpact.compat ||
1553 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
1554 ofputil_put_OFPAT11_DEC_NW_TTL(out);
1556 ofpact_dec_ttl_to_nxast(dec_ttl, out);
1561 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1565 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1567 case OFPACT_ENQUEUE:
1571 case OFPACT_SET_VLAN_VID:
1572 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1573 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1576 case OFPACT_SET_VLAN_PCP:
1577 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1578 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1581 case OFPACT_STRIP_VLAN:
1582 ofputil_put_OFPAT11_POP_VLAN(out);
1585 case OFPACT_PUSH_VLAN:
1586 /* XXX ETH_TYPE_VLAN_8021AD case */
1587 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
1588 htons(ETH_TYPE_VLAN_8021Q);
1591 case OFPACT_SET_QUEUE:
1592 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
1593 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1596 case OFPACT_SET_ETH_SRC:
1597 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1598 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1601 case OFPACT_SET_ETH_DST:
1602 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1603 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1606 case OFPACT_SET_IPV4_SRC:
1607 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1608 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1611 case OFPACT_SET_IPV4_DST:
1612 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1613 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1616 case OFPACT_SET_IPV4_DSCP:
1617 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1618 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1621 case OFPACT_SET_L4_SRC_PORT:
1622 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1623 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1626 case OFPACT_SET_L4_DST_PORT:
1627 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1628 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1631 case OFPACT_DEC_TTL:
1632 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
1635 case OFPACT_WRITE_METADATA:
1636 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
1639 case OFPACT_CLEAR_ACTIONS:
1640 case OFPACT_GOTO_TABLE:
1643 case OFPACT_CONTROLLER:
1644 case OFPACT_OUTPUT_REG:
1646 case OFPACT_REG_MOVE:
1647 case OFPACT_REG_LOAD:
1648 case OFPACT_SET_TUNNEL:
1649 case OFPACT_POP_QUEUE:
1650 case OFPACT_FIN_TIMEOUT:
1651 case OFPACT_RESUBMIT:
1653 case OFPACT_MULTIPATH:
1654 case OFPACT_AUTOPATH:
1657 ofpact_to_nxast(a, out);
1662 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1663 * 1.1 actions in 'openflow', appending the actions to any existing data in
1666 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1667 size_t ofpacts_len, struct ofpbuf *openflow)
1669 const struct ofpact *a;
1670 size_t start_size = openflow->size;
1672 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1673 ofpact_to_openflow11(a, openflow);
1676 return openflow->size - start_size;
1680 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
1682 struct ofp11_instruction_actions *oia;
1684 /* Update the instruction's length (or, if it's empty, delete it). */
1685 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1686 if (openflow->size > ofs + sizeof *oia) {
1687 oia->len = htons(openflow->size - ofs);
1689 openflow->size = ofs;
1694 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1696 struct ofpbuf *openflow)
1698 const struct ofpact *a;
1700 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1701 /* XXX Write-Actions */
1703 if (a->type == OFPACT_CLEAR_ACTIONS) {
1704 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
1705 } else if (a->type == OFPACT_GOTO_TABLE) {
1706 struct ofp11_instruction_goto_table *oigt;
1708 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1709 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1710 memset(oigt->pad, 0, sizeof oigt->pad);
1711 } else if (a->type == OFPACT_WRITE_METADATA) {
1712 const struct ofpact_metadata *om;
1713 struct ofp11_instruction_write_metadata *oiwm;
1715 om = ofpact_get_WRITE_METADATA(a);
1716 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
1717 oiwm->metadata = om->metadata;
1718 oiwm->metadata_mask = om->mask;
1719 } else if (!ofpact_is_instruction(a)) {
1721 const size_t ofs = openflow->size;
1722 const size_t ofpacts_len_left =
1723 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1724 const struct ofpact *action;
1725 const struct ofpact *processed = a;
1727 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1728 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
1729 if (ofpact_is_instruction(action)) {
1732 ofpact_to_openflow11(action, openflow);
1735 ofpacts_update_instruction_actions(openflow, ofs);
1741 /* Returns true if 'action' outputs to 'port', false otherwise. */
1743 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1745 switch (ofpact->type) {
1747 return ofpact_get_OUTPUT(ofpact)->port == port;
1748 case OFPACT_ENQUEUE:
1749 return ofpact_get_ENQUEUE(ofpact)->port == port;
1750 case OFPACT_CONTROLLER:
1751 return port == OFPP_CONTROLLER;
1753 case OFPACT_OUTPUT_REG:
1755 case OFPACT_SET_VLAN_VID:
1756 case OFPACT_SET_VLAN_PCP:
1757 case OFPACT_STRIP_VLAN:
1758 case OFPACT_PUSH_VLAN:
1759 case OFPACT_SET_ETH_SRC:
1760 case OFPACT_SET_ETH_DST:
1761 case OFPACT_SET_IPV4_SRC:
1762 case OFPACT_SET_IPV4_DST:
1763 case OFPACT_SET_IPV4_DSCP:
1764 case OFPACT_SET_L4_SRC_PORT:
1765 case OFPACT_SET_L4_DST_PORT:
1766 case OFPACT_REG_MOVE:
1767 case OFPACT_REG_LOAD:
1768 case OFPACT_DEC_TTL:
1769 case OFPACT_SET_TUNNEL:
1770 case OFPACT_WRITE_METADATA:
1771 case OFPACT_SET_QUEUE:
1772 case OFPACT_POP_QUEUE:
1773 case OFPACT_FIN_TIMEOUT:
1774 case OFPACT_RESUBMIT:
1776 case OFPACT_MULTIPATH:
1777 case OFPACT_AUTOPATH:
1780 case OFPACT_CLEAR_ACTIONS:
1781 case OFPACT_GOTO_TABLE:
1787 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1788 * to 'port', false otherwise. */
1790 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1793 const struct ofpact *a;
1795 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1796 if (ofpact_outputs_to_port(a, port)) {
1805 ofpacts_equal(const struct ofpact *a, size_t a_len,
1806 const struct ofpact *b, size_t b_len)
1808 return a_len == b_len && !memcmp(a, b, a_len);
1811 /* Formatting ofpacts. */
1814 print_note(const struct ofpact_note *note, struct ds *string)
1818 ds_put_cstr(string, "note:");
1819 for (i = 0; i < note->length; i++) {
1821 ds_put_char(string, '.');
1823 ds_put_format(string, "%02"PRIx8, note->data[i]);
1828 print_dec_ttl(const struct ofpact_cnt_ids *ids,
1833 ds_put_cstr(s, "dec_ttl");
1834 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
1835 ds_put_cstr(s, "(");
1836 for (i = 0; i < ids->n_controllers; i++) {
1838 ds_put_cstr(s, ",");
1840 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
1842 ds_put_cstr(s, ")");
1847 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1850 ds_put_cstr(s, "fin_timeout(");
1851 if (fin_timeout->fin_idle_timeout) {
1852 ds_put_format(s, "idle_timeout=%"PRIu16",",
1853 fin_timeout->fin_idle_timeout);
1855 if (fin_timeout->fin_hard_timeout) {
1856 ds_put_format(s, "hard_timeout=%"PRIu16",",
1857 fin_timeout->fin_hard_timeout);
1860 ds_put_char(s, ')');
1864 ofpact_format(const struct ofpact *a, struct ds *s)
1866 const struct ofpact_enqueue *enqueue;
1867 const struct ofpact_resubmit *resubmit;
1868 const struct ofpact_autopath *autopath;
1869 const struct ofpact_controller *controller;
1870 const struct ofpact_metadata *metadata;
1871 const struct ofpact_tunnel *tunnel;
1876 port = ofpact_get_OUTPUT(a)->port;
1877 if (port < OFPP_MAX) {
1878 ds_put_format(s, "output:%"PRIu16, port);
1880 ofputil_format_port(port, s);
1881 if (port == OFPP_CONTROLLER) {
1882 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1887 case OFPACT_CONTROLLER:
1888 controller = ofpact_get_CONTROLLER(a);
1889 if (controller->reason == OFPR_ACTION &&
1890 controller->controller_id == 0) {
1891 ds_put_format(s, "CONTROLLER:%"PRIu16,
1892 ofpact_get_CONTROLLER(a)->max_len);
1894 enum ofp_packet_in_reason reason = controller->reason;
1896 ds_put_cstr(s, "controller(");
1897 if (reason != OFPR_ACTION) {
1898 ds_put_format(s, "reason=%s,",
1899 ofputil_packet_in_reason_to_string(reason));
1901 if (controller->max_len != UINT16_MAX) {
1902 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1904 if (controller->controller_id != 0) {
1905 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1908 ds_put_char(s, ')');
1912 case OFPACT_ENQUEUE:
1913 enqueue = ofpact_get_ENQUEUE(a);
1914 ds_put_format(s, "enqueue:");
1915 ofputil_format_port(enqueue->port, s);
1916 ds_put_format(s, "q%"PRIu32, enqueue->queue);
1919 case OFPACT_OUTPUT_REG:
1920 ds_put_cstr(s, "output:");
1921 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1925 bundle_format(ofpact_get_BUNDLE(a), s);
1928 case OFPACT_SET_VLAN_VID:
1929 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1930 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1933 case OFPACT_SET_VLAN_PCP:
1934 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1935 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1938 case OFPACT_STRIP_VLAN:
1939 ds_put_cstr(s, "strip_vlan");
1942 case OFPACT_PUSH_VLAN:
1943 /* XXX 802.1AD case*/
1944 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1947 case OFPACT_SET_ETH_SRC:
1948 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1949 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1952 case OFPACT_SET_ETH_DST:
1953 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1954 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1957 case OFPACT_SET_IPV4_SRC:
1958 ds_put_format(s, "mod_nw_src:"IP_FMT,
1959 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
1962 case OFPACT_SET_IPV4_DST:
1963 ds_put_format(s, "mod_nw_dst:"IP_FMT,
1964 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
1967 case OFPACT_SET_IPV4_DSCP:
1968 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1971 case OFPACT_SET_L4_SRC_PORT:
1972 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1975 case OFPACT_SET_L4_DST_PORT:
1976 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1979 case OFPACT_REG_MOVE:
1980 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1983 case OFPACT_REG_LOAD:
1984 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1987 case OFPACT_DEC_TTL:
1988 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
1991 case OFPACT_SET_TUNNEL:
1992 tunnel = ofpact_get_SET_TUNNEL(a);
1993 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1994 (tunnel->tun_id > UINT32_MAX
1995 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1999 case OFPACT_SET_QUEUE:
2000 ds_put_format(s, "set_queue:%"PRIu32,
2001 ofpact_get_SET_QUEUE(a)->queue_id);
2004 case OFPACT_POP_QUEUE:
2005 ds_put_cstr(s, "pop_queue");
2008 case OFPACT_FIN_TIMEOUT:
2009 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2012 case OFPACT_RESUBMIT:
2013 resubmit = ofpact_get_RESUBMIT(a);
2014 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2015 ds_put_cstr(s, "resubmit:");
2016 ofputil_format_port(resubmit->in_port, s);
2018 ds_put_format(s, "resubmit(");
2019 if (resubmit->in_port != OFPP_IN_PORT) {
2020 ofputil_format_port(resubmit->in_port, s);
2022 ds_put_char(s, ',');
2023 if (resubmit->table_id != 255) {
2024 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2026 ds_put_char(s, ')');
2031 learn_format(ofpact_get_LEARN(a), s);
2034 case OFPACT_MULTIPATH:
2035 multipath_format(ofpact_get_MULTIPATH(a), s);
2038 case OFPACT_AUTOPATH:
2039 autopath = ofpact_get_AUTOPATH(a);
2040 ds_put_cstr(s, "autopath(");
2041 ofputil_format_port(autopath->port, s);
2042 ds_put_char(s, ',');
2043 mf_format_subfield(&autopath->dst, s);
2044 ds_put_char(s, ')');
2048 print_note(ofpact_get_NOTE(a), s);
2052 ds_put_cstr(s, "exit");
2055 case OFPACT_CLEAR_ACTIONS:
2056 ds_put_format(s, "%s",
2057 ofpact_instruction_name_from_type(
2058 OVSINST_OFPIT11_CLEAR_ACTIONS));
2061 case OFPACT_WRITE_METADATA:
2062 metadata = ofpact_get_WRITE_METADATA(a);
2063 ds_put_format(s, "%s:%#"PRIx64,
2064 ofpact_instruction_name_from_type(
2065 OVSINST_OFPIT11_WRITE_METADATA),
2066 ntohll(metadata->metadata));
2067 if (metadata->mask != htonll(UINT64_MAX)) {
2068 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2072 case OFPACT_GOTO_TABLE:
2073 ds_put_format(s, "%s:%"PRIu8,
2074 ofpact_instruction_name_from_type(
2075 OVSINST_OFPIT11_GOTO_TABLE),
2076 ofpact_get_GOTO_TABLE(a)->table_id);
2081 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2082 * 'ofpacts' to 'string'. */
2084 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2087 ds_put_cstr(string, "actions=");
2089 ds_put_cstr(string, "drop");
2091 const struct ofpact *a;
2093 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2095 ds_put_cstr(string, ",");
2098 /* XXX write-actions */
2099 ofpact_format(a, string);
2104 /* Internal use by helpers. */
2107 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2109 struct ofpact *ofpact;
2111 ofpact_pad(ofpacts);
2112 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2113 ofpact_init(ofpact, type, len);
2118 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2120 memset(ofpact, 0, len);
2121 ofpact->type = type;
2122 ofpact->compat = OFPUTIL_ACTION_INVALID;
2126 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2127 * starting at 'ofpact'.
2129 * This is the correct way to update a variable-length ofpact's length after
2130 * adding the variable-length part of the payload. (See the large comment
2131 * near the end of ofp-actions.h for more information.) */
2133 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2135 ovs_assert(ofpact == ofpacts->l2);
2136 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2139 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2140 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2141 * client must call this itself after adding the final ofpact to an array of
2144 * (The consequences of failing to call this function are probably not dire.
2145 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2146 * not dereference it. That's undefined behavior, technically, but it will not
2147 * cause a real problem on common systems. Still, it seems better to call
2150 ofpact_pad(struct ofpbuf *ofpacts)
2152 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2154 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2159 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2162 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2163 load->dst.field = mf;
2165 load->dst.n_bits = mf->n_bits;
2166 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2167 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);