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);
406 case OFPUTIL_NXAST_PUSH_MPLS: {
407 struct nx_action_push_mpls *nxapm = (struct nx_action_push_mpls *)a;
408 if (!eth_type_mpls(nxapm->ethertype)) {
409 return OFPERR_OFPBAC_BAD_ARGUMENT;
411 ofpact_put_PUSH_MPLS(out)->ethertype = nxapm->ethertype;
415 case OFPUTIL_NXAST_POP_MPLS: {
416 struct nx_action_pop_mpls *nxapm = (struct nx_action_pop_mpls *)a;
417 if (eth_type_mpls(nxapm->ethertype)) {
418 return OFPERR_OFPBAC_BAD_ARGUMENT;
420 ofpact_put_POP_MPLS(out)->ethertype = nxapm->ethertype;
429 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
431 enum ofputil_action_code code;
434 error = decode_openflow10_action(a, &code);
440 case OFPUTIL_ACTION_INVALID:
441 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
442 #include "ofp-util.def"
445 case OFPUTIL_OFPAT10_OUTPUT:
446 return output_from_openflow10(&a->output10, out);
448 case OFPUTIL_OFPAT10_SET_VLAN_VID:
449 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
450 return OFPERR_OFPBAC_BAD_ARGUMENT;
452 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
455 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
456 if (a->vlan_pcp.vlan_pcp & ~7) {
457 return OFPERR_OFPBAC_BAD_ARGUMENT;
459 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
462 case OFPUTIL_OFPAT10_STRIP_VLAN:
463 ofpact_put_STRIP_VLAN(out);
466 case OFPUTIL_OFPAT10_SET_DL_SRC:
467 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
468 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
471 case OFPUTIL_OFPAT10_SET_DL_DST:
472 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
473 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
476 case OFPUTIL_OFPAT10_SET_NW_SRC:
477 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
480 case OFPUTIL_OFPAT10_SET_NW_DST:
481 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
484 case OFPUTIL_OFPAT10_SET_NW_TOS:
485 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
486 return OFPERR_OFPBAC_BAD_ARGUMENT;
488 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
491 case OFPUTIL_OFPAT10_SET_TP_SRC:
492 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
495 case OFPUTIL_OFPAT10_SET_TP_DST:
496 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
500 case OFPUTIL_OFPAT10_ENQUEUE:
501 error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
505 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
506 #include "ofp-util.def"
507 return ofpact_from_nxast(a, code, out);
513 static inline union ofp_action *
514 action_next(const union ofp_action *a)
516 return ((union ofp_action *) (void *)
517 ((uint8_t *) a + ntohs(a->header.len)));
521 action_is_valid(const union ofp_action *a, size_t n_actions)
523 uint16_t len = ntohs(a->header.len);
524 return (!(len % OFP_ACTION_ALIGN)
526 && len / sizeof *a <= n_actions);
529 /* This macro is careful to check for actions with bad lengths. */
530 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
531 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
532 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
533 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
534 (ITER) = action_next(ITER)))
537 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
540 if (!VLOG_DROP_WARN(&rl)) {
544 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
545 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
546 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
552 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
554 enum ofperr (*ofpact_from_openflow)(
555 const union ofp_action *a, struct ofpbuf *out))
557 const union ofp_action *a;
560 ACTION_FOR_EACH (a, left, in, n_in) {
561 enum ofperr error = ofpact_from_openflow(a, out);
563 log_bad_action(in, n_in, a - in, error);
568 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
569 log_bad_action(in, n_in, n_in - left, error);
578 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
581 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
585 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
586 struct ofpbuf *ofpacts,
587 enum ofperr (*translate)(const union ofp_action *actions,
589 struct ofpbuf *ofpacts))
591 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
592 const union ofp_action *actions;
595 ofpbuf_clear(ofpacts);
597 if (actions_len % OFP_ACTION_ALIGN != 0) {
598 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
599 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
600 return OFPERR_OFPBRC_BAD_LEN;
603 actions = ofpbuf_try_pull(openflow, actions_len);
604 if (actions == NULL) {
605 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
606 "remaining message length (%zu)",
607 actions_len, openflow->size);
608 return OFPERR_OFPBRC_BAD_LEN;
611 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
613 ofpbuf_clear(ofpacts);
617 error = ofpacts_verify(ofpacts->data, ofpacts->size);
619 ofpbuf_clear(ofpacts);
624 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
625 * front of 'openflow' into ofpacts. On success, replaces any existing content
626 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
627 * Returns 0 if successful, otherwise an OpenFlow error.
629 * The parsed actions are valid generically, but they may not be valid in a
630 * specific context. For example, port numbers up to OFPP_MAX are valid
631 * generically, but specific datapaths may only support port numbers in a
632 * smaller range. Use ofpacts_check() to additional check whether actions are
633 * valid in a specific context. */
635 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
636 struct ofpbuf *ofpacts)
638 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
639 ofpacts_from_openflow10);
642 /* OpenFlow 1.1 actions. */
644 /* Parses 'a' to determine its type. On success stores the correct type into
645 * '*code' and returns 0. On failure returns an OFPERR_* error code and
646 * '*code' is indeterminate.
648 * The caller must have already verified that 'a''s length is potentially
649 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
650 * ofp_action) and no longer than the amount of space allocated to 'a').
652 * This function verifies that 'a''s length is correct for the type of action
653 * that it represents. */
655 decode_openflow11_action(const union ofp_action *a,
656 enum ofputil_action_code *code)
661 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
662 return decode_nxast_action(a, code);
664 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
665 case CONSTANT_HTONS(ENUM): \
666 len = ntohs(a->header.len); \
668 ? len >= sizeof(struct STRUCT) \
669 : len == sizeof(struct STRUCT)) { \
670 *code = OFPUTIL_##ENUM; \
673 return OFPERR_OFPBAC_BAD_LEN; \
676 #include "ofp-util.def"
679 return OFPERR_OFPBAC_BAD_TYPE;
684 output_from_openflow11(const struct ofp11_action_output *oao,
687 struct ofpact_output *output;
690 output = ofpact_put_OUTPUT(out);
691 output->max_len = ntohs(oao->max_len);
693 error = ofputil_port_from_ofp11(oao->port, &output->port);
698 return ofputil_check_output_port(output->port, OFPP_MAX);
702 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
704 enum ofputil_action_code code;
707 error = decode_openflow11_action(a, &code);
713 case OFPUTIL_ACTION_INVALID:
714 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
715 #include "ofp-util.def"
718 case OFPUTIL_OFPAT11_OUTPUT:
719 return output_from_openflow11((const struct ofp11_action_output *) a,
722 case OFPUTIL_OFPAT11_SET_VLAN_VID:
723 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
724 return OFPERR_OFPBAC_BAD_ARGUMENT;
726 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
729 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
730 if (a->vlan_pcp.vlan_pcp & ~7) {
731 return OFPERR_OFPBAC_BAD_ARGUMENT;
733 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
736 case OFPUTIL_OFPAT11_PUSH_VLAN:
737 if (((const struct ofp11_action_push *)a)->ethertype !=
738 htons(ETH_TYPE_VLAN_8021Q)) {
739 /* XXX 802.1AD(QinQ) isn't supported at the moment */
740 return OFPERR_OFPBAC_BAD_ARGUMENT;
742 ofpact_put_PUSH_VLAN(out);
745 case OFPUTIL_OFPAT11_POP_VLAN:
746 ofpact_put_STRIP_VLAN(out);
749 case OFPUTIL_OFPAT11_SET_QUEUE:
750 ofpact_put_SET_QUEUE(out)->queue_id =
751 ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
754 case OFPUTIL_OFPAT11_SET_DL_SRC:
755 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
756 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
759 case OFPUTIL_OFPAT11_SET_DL_DST:
760 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
761 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
764 case OFPUTIL_OFPAT11_DEC_NW_TTL:
765 dec_ttl_from_openflow(out, code);
768 case OFPUTIL_OFPAT11_SET_NW_SRC:
769 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
772 case OFPUTIL_OFPAT11_SET_NW_DST:
773 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
776 case OFPUTIL_OFPAT11_SET_NW_TOS:
777 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
778 return OFPERR_OFPBAC_BAD_ARGUMENT;
780 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
783 case OFPUTIL_OFPAT11_SET_TP_SRC:
784 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
787 case OFPUTIL_OFPAT11_SET_TP_DST:
788 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
791 case OFPUTIL_OFPAT12_SET_FIELD:
792 return nxm_reg_load_from_openflow12_set_field(
793 (const struct ofp12_action_set_field *)a, out);
795 case OFPUTIL_OFPAT11_PUSH_MPLS: {
796 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
797 if (!eth_type_mpls(oap->ethertype)) {
798 return OFPERR_OFPBAC_BAD_ARGUMENT;
800 ofpact_put_PUSH_MPLS(out)->ethertype = oap->ethertype;
804 case OFPUTIL_OFPAT11_POP_MPLS: {
805 struct ofp11_action_pop_mpls *oapm = (struct ofp11_action_pop_mpls *)a;
806 if (eth_type_mpls(oapm->ethertype)) {
807 return OFPERR_OFPBAC_BAD_ARGUMENT;
809 ofpact_put_POP_MPLS(out)->ethertype = oapm->ethertype;
813 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
814 #include "ofp-util.def"
815 return ofpact_from_nxast(a, code, out);
822 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
825 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
828 /* OpenFlow 1.1 instructions. */
830 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
831 static inline const struct STRUCT * \
832 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
834 ovs_assert(inst->type == htons(ENUM)); \
835 return (struct STRUCT *)inst; \
839 instruction_init_##ENUM(struct STRUCT *s) \
841 memset(s, 0, sizeof *s); \
842 s->type = htons(ENUM); \
843 s->len = htons(sizeof *s); \
846 static inline struct STRUCT * \
847 instruction_put_##ENUM(struct ofpbuf *buf) \
849 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
850 instruction_init_##ENUM(s); \
856 struct instruction_type_info {
857 enum ovs_instruction_type type;
861 static const struct instruction_type_info inst_info[] = {
862 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
868 ofpact_instruction_name_from_type(enum ovs_instruction_type type)
870 return inst_info[type].name;
874 ofpact_instruction_type_from_name(const char *name)
876 const struct instruction_type_info *p;
877 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
878 if (!strcasecmp(name, p->name)) {
885 static inline struct ofp11_instruction *
886 instruction_next(const struct ofp11_instruction *inst)
888 return ((struct ofp11_instruction *) (void *)
889 ((uint8_t *) inst + ntohs(inst->len)));
893 instruction_is_valid(const struct ofp11_instruction *inst,
894 size_t n_instructions)
896 uint16_t len = ntohs(inst->len);
897 return (!(len % OFP11_INSTRUCTION_ALIGN)
898 && len >= sizeof *inst
899 && len / sizeof *inst <= n_instructions);
902 /* This macro is careful to check for instructions with bad lengths. */
903 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
904 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
905 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
906 ((LEFT) -= (ntohs((ITER)->len) \
907 / sizeof(struct ofp11_instruction)), \
908 (ITER) = instruction_next(ITER)))
911 decode_openflow11_instruction(const struct ofp11_instruction *inst,
912 enum ovs_instruction_type *type)
914 uint16_t len = ntohs(inst->len);
916 switch (inst->type) {
917 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
918 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
920 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
921 case CONSTANT_HTONS(ENUM): \
923 ? len >= sizeof(struct STRUCT) \
924 : len == sizeof(struct STRUCT)) { \
925 *type = OVSINST_##ENUM; \
928 return OFPERR_OFPBIC_BAD_LEN; \
934 return OFPERR_OFPBIC_UNKNOWN_INST;
939 decode_openflow11_instructions(const struct ofp11_instruction insts[],
941 const struct ofp11_instruction *out[])
943 const struct ofp11_instruction *inst;
946 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
947 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
948 enum ovs_instruction_type type;
951 error = decode_openflow11_instruction(inst, &type);
957 return OFPERR_OFPBAC_UNSUPPORTED_ORDER; /* No specific code for
958 * a duplicate instruction
965 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
966 (n_insts - left) * sizeof *inst);
967 return OFPERR_OFPBIC_BAD_LEN;
973 get_actions_from_instruction(const struct ofp11_instruction *inst,
974 const union ofp_action **actions,
977 *actions = (const union ofp_action *) (inst + 1);
978 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
981 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
982 * front of 'openflow' into ofpacts. On success, replaces any existing content
983 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
984 * Returns 0 if successful, otherwise an OpenFlow error.
986 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
987 * instructions, so you should call ofpacts_pull_openflow11_instructions()
988 * instead of this function.
990 * The parsed actions are valid generically, but they may not be valid in a
991 * specific context. For example, port numbers up to OFPP_MAX are valid
992 * generically, but specific datapaths may only support port numbers in a
993 * smaller range. Use ofpacts_check() to additional check whether actions are
994 * valid in a specific context. */
996 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
997 unsigned int actions_len,
998 struct ofpbuf *ofpacts)
1000 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1001 ofpacts_from_openflow11);
1005 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1006 unsigned int instructions_len,
1007 struct ofpbuf *ofpacts)
1009 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1010 const struct ofp11_instruction *instructions;
1011 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1014 ofpbuf_clear(ofpacts);
1016 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1017 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1019 instructions_len, OFP11_INSTRUCTION_ALIGN);
1020 error = OFPERR_OFPBIC_BAD_LEN;
1024 instructions = ofpbuf_try_pull(openflow, instructions_len);
1025 if (instructions == NULL) {
1026 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1027 "remaining message length (%zu)",
1028 instructions_len, openflow->size);
1029 error = OFPERR_OFPBIC_BAD_LEN;
1033 error = decode_openflow11_instructions(
1034 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1040 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1041 const union ofp_action *actions;
1044 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1045 &actions, &n_actions);
1046 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1051 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1052 instruction_get_OFPIT11_CLEAR_ACTIONS(
1053 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1054 ofpact_put_CLEAR_ACTIONS(ofpacts);
1056 /* XXX Write-Actions */
1057 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1058 const struct ofp11_instruction_write_metadata *oiwm;
1059 struct ofpact_metadata *om;
1061 oiwm = (const struct ofp11_instruction_write_metadata *)
1062 insts[OVSINST_OFPIT11_WRITE_METADATA];
1064 om = ofpact_put_WRITE_METADATA(ofpacts);
1065 om->metadata = oiwm->metadata;
1066 om->mask = oiwm->metadata_mask;
1068 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1069 const struct ofp11_instruction_goto_table *oigt;
1070 struct ofpact_goto_table *ogt;
1072 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1073 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1074 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1075 ogt->table_id = oigt->table_id;
1078 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1079 error = OFPERR_OFPBIC_UNSUP_INST;
1083 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1086 ofpbuf_clear(ofpacts);
1092 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports,
1095 const struct ofpact_enqueue *enqueue;
1099 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1102 case OFPACT_CONTROLLER:
1105 case OFPACT_ENQUEUE:
1106 enqueue = ofpact_get_ENQUEUE(a);
1107 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
1108 && enqueue->port != OFPP_LOCAL) {
1109 return OFPERR_OFPBAC_BAD_OUT_PORT;
1113 case OFPACT_OUTPUT_REG:
1114 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1117 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1119 case OFPACT_SET_VLAN_VID:
1120 case OFPACT_SET_VLAN_PCP:
1121 case OFPACT_STRIP_VLAN:
1122 case OFPACT_PUSH_VLAN:
1123 case OFPACT_SET_ETH_SRC:
1124 case OFPACT_SET_ETH_DST:
1125 case OFPACT_SET_IPV4_SRC:
1126 case OFPACT_SET_IPV4_DST:
1127 case OFPACT_SET_IPV4_DSCP:
1128 case OFPACT_SET_L4_SRC_PORT:
1129 case OFPACT_SET_L4_DST_PORT:
1132 case OFPACT_REG_MOVE:
1133 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1135 case OFPACT_REG_LOAD:
1136 if (*dl_type != flow->dl_type) {
1137 struct flow updated_flow = *flow;
1138 updated_flow.dl_type = *dl_type;
1139 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), &updated_flow);
1141 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1144 case OFPACT_DEC_TTL:
1145 case OFPACT_SET_TUNNEL:
1146 case OFPACT_SET_QUEUE:
1147 case OFPACT_POP_QUEUE:
1148 case OFPACT_FIN_TIMEOUT:
1149 case OFPACT_RESUBMIT:
1153 return learn_check(ofpact_get_LEARN(a), flow);
1155 case OFPACT_MULTIPATH:
1156 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1158 case OFPACT_AUTOPATH:
1159 return autopath_check(ofpact_get_AUTOPATH(a), flow);
1165 case OFPACT_PUSH_MPLS:
1166 *dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1169 case OFPACT_POP_MPLS:
1170 *dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1173 case OFPACT_CLEAR_ACTIONS:
1174 case OFPACT_WRITE_METADATA:
1175 case OFPACT_GOTO_TABLE:
1183 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1184 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1185 * switch with no more than 'max_ports' ports. */
1187 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1188 const struct flow *flow, int max_ports)
1190 const struct ofpact *a;
1191 ovs_be16 dl_type = flow->dl_type;
1193 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1194 enum ofperr error = ofpact_check__(a, flow, max_ports, &dl_type);
1203 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1204 * in the appropriate order as defined by the OpenFlow spec. */
1206 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1208 const struct ofpact *a;
1209 enum ovs_instruction_type inst;
1211 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1212 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1213 enum ovs_instruction_type next;
1215 if (a->type == OFPACT_CLEAR_ACTIONS) {
1216 next = OVSINST_OFPIT11_CLEAR_ACTIONS;
1217 } else if (a->type == OFPACT_WRITE_METADATA) {
1218 next = OVSINST_OFPIT11_WRITE_METADATA;
1219 } else if (a->type == OFPACT_GOTO_TABLE) {
1220 next = OVSINST_OFPIT11_GOTO_TABLE;
1222 next = OVSINST_OFPIT11_APPLY_ACTIONS;
1225 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS && next <= inst) {
1226 const char *name = ofpact_instruction_name_from_type(inst);
1227 const char *next_name = ofpact_instruction_name_from_type(next);
1230 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1231 "1.1+ compatibility", name);
1233 VLOG_WARN("invalid instruction ordering: %s must appear "
1234 "before %s, for OpenFlow 1.1+ compatibility",
1237 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1246 /* Converting ofpacts to Nicira OpenFlow extensions. */
1249 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1252 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1254 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1255 output_reg->src.n_bits);
1256 naor->src = htonl(output_reg->src.field->nxm_header);
1257 naor->max_len = htons(output_reg->max_len);
1261 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1264 struct nx_action_resubmit *nar;
1266 if (resubmit->table_id == 0xff
1267 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1268 nar = ofputil_put_NXAST_RESUBMIT(out);
1270 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1271 nar->table = resubmit->table_id;
1273 nar->in_port = htons(resubmit->in_port);
1277 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1280 uint64_t tun_id = tunnel->tun_id;
1282 if (tun_id <= UINT32_MAX
1283 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1284 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1286 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1291 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1294 struct nx_action_write_metadata *nawm;
1296 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1297 nawm->metadata = om->metadata;
1298 nawm->mask = om->mask;
1302 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1304 size_t start_ofs = out->size;
1305 struct nx_action_note *nan;
1306 unsigned int remainder;
1309 nan = ofputil_put_NXAST_NOTE(out);
1310 out->size -= sizeof nan->note;
1312 ofpbuf_put(out, note->data, note->length);
1314 len = out->size - start_ofs;
1315 remainder = len % OFP_ACTION_ALIGN;
1317 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1319 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1320 nan->len = htons(out->size - start_ofs);
1324 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1327 struct nx_action_controller *nac;
1329 nac = ofputil_put_NXAST_CONTROLLER(out);
1330 nac->max_len = htons(oc->max_len);
1331 nac->controller_id = htons(oc->controller_id);
1332 nac->reason = oc->reason;
1336 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1339 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1340 ofputil_put_NXAST_DEC_TTL(out);
1342 struct nx_action_cnt_ids *nac_ids =
1343 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1344 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1348 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1349 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1351 ids = ofpbuf_put_zeros(out, ids_len);
1352 for (i = 0; i < oc_ids->n_controllers; i++) {
1353 ids[i] = htons(oc_ids->cnt_ids[i]);
1359 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1362 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1363 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1364 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1368 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1371 case OFPACT_CONTROLLER:
1372 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1375 case OFPACT_OUTPUT_REG:
1376 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1380 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1383 case OFPACT_REG_MOVE:
1384 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1387 case OFPACT_REG_LOAD:
1388 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1391 case OFPACT_DEC_TTL:
1392 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1395 case OFPACT_SET_TUNNEL:
1396 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1399 case OFPACT_WRITE_METADATA:
1400 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1403 case OFPACT_SET_QUEUE:
1404 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1405 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1408 case OFPACT_POP_QUEUE:
1409 ofputil_put_NXAST_POP_QUEUE(out);
1412 case OFPACT_FIN_TIMEOUT:
1413 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1416 case OFPACT_RESUBMIT:
1417 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1421 learn_to_nxast(ofpact_get_LEARN(a), out);
1424 case OFPACT_MULTIPATH:
1425 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1428 case OFPACT_AUTOPATH:
1429 autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1433 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1437 ofputil_put_NXAST_EXIT(out);
1440 case OFPACT_PUSH_MPLS:
1441 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1442 ofpact_get_PUSH_MPLS(a)->ethertype;
1445 case OFPACT_POP_MPLS:
1446 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1447 ofpact_get_POP_MPLS(a)->ethertype;
1451 case OFPACT_ENQUEUE:
1452 case OFPACT_SET_VLAN_VID:
1453 case OFPACT_SET_VLAN_PCP:
1454 case OFPACT_STRIP_VLAN:
1455 case OFPACT_PUSH_VLAN:
1456 case OFPACT_SET_ETH_SRC:
1457 case OFPACT_SET_ETH_DST:
1458 case OFPACT_SET_IPV4_SRC:
1459 case OFPACT_SET_IPV4_DST:
1460 case OFPACT_SET_IPV4_DSCP:
1461 case OFPACT_SET_L4_SRC_PORT:
1462 case OFPACT_SET_L4_DST_PORT:
1463 case OFPACT_CLEAR_ACTIONS:
1464 case OFPACT_GOTO_TABLE:
1469 /* Converting ofpacts to OpenFlow 1.0. */
1472 ofpact_output_to_openflow10(const struct ofpact_output *output,
1475 struct ofp10_action_output *oao;
1477 oao = ofputil_put_OFPAT10_OUTPUT(out);
1478 oao->port = htons(output->port);
1479 oao->max_len = htons(output->max_len);
1483 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1486 struct ofp10_action_enqueue *oae;
1488 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1489 oae->port = htons(enqueue->port);
1490 oae->queue_id = htonl(enqueue->queue);
1494 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1498 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1501 case OFPACT_ENQUEUE:
1502 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1505 case OFPACT_SET_VLAN_VID:
1506 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1507 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1510 case OFPACT_SET_VLAN_PCP:
1511 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1512 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1515 case OFPACT_STRIP_VLAN:
1516 ofputil_put_OFPAT10_STRIP_VLAN(out);
1519 case OFPACT_SET_ETH_SRC:
1520 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1521 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1524 case OFPACT_SET_ETH_DST:
1525 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1526 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1529 case OFPACT_SET_IPV4_SRC:
1530 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1531 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1534 case OFPACT_SET_IPV4_DST:
1535 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1536 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1539 case OFPACT_SET_IPV4_DSCP:
1540 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1541 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1544 case OFPACT_SET_L4_SRC_PORT:
1545 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1546 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1549 case OFPACT_SET_L4_DST_PORT:
1550 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1551 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1554 case OFPACT_PUSH_VLAN:
1555 case OFPACT_CLEAR_ACTIONS:
1556 case OFPACT_GOTO_TABLE:
1560 case OFPACT_CONTROLLER:
1561 case OFPACT_OUTPUT_REG:
1563 case OFPACT_REG_MOVE:
1564 case OFPACT_REG_LOAD:
1565 case OFPACT_DEC_TTL:
1566 case OFPACT_SET_TUNNEL:
1567 case OFPACT_WRITE_METADATA:
1568 case OFPACT_SET_QUEUE:
1569 case OFPACT_POP_QUEUE:
1570 case OFPACT_FIN_TIMEOUT:
1571 case OFPACT_RESUBMIT:
1573 case OFPACT_MULTIPATH:
1574 case OFPACT_AUTOPATH:
1577 case OFPACT_PUSH_MPLS:
1578 case OFPACT_POP_MPLS:
1579 ofpact_to_nxast(a, out);
1584 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1585 * actions in 'openflow', appending the actions to any existing data in
1588 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1589 struct ofpbuf *openflow)
1591 const struct ofpact *a;
1593 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1594 ofpact_to_openflow10(a, openflow);
1598 /* Converting ofpacts to OpenFlow 1.1. */
1601 ofpact_output_to_openflow11(const struct ofpact_output *output,
1604 struct ofp11_action_output *oao;
1606 oao = ofputil_put_OFPAT11_OUTPUT(out);
1607 oao->port = ofputil_port_to_ofp11(output->port);
1608 oao->max_len = htons(output->max_len);
1612 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
1615 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
1616 && (!dec_ttl->ofpact.compat ||
1617 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
1618 ofputil_put_OFPAT11_DEC_NW_TTL(out);
1620 ofpact_dec_ttl_to_nxast(dec_ttl, out);
1625 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1629 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1631 case OFPACT_ENQUEUE:
1635 case OFPACT_SET_VLAN_VID:
1636 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1637 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1640 case OFPACT_SET_VLAN_PCP:
1641 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1642 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1645 case OFPACT_STRIP_VLAN:
1646 ofputil_put_OFPAT11_POP_VLAN(out);
1649 case OFPACT_PUSH_VLAN:
1650 /* XXX ETH_TYPE_VLAN_8021AD case */
1651 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
1652 htons(ETH_TYPE_VLAN_8021Q);
1655 case OFPACT_SET_QUEUE:
1656 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
1657 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1660 case OFPACT_SET_ETH_SRC:
1661 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1662 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1665 case OFPACT_SET_ETH_DST:
1666 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1667 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1670 case OFPACT_SET_IPV4_SRC:
1671 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1672 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1675 case OFPACT_SET_IPV4_DST:
1676 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1677 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1680 case OFPACT_SET_IPV4_DSCP:
1681 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1682 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1685 case OFPACT_SET_L4_SRC_PORT:
1686 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1687 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1690 case OFPACT_SET_L4_DST_PORT:
1691 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1692 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1695 case OFPACT_DEC_TTL:
1696 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
1699 case OFPACT_WRITE_METADATA:
1700 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
1703 case OFPACT_PUSH_MPLS:
1704 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
1705 ofpact_get_PUSH_MPLS(a)->ethertype;
1708 case OFPACT_POP_MPLS:
1709 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
1710 ofpact_get_POP_MPLS(a)->ethertype;
1714 case OFPACT_CLEAR_ACTIONS:
1715 case OFPACT_GOTO_TABLE:
1718 case OFPACT_CONTROLLER:
1719 case OFPACT_OUTPUT_REG:
1721 case OFPACT_REG_MOVE:
1722 case OFPACT_REG_LOAD:
1723 case OFPACT_SET_TUNNEL:
1724 case OFPACT_POP_QUEUE:
1725 case OFPACT_FIN_TIMEOUT:
1726 case OFPACT_RESUBMIT:
1728 case OFPACT_MULTIPATH:
1729 case OFPACT_AUTOPATH:
1732 ofpact_to_nxast(a, out);
1737 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1738 * 1.1 actions in 'openflow', appending the actions to any existing data in
1741 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1742 size_t ofpacts_len, struct ofpbuf *openflow)
1744 const struct ofpact *a;
1745 size_t start_size = openflow->size;
1747 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1748 ofpact_to_openflow11(a, openflow);
1751 return openflow->size - start_size;
1755 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
1757 struct ofp11_instruction_actions *oia;
1759 /* Update the instruction's length (or, if it's empty, delete it). */
1760 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1761 if (openflow->size > ofs + sizeof *oia) {
1762 oia->len = htons(openflow->size - ofs);
1764 openflow->size = ofs;
1769 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1771 struct ofpbuf *openflow)
1773 const struct ofpact *a;
1775 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1776 /* XXX Write-Actions */
1778 if (a->type == OFPACT_CLEAR_ACTIONS) {
1779 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
1780 } else if (a->type == OFPACT_GOTO_TABLE) {
1781 struct ofp11_instruction_goto_table *oigt;
1783 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1784 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1785 memset(oigt->pad, 0, sizeof oigt->pad);
1786 } else if (a->type == OFPACT_WRITE_METADATA) {
1787 const struct ofpact_metadata *om;
1788 struct ofp11_instruction_write_metadata *oiwm;
1790 om = ofpact_get_WRITE_METADATA(a);
1791 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
1792 oiwm->metadata = om->metadata;
1793 oiwm->metadata_mask = om->mask;
1794 } else if (!ofpact_is_instruction(a)) {
1796 const size_t ofs = openflow->size;
1797 const size_t ofpacts_len_left =
1798 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1799 const struct ofpact *action;
1800 const struct ofpact *processed = a;
1802 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1803 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
1804 if (ofpact_is_instruction(action)) {
1807 ofpact_to_openflow11(action, openflow);
1810 ofpacts_update_instruction_actions(openflow, ofs);
1816 /* Returns true if 'action' outputs to 'port', false otherwise. */
1818 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1820 switch (ofpact->type) {
1822 return ofpact_get_OUTPUT(ofpact)->port == port;
1823 case OFPACT_ENQUEUE:
1824 return ofpact_get_ENQUEUE(ofpact)->port == port;
1825 case OFPACT_CONTROLLER:
1826 return port == OFPP_CONTROLLER;
1828 case OFPACT_OUTPUT_REG:
1830 case OFPACT_SET_VLAN_VID:
1831 case OFPACT_SET_VLAN_PCP:
1832 case OFPACT_STRIP_VLAN:
1833 case OFPACT_PUSH_VLAN:
1834 case OFPACT_SET_ETH_SRC:
1835 case OFPACT_SET_ETH_DST:
1836 case OFPACT_SET_IPV4_SRC:
1837 case OFPACT_SET_IPV4_DST:
1838 case OFPACT_SET_IPV4_DSCP:
1839 case OFPACT_SET_L4_SRC_PORT:
1840 case OFPACT_SET_L4_DST_PORT:
1841 case OFPACT_REG_MOVE:
1842 case OFPACT_REG_LOAD:
1843 case OFPACT_DEC_TTL:
1844 case OFPACT_SET_TUNNEL:
1845 case OFPACT_WRITE_METADATA:
1846 case OFPACT_SET_QUEUE:
1847 case OFPACT_POP_QUEUE:
1848 case OFPACT_FIN_TIMEOUT:
1849 case OFPACT_RESUBMIT:
1851 case OFPACT_MULTIPATH:
1852 case OFPACT_AUTOPATH:
1855 case OFPACT_PUSH_MPLS:
1856 case OFPACT_POP_MPLS:
1857 case OFPACT_CLEAR_ACTIONS:
1858 case OFPACT_GOTO_TABLE:
1864 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1865 * to 'port', false otherwise. */
1867 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1870 const struct ofpact *a;
1872 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1873 if (ofpact_outputs_to_port(a, port)) {
1882 ofpacts_equal(const struct ofpact *a, size_t a_len,
1883 const struct ofpact *b, size_t b_len)
1885 return a_len == b_len && !memcmp(a, b, a_len);
1888 /* Formatting ofpacts. */
1891 print_note(const struct ofpact_note *note, struct ds *string)
1895 ds_put_cstr(string, "note:");
1896 for (i = 0; i < note->length; i++) {
1898 ds_put_char(string, '.');
1900 ds_put_format(string, "%02"PRIx8, note->data[i]);
1905 print_dec_ttl(const struct ofpact_cnt_ids *ids,
1910 ds_put_cstr(s, "dec_ttl");
1911 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
1912 ds_put_cstr(s, "(");
1913 for (i = 0; i < ids->n_controllers; i++) {
1915 ds_put_cstr(s, ",");
1917 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
1919 ds_put_cstr(s, ")");
1924 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1927 ds_put_cstr(s, "fin_timeout(");
1928 if (fin_timeout->fin_idle_timeout) {
1929 ds_put_format(s, "idle_timeout=%"PRIu16",",
1930 fin_timeout->fin_idle_timeout);
1932 if (fin_timeout->fin_hard_timeout) {
1933 ds_put_format(s, "hard_timeout=%"PRIu16",",
1934 fin_timeout->fin_hard_timeout);
1937 ds_put_char(s, ')');
1941 ofpact_format(const struct ofpact *a, struct ds *s)
1943 const struct ofpact_enqueue *enqueue;
1944 const struct ofpact_resubmit *resubmit;
1945 const struct ofpact_autopath *autopath;
1946 const struct ofpact_controller *controller;
1947 const struct ofpact_metadata *metadata;
1948 const struct ofpact_tunnel *tunnel;
1953 port = ofpact_get_OUTPUT(a)->port;
1954 if (port < OFPP_MAX) {
1955 ds_put_format(s, "output:%"PRIu16, port);
1957 ofputil_format_port(port, s);
1958 if (port == OFPP_CONTROLLER) {
1959 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1964 case OFPACT_CONTROLLER:
1965 controller = ofpact_get_CONTROLLER(a);
1966 if (controller->reason == OFPR_ACTION &&
1967 controller->controller_id == 0) {
1968 ds_put_format(s, "CONTROLLER:%"PRIu16,
1969 ofpact_get_CONTROLLER(a)->max_len);
1971 enum ofp_packet_in_reason reason = controller->reason;
1973 ds_put_cstr(s, "controller(");
1974 if (reason != OFPR_ACTION) {
1975 ds_put_format(s, "reason=%s,",
1976 ofputil_packet_in_reason_to_string(reason));
1978 if (controller->max_len != UINT16_MAX) {
1979 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1981 if (controller->controller_id != 0) {
1982 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1985 ds_put_char(s, ')');
1989 case OFPACT_ENQUEUE:
1990 enqueue = ofpact_get_ENQUEUE(a);
1991 ds_put_format(s, "enqueue:");
1992 ofputil_format_port(enqueue->port, s);
1993 ds_put_format(s, "q%"PRIu32, enqueue->queue);
1996 case OFPACT_OUTPUT_REG:
1997 ds_put_cstr(s, "output:");
1998 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2002 bundle_format(ofpact_get_BUNDLE(a), s);
2005 case OFPACT_SET_VLAN_VID:
2006 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2007 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2010 case OFPACT_SET_VLAN_PCP:
2011 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2012 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2015 case OFPACT_STRIP_VLAN:
2016 ds_put_cstr(s, "strip_vlan");
2019 case OFPACT_PUSH_VLAN:
2020 /* XXX 802.1AD case*/
2021 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2024 case OFPACT_SET_ETH_SRC:
2025 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2026 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2029 case OFPACT_SET_ETH_DST:
2030 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2031 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2034 case OFPACT_SET_IPV4_SRC:
2035 ds_put_format(s, "mod_nw_src:"IP_FMT,
2036 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2039 case OFPACT_SET_IPV4_DST:
2040 ds_put_format(s, "mod_nw_dst:"IP_FMT,
2041 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2044 case OFPACT_SET_IPV4_DSCP:
2045 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
2048 case OFPACT_SET_L4_SRC_PORT:
2049 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2052 case OFPACT_SET_L4_DST_PORT:
2053 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2056 case OFPACT_REG_MOVE:
2057 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2060 case OFPACT_REG_LOAD:
2061 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2064 case OFPACT_DEC_TTL:
2065 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2068 case OFPACT_SET_TUNNEL:
2069 tunnel = ofpact_get_SET_TUNNEL(a);
2070 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2071 (tunnel->tun_id > UINT32_MAX
2072 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2076 case OFPACT_SET_QUEUE:
2077 ds_put_format(s, "set_queue:%"PRIu32,
2078 ofpact_get_SET_QUEUE(a)->queue_id);
2081 case OFPACT_POP_QUEUE:
2082 ds_put_cstr(s, "pop_queue");
2085 case OFPACT_FIN_TIMEOUT:
2086 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2089 case OFPACT_RESUBMIT:
2090 resubmit = ofpact_get_RESUBMIT(a);
2091 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2092 ds_put_cstr(s, "resubmit:");
2093 ofputil_format_port(resubmit->in_port, s);
2095 ds_put_format(s, "resubmit(");
2096 if (resubmit->in_port != OFPP_IN_PORT) {
2097 ofputil_format_port(resubmit->in_port, s);
2099 ds_put_char(s, ',');
2100 if (resubmit->table_id != 255) {
2101 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2103 ds_put_char(s, ')');
2108 learn_format(ofpact_get_LEARN(a), s);
2111 case OFPACT_MULTIPATH:
2112 multipath_format(ofpact_get_MULTIPATH(a), s);
2115 case OFPACT_AUTOPATH:
2116 autopath = ofpact_get_AUTOPATH(a);
2117 ds_put_cstr(s, "autopath(");
2118 ofputil_format_port(autopath->port, s);
2119 ds_put_char(s, ',');
2120 mf_format_subfield(&autopath->dst, s);
2121 ds_put_char(s, ')');
2125 print_note(ofpact_get_NOTE(a), s);
2128 case OFPACT_PUSH_MPLS:
2129 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2130 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2133 case OFPACT_POP_MPLS:
2134 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2135 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2139 ds_put_cstr(s, "exit");
2142 case OFPACT_CLEAR_ACTIONS:
2143 ds_put_format(s, "%s",
2144 ofpact_instruction_name_from_type(
2145 OVSINST_OFPIT11_CLEAR_ACTIONS));
2148 case OFPACT_WRITE_METADATA:
2149 metadata = ofpact_get_WRITE_METADATA(a);
2150 ds_put_format(s, "%s:%#"PRIx64,
2151 ofpact_instruction_name_from_type(
2152 OVSINST_OFPIT11_WRITE_METADATA),
2153 ntohll(metadata->metadata));
2154 if (metadata->mask != htonll(UINT64_MAX)) {
2155 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2159 case OFPACT_GOTO_TABLE:
2160 ds_put_format(s, "%s:%"PRIu8,
2161 ofpact_instruction_name_from_type(
2162 OVSINST_OFPIT11_GOTO_TABLE),
2163 ofpact_get_GOTO_TABLE(a)->table_id);
2168 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2169 * 'ofpacts' to 'string'. */
2171 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2174 ds_put_cstr(string, "actions=");
2176 ds_put_cstr(string, "drop");
2178 const struct ofpact *a;
2180 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2182 ds_put_cstr(string, ",");
2185 /* XXX write-actions */
2186 ofpact_format(a, string);
2191 /* Internal use by helpers. */
2194 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2196 struct ofpact *ofpact;
2198 ofpact_pad(ofpacts);
2199 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2200 ofpact_init(ofpact, type, len);
2205 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2207 memset(ofpact, 0, len);
2208 ofpact->type = type;
2209 ofpact->compat = OFPUTIL_ACTION_INVALID;
2213 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2214 * starting at 'ofpact'.
2216 * This is the correct way to update a variable-length ofpact's length after
2217 * adding the variable-length part of the payload. (See the large comment
2218 * near the end of ofp-actions.h for more information.) */
2220 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2222 ovs_assert(ofpact == ofpacts->l2);
2223 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2226 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2227 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2228 * client must call this itself after adding the final ofpact to an array of
2231 * (The consequences of failing to call this function are probably not dire.
2232 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2233 * not dereference it. That's undefined behavior, technically, but it will not
2234 * cause a real problem on common systems. Still, it seems better to call
2237 ofpact_pad(struct ofpbuf *ofpacts)
2239 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2241 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2246 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2249 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2250 load->dst.field = mf;
2252 load->dst.n_bits = mf->n_bits;
2253 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2254 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);