2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-actions.h"
20 #include "byte-order.h"
22 #include "dynamic-string.h"
24 #include "meta-flow.h"
25 #include "multipath.h"
32 VLOG_DEFINE_THIS_MODULE(ofp_actions);
34 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
36 /* Converting OpenFlow 1.0 to ofpacts. */
40 struct ofp_action_header header;
41 struct ofp_action_vendor_header vendor;
42 struct ofp10_action_output output10;
43 struct ofp_action_vlan_vid vlan_vid;
44 struct ofp_action_vlan_pcp vlan_pcp;
45 struct ofp_action_nw_addr nw_addr;
46 struct ofp_action_nw_tos nw_tos;
47 struct ofp11_action_nw_ecn nw_ecn;
48 struct ofp11_action_nw_ttl nw_ttl;
49 struct ofp_action_tp_port tp_port;
51 OFP_ASSERT(sizeof(union ofp_action) == 8);
54 output_from_openflow10(const struct ofp10_action_output *oao,
57 struct ofpact_output *output;
59 output = ofpact_put_OUTPUT(out);
60 output->port = u16_to_ofp(ntohs(oao->port));
61 output->max_len = ntohs(oao->max_len);
63 return ofputil_check_output_port(output->port, OFPP_MAX);
67 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
70 struct ofpact_enqueue *enqueue;
72 enqueue = ofpact_put_ENQUEUE(out);
73 enqueue->port = u16_to_ofp(ntohs(oae->port));
74 enqueue->queue = ntohl(oae->queue_id);
75 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
76 && enqueue->port != OFPP_IN_PORT
77 && enqueue->port != OFPP_LOCAL) {
78 return OFPERR_OFPBAC_BAD_OUT_PORT;
84 resubmit_from_openflow(const struct nx_action_resubmit *nar,
87 struct ofpact_resubmit *resubmit;
89 resubmit = ofpact_put_RESUBMIT(out);
90 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
91 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
92 resubmit->table_id = 0xff;
96 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
99 struct ofpact_resubmit *resubmit;
101 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
102 return OFPERR_OFPBAC_BAD_ARGUMENT;
105 resubmit = ofpact_put_RESUBMIT(out);
106 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
107 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
108 resubmit->table_id = nar->table;
113 output_reg_from_openflow(const struct nx_action_output_reg *naor,
116 struct ofpact_output_reg *output_reg;
118 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
119 return OFPERR_OFPBAC_BAD_ARGUMENT;
122 output_reg = ofpact_put_OUTPUT_REG(out);
123 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
124 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
125 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
126 output_reg->max_len = ntohs(naor->max_len);
128 return mf_check_src(&output_reg->src, NULL);
132 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
135 struct ofpact_fin_timeout *oft;
137 oft = ofpact_put_FIN_TIMEOUT(out);
138 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
139 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
143 controller_from_openflow(const struct nx_action_controller *nac,
146 struct ofpact_controller *oc;
148 oc = ofpact_put_CONTROLLER(out);
149 oc->max_len = ntohs(nac->max_len);
150 oc->controller_id = ntohs(nac->controller_id);
151 oc->reason = nac->reason;
155 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
158 struct ofpact_metadata *om;
160 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
161 return OFPERR_NXBRC_MUST_BE_ZERO;
164 om = ofpact_put_WRITE_METADATA(out);
165 om->metadata = nawm->metadata;
166 om->mask = nawm->mask;
172 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
174 struct ofpact_note *note;
177 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
178 note = ofpact_put(out, OFPACT_NOTE,
179 offsetof(struct ofpact_note, data) + length);
180 note->length = length;
181 memcpy(note->data, nan->note, length);
185 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
188 struct ofpact_cnt_ids *ids;
189 enum ofperr error = 0;
191 ids = ofpact_put_DEC_TTL(out);
192 ids->ofpact.compat = compat;
193 ids->n_controllers = 1;
194 ofpbuf_put(out, &id, sizeof id);
196 ofpact_update_len(out, &ids->ofpact);
201 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
204 struct ofpact_cnt_ids *ids;
208 ids = ofpact_put_DEC_TTL(out);
209 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
210 ids->n_controllers = ntohs(nac_ids->n_controllers);
211 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
213 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
214 return OFPERR_NXBRC_MUST_BE_ZERO;
217 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
218 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
219 "allocated for controller ids. %zu bytes are required for "
220 "%"PRIu16" controllers.", ids_size,
221 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
222 return OFPERR_OFPBAC_BAD_LEN;
225 for (i = 0; i < ids->n_controllers; i++) {
226 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
227 ofpbuf_put(out, &id, sizeof id);
231 ofpact_update_len(out, &ids->ofpact);
237 sample_from_openflow(const struct nx_action_sample *nas,
240 struct ofpact_sample *sample;
242 sample = ofpact_put_SAMPLE(out);
243 sample->probability = ntohs(nas->probability);
244 sample->collector_set_id = ntohl(nas->collector_set_id);
245 sample->obs_domain_id = ntohl(nas->obs_domain_id);
246 sample->obs_point_id = ntohl(nas->obs_point_id);
248 if (sample->probability == 0) {
249 return OFPERR_OFPBAC_BAD_ARGUMENT;
256 push_mpls_from_openflow(ovs_be16 ethertype, enum ofpact_mpls_position position,
259 struct ofpact_push_mpls *oam;
261 if (!eth_type_mpls(ethertype)) {
262 return OFPERR_OFPBAC_BAD_ARGUMENT;
264 oam = ofpact_put_PUSH_MPLS(out);
265 oam->ethertype = ethertype;
266 oam->position = position;
272 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
274 const struct nx_action_header *nah = (const struct nx_action_header *) a;
275 uint16_t len = ntohs(a->header.len);
277 if (len < sizeof(struct nx_action_header)) {
278 return OFPERR_OFPBAC_BAD_LEN;
279 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
280 return OFPERR_OFPBAC_BAD_VENDOR;
283 switch (nah->subtype) {
284 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
285 case CONSTANT_HTONS(ENUM): \
287 ? len >= sizeof(struct STRUCT) \
288 : len == sizeof(struct STRUCT)) { \
289 *code = OFPUTIL_##ENUM; \
292 return OFPERR_OFPBAC_BAD_LEN; \
295 #include "ofp-util.def"
297 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
298 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
300 return OFPERR_OFPBAC_BAD_TYPE;
304 /* Parses 'a' to determine its type. On success stores the correct type into
305 * '*code' and returns 0. On failure returns an OFPERR_* error code and
306 * '*code' is indeterminate.
308 * The caller must have already verified that 'a''s length is potentially
309 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
310 * ofp_action) and no longer than the amount of space allocated to 'a').
312 * This function verifies that 'a''s length is correct for the type of action
313 * that it represents. */
315 decode_openflow10_action(const union ofp_action *a,
316 enum ofputil_action_code *code)
319 case CONSTANT_HTONS(OFPAT10_VENDOR):
320 return decode_nxast_action(a, code);
322 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
323 case CONSTANT_HTONS(ENUM): \
324 if (a->header.len == htons(sizeof(struct STRUCT))) { \
325 *code = OFPUTIL_##ENUM; \
328 return OFPERR_OFPBAC_BAD_LEN; \
331 #include "ofp-util.def"
334 return OFPERR_OFPBAC_BAD_TYPE;
339 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
342 const struct nx_action_resubmit *nar;
343 const struct nx_action_set_tunnel *nast;
344 const struct nx_action_set_queue *nasq;
345 const struct nx_action_note *nan;
346 const struct nx_action_set_tunnel64 *nast64;
347 const struct nx_action_write_metadata *nawm;
348 struct ofpact_tunnel *tunnel;
349 enum ofperr error = 0;
352 case OFPUTIL_ACTION_INVALID:
353 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
354 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
355 #include "ofp-util.def"
358 case OFPUTIL_NXAST_RESUBMIT:
359 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
362 case OFPUTIL_NXAST_SET_TUNNEL:
363 nast = (const struct nx_action_set_tunnel *) a;
364 tunnel = ofpact_put_SET_TUNNEL(out);
365 tunnel->ofpact.compat = code;
366 tunnel->tun_id = ntohl(nast->tun_id);
369 case OFPUTIL_NXAST_WRITE_METADATA:
370 nawm = ALIGNED_CAST(const struct nx_action_write_metadata *, a);
371 error = metadata_from_nxast(nawm, out);
374 case OFPUTIL_NXAST_SET_QUEUE:
375 nasq = (const struct nx_action_set_queue *) a;
376 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
379 case OFPUTIL_NXAST_POP_QUEUE:
380 ofpact_put_POP_QUEUE(out);
383 case OFPUTIL_NXAST_REG_MOVE:
384 error = nxm_reg_move_from_openflow(
385 (const struct nx_action_reg_move *) a, out);
388 case OFPUTIL_NXAST_REG_LOAD:
389 error = nxm_reg_load_from_openflow(
390 ALIGNED_CAST(const struct nx_action_reg_load *, a), out);
393 case OFPUTIL_NXAST_STACK_PUSH:
394 error = nxm_stack_push_from_openflow(
395 (const struct nx_action_stack *) a, out);
398 case OFPUTIL_NXAST_STACK_POP:
399 error = nxm_stack_pop_from_openflow(
400 (const struct nx_action_stack *) a, out);
403 case OFPUTIL_NXAST_NOTE:
404 nan = (const struct nx_action_note *) a;
405 note_from_openflow(nan, out);
408 case OFPUTIL_NXAST_SET_TUNNEL64:
409 nast64 = ALIGNED_CAST(const struct nx_action_set_tunnel64 *, a);
410 tunnel = ofpact_put_SET_TUNNEL(out);
411 tunnel->ofpact.compat = code;
412 tunnel->tun_id = ntohll(nast64->tun_id);
415 case OFPUTIL_NXAST_MULTIPATH:
416 error = multipath_from_openflow((const struct nx_action_multipath *) a,
417 ofpact_put_MULTIPATH(out));
420 case OFPUTIL_NXAST_BUNDLE:
421 case OFPUTIL_NXAST_BUNDLE_LOAD:
422 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
425 case OFPUTIL_NXAST_OUTPUT_REG:
426 error = output_reg_from_openflow(
427 (const struct nx_action_output_reg *) a, out);
430 case OFPUTIL_NXAST_RESUBMIT_TABLE:
431 nar = (const struct nx_action_resubmit *) a;
432 error = resubmit_table_from_openflow(nar, out);
435 case OFPUTIL_NXAST_LEARN:
436 error = learn_from_openflow(
437 ALIGNED_CAST(const struct nx_action_learn *, a), out);
440 case OFPUTIL_NXAST_EXIT:
441 ofpact_put_EXIT(out);
444 case OFPUTIL_NXAST_DEC_TTL:
445 error = dec_ttl_from_openflow(out, code);
448 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
449 error = dec_ttl_cnt_ids_from_openflow(
450 (const struct nx_action_cnt_ids *) a, out);
453 case OFPUTIL_NXAST_FIN_TIMEOUT:
454 fin_timeout_from_openflow(
455 (const struct nx_action_fin_timeout *) a, out);
458 case OFPUTIL_NXAST_CONTROLLER:
459 controller_from_openflow((const struct nx_action_controller *) a, out);
462 case OFPUTIL_NXAST_PUSH_MPLS: {
463 struct nx_action_push_mpls *nxapm = (struct nx_action_push_mpls *)a;
464 error = push_mpls_from_openflow(nxapm->ethertype,
465 OFPACT_MPLS_AFTER_VLAN, out);
469 case OFPUTIL_NXAST_SET_MPLS_TTL: {
470 struct nx_action_mpls_ttl *nxamt = (struct nx_action_mpls_ttl *)a;
471 ofpact_put_SET_MPLS_TTL(out)->ttl = nxamt->ttl;
475 case OFPUTIL_NXAST_DEC_MPLS_TTL:
476 ofpact_put_DEC_MPLS_TTL(out);
479 case OFPUTIL_NXAST_POP_MPLS: {
480 struct nx_action_pop_mpls *nxapm = (struct nx_action_pop_mpls *)a;
481 if (eth_type_mpls(nxapm->ethertype)) {
482 return OFPERR_OFPBAC_BAD_ARGUMENT;
484 ofpact_put_POP_MPLS(out)->ethertype = nxapm->ethertype;
488 case OFPUTIL_NXAST_SAMPLE:
489 error = sample_from_openflow(
490 (const struct nx_action_sample *) a, out);
498 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
500 enum ofputil_action_code code;
503 error = decode_openflow10_action(a, &code);
509 case OFPUTIL_ACTION_INVALID:
510 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
511 #include "ofp-util.def"
514 case OFPUTIL_OFPAT10_OUTPUT:
515 return output_from_openflow10(&a->output10, out);
517 case OFPUTIL_OFPAT10_SET_VLAN_VID:
518 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
519 return OFPERR_OFPBAC_BAD_ARGUMENT;
521 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
524 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
525 if (a->vlan_pcp.vlan_pcp & ~7) {
526 return OFPERR_OFPBAC_BAD_ARGUMENT;
528 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
531 case OFPUTIL_OFPAT10_STRIP_VLAN:
532 ofpact_put_STRIP_VLAN(out);
535 case OFPUTIL_OFPAT10_SET_DL_SRC:
536 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
537 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
540 case OFPUTIL_OFPAT10_SET_DL_DST:
541 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
542 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
545 case OFPUTIL_OFPAT10_SET_NW_SRC:
546 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
549 case OFPUTIL_OFPAT10_SET_NW_DST:
550 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
553 case OFPUTIL_OFPAT10_SET_NW_TOS:
554 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
555 return OFPERR_OFPBAC_BAD_ARGUMENT;
557 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
560 case OFPUTIL_OFPAT10_SET_TP_SRC:
561 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
564 case OFPUTIL_OFPAT10_SET_TP_DST:
565 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
569 case OFPUTIL_OFPAT10_ENQUEUE:
570 error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
574 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
575 #include "ofp-util.def"
576 return ofpact_from_nxast(a, code, out);
582 static inline union ofp_action *
583 action_next(const union ofp_action *a)
585 return ((union ofp_action *) (void *)
586 ((uint8_t *) a + ntohs(a->header.len)));
590 action_is_valid(const union ofp_action *a, size_t n_actions)
592 uint16_t len = ntohs(a->header.len);
593 return (!(len % OFP_ACTION_ALIGN)
595 && len / sizeof *a <= n_actions);
598 /* This macro is careful to check for actions with bad lengths. */
599 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
600 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
601 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
602 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
603 (ITER) = action_next(ITER)))
606 log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
609 if (!VLOG_DROP_WARN(&rl)) {
613 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
614 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
615 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
621 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
623 enum ofperr (*ofpact_from_openflow)(
624 const union ofp_action *a, struct ofpbuf *out))
626 const union ofp_action *a;
629 ACTION_FOR_EACH (a, left, in, n_in) {
630 enum ofperr error = ofpact_from_openflow(a, out);
632 log_bad_action(in, n_in, a - in, error);
637 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
638 log_bad_action(in, n_in, n_in - left, error);
647 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
650 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
654 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
655 struct ofpbuf *ofpacts,
656 enum ofperr (*translate)(const union ofp_action *actions,
658 struct ofpbuf *ofpacts))
660 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
661 const union ofp_action *actions;
664 ofpbuf_clear(ofpacts);
666 if (actions_len % OFP_ACTION_ALIGN != 0) {
667 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
668 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
669 return OFPERR_OFPBRC_BAD_LEN;
672 actions = ofpbuf_try_pull(openflow, actions_len);
673 if (actions == NULL) {
674 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
675 "remaining message length (%zu)",
676 actions_len, openflow->size);
677 return OFPERR_OFPBRC_BAD_LEN;
680 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
682 ofpbuf_clear(ofpacts);
686 error = ofpacts_verify(ofpacts->data, ofpacts->size);
688 ofpbuf_clear(ofpacts);
693 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
694 * front of 'openflow' into ofpacts. On success, replaces any existing content
695 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
696 * Returns 0 if successful, otherwise an OpenFlow error.
698 * The parsed actions are valid generically, but they may not be valid in a
699 * specific context. For example, port numbers up to OFPP_MAX are valid
700 * generically, but specific datapaths may only support port numbers in a
701 * smaller range. Use ofpacts_check() to additional check whether actions are
702 * valid in a specific context. */
704 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
705 struct ofpbuf *ofpacts)
707 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
708 ofpacts_from_openflow10);
711 /* OpenFlow 1.1 actions. */
713 /* Parses 'a' to determine its type. On success stores the correct type into
714 * '*code' and returns 0. On failure returns an OFPERR_* error code and
715 * '*code' is indeterminate.
717 * The caller must have already verified that 'a''s length is potentially
718 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
719 * ofp_action) and no longer than the amount of space allocated to 'a').
721 * This function verifies that 'a''s length is correct for the type of action
722 * that it represents. */
724 decode_openflow11_action(const union ofp_action *a,
725 enum ofputil_action_code *code)
730 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
731 return decode_nxast_action(a, code);
733 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
734 case CONSTANT_HTONS(ENUM): \
735 len = ntohs(a->header.len); \
737 ? len >= sizeof(struct STRUCT) \
738 : len == sizeof(struct STRUCT)) { \
739 *code = OFPUTIL_##ENUM; \
742 return OFPERR_OFPBAC_BAD_LEN; \
745 #include "ofp-util.def"
748 return OFPERR_OFPBAC_BAD_TYPE;
753 output_from_openflow11(const struct ofp11_action_output *oao,
756 struct ofpact_output *output;
759 output = ofpact_put_OUTPUT(out);
760 output->max_len = ntohs(oao->max_len);
762 error = ofputil_port_from_ofp11(oao->port, &output->port);
767 return ofputil_check_output_port(output->port, OFPP_MAX);
771 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
773 enum ofputil_action_code code;
776 error = decode_openflow11_action(a, &code);
782 case OFPUTIL_ACTION_INVALID:
783 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
784 #include "ofp-util.def"
787 case OFPUTIL_OFPAT11_OUTPUT:
788 return output_from_openflow11((const struct ofp11_action_output *) a,
791 case OFPUTIL_OFPAT11_SET_VLAN_VID:
792 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
793 return OFPERR_OFPBAC_BAD_ARGUMENT;
795 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
798 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
799 if (a->vlan_pcp.vlan_pcp & ~7) {
800 return OFPERR_OFPBAC_BAD_ARGUMENT;
802 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
805 case OFPUTIL_OFPAT11_PUSH_VLAN:
806 if (((const struct ofp11_action_push *)a)->ethertype !=
807 htons(ETH_TYPE_VLAN_8021Q)) {
808 /* XXX 802.1AD(QinQ) isn't supported at the moment */
809 return OFPERR_OFPBAC_BAD_ARGUMENT;
811 ofpact_put_PUSH_VLAN(out);
814 case OFPUTIL_OFPAT11_POP_VLAN:
815 ofpact_put_STRIP_VLAN(out);
818 case OFPUTIL_OFPAT11_SET_QUEUE:
819 ofpact_put_SET_QUEUE(out)->queue_id =
820 ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
823 case OFPUTIL_OFPAT11_SET_DL_SRC:
824 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
825 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
828 case OFPUTIL_OFPAT11_SET_DL_DST:
829 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
830 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
833 case OFPUTIL_OFPAT11_DEC_NW_TTL:
834 dec_ttl_from_openflow(out, code);
837 case OFPUTIL_OFPAT11_SET_NW_SRC:
838 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
841 case OFPUTIL_OFPAT11_SET_NW_DST:
842 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
845 case OFPUTIL_OFPAT11_SET_NW_TOS:
846 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
847 return OFPERR_OFPBAC_BAD_ARGUMENT;
849 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
852 case OFPUTIL_OFPAT11_SET_NW_ECN:
853 if (a->nw_ecn.nw_ecn & ~IP_ECN_MASK) {
854 return OFPERR_OFPBAC_BAD_ARGUMENT;
856 ofpact_put_SET_IP_ECN(out)->ecn = a->nw_ecn.nw_ecn;
859 case OFPUTIL_OFPAT11_SET_NW_TTL:
860 ofpact_put_SET_IP_TTL(out)->ttl = a->nw_ttl.nw_ttl;
863 case OFPUTIL_OFPAT11_SET_TP_SRC:
864 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
867 case OFPUTIL_OFPAT11_SET_TP_DST:
868 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
871 case OFPUTIL_OFPAT12_SET_FIELD:
872 return nxm_reg_load_from_openflow12_set_field(
873 (const struct ofp12_action_set_field *)a, out);
875 case OFPUTIL_OFPAT11_SET_MPLS_TTL: {
876 struct ofp11_action_mpls_ttl *oamt = (struct ofp11_action_mpls_ttl *)a;
877 ofpact_put_SET_MPLS_TTL(out)->ttl = oamt->mpls_ttl;
881 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
882 ofpact_put_DEC_MPLS_TTL(out);
885 case OFPUTIL_OFPAT11_PUSH_MPLS: {
886 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
887 error = push_mpls_from_openflow(oap->ethertype,
888 OFPACT_MPLS_AFTER_VLAN, out);
892 case OFPUTIL_OFPAT11_POP_MPLS: {
893 struct ofp11_action_pop_mpls *oapm = (struct ofp11_action_pop_mpls *)a;
894 if (eth_type_mpls(oapm->ethertype)) {
895 return OFPERR_OFPBAC_BAD_ARGUMENT;
897 ofpact_put_POP_MPLS(out)->ethertype = oapm->ethertype;
901 case OFPUTIL_OFPAT11_GROUP: {
902 struct ofp11_action_group *oag = (struct ofp11_action_group *)a;
903 ofpact_put_GROUP(out)->group_id = ntohl(oag->group_id);
907 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
908 #include "ofp-util.def"
909 return ofpact_from_nxast(a, code, out);
916 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
919 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
922 /* True if an action sets the value of a field
923 * in a way that is compatibile with the action set.
924 * False otherwise. */
926 ofpact_is_set_action(const struct ofpact *a)
929 case OFPACT_REG_LOAD:
930 case OFPACT_SET_ETH_DST:
931 case OFPACT_SET_ETH_SRC:
932 case OFPACT_SET_IP_DSCP:
933 case OFPACT_SET_IP_ECN:
934 case OFPACT_SET_IP_TTL:
935 case OFPACT_SET_IPV4_DST:
936 case OFPACT_SET_IPV4_SRC:
937 case OFPACT_SET_L4_DST_PORT:
938 case OFPACT_SET_L4_SRC_PORT:
939 case OFPACT_SET_MPLS_TTL:
940 case OFPACT_SET_QUEUE:
941 case OFPACT_SET_TUNNEL:
942 case OFPACT_SET_VLAN_PCP:
943 case OFPACT_SET_VLAN_VID:
946 case OFPACT_CLEAR_ACTIONS:
947 case OFPACT_CONTROLLER:
948 case OFPACT_DEC_MPLS_TTL:
952 case OFPACT_FIN_TIMEOUT:
953 case OFPACT_GOTO_TABLE:
957 case OFPACT_MULTIPATH:
960 case OFPACT_OUTPUT_REG:
961 case OFPACT_POP_MPLS:
962 case OFPACT_POP_QUEUE:
963 case OFPACT_PUSH_MPLS:
964 case OFPACT_PUSH_VLAN:
965 case OFPACT_REG_MOVE:
966 case OFPACT_RESUBMIT:
968 case OFPACT_STACK_POP:
969 case OFPACT_STACK_PUSH:
970 case OFPACT_STRIP_VLAN:
971 case OFPACT_WRITE_ACTIONS:
972 case OFPACT_WRITE_METADATA:
979 /* True if an action is allowed in the action set.
980 * False otherwise. */
982 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
985 case OFPACT_DEC_MPLS_TTL:
989 case OFPACT_POP_MPLS:
990 case OFPACT_PUSH_MPLS:
991 case OFPACT_PUSH_VLAN:
992 case OFPACT_REG_LOAD:
993 case OFPACT_SET_ETH_DST:
994 case OFPACT_SET_ETH_SRC:
995 case OFPACT_SET_IP_DSCP:
996 case OFPACT_SET_IP_ECN:
997 case OFPACT_SET_IP_TTL:
998 case OFPACT_SET_IPV4_DST:
999 case OFPACT_SET_IPV4_SRC:
1000 case OFPACT_SET_L4_DST_PORT:
1001 case OFPACT_SET_L4_SRC_PORT:
1002 case OFPACT_SET_MPLS_TTL:
1003 case OFPACT_SET_QUEUE:
1004 case OFPACT_SET_TUNNEL:
1005 case OFPACT_SET_VLAN_PCP:
1006 case OFPACT_SET_VLAN_VID:
1007 case OFPACT_STRIP_VLAN:
1010 /* In general these actions are excluded because they are not part of
1011 * the OpenFlow specification nor map to actions that are defined in
1012 * the specification. Thus the order in which they should be applied
1013 * in the action set is undefined. */
1015 case OFPACT_CONTROLLER:
1016 case OFPACT_ENQUEUE:
1018 case OFPACT_FIN_TIMEOUT:
1020 case OFPACT_MULTIPATH:
1022 case OFPACT_OUTPUT_REG:
1023 case OFPACT_POP_QUEUE:
1024 case OFPACT_REG_MOVE:
1025 case OFPACT_RESUBMIT:
1027 case OFPACT_STACK_POP:
1028 case OFPACT_STACK_PUSH:
1030 /* The action set may only include actions and thus
1031 * may not include any instructions */
1032 case OFPACT_CLEAR_ACTIONS:
1033 case OFPACT_GOTO_TABLE:
1035 case OFPACT_WRITE_ACTIONS:
1036 case OFPACT_WRITE_METADATA:
1043 /* Append ofpact 'a' onto the tail of 'out' */
1045 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1047 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1050 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1052 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1053 enum ofpact_type filter)
1055 const struct ofpact *target;
1056 const struct ofpact *a;
1059 OFPACT_FOR_EACH (a, in->data, in->size) {
1060 if (a->type == filter) {
1065 ofpact_copy(out, target);
1067 return target != NULL;
1070 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1071 * The order of appended ofpacts is preserved between 'in' and 'out' */
1073 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1074 bool (*filter)(const struct ofpact *))
1076 const struct ofpact *a;
1078 OFPACT_FOR_EACH (a, in->data, in->size) {
1080 ofpact_copy(out, a);
1085 /* Reads 'action_set', which contains ofpacts accumulated by
1086 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1087 * executed directly into 'action_list'. (These names correspond to the
1088 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1090 * In general this involves appending the last instance of each action that is
1091 * adimissible in the action set in the order described in the OpenFlow
1095 * + output action is only appended if no group action was present in 'in'.
1096 * + As a simplification all set actions are copied in the order the are
1097 * provided in 'in' as many set actions applied to a field has the same
1098 * affect as only applying the last action that sets a field and
1099 * duplicates are removed by do_xlate_actions().
1100 * This has an unwanted side-effect of compsoting multiple
1101 * LOAD_REG actions that touch different regions of the same field. */
1103 ofpacts_execute_action_set(struct ofpbuf *action_list,
1104 const struct ofpbuf *action_set)
1106 /* The OpenFlow spec "Action Set" section specifies this order. */
1107 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1108 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1109 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1110 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1111 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1112 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1113 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1114 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1116 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1117 * we should execute only OFPACT_GROUP.
1119 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1120 * all the actions because there's no point in modifying a packet that will
1121 * not be sent anywhere. */
1122 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1123 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1124 ofpbuf_clear(action_list);
1130 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1131 size_t n_in, struct ofpbuf *out)
1135 size_t start = out->size;
1137 error = ofpacts_from_openflow11(in, n_in, out);
1142 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1143 if (!ofpact_is_allowed_in_actions_set(a)) {
1144 VLOG_WARN_RL(&rl, "disallowed action in action set");
1145 return OFPERR_OFPBAC_BAD_TYPE;
1154 ofpact_from_openflow13(const union ofp_action *a, struct ofpbuf *out)
1156 enum ofputil_action_code code;
1159 error = decode_openflow11_action(a, &code);
1164 if (code == OFPUTIL_OFPAT11_PUSH_MPLS) {
1165 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
1166 error = push_mpls_from_openflow(oap->ethertype,
1167 OFPACT_MPLS_BEFORE_VLAN, out);
1169 error = ofpact_from_openflow11(a, out);
1176 ofpacts_from_openflow13(const union ofp_action *in, size_t n_in,
1179 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow13);
1182 /* OpenFlow 1.1 instructions. */
1184 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1185 static inline const struct STRUCT * OVS_UNUSED \
1186 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1188 ovs_assert(inst->type == htons(ENUM)); \
1189 return ALIGNED_CAST(struct STRUCT *, inst); \
1192 static inline void OVS_UNUSED \
1193 instruction_init_##ENUM(struct STRUCT *s) \
1195 memset(s, 0, sizeof *s); \
1196 s->type = htons(ENUM); \
1197 s->len = htons(sizeof *s); \
1200 static inline struct STRUCT * OVS_UNUSED \
1201 instruction_put_##ENUM(struct ofpbuf *buf) \
1203 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1204 instruction_init_##ENUM(s); \
1210 struct instruction_type_info {
1211 enum ovs_instruction_type type;
1215 static const struct instruction_type_info inst_info[] = {
1216 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1222 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1224 return inst_info[type].name;
1228 ovs_instruction_type_from_name(const char *name)
1230 const struct instruction_type_info *p;
1231 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1232 if (!strcasecmp(name, p->name)) {
1239 enum ovs_instruction_type
1240 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1244 return OVSINST_OFPIT13_METER;
1245 case OFPACT_CLEAR_ACTIONS:
1246 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1247 case OFPACT_WRITE_ACTIONS:
1248 return OVSINST_OFPIT11_WRITE_ACTIONS;
1249 case OFPACT_WRITE_METADATA:
1250 return OVSINST_OFPIT11_WRITE_METADATA;
1251 case OFPACT_GOTO_TABLE:
1252 return OVSINST_OFPIT11_GOTO_TABLE;
1255 case OFPACT_CONTROLLER:
1256 case OFPACT_ENQUEUE:
1257 case OFPACT_OUTPUT_REG:
1259 case OFPACT_SET_VLAN_VID:
1260 case OFPACT_SET_VLAN_PCP:
1261 case OFPACT_STRIP_VLAN:
1262 case OFPACT_PUSH_VLAN:
1263 case OFPACT_SET_ETH_SRC:
1264 case OFPACT_SET_ETH_DST:
1265 case OFPACT_SET_IPV4_SRC:
1266 case OFPACT_SET_IPV4_DST:
1267 case OFPACT_SET_IP_DSCP:
1268 case OFPACT_SET_IP_ECN:
1269 case OFPACT_SET_IP_TTL:
1270 case OFPACT_SET_L4_SRC_PORT:
1271 case OFPACT_SET_L4_DST_PORT:
1272 case OFPACT_REG_MOVE:
1273 case OFPACT_REG_LOAD:
1274 case OFPACT_STACK_PUSH:
1275 case OFPACT_STACK_POP:
1276 case OFPACT_DEC_TTL:
1277 case OFPACT_SET_MPLS_TTL:
1278 case OFPACT_DEC_MPLS_TTL:
1279 case OFPACT_PUSH_MPLS:
1280 case OFPACT_POP_MPLS:
1281 case OFPACT_SET_TUNNEL:
1282 case OFPACT_SET_QUEUE:
1283 case OFPACT_POP_QUEUE:
1284 case OFPACT_FIN_TIMEOUT:
1285 case OFPACT_RESUBMIT:
1287 case OFPACT_MULTIPATH:
1292 return OVSINST_OFPIT11_APPLY_ACTIONS;
1296 static inline struct ofp11_instruction *
1297 instruction_next(const struct ofp11_instruction *inst)
1299 return ((struct ofp11_instruction *) (void *)
1300 ((uint8_t *) inst + ntohs(inst->len)));
1304 instruction_is_valid(const struct ofp11_instruction *inst,
1305 size_t n_instructions)
1307 uint16_t len = ntohs(inst->len);
1308 return (!(len % OFP11_INSTRUCTION_ALIGN)
1309 && len >= sizeof *inst
1310 && len / sizeof *inst <= n_instructions);
1313 /* This macro is careful to check for instructions with bad lengths. */
1314 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1315 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1316 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1317 ((LEFT) -= (ntohs((ITER)->len) \
1318 / sizeof(struct ofp11_instruction)), \
1319 (ITER) = instruction_next(ITER)))
1322 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1323 enum ovs_instruction_type *type)
1325 uint16_t len = ntohs(inst->len);
1327 switch (inst->type) {
1328 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1329 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1331 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1332 case CONSTANT_HTONS(ENUM): \
1334 ? len >= sizeof(struct STRUCT) \
1335 : len == sizeof(struct STRUCT)) { \
1336 *type = OVSINST_##ENUM; \
1339 return OFPERR_OFPBIC_BAD_LEN; \
1345 return OFPERR_OFPBIC_UNKNOWN_INST;
1350 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1352 const struct ofp11_instruction *out[])
1354 const struct ofp11_instruction *inst;
1357 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1358 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1359 enum ovs_instruction_type type;
1362 error = decode_openflow11_instruction(inst, &type);
1368 return OFPERR_ONFBIC_DUP_INSTRUCTION;
1374 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1375 (n_insts - left) * sizeof *inst);
1376 return OFPERR_OFPBIC_BAD_LEN;
1382 get_actions_from_instruction(const struct ofp11_instruction *inst,
1383 const union ofp_action **actions,
1386 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1387 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1390 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
1391 * front of 'openflow' into ofpacts. On success, replaces any existing content
1392 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1393 * Returns 0 if successful, otherwise an OpenFlow error.
1395 * Actions are processed according to their OpenFlow version which
1396 * is provided in the 'version' parameter.
1398 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1399 * instructions, so you should call ofpacts_pull_openflow11_instructions()
1400 * instead of this function.
1402 * The parsed actions are valid generically, but they may not be valid in a
1403 * specific context. For example, port numbers up to OFPP_MAX are valid
1404 * generically, but specific datapaths may only support port numbers in a
1405 * smaller range. Use ofpacts_check() to additional check whether actions are
1406 * valid in a specific context. */
1408 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1409 enum ofp_version version,
1410 unsigned int actions_len,
1411 struct ofpbuf *ofpacts)
1417 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1418 ofpacts_from_openflow11);
1420 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1421 ofpacts_from_openflow13);
1428 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1429 enum ofp_version version,
1430 unsigned int instructions_len,
1431 struct ofpbuf *ofpacts)
1433 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1434 const struct ofp11_instruction *instructions;
1435 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1438 ofpbuf_clear(ofpacts);
1440 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1441 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1443 instructions_len, OFP11_INSTRUCTION_ALIGN);
1444 error = OFPERR_OFPBIC_BAD_LEN;
1448 instructions = ofpbuf_try_pull(openflow, instructions_len);
1449 if (instructions == NULL) {
1450 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1451 "remaining message length (%zu)",
1452 instructions_len, openflow->size);
1453 error = OFPERR_OFPBIC_BAD_LEN;
1457 error = decode_openflow11_instructions(
1458 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1464 if (insts[OVSINST_OFPIT13_METER]) {
1465 const struct ofp13_instruction_meter *oim;
1466 struct ofpact_meter *om;
1468 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1469 insts[OVSINST_OFPIT13_METER]);
1471 om = ofpact_put_METER(ofpacts);
1472 om->meter_id = ntohl(oim->meter_id);
1474 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1475 const union ofp_action *actions;
1478 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1479 &actions, &n_actions);
1484 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1487 error = ofpacts_from_openflow13(actions, n_actions, ofpacts);
1496 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1497 instruction_get_OFPIT11_CLEAR_ACTIONS(
1498 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1499 ofpact_put_CLEAR_ACTIONS(ofpacts);
1501 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1502 struct ofpact_nest *on;
1503 const union ofp_action *actions;
1507 ofpact_pad(ofpacts);
1508 start = ofpacts->size;
1509 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1510 offsetof(struct ofpact_nest, actions));
1511 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1512 &actions, &n_actions);
1513 error = ofpacts_from_openflow11_for_action_set(actions, n_actions,
1518 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1519 on->ofpact.len = ofpacts->size - start;
1521 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1522 const struct ofp11_instruction_write_metadata *oiwm;
1523 struct ofpact_metadata *om;
1525 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1526 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1528 om = ofpact_put_WRITE_METADATA(ofpacts);
1529 om->metadata = oiwm->metadata;
1530 om->mask = oiwm->metadata_mask;
1532 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1533 const struct ofp11_instruction_goto_table *oigt;
1534 struct ofpact_goto_table *ogt;
1536 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1537 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1538 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1539 ogt->table_id = oigt->table_id;
1542 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1545 ofpbuf_clear(ofpacts);
1550 /* May modify flow->dl_type, caller must restore it. */
1552 ofpact_check__(const struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
1555 const struct ofpact_enqueue *enqueue;
1559 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1562 case OFPACT_CONTROLLER:
1565 case OFPACT_ENQUEUE:
1566 enqueue = ofpact_get_ENQUEUE(a);
1567 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1568 && enqueue->port != OFPP_IN_PORT
1569 && enqueue->port != OFPP_LOCAL) {
1570 return OFPERR_OFPBAC_BAD_OUT_PORT;
1574 case OFPACT_OUTPUT_REG:
1575 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1578 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1580 case OFPACT_SET_VLAN_VID:
1581 case OFPACT_SET_VLAN_PCP:
1582 case OFPACT_STRIP_VLAN:
1583 case OFPACT_PUSH_VLAN:
1584 case OFPACT_SET_ETH_SRC:
1585 case OFPACT_SET_ETH_DST:
1586 case OFPACT_SET_IPV4_SRC:
1587 case OFPACT_SET_IPV4_DST:
1588 case OFPACT_SET_IP_DSCP:
1589 case OFPACT_SET_IP_ECN:
1590 case OFPACT_SET_IP_TTL:
1591 case OFPACT_SET_L4_SRC_PORT:
1592 case OFPACT_SET_L4_DST_PORT:
1595 case OFPACT_REG_MOVE:
1596 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1598 case OFPACT_REG_LOAD:
1599 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1601 case OFPACT_STACK_PUSH:
1602 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1604 case OFPACT_STACK_POP:
1605 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1607 case OFPACT_DEC_TTL:
1608 case OFPACT_SET_MPLS_TTL:
1609 case OFPACT_DEC_MPLS_TTL:
1610 case OFPACT_SET_TUNNEL:
1611 case OFPACT_SET_QUEUE:
1612 case OFPACT_POP_QUEUE:
1613 case OFPACT_FIN_TIMEOUT:
1614 case OFPACT_RESUBMIT:
1618 return learn_check(ofpact_get_LEARN(a), flow);
1620 case OFPACT_MULTIPATH:
1621 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1627 case OFPACT_PUSH_MPLS:
1628 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1631 case OFPACT_POP_MPLS:
1632 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1638 case OFPACT_CLEAR_ACTIONS:
1641 case OFPACT_WRITE_ACTIONS: {
1642 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
1643 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
1644 flow, max_ports, table_id);
1647 case OFPACT_WRITE_METADATA:
1650 case OFPACT_METER: {
1651 uint32_t mid = ofpact_get_METER(a)->meter_id;
1652 if (mid == 0 || mid > OFPM13_MAX) {
1653 return OFPERR_OFPMMFC_INVALID_METER;
1658 case OFPACT_GOTO_TABLE:
1659 if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
1660 return OFPERR_OFPBRC_BAD_TABLE_ID;
1672 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1673 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1674 * switch with no more than 'max_ports' ports.
1676 * May temporarily modify 'flow', but restores the changes before returning. */
1678 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1679 struct flow *flow, ofp_port_t max_ports, uint8_t table_id)
1681 const struct ofpact *a;
1682 ovs_be16 dl_type = flow->dl_type;
1683 enum ofperr error = 0;
1685 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1686 error = ofpact_check__(a, flow, max_ports, table_id);
1691 flow->dl_type = dl_type; /* Restore. */
1695 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1696 * in the appropriate order as defined by the OpenFlow spec. */
1698 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1700 const struct ofpact *a;
1701 enum ovs_instruction_type inst;
1703 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1704 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1705 enum ovs_instruction_type next;
1707 next = ovs_instruction_type_from_ofpact_type(a->type);
1708 if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
1711 const char *name = ovs_instruction_name_from_type(inst);
1712 const char *next_name = ovs_instruction_name_from_type(next);
1715 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1716 "1.1+ compatibility", name);
1718 VLOG_WARN("invalid instruction ordering: %s must appear "
1719 "before %s, for OpenFlow 1.1+ compatibility",
1722 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1731 /* Converting ofpacts to Nicira OpenFlow extensions. */
1734 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1737 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1739 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1740 output_reg->src.n_bits);
1741 naor->src = htonl(output_reg->src.field->nxm_header);
1742 naor->max_len = htons(output_reg->max_len);
1746 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1749 struct nx_action_resubmit *nar;
1751 if (resubmit->table_id == 0xff
1752 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1753 nar = ofputil_put_NXAST_RESUBMIT(out);
1755 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1756 nar->table = resubmit->table_id;
1758 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
1762 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1765 uint64_t tun_id = tunnel->tun_id;
1767 if (tun_id <= UINT32_MAX
1768 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1769 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1771 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1776 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1779 struct nx_action_write_metadata *nawm;
1781 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1782 nawm->metadata = om->metadata;
1783 nawm->mask = om->mask;
1787 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1789 size_t start_ofs = out->size;
1790 struct nx_action_note *nan;
1791 unsigned int remainder;
1794 nan = ofputil_put_NXAST_NOTE(out);
1795 out->size -= sizeof nan->note;
1797 ofpbuf_put(out, note->data, note->length);
1799 len = out->size - start_ofs;
1800 remainder = len % OFP_ACTION_ALIGN;
1802 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1804 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
1805 nan->len = htons(out->size - start_ofs);
1809 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1812 struct nx_action_controller *nac;
1814 nac = ofputil_put_NXAST_CONTROLLER(out);
1815 nac->max_len = htons(oc->max_len);
1816 nac->controller_id = htons(oc->controller_id);
1817 nac->reason = oc->reason;
1821 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1824 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1825 ofputil_put_NXAST_DEC_TTL(out);
1827 struct nx_action_cnt_ids *nac_ids =
1828 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1829 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1833 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1834 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1836 ids = ofpbuf_put_zeros(out, ids_len);
1837 for (i = 0; i < oc_ids->n_controllers; i++) {
1838 ids[i] = htons(oc_ids->cnt_ids[i]);
1844 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1847 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1848 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1849 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1853 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1856 struct nx_action_sample *nas;
1858 nas = ofputil_put_NXAST_SAMPLE(out);
1859 nas->probability = htons(os->probability);
1860 nas->collector_set_id = htonl(os->collector_set_id);
1861 nas->obs_domain_id = htonl(os->obs_domain_id);
1862 nas->obs_point_id = htonl(os->obs_point_id);
1866 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1869 case OFPACT_CONTROLLER:
1870 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1873 case OFPACT_OUTPUT_REG:
1874 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1878 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1881 case OFPACT_REG_MOVE:
1882 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1885 case OFPACT_REG_LOAD:
1886 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1889 case OFPACT_STACK_PUSH:
1890 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1893 case OFPACT_STACK_POP:
1894 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1897 case OFPACT_DEC_TTL:
1898 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1901 case OFPACT_SET_MPLS_TTL:
1902 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1903 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1906 case OFPACT_DEC_MPLS_TTL:
1907 ofputil_put_NXAST_DEC_MPLS_TTL(out);
1910 case OFPACT_SET_TUNNEL:
1911 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1914 case OFPACT_WRITE_METADATA:
1915 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1918 case OFPACT_SET_QUEUE:
1919 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1920 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1923 case OFPACT_POP_QUEUE:
1924 ofputil_put_NXAST_POP_QUEUE(out);
1927 case OFPACT_FIN_TIMEOUT:
1928 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1931 case OFPACT_RESUBMIT:
1932 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1936 learn_to_nxast(ofpact_get_LEARN(a), out);
1939 case OFPACT_MULTIPATH:
1940 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1944 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1948 ofputil_put_NXAST_EXIT(out);
1951 case OFPACT_PUSH_MPLS:
1952 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1953 ofpact_get_PUSH_MPLS(a)->ethertype;
1956 case OFPACT_POP_MPLS:
1957 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1958 ofpact_get_POP_MPLS(a)->ethertype;
1962 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
1967 case OFPACT_ENQUEUE:
1968 case OFPACT_SET_VLAN_VID:
1969 case OFPACT_SET_VLAN_PCP:
1970 case OFPACT_STRIP_VLAN:
1971 case OFPACT_PUSH_VLAN:
1972 case OFPACT_SET_ETH_SRC:
1973 case OFPACT_SET_ETH_DST:
1974 case OFPACT_SET_IPV4_SRC:
1975 case OFPACT_SET_IPV4_DST:
1976 case OFPACT_SET_IP_DSCP:
1977 case OFPACT_SET_IP_ECN:
1978 case OFPACT_SET_IP_TTL:
1979 case OFPACT_SET_L4_SRC_PORT:
1980 case OFPACT_SET_L4_DST_PORT:
1981 case OFPACT_WRITE_ACTIONS:
1982 case OFPACT_CLEAR_ACTIONS:
1983 case OFPACT_GOTO_TABLE:
1989 /* Converting ofpacts to OpenFlow 1.0. */
1992 ofpact_output_to_openflow10(const struct ofpact_output *output,
1995 struct ofp10_action_output *oao;
1997 oao = ofputil_put_OFPAT10_OUTPUT(out);
1998 oao->port = htons(ofp_to_u16(output->port));
1999 oao->max_len = htons(output->max_len);
2003 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2006 struct ofp10_action_enqueue *oae;
2008 oae = ofputil_put_OFPAT10_ENQUEUE(out);
2009 oae->port = htons(ofp_to_u16(enqueue->port));
2010 oae->queue_id = htonl(enqueue->queue);
2014 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2018 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2021 case OFPACT_ENQUEUE:
2022 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2025 case OFPACT_SET_VLAN_VID:
2026 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2027 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2030 case OFPACT_SET_VLAN_PCP:
2031 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2032 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2035 case OFPACT_STRIP_VLAN:
2036 ofputil_put_OFPAT10_STRIP_VLAN(out);
2039 case OFPACT_SET_ETH_SRC:
2040 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2041 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2044 case OFPACT_SET_ETH_DST:
2045 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2046 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2049 case OFPACT_SET_IPV4_SRC:
2050 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2051 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2054 case OFPACT_SET_IPV4_DST:
2055 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2056 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2059 case OFPACT_SET_IP_DSCP:
2060 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2061 = ofpact_get_SET_IP_DSCP(a)->dscp;
2064 case OFPACT_SET_L4_SRC_PORT:
2065 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2066 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2069 case OFPACT_SET_L4_DST_PORT:
2070 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2071 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2074 case OFPACT_PUSH_VLAN:
2075 case OFPACT_CLEAR_ACTIONS:
2076 case OFPACT_WRITE_ACTIONS:
2077 case OFPACT_GOTO_TABLE:
2085 case OFPACT_CONTROLLER:
2086 case OFPACT_OUTPUT_REG:
2088 case OFPACT_REG_MOVE:
2089 case OFPACT_REG_LOAD:
2090 case OFPACT_STACK_PUSH:
2091 case OFPACT_STACK_POP:
2092 case OFPACT_DEC_TTL:
2093 case OFPACT_SET_IP_ECN:
2094 case OFPACT_SET_IP_TTL:
2095 case OFPACT_SET_MPLS_TTL:
2096 case OFPACT_DEC_MPLS_TTL:
2097 case OFPACT_SET_TUNNEL:
2098 case OFPACT_WRITE_METADATA:
2099 case OFPACT_SET_QUEUE:
2100 case OFPACT_POP_QUEUE:
2101 case OFPACT_FIN_TIMEOUT:
2102 case OFPACT_RESUBMIT:
2104 case OFPACT_MULTIPATH:
2107 case OFPACT_PUSH_MPLS:
2108 case OFPACT_POP_MPLS:
2110 ofpact_to_nxast(a, out);
2115 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
2116 * actions in 'openflow', appending the actions to any existing data in
2119 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
2120 struct ofpbuf *openflow)
2122 const struct ofpact *a;
2124 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2125 ofpact_to_openflow10(a, openflow);
2129 /* Converting ofpacts to OpenFlow 1.1. */
2132 ofpact_output_to_openflow11(const struct ofpact_output *output,
2135 struct ofp11_action_output *oao;
2137 oao = ofputil_put_OFPAT11_OUTPUT(out);
2138 oao->port = ofputil_port_to_ofp11(output->port);
2139 oao->max_len = htons(output->max_len);
2143 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2146 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2147 && (!dec_ttl->ofpact.compat ||
2148 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2149 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2151 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2156 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2160 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2162 case OFPACT_ENQUEUE:
2166 case OFPACT_SET_VLAN_VID:
2167 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2168 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2171 case OFPACT_SET_VLAN_PCP:
2172 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2173 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2176 case OFPACT_STRIP_VLAN:
2177 ofputil_put_OFPAT11_POP_VLAN(out);
2180 case OFPACT_PUSH_VLAN:
2181 /* XXX ETH_TYPE_VLAN_8021AD case */
2182 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2183 htons(ETH_TYPE_VLAN_8021Q);
2186 case OFPACT_SET_QUEUE:
2187 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2188 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2191 case OFPACT_SET_ETH_SRC:
2192 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2193 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2196 case OFPACT_SET_ETH_DST:
2197 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2198 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2201 case OFPACT_SET_IPV4_SRC:
2202 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2203 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2206 case OFPACT_SET_IPV4_DST:
2207 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2208 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2211 case OFPACT_SET_IP_DSCP:
2212 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2213 = ofpact_get_SET_IP_DSCP(a)->dscp;
2216 case OFPACT_SET_IP_ECN:
2217 ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2218 = ofpact_get_SET_IP_ECN(a)->ecn;
2221 case OFPACT_SET_IP_TTL:
2222 ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2223 = ofpact_get_SET_IP_TTL(a)->ttl;
2226 case OFPACT_SET_L4_SRC_PORT:
2227 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2228 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2231 case OFPACT_SET_L4_DST_PORT:
2232 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2233 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2236 case OFPACT_DEC_TTL:
2237 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2240 case OFPACT_SET_MPLS_TTL:
2241 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2242 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2245 case OFPACT_DEC_MPLS_TTL:
2246 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2249 case OFPACT_WRITE_METADATA:
2250 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2253 case OFPACT_PUSH_MPLS:
2254 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2255 ofpact_get_PUSH_MPLS(a)->ethertype;
2258 case OFPACT_POP_MPLS:
2259 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2260 ofpact_get_POP_MPLS(a)->ethertype;
2264 case OFPACT_CLEAR_ACTIONS:
2265 case OFPACT_WRITE_ACTIONS:
2266 case OFPACT_GOTO_TABLE:
2271 ofputil_put_OFPAT11_GROUP(out)->group_id =
2272 htonl(ofpact_get_GROUP(a)->group_id);
2275 case OFPACT_CONTROLLER:
2276 case OFPACT_OUTPUT_REG:
2278 case OFPACT_REG_MOVE:
2279 case OFPACT_REG_LOAD:
2280 case OFPACT_STACK_PUSH:
2281 case OFPACT_STACK_POP:
2282 case OFPACT_SET_TUNNEL:
2283 case OFPACT_POP_QUEUE:
2284 case OFPACT_FIN_TIMEOUT:
2285 case OFPACT_RESUBMIT:
2287 case OFPACT_MULTIPATH:
2291 ofpact_to_nxast(a, out);
2296 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
2297 * 1.1 actions in 'openflow', appending the actions to any existing data in
2300 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
2301 size_t ofpacts_len, struct ofpbuf *openflow)
2303 const struct ofpact *a;
2304 size_t start_size = openflow->size;
2306 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2307 ofpact_to_openflow11(a, openflow);
2310 return openflow->size - start_size;
2314 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2316 struct ofp11_instruction_actions *oia;
2318 /* Update the instruction's length (or, if it's empty, delete it). */
2319 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2320 if (openflow->size > ofs + sizeof *oia) {
2321 oia->len = htons(openflow->size - ofs);
2323 openflow->size = ofs;
2328 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
2330 struct ofpbuf *openflow)
2332 const struct ofpact *a;
2334 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2335 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2336 case OVSINST_OFPIT11_CLEAR_ACTIONS:
2337 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2340 case OVSINST_OFPIT11_GOTO_TABLE: {
2341 struct ofp11_instruction_goto_table *oigt;
2342 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2343 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2344 memset(oigt->pad, 0, sizeof oigt->pad);
2348 case OVSINST_OFPIT11_WRITE_METADATA: {
2349 const struct ofpact_metadata *om;
2350 struct ofp11_instruction_write_metadata *oiwm;
2352 om = ofpact_get_WRITE_METADATA(a);
2353 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2354 oiwm->metadata = om->metadata;
2355 oiwm->metadata_mask = om->mask;
2359 case OVSINST_OFPIT13_METER: {
2360 const struct ofpact_meter *om;
2361 struct ofp13_instruction_meter *oim;
2363 om = ofpact_get_METER(a);
2364 oim = instruction_put_OFPIT13_METER(openflow);
2365 oim->meter_id = htonl(om->meter_id);
2369 case OVSINST_OFPIT11_APPLY_ACTIONS: {
2370 const size_t ofs = openflow->size;
2371 const size_t ofpacts_len_left =
2372 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2373 const struct ofpact *action;
2374 const struct ofpact *processed = a;
2376 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2377 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
2378 if (ovs_instruction_type_from_ofpact_type(action->type)
2379 != OVSINST_OFPIT11_APPLY_ACTIONS) {
2382 ofpact_to_openflow11(action, openflow);
2385 ofpacts_update_instruction_actions(openflow, ofs);
2390 case OVSINST_OFPIT11_WRITE_ACTIONS: {
2391 const size_t ofs = openflow->size;
2392 const struct ofpact_nest *on;
2394 on = ofpact_get_WRITE_ACTIONS(a);
2395 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2396 ofpacts_put_openflow11_actions(on->actions,
2397 ofpact_nest_get_action_len(on),
2399 ofpacts_update_instruction_actions(openflow, ofs);
2407 /* Returns true if 'action' outputs to 'port', false otherwise. */
2409 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
2411 switch (ofpact->type) {
2413 return ofpact_get_OUTPUT(ofpact)->port == port;
2414 case OFPACT_ENQUEUE:
2415 return ofpact_get_ENQUEUE(ofpact)->port == port;
2416 case OFPACT_CONTROLLER:
2417 return port == OFPP_CONTROLLER;
2419 case OFPACT_OUTPUT_REG:
2421 case OFPACT_SET_VLAN_VID:
2422 case OFPACT_SET_VLAN_PCP:
2423 case OFPACT_STRIP_VLAN:
2424 case OFPACT_PUSH_VLAN:
2425 case OFPACT_SET_ETH_SRC:
2426 case OFPACT_SET_ETH_DST:
2427 case OFPACT_SET_IPV4_SRC:
2428 case OFPACT_SET_IPV4_DST:
2429 case OFPACT_SET_IP_DSCP:
2430 case OFPACT_SET_IP_ECN:
2431 case OFPACT_SET_IP_TTL:
2432 case OFPACT_SET_L4_SRC_PORT:
2433 case OFPACT_SET_L4_DST_PORT:
2434 case OFPACT_REG_MOVE:
2435 case OFPACT_REG_LOAD:
2436 case OFPACT_STACK_PUSH:
2437 case OFPACT_STACK_POP:
2438 case OFPACT_DEC_TTL:
2439 case OFPACT_SET_MPLS_TTL:
2440 case OFPACT_DEC_MPLS_TTL:
2441 case OFPACT_SET_TUNNEL:
2442 case OFPACT_WRITE_METADATA:
2443 case OFPACT_SET_QUEUE:
2444 case OFPACT_POP_QUEUE:
2445 case OFPACT_FIN_TIMEOUT:
2446 case OFPACT_RESUBMIT:
2448 case OFPACT_MULTIPATH:
2451 case OFPACT_PUSH_MPLS:
2452 case OFPACT_POP_MPLS:
2454 case OFPACT_CLEAR_ACTIONS:
2455 case OFPACT_WRITE_ACTIONS:
2456 case OFPACT_GOTO_TABLE:
2464 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2465 * to 'port', false otherwise. */
2467 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
2470 const struct ofpact *a;
2472 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2473 if (ofpact_outputs_to_port(a, port)) {
2481 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2482 * to 'group', false otherwise. */
2484 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
2487 const struct ofpact *a;
2489 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2490 if (a->type == OFPACT_GROUP
2491 && ofpact_get_GROUP(a)->group_id == group_id) {
2500 ofpacts_equal(const struct ofpact *a, size_t a_len,
2501 const struct ofpact *b, size_t b_len)
2503 return a_len == b_len && !memcmp(a, b, a_len);
2506 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2507 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2509 * This function relies on the order of 'ofpacts' being correct (as checked by
2510 * ofpacts_verify()). */
2512 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2514 const struct ofpact *a;
2516 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2517 enum ovs_instruction_type inst;
2519 inst = ovs_instruction_type_from_ofpact_type(a->type);
2520 if (a->type == OFPACT_METER) {
2521 return ofpact_get_METER(a)->meter_id;
2522 } else if (inst > OVSINST_OFPIT13_METER) {
2530 /* Formatting ofpacts. */
2533 print_note(const struct ofpact_note *note, struct ds *string)
2537 ds_put_cstr(string, "note:");
2538 for (i = 0; i < note->length; i++) {
2540 ds_put_char(string, '.');
2542 ds_put_format(string, "%02"PRIx8, note->data[i]);
2547 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2552 ds_put_cstr(s, "dec_ttl");
2553 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2554 ds_put_cstr(s, "(");
2555 for (i = 0; i < ids->n_controllers; i++) {
2557 ds_put_cstr(s, ",");
2559 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2561 ds_put_cstr(s, ")");
2566 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2569 ds_put_cstr(s, "fin_timeout(");
2570 if (fin_timeout->fin_idle_timeout) {
2571 ds_put_format(s, "idle_timeout=%"PRIu16",",
2572 fin_timeout->fin_idle_timeout);
2574 if (fin_timeout->fin_hard_timeout) {
2575 ds_put_format(s, "hard_timeout=%"PRIu16",",
2576 fin_timeout->fin_hard_timeout);
2579 ds_put_char(s, ')');
2583 ofpact_format(const struct ofpact *a, struct ds *s)
2585 const struct ofpact_enqueue *enqueue;
2586 const struct ofpact_resubmit *resubmit;
2587 const struct ofpact_controller *controller;
2588 const struct ofpact_metadata *metadata;
2589 const struct ofpact_tunnel *tunnel;
2590 const struct ofpact_sample *sample;
2595 port = ofpact_get_OUTPUT(a)->port;
2596 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
2597 ds_put_format(s, "output:%"PRIu16, port);
2599 ofputil_format_port(port, s);
2600 if (port == OFPP_CONTROLLER) {
2601 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2606 case OFPACT_CONTROLLER:
2607 controller = ofpact_get_CONTROLLER(a);
2608 if (controller->reason == OFPR_ACTION &&
2609 controller->controller_id == 0) {
2610 ds_put_format(s, "CONTROLLER:%"PRIu16,
2611 ofpact_get_CONTROLLER(a)->max_len);
2613 enum ofp_packet_in_reason reason = controller->reason;
2615 ds_put_cstr(s, "controller(");
2616 if (reason != OFPR_ACTION) {
2617 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2619 ds_put_format(s, "reason=%s,",
2620 ofputil_packet_in_reason_to_string(
2621 reason, reasonbuf, sizeof reasonbuf));
2623 if (controller->max_len != UINT16_MAX) {
2624 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2626 if (controller->controller_id != 0) {
2627 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2630 ds_put_char(s, ')');
2634 case OFPACT_ENQUEUE:
2635 enqueue = ofpact_get_ENQUEUE(a);
2636 ds_put_format(s, "enqueue:");
2637 ofputil_format_port(enqueue->port, s);
2638 ds_put_format(s, "q%"PRIu32, enqueue->queue);
2641 case OFPACT_OUTPUT_REG:
2642 ds_put_cstr(s, "output:");
2643 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2647 bundle_format(ofpact_get_BUNDLE(a), s);
2650 case OFPACT_SET_VLAN_VID:
2651 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2652 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2655 case OFPACT_SET_VLAN_PCP:
2656 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2657 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2660 case OFPACT_STRIP_VLAN:
2661 ds_put_cstr(s, "strip_vlan");
2664 case OFPACT_PUSH_VLAN:
2665 /* XXX 802.1AD case*/
2666 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2669 case OFPACT_SET_ETH_SRC:
2670 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2671 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2674 case OFPACT_SET_ETH_DST:
2675 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2676 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2679 case OFPACT_SET_IPV4_SRC:
2680 ds_put_format(s, "mod_nw_src:"IP_FMT,
2681 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2684 case OFPACT_SET_IPV4_DST:
2685 ds_put_format(s, "mod_nw_dst:"IP_FMT,
2686 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2689 case OFPACT_SET_IP_DSCP:
2690 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
2693 case OFPACT_SET_IP_ECN:
2694 ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
2697 case OFPACT_SET_IP_TTL:
2698 ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
2701 case OFPACT_SET_L4_SRC_PORT:
2702 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2705 case OFPACT_SET_L4_DST_PORT:
2706 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2709 case OFPACT_REG_MOVE:
2710 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2713 case OFPACT_REG_LOAD:
2714 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2717 case OFPACT_STACK_PUSH:
2718 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2721 case OFPACT_STACK_POP:
2722 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2725 case OFPACT_DEC_TTL:
2726 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2729 case OFPACT_SET_MPLS_TTL:
2730 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2731 ofpact_get_SET_MPLS_TTL(a)->ttl);
2734 case OFPACT_DEC_MPLS_TTL:
2735 ds_put_cstr(s, "dec_mpls_ttl");
2738 case OFPACT_SET_TUNNEL:
2739 tunnel = ofpact_get_SET_TUNNEL(a);
2740 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2741 (tunnel->tun_id > UINT32_MAX
2742 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2746 case OFPACT_SET_QUEUE:
2747 ds_put_format(s, "set_queue:%"PRIu32,
2748 ofpact_get_SET_QUEUE(a)->queue_id);
2751 case OFPACT_POP_QUEUE:
2752 ds_put_cstr(s, "pop_queue");
2755 case OFPACT_FIN_TIMEOUT:
2756 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2759 case OFPACT_RESUBMIT:
2760 resubmit = ofpact_get_RESUBMIT(a);
2761 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2762 ds_put_cstr(s, "resubmit:");
2763 ofputil_format_port(resubmit->in_port, s);
2765 ds_put_format(s, "resubmit(");
2766 if (resubmit->in_port != OFPP_IN_PORT) {
2767 ofputil_format_port(resubmit->in_port, s);
2769 ds_put_char(s, ',');
2770 if (resubmit->table_id != 255) {
2771 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2773 ds_put_char(s, ')');
2778 learn_format(ofpact_get_LEARN(a), s);
2781 case OFPACT_MULTIPATH:
2782 multipath_format(ofpact_get_MULTIPATH(a), s);
2786 print_note(ofpact_get_NOTE(a), s);
2789 case OFPACT_PUSH_MPLS:
2790 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2791 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2794 case OFPACT_POP_MPLS:
2795 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2796 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2800 ds_put_cstr(s, "exit");
2804 sample = ofpact_get_SAMPLE(a);
2806 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2807 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2808 sample->probability, sample->collector_set_id,
2809 sample->obs_domain_id, sample->obs_point_id);
2812 case OFPACT_WRITE_ACTIONS: {
2813 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2814 ds_put_format(s, "%s(",
2815 ovs_instruction_name_from_type(
2816 OVSINST_OFPIT11_WRITE_ACTIONS));
2817 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
2818 ds_put_char(s, ')');
2822 case OFPACT_CLEAR_ACTIONS:
2823 ds_put_format(s, "%s",
2824 ovs_instruction_name_from_type(
2825 OVSINST_OFPIT11_CLEAR_ACTIONS));
2828 case OFPACT_WRITE_METADATA:
2829 metadata = ofpact_get_WRITE_METADATA(a);
2830 ds_put_format(s, "%s:%#"PRIx64,
2831 ovs_instruction_name_from_type(
2832 OVSINST_OFPIT11_WRITE_METADATA),
2833 ntohll(metadata->metadata));
2834 if (metadata->mask != OVS_BE64_MAX) {
2835 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2839 case OFPACT_GOTO_TABLE:
2840 ds_put_format(s, "%s:%"PRIu8,
2841 ovs_instruction_name_from_type(
2842 OVSINST_OFPIT11_GOTO_TABLE),
2843 ofpact_get_GOTO_TABLE(a)->table_id);
2847 ds_put_format(s, "%s:%"PRIu32,
2848 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
2849 ofpact_get_METER(a)->meter_id);
2853 ds_put_format(s, "group:%"PRIu32,
2854 ofpact_get_GROUP(a)->group_id);
2859 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2860 * 'ofpacts' to 'string'. */
2862 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2866 ds_put_cstr(string, "drop");
2868 const struct ofpact *a;
2870 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2872 ds_put_cstr(string, ",");
2875 /* XXX write-actions */
2876 ofpact_format(a, string);
2881 /* Internal use by helpers. */
2884 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2886 struct ofpact *ofpact;
2888 ofpact_pad(ofpacts);
2889 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2890 ofpact_init(ofpact, type, len);
2895 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2897 memset(ofpact, 0, len);
2898 ofpact->type = type;
2899 ofpact->compat = OFPUTIL_ACTION_INVALID;
2903 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2904 * starting at 'ofpact'.
2906 * This is the correct way to update a variable-length ofpact's length after
2907 * adding the variable-length part of the payload. (See the large comment
2908 * near the end of ofp-actions.h for more information.) */
2910 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2912 ovs_assert(ofpact == ofpacts->l2);
2913 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2916 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2917 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2918 * client must call this itself after adding the final ofpact to an array of
2921 * (The consequences of failing to call this function are probably not dire.
2922 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2923 * not dereference it. That's undefined behavior, technically, but it will not
2924 * cause a real problem on common systems. Still, it seems better to call
2927 ofpact_pad(struct ofpbuf *ofpacts)
2929 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2931 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2936 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2939 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2940 load->dst.field = mf;
2942 load->dst.n_bits = mf->n_bits;
2943 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2944 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);