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;
50 struct ofp_action_dl_addr dl_addr;
51 struct ofp10_action_enqueue enqueue;
52 struct ofp11_action_output ofp11_output;
53 struct ofp11_action_push push;
54 struct ofp11_action_pop_mpls ofp11_pop_mpls;
55 struct ofp11_action_set_queue ofp11_set_queue;
56 struct ofp11_action_mpls_ttl ofp11_mpls_ttl;
57 struct ofp11_action_group group;
58 struct ofp12_action_set_field set_field;
59 struct nx_action_header nxa_header;
60 struct nx_action_resubmit resubmit;
61 struct nx_action_set_tunnel set_tunnel;
62 struct nx_action_set_tunnel64 set_tunnel64;
63 struct nx_action_write_metadata write_metadata;
64 struct nx_action_set_queue set_queue;
65 struct nx_action_reg_move reg_move;
66 struct nx_action_reg_load reg_load;
67 struct nx_action_stack stack;
68 struct nx_action_note note;
69 struct nx_action_multipath multipath;
70 struct nx_action_bundle bundle;
71 struct nx_action_output_reg output_reg;
72 struct nx_action_cnt_ids cnt_ids;
73 struct nx_action_fin_timeout fin_timeout;
74 struct nx_action_controller controller;
75 struct nx_action_push_mpls push_mpls;
76 struct nx_action_mpls_ttl mpls_ttl;
77 struct nx_action_pop_mpls pop_mpls;
78 struct nx_action_sample sample;
79 struct nx_action_learn learn;
83 output_from_openflow10(const struct ofp10_action_output *oao,
86 struct ofpact_output *output;
88 output = ofpact_put_OUTPUT(out);
89 output->port = u16_to_ofp(ntohs(oao->port));
90 output->max_len = ntohs(oao->max_len);
92 return ofputil_check_output_port(output->port, OFPP_MAX);
96 enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
99 struct ofpact_enqueue *enqueue;
101 enqueue = ofpact_put_ENQUEUE(out);
102 enqueue->port = u16_to_ofp(ntohs(oae->port));
103 enqueue->queue = ntohl(oae->queue_id);
104 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
105 && enqueue->port != OFPP_IN_PORT
106 && enqueue->port != OFPP_LOCAL) {
107 return OFPERR_OFPBAC_BAD_OUT_PORT;
113 resubmit_from_openflow(const struct nx_action_resubmit *nar,
116 struct ofpact_resubmit *resubmit;
118 resubmit = ofpact_put_RESUBMIT(out);
119 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
120 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
121 resubmit->table_id = 0xff;
125 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
128 struct ofpact_resubmit *resubmit;
130 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
131 return OFPERR_OFPBAC_BAD_ARGUMENT;
134 resubmit = ofpact_put_RESUBMIT(out);
135 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
136 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
137 resubmit->table_id = nar->table;
142 output_reg_from_openflow(const struct nx_action_output_reg *naor,
145 struct ofpact_output_reg *output_reg;
147 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
148 return OFPERR_OFPBAC_BAD_ARGUMENT;
151 output_reg = ofpact_put_OUTPUT_REG(out);
152 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
153 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
154 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
155 output_reg->max_len = ntohs(naor->max_len);
157 return mf_check_src(&output_reg->src, NULL);
161 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
164 struct ofpact_fin_timeout *oft;
166 oft = ofpact_put_FIN_TIMEOUT(out);
167 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
168 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
172 controller_from_openflow(const struct nx_action_controller *nac,
175 struct ofpact_controller *oc;
177 oc = ofpact_put_CONTROLLER(out);
178 oc->max_len = ntohs(nac->max_len);
179 oc->controller_id = ntohs(nac->controller_id);
180 oc->reason = nac->reason;
184 metadata_from_nxast(const struct nx_action_write_metadata *nawm,
187 struct ofpact_metadata *om;
189 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
190 return OFPERR_NXBRC_MUST_BE_ZERO;
193 om = ofpact_put_WRITE_METADATA(out);
194 om->metadata = nawm->metadata;
195 om->mask = nawm->mask;
201 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
203 struct ofpact_note *note;
206 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
207 note = ofpact_put(out, OFPACT_NOTE,
208 offsetof(struct ofpact_note, data) + length);
209 note->length = length;
210 memcpy(note->data, nan->note, length);
214 dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
217 struct ofpact_cnt_ids *ids;
218 enum ofperr error = 0;
220 ids = ofpact_put_DEC_TTL(out);
221 ids->ofpact.compat = compat;
222 ids->n_controllers = 1;
223 ofpbuf_put(out, &id, sizeof id);
225 ofpact_update_len(out, &ids->ofpact);
230 dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
233 struct ofpact_cnt_ids *ids;
237 ids = ofpact_put_DEC_TTL(out);
238 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
239 ids->n_controllers = ntohs(nac_ids->n_controllers);
240 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
242 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
243 return OFPERR_NXBRC_MUST_BE_ZERO;
246 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
247 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
248 "allocated for controller ids. %zu bytes are required for "
249 "%"PRIu16" controllers.", ids_size,
250 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
251 return OFPERR_OFPBAC_BAD_LEN;
254 for (i = 0; i < ids->n_controllers; i++) {
255 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
256 ofpbuf_put(out, &id, sizeof id);
260 ofpact_update_len(out, &ids->ofpact);
266 sample_from_openflow(const struct nx_action_sample *nas,
269 struct ofpact_sample *sample;
271 sample = ofpact_put_SAMPLE(out);
272 sample->probability = ntohs(nas->probability);
273 sample->collector_set_id = ntohl(nas->collector_set_id);
274 sample->obs_domain_id = ntohl(nas->obs_domain_id);
275 sample->obs_point_id = ntohl(nas->obs_point_id);
277 if (sample->probability == 0) {
278 return OFPERR_OFPBAC_BAD_ARGUMENT;
285 push_mpls_from_openflow(ovs_be16 ethertype, enum ofpact_mpls_position position,
288 struct ofpact_push_mpls *oam;
290 if (!eth_type_mpls(ethertype)) {
291 return OFPERR_OFPBAC_BAD_ARGUMENT;
293 oam = ofpact_put_PUSH_MPLS(out);
294 oam->ethertype = ethertype;
295 oam->position = position;
301 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
303 const struct nx_action_header *nah = &a->nxa_header;
304 uint16_t len = ntohs(a->header.len);
306 if (len < sizeof(struct nx_action_header)) {
307 return OFPERR_OFPBAC_BAD_LEN;
308 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
309 return OFPERR_OFPBAC_BAD_VENDOR;
312 switch (nah->subtype) {
313 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
314 case CONSTANT_HTONS(ENUM): \
316 ? len >= sizeof(struct STRUCT) \
317 : len == sizeof(struct STRUCT)) { \
318 *code = OFPUTIL_##ENUM; \
321 return OFPERR_OFPBAC_BAD_LEN; \
324 #include "ofp-util.def"
326 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
327 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
329 return OFPERR_OFPBAC_BAD_TYPE;
333 /* Parses 'a' to determine its type. On success stores the correct type into
334 * '*code' and returns 0. On failure returns an OFPERR_* error code and
335 * '*code' is indeterminate.
337 * The caller must have already verified that 'a''s length is potentially
338 * correct (that is, a->header.len is nonzero and a multiple of
339 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
341 * This function verifies that 'a''s length is correct for the type of action
342 * that it represents. */
344 decode_openflow10_action(const union ofp_action *a,
345 enum ofputil_action_code *code)
348 case CONSTANT_HTONS(OFPAT10_VENDOR):
349 return decode_nxast_action(a, code);
351 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
352 case CONSTANT_HTONS(ENUM): \
353 if (a->header.len == htons(sizeof(struct STRUCT))) { \
354 *code = OFPUTIL_##ENUM; \
357 return OFPERR_OFPBAC_BAD_LEN; \
360 #include "ofp-util.def"
363 return OFPERR_OFPBAC_BAD_TYPE;
368 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
371 struct ofpact_tunnel *tunnel;
372 enum ofperr error = 0;
375 case OFPUTIL_ACTION_INVALID:
376 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
377 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
378 #include "ofp-util.def"
381 case OFPUTIL_NXAST_RESUBMIT:
382 resubmit_from_openflow(&a->resubmit, out);
385 case OFPUTIL_NXAST_SET_TUNNEL:
386 tunnel = ofpact_put_SET_TUNNEL(out);
387 tunnel->ofpact.compat = code;
388 tunnel->tun_id = ntohl(a->set_tunnel.tun_id);
391 case OFPUTIL_NXAST_WRITE_METADATA:
392 error = metadata_from_nxast(&a->write_metadata, out);
395 case OFPUTIL_NXAST_SET_QUEUE:
396 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(a->set_queue.queue_id);
399 case OFPUTIL_NXAST_POP_QUEUE:
400 ofpact_put_POP_QUEUE(out);
403 case OFPUTIL_NXAST_REG_MOVE:
404 error = nxm_reg_move_from_openflow(&a->reg_move, out);
407 case OFPUTIL_NXAST_REG_LOAD:
408 error = nxm_reg_load_from_openflow(&a->reg_load, out);
411 case OFPUTIL_NXAST_STACK_PUSH:
412 error = nxm_stack_push_from_openflow(&a->stack, out);
415 case OFPUTIL_NXAST_STACK_POP:
416 error = nxm_stack_pop_from_openflow(&a->stack, out);
419 case OFPUTIL_NXAST_NOTE:
420 note_from_openflow(&a->note, out);
423 case OFPUTIL_NXAST_SET_TUNNEL64:
424 tunnel = ofpact_put_SET_TUNNEL(out);
425 tunnel->ofpact.compat = code;
426 tunnel->tun_id = ntohll(a->set_tunnel64.tun_id);
429 case OFPUTIL_NXAST_MULTIPATH:
430 error = multipath_from_openflow(&a->multipath,
431 ofpact_put_MULTIPATH(out));
434 case OFPUTIL_NXAST_BUNDLE:
435 case OFPUTIL_NXAST_BUNDLE_LOAD:
436 error = bundle_from_openflow(&a->bundle, out);
439 case OFPUTIL_NXAST_OUTPUT_REG:
440 error = output_reg_from_openflow(&a->output_reg, out);
443 case OFPUTIL_NXAST_RESUBMIT_TABLE:
444 error = resubmit_table_from_openflow(&a->resubmit, out);
447 case OFPUTIL_NXAST_LEARN:
448 error = learn_from_openflow(&a->learn, out);
451 case OFPUTIL_NXAST_EXIT:
452 ofpact_put_EXIT(out);
455 case OFPUTIL_NXAST_DEC_TTL:
456 error = dec_ttl_from_openflow(out, code);
459 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
460 error = dec_ttl_cnt_ids_from_openflow(&a->cnt_ids, out);
463 case OFPUTIL_NXAST_FIN_TIMEOUT:
464 fin_timeout_from_openflow(&a->fin_timeout, out);
467 case OFPUTIL_NXAST_CONTROLLER:
468 controller_from_openflow(&a->controller, out);
471 case OFPUTIL_NXAST_PUSH_MPLS:
472 error = push_mpls_from_openflow(a->push_mpls.ethertype,
473 OFPACT_MPLS_AFTER_VLAN, out);
476 case OFPUTIL_NXAST_SET_MPLS_TTL:
477 ofpact_put_SET_MPLS_TTL(out)->ttl = a->mpls_ttl.ttl;
480 case OFPUTIL_NXAST_DEC_MPLS_TTL:
481 ofpact_put_DEC_MPLS_TTL(out);
484 case OFPUTIL_NXAST_POP_MPLS:
485 if (eth_type_mpls(a->pop_mpls.ethertype)) {
486 return OFPERR_OFPBAC_BAD_ARGUMENT;
488 ofpact_put_POP_MPLS(out)->ethertype = a->pop_mpls.ethertype;
491 case OFPUTIL_NXAST_SAMPLE:
492 error = sample_from_openflow(&a->sample, out);
500 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
502 enum ofputil_action_code code;
505 error = decode_openflow10_action(a, &code);
511 case OFPUTIL_ACTION_INVALID:
512 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
513 #include "ofp-util.def"
516 case OFPUTIL_OFPAT10_OUTPUT:
517 return output_from_openflow10(&a->output10, out);
519 case OFPUTIL_OFPAT10_SET_VLAN_VID:
520 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
521 return OFPERR_OFPBAC_BAD_ARGUMENT;
523 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
526 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
527 if (a->vlan_pcp.vlan_pcp & ~7) {
528 return OFPERR_OFPBAC_BAD_ARGUMENT;
530 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
533 case OFPUTIL_OFPAT10_STRIP_VLAN:
534 ofpact_put_STRIP_VLAN(out);
537 case OFPUTIL_OFPAT10_SET_DL_SRC:
538 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
542 case OFPUTIL_OFPAT10_SET_DL_DST:
543 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
547 case OFPUTIL_OFPAT10_SET_NW_SRC:
548 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
551 case OFPUTIL_OFPAT10_SET_NW_DST:
552 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
555 case OFPUTIL_OFPAT10_SET_NW_TOS:
556 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
557 return OFPERR_OFPBAC_BAD_ARGUMENT;
559 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
562 case OFPUTIL_OFPAT10_SET_TP_SRC:
563 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
566 case OFPUTIL_OFPAT10_SET_TP_DST:
567 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
571 case OFPUTIL_OFPAT10_ENQUEUE:
572 error = enqueue_from_openflow10(&a->enqueue, out);
575 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
576 #include "ofp-util.def"
577 return ofpact_from_nxast(a, code, out);
583 static inline union ofp_action *
584 action_next(const union ofp_action *a)
586 return ((union ofp_action *) (void *)
587 ((uint8_t *) a + ntohs(a->header.len)));
591 action_is_valid(const union ofp_action *a, size_t max_actions)
593 uint16_t len = ntohs(a->header.len);
594 return (!(len % OFP_ACTION_ALIGN)
595 && len >= OFP_ACTION_ALIGN
596 && len / OFP_ACTION_ALIGN <= max_actions);
599 /* This macro is careful to check for actions with bad lengths. */
600 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, MAX_ACTIONS) \
601 for ((ITER) = (ACTIONS), (LEFT) = (MAX_ACTIONS); \
602 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
603 ((LEFT) -= ntohs((ITER)->header.len) / OFP_ACTION_ALIGN, \
604 (ITER) = action_next(ITER)))
607 log_bad_action(const union ofp_action *actions, size_t max_actions,
608 const union ofp_action *bad_action, enum ofperr error)
610 if (!VLOG_DROP_WARN(&rl)) {
614 ds_put_hex_dump(&s, actions, max_actions * OFP_ACTION_ALIGN, 0, false);
615 VLOG_WARN("bad action at offset %#tx (%s):\n%s",
616 (char *)bad_action - (char *)actions,
617 ofperr_get_name(error), ds_cstr(&s));
623 ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
625 enum ofperr (*ofpact_from_openflow)(
626 const union ofp_action *a, struct ofpbuf *out))
628 const union ofp_action *a;
631 ACTION_FOR_EACH (a, left, in, n_in) {
632 enum ofperr error = ofpact_from_openflow(a, out);
634 log_bad_action(in, n_in, a, error);
639 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
640 log_bad_action(in, n_in, a, error);
649 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
652 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
656 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
657 struct ofpbuf *ofpacts,
658 enum ofperr (*translate)(const union ofp_action *actions,
660 struct ofpbuf *ofpacts))
662 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
663 const union ofp_action *actions;
666 ofpbuf_clear(ofpacts);
668 if (actions_len % OFP_ACTION_ALIGN != 0) {
669 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
670 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
671 return OFPERR_OFPBRC_BAD_LEN;
674 actions = ofpbuf_try_pull(openflow, actions_len);
675 if (actions == NULL) {
676 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
677 "remaining message length (%zu)",
678 actions_len, openflow->size);
679 return OFPERR_OFPBRC_BAD_LEN;
682 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
684 ofpbuf_clear(ofpacts);
688 error = ofpacts_verify(ofpacts->data, ofpacts->size);
690 ofpbuf_clear(ofpacts);
695 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
696 * front of 'openflow' into ofpacts. On success, replaces any existing content
697 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
698 * Returns 0 if successful, otherwise an OpenFlow error.
700 * The parsed actions are valid generically, but they may not be valid in a
701 * specific context. For example, port numbers up to OFPP_MAX are valid
702 * generically, but specific datapaths may only support port numbers in a
703 * smaller range. Use ofpacts_check() to additional check whether actions are
704 * valid in a specific context. */
706 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
707 struct ofpbuf *ofpacts)
709 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
710 ofpacts_from_openflow10);
713 /* OpenFlow 1.1 actions. */
715 /* Parses 'a' to determine its type. On success stores the correct type into
716 * '*code' and returns 0. On failure returns an OFPERR_* error code and
717 * '*code' is indeterminate.
719 * The caller must have already verified that 'a''s length is potentially
720 * correct (that is, a->header.len is nonzero and a multiple of
721 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
723 * This function verifies that 'a''s length is correct for the type of action
724 * that it represents. */
726 decode_openflow11_action(const union ofp_action *a,
727 enum ofputil_action_code *code)
732 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
733 return decode_nxast_action(a, code);
735 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
736 case CONSTANT_HTONS(ENUM): \
737 len = ntohs(a->header.len); \
739 ? len >= sizeof(struct STRUCT) \
740 : len == sizeof(struct STRUCT)) { \
741 *code = OFPUTIL_##ENUM; \
744 return OFPERR_OFPBAC_BAD_LEN; \
747 #include "ofp-util.def"
750 return OFPERR_OFPBAC_BAD_TYPE;
755 output_from_openflow11(const struct ofp11_action_output *oao,
758 struct ofpact_output *output;
761 output = ofpact_put_OUTPUT(out);
762 output->max_len = ntohs(oao->max_len);
764 error = ofputil_port_from_ofp11(oao->port, &output->port);
769 return ofputil_check_output_port(output->port, OFPP_MAX);
773 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
775 enum ofputil_action_code code;
778 error = decode_openflow11_action(a, &code);
784 case OFPUTIL_ACTION_INVALID:
785 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
786 #include "ofp-util.def"
789 case OFPUTIL_OFPAT11_OUTPUT:
790 return output_from_openflow11(&a->ofp11_output, out);
792 case OFPUTIL_OFPAT11_SET_VLAN_VID:
793 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
794 return OFPERR_OFPBAC_BAD_ARGUMENT;
796 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
799 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
800 if (a->vlan_pcp.vlan_pcp & ~7) {
801 return OFPERR_OFPBAC_BAD_ARGUMENT;
803 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
806 case OFPUTIL_OFPAT11_PUSH_VLAN:
807 if (a->push.ethertype != 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(a->ofp11_set_queue.queue_id);
823 case OFPUTIL_OFPAT11_SET_DL_SRC:
824 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
828 case OFPUTIL_OFPAT11_SET_DL_DST:
829 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
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(&a->set_field, out);
874 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
875 ofpact_put_SET_MPLS_TTL(out)->ttl = a->ofp11_mpls_ttl.mpls_ttl;
878 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
879 ofpact_put_DEC_MPLS_TTL(out);
882 case OFPUTIL_OFPAT11_PUSH_MPLS:
883 error = push_mpls_from_openflow(a->push.ethertype,
884 OFPACT_MPLS_AFTER_VLAN, out);
887 case OFPUTIL_OFPAT11_POP_MPLS:
888 if (eth_type_mpls(a->ofp11_pop_mpls.ethertype)) {
889 return OFPERR_OFPBAC_BAD_ARGUMENT;
891 ofpact_put_POP_MPLS(out)->ethertype = a->ofp11_pop_mpls.ethertype;
894 case OFPUTIL_OFPAT11_GROUP:
895 ofpact_put_GROUP(out)->group_id = ntohl(a->group.group_id);
898 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
899 #include "ofp-util.def"
900 return ofpact_from_nxast(a, code, out);
907 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
910 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
913 /* True if an action sets the value of a field
914 * in a way that is compatibile with the action set.
915 * False otherwise. */
917 ofpact_is_set_action(const struct ofpact *a)
920 case OFPACT_REG_LOAD:
921 case OFPACT_SET_ETH_DST:
922 case OFPACT_SET_ETH_SRC:
923 case OFPACT_SET_IP_DSCP:
924 case OFPACT_SET_IP_ECN:
925 case OFPACT_SET_IP_TTL:
926 case OFPACT_SET_IPV4_DST:
927 case OFPACT_SET_IPV4_SRC:
928 case OFPACT_SET_L4_DST_PORT:
929 case OFPACT_SET_L4_SRC_PORT:
930 case OFPACT_SET_MPLS_TTL:
931 case OFPACT_SET_QUEUE:
932 case OFPACT_SET_TUNNEL:
933 case OFPACT_SET_VLAN_PCP:
934 case OFPACT_SET_VLAN_VID:
937 case OFPACT_CLEAR_ACTIONS:
938 case OFPACT_CONTROLLER:
939 case OFPACT_DEC_MPLS_TTL:
943 case OFPACT_FIN_TIMEOUT:
944 case OFPACT_GOTO_TABLE:
948 case OFPACT_MULTIPATH:
951 case OFPACT_OUTPUT_REG:
952 case OFPACT_POP_MPLS:
953 case OFPACT_POP_QUEUE:
954 case OFPACT_PUSH_MPLS:
955 case OFPACT_PUSH_VLAN:
956 case OFPACT_REG_MOVE:
957 case OFPACT_RESUBMIT:
959 case OFPACT_STACK_POP:
960 case OFPACT_STACK_PUSH:
961 case OFPACT_STRIP_VLAN:
962 case OFPACT_WRITE_ACTIONS:
963 case OFPACT_WRITE_METADATA:
970 /* True if an action is allowed in the action set.
971 * False otherwise. */
973 ofpact_is_allowed_in_actions_set(const struct ofpact *a)
976 case OFPACT_DEC_MPLS_TTL:
980 case OFPACT_POP_MPLS:
981 case OFPACT_PUSH_MPLS:
982 case OFPACT_PUSH_VLAN:
983 case OFPACT_REG_LOAD:
984 case OFPACT_SET_ETH_DST:
985 case OFPACT_SET_ETH_SRC:
986 case OFPACT_SET_IP_DSCP:
987 case OFPACT_SET_IP_ECN:
988 case OFPACT_SET_IP_TTL:
989 case OFPACT_SET_IPV4_DST:
990 case OFPACT_SET_IPV4_SRC:
991 case OFPACT_SET_L4_DST_PORT:
992 case OFPACT_SET_L4_SRC_PORT:
993 case OFPACT_SET_MPLS_TTL:
994 case OFPACT_SET_QUEUE:
995 case OFPACT_SET_TUNNEL:
996 case OFPACT_SET_VLAN_PCP:
997 case OFPACT_SET_VLAN_VID:
998 case OFPACT_STRIP_VLAN:
1001 /* In general these actions are excluded because they are not part of
1002 * the OpenFlow specification nor map to actions that are defined in
1003 * the specification. Thus the order in which they should be applied
1004 * in the action set is undefined. */
1006 case OFPACT_CONTROLLER:
1007 case OFPACT_ENQUEUE:
1009 case OFPACT_FIN_TIMEOUT:
1011 case OFPACT_MULTIPATH:
1013 case OFPACT_OUTPUT_REG:
1014 case OFPACT_POP_QUEUE:
1015 case OFPACT_REG_MOVE:
1016 case OFPACT_RESUBMIT:
1018 case OFPACT_STACK_POP:
1019 case OFPACT_STACK_PUSH:
1021 /* The action set may only include actions and thus
1022 * may not include any instructions */
1023 case OFPACT_CLEAR_ACTIONS:
1024 case OFPACT_GOTO_TABLE:
1026 case OFPACT_WRITE_ACTIONS:
1027 case OFPACT_WRITE_METADATA:
1034 /* Append ofpact 'a' onto the tail of 'out' */
1036 ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1038 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1041 /* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1043 ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1044 enum ofpact_type filter)
1046 const struct ofpact *target;
1047 const struct ofpact *a;
1050 OFPACT_FOR_EACH (a, in->data, in->size) {
1051 if (a->type == filter) {
1056 ofpact_copy(out, target);
1058 return target != NULL;
1061 /* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1062 * The order of appended ofpacts is preserved between 'in' and 'out' */
1064 ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1065 bool (*filter)(const struct ofpact *))
1067 const struct ofpact *a;
1069 OFPACT_FOR_EACH (a, in->data, in->size) {
1071 ofpact_copy(out, a);
1076 /* Reads 'action_set', which contains ofpacts accumulated by
1077 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1078 * executed directly into 'action_list'. (These names correspond to the
1079 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1081 * In general this involves appending the last instance of each action that is
1082 * adimissible in the action set in the order described in the OpenFlow
1086 * + output action is only appended if no group action was present in 'in'.
1087 * + As a simplification all set actions are copied in the order the are
1088 * provided in 'in' as many set actions applied to a field has the same
1089 * affect as only applying the last action that sets a field and
1090 * duplicates are removed by do_xlate_actions().
1091 * This has an unwanted side-effect of compsoting multiple
1092 * LOAD_REG actions that touch different regions of the same field. */
1094 ofpacts_execute_action_set(struct ofpbuf *action_list,
1095 const struct ofpbuf *action_set)
1097 /* The OpenFlow spec "Action Set" section specifies this order. */
1098 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1099 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1100 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1101 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1102 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1103 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1104 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1105 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1107 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1108 * we should execute only OFPACT_GROUP.
1110 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1111 * all the actions because there's no point in modifying a packet that will
1112 * not be sent anywhere. */
1113 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1114 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1115 ofpbuf_clear(action_list);
1121 ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1122 size_t n_in, struct ofpbuf *out)
1126 size_t start = out->size;
1128 error = ofpacts_from_openflow11(in, n_in, out);
1133 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1134 if (!ofpact_is_allowed_in_actions_set(a)) {
1135 VLOG_WARN_RL(&rl, "disallowed action in action set");
1136 return OFPERR_OFPBAC_BAD_TYPE;
1145 ofpact_from_openflow13(const union ofp_action *a, struct ofpbuf *out)
1147 enum ofputil_action_code code;
1150 error = decode_openflow11_action(a, &code);
1155 if (code == OFPUTIL_OFPAT11_PUSH_MPLS) {
1156 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
1157 error = push_mpls_from_openflow(oap->ethertype,
1158 OFPACT_MPLS_BEFORE_VLAN, out);
1160 error = ofpact_from_openflow11(a, out);
1167 ofpacts_from_openflow13(const union ofp_action *in, size_t n_in,
1170 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow13);
1173 /* OpenFlow 1.1 instructions. */
1175 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1176 static inline const struct STRUCT * OVS_UNUSED \
1177 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1179 ovs_assert(inst->type == htons(ENUM)); \
1180 return ALIGNED_CAST(struct STRUCT *, inst); \
1183 static inline void OVS_UNUSED \
1184 instruction_init_##ENUM(struct STRUCT *s) \
1186 memset(s, 0, sizeof *s); \
1187 s->type = htons(ENUM); \
1188 s->len = htons(sizeof *s); \
1191 static inline struct STRUCT * OVS_UNUSED \
1192 instruction_put_##ENUM(struct ofpbuf *buf) \
1194 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1195 instruction_init_##ENUM(s); \
1201 struct instruction_type_info {
1202 enum ovs_instruction_type type;
1206 static const struct instruction_type_info inst_info[] = {
1207 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1213 ovs_instruction_name_from_type(enum ovs_instruction_type type)
1215 return inst_info[type].name;
1219 ovs_instruction_type_from_name(const char *name)
1221 const struct instruction_type_info *p;
1222 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1223 if (!strcasecmp(name, p->name)) {
1230 enum ovs_instruction_type
1231 ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1235 return OVSINST_OFPIT13_METER;
1236 case OFPACT_CLEAR_ACTIONS:
1237 return OVSINST_OFPIT11_CLEAR_ACTIONS;
1238 case OFPACT_WRITE_ACTIONS:
1239 return OVSINST_OFPIT11_WRITE_ACTIONS;
1240 case OFPACT_WRITE_METADATA:
1241 return OVSINST_OFPIT11_WRITE_METADATA;
1242 case OFPACT_GOTO_TABLE:
1243 return OVSINST_OFPIT11_GOTO_TABLE;
1246 case OFPACT_CONTROLLER:
1247 case OFPACT_ENQUEUE:
1248 case OFPACT_OUTPUT_REG:
1250 case OFPACT_SET_VLAN_VID:
1251 case OFPACT_SET_VLAN_PCP:
1252 case OFPACT_STRIP_VLAN:
1253 case OFPACT_PUSH_VLAN:
1254 case OFPACT_SET_ETH_SRC:
1255 case OFPACT_SET_ETH_DST:
1256 case OFPACT_SET_IPV4_SRC:
1257 case OFPACT_SET_IPV4_DST:
1258 case OFPACT_SET_IP_DSCP:
1259 case OFPACT_SET_IP_ECN:
1260 case OFPACT_SET_IP_TTL:
1261 case OFPACT_SET_L4_SRC_PORT:
1262 case OFPACT_SET_L4_DST_PORT:
1263 case OFPACT_REG_MOVE:
1264 case OFPACT_REG_LOAD:
1265 case OFPACT_STACK_PUSH:
1266 case OFPACT_STACK_POP:
1267 case OFPACT_DEC_TTL:
1268 case OFPACT_SET_MPLS_TTL:
1269 case OFPACT_DEC_MPLS_TTL:
1270 case OFPACT_PUSH_MPLS:
1271 case OFPACT_POP_MPLS:
1272 case OFPACT_SET_TUNNEL:
1273 case OFPACT_SET_QUEUE:
1274 case OFPACT_POP_QUEUE:
1275 case OFPACT_FIN_TIMEOUT:
1276 case OFPACT_RESUBMIT:
1278 case OFPACT_MULTIPATH:
1283 return OVSINST_OFPIT11_APPLY_ACTIONS;
1287 static inline struct ofp11_instruction *
1288 instruction_next(const struct ofp11_instruction *inst)
1290 return ((struct ofp11_instruction *) (void *)
1291 ((uint8_t *) inst + ntohs(inst->len)));
1295 instruction_is_valid(const struct ofp11_instruction *inst,
1296 size_t n_instructions)
1298 uint16_t len = ntohs(inst->len);
1299 return (!(len % OFP11_INSTRUCTION_ALIGN)
1300 && len >= sizeof *inst
1301 && len / sizeof *inst <= n_instructions);
1304 /* This macro is careful to check for instructions with bad lengths. */
1305 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1306 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1307 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1308 ((LEFT) -= (ntohs((ITER)->len) \
1309 / sizeof(struct ofp11_instruction)), \
1310 (ITER) = instruction_next(ITER)))
1313 decode_openflow11_instruction(const struct ofp11_instruction *inst,
1314 enum ovs_instruction_type *type)
1316 uint16_t len = ntohs(inst->len);
1318 switch (inst->type) {
1319 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1320 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1322 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1323 case CONSTANT_HTONS(ENUM): \
1325 ? len >= sizeof(struct STRUCT) \
1326 : len == sizeof(struct STRUCT)) { \
1327 *type = OVSINST_##ENUM; \
1330 return OFPERR_OFPBIC_BAD_LEN; \
1336 return OFPERR_OFPBIC_UNKNOWN_INST;
1341 decode_openflow11_instructions(const struct ofp11_instruction insts[],
1343 const struct ofp11_instruction *out[])
1345 const struct ofp11_instruction *inst;
1348 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1349 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1350 enum ovs_instruction_type type;
1353 error = decode_openflow11_instruction(inst, &type);
1359 return OFPERR_ONFBIC_DUP_INSTRUCTION;
1365 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1366 (n_insts - left) * sizeof *inst);
1367 return OFPERR_OFPBIC_BAD_LEN;
1373 get_actions_from_instruction(const struct ofp11_instruction *inst,
1374 const union ofp_action **actions,
1375 size_t *max_actions)
1377 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
1378 *max_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1381 /* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
1382 * front of 'openflow' into ofpacts. On success, replaces any existing content
1383 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1384 * Returns 0 if successful, otherwise an OpenFlow error.
1386 * Actions are processed according to their OpenFlow version which
1387 * is provided in the 'version' parameter.
1389 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1390 * instructions, so you should call ofpacts_pull_openflow11_instructions()
1391 * instead of this function.
1393 * The parsed actions are valid generically, but they may not be valid in a
1394 * specific context. For example, port numbers up to OFPP_MAX are valid
1395 * generically, but specific datapaths may only support port numbers in a
1396 * smaller range. Use ofpacts_check() to additional check whether actions are
1397 * valid in a specific context. */
1399 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1400 enum ofp_version version,
1401 unsigned int actions_len,
1402 struct ofpbuf *ofpacts)
1408 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1409 ofpacts_from_openflow11);
1411 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1412 ofpacts_from_openflow13);
1419 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1420 enum ofp_version version,
1421 unsigned int instructions_len,
1422 struct ofpbuf *ofpacts)
1424 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1425 const struct ofp11_instruction *instructions;
1426 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1429 ofpbuf_clear(ofpacts);
1431 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1432 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1434 instructions_len, OFP11_INSTRUCTION_ALIGN);
1435 error = OFPERR_OFPBIC_BAD_LEN;
1439 instructions = ofpbuf_try_pull(openflow, instructions_len);
1440 if (instructions == NULL) {
1441 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1442 "remaining message length (%zu)",
1443 instructions_len, openflow->size);
1444 error = OFPERR_OFPBIC_BAD_LEN;
1448 error = decode_openflow11_instructions(
1449 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1455 if (insts[OVSINST_OFPIT13_METER]) {
1456 const struct ofp13_instruction_meter *oim;
1457 struct ofpact_meter *om;
1459 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1460 insts[OVSINST_OFPIT13_METER]);
1462 om = ofpact_put_METER(ofpacts);
1463 om->meter_id = ntohl(oim->meter_id);
1465 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1466 const union ofp_action *actions;
1469 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1470 &actions, &max_actions);
1475 error = ofpacts_from_openflow11(actions, max_actions, ofpacts);
1478 error = ofpacts_from_openflow13(actions, max_actions, ofpacts);
1487 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1488 instruction_get_OFPIT11_CLEAR_ACTIONS(
1489 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1490 ofpact_put_CLEAR_ACTIONS(ofpacts);
1492 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1493 struct ofpact_nest *on;
1494 const union ofp_action *actions;
1498 ofpact_pad(ofpacts);
1499 start = ofpacts->size;
1500 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1501 offsetof(struct ofpact_nest, actions));
1502 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
1503 &actions, &max_actions);
1504 error = ofpacts_from_openflow11_for_action_set(actions, max_actions,
1509 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
1510 on->ofpact.len = ofpacts->size - start;
1512 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1513 const struct ofp11_instruction_write_metadata *oiwm;
1514 struct ofpact_metadata *om;
1516 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1517 insts[OVSINST_OFPIT11_WRITE_METADATA]);
1519 om = ofpact_put_WRITE_METADATA(ofpacts);
1520 om->metadata = oiwm->metadata;
1521 om->mask = oiwm->metadata_mask;
1523 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1524 const struct ofp11_instruction_goto_table *oigt;
1525 struct ofpact_goto_table *ogt;
1527 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1528 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1529 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1530 ogt->table_id = oigt->table_id;
1533 error = ofpacts_verify(ofpacts->data, ofpacts->size);
1536 ofpbuf_clear(ofpacts);
1541 /* May modify flow->dl_type, caller must restore it. */
1543 ofpact_check__(const struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
1544 uint8_t table_id, bool enforce_consistency)
1546 const struct ofpact_enqueue *enqueue;
1550 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1553 case OFPACT_CONTROLLER:
1556 case OFPACT_ENQUEUE:
1557 enqueue = ofpact_get_ENQUEUE(a);
1558 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1559 && enqueue->port != OFPP_IN_PORT
1560 && enqueue->port != OFPP_LOCAL) {
1561 return OFPERR_OFPBAC_BAD_OUT_PORT;
1565 case OFPACT_OUTPUT_REG:
1566 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1569 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1571 case OFPACT_SET_VLAN_VID:
1572 case OFPACT_SET_VLAN_PCP:
1575 case OFPACT_STRIP_VLAN:
1576 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
1581 case OFPACT_PUSH_VLAN:
1582 if (flow->vlan_tci & htons(VLAN_CFI)) {
1583 /* Multiple VLAN headers not supported. */
1584 return OFPERR_OFPBAC_BAD_TAG;
1588 case OFPACT_SET_ETH_SRC:
1589 case OFPACT_SET_ETH_DST:
1592 case OFPACT_SET_IPV4_SRC:
1593 case OFPACT_SET_IPV4_DST:
1594 if (flow->dl_type != htons(ETH_TYPE_IP)) {
1599 case OFPACT_SET_IP_DSCP:
1600 case OFPACT_SET_IP_ECN:
1601 case OFPACT_SET_IP_TTL:
1602 case OFPACT_DEC_TTL:
1603 if (!is_ip_any(flow)) {
1608 case OFPACT_SET_L4_SRC_PORT:
1609 case OFPACT_SET_L4_DST_PORT:
1610 if (!is_ip_any(flow) ||
1611 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1612 && flow->nw_proto != IPPROTO_SCTP)) {
1617 case OFPACT_REG_MOVE:
1618 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1620 case OFPACT_REG_LOAD:
1621 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1623 case OFPACT_STACK_PUSH:
1624 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1626 case OFPACT_STACK_POP:
1627 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1629 case OFPACT_SET_MPLS_TTL:
1630 case OFPACT_DEC_MPLS_TTL:
1631 if (!eth_type_mpls(flow->dl_type)) {
1636 case OFPACT_SET_TUNNEL:
1637 case OFPACT_SET_QUEUE:
1638 case OFPACT_POP_QUEUE:
1639 case OFPACT_RESUBMIT:
1642 case OFPACT_FIN_TIMEOUT:
1643 if (flow->nw_proto != IPPROTO_TCP) {
1649 return learn_check(ofpact_get_LEARN(a), flow);
1651 case OFPACT_MULTIPATH:
1652 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1658 case OFPACT_PUSH_MPLS:
1659 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
1662 case OFPACT_POP_MPLS:
1663 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
1664 if (!eth_type_mpls(flow->dl_type)) {
1672 case OFPACT_CLEAR_ACTIONS:
1675 case OFPACT_WRITE_ACTIONS: {
1676 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
1677 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
1678 flow, max_ports, table_id, false);
1681 case OFPACT_WRITE_METADATA:
1684 case OFPACT_METER: {
1685 uint32_t mid = ofpact_get_METER(a)->meter_id;
1686 if (mid == 0 || mid > OFPM13_MAX) {
1687 return OFPERR_OFPMMFC_INVALID_METER;
1692 case OFPACT_GOTO_TABLE:
1693 if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
1694 return OFPERR_OFPBRC_BAD_TABLE_ID;
1706 if (enforce_consistency) {
1707 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1712 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1713 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1714 * switch with no more than 'max_ports' ports.
1716 * May temporarily modify 'flow', but restores the changes before returning. */
1718 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1719 struct flow *flow, ofp_port_t max_ports, uint8_t table_id,
1720 bool enforce_consistency)
1722 const struct ofpact *a;
1723 ovs_be16 dl_type = flow->dl_type;
1724 enum ofperr error = 0;
1726 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1727 error = ofpact_check__(a, flow, max_ports, table_id,
1728 enforce_consistency);
1733 flow->dl_type = dl_type; /* Restore. */
1737 /* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1738 * in the appropriate order as defined by the OpenFlow spec. */
1740 ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1742 const struct ofpact *a;
1743 enum ovs_instruction_type inst;
1745 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
1746 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1747 enum ovs_instruction_type next;
1749 next = ovs_instruction_type_from_ofpact_type(a->type);
1750 if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
1753 const char *name = ovs_instruction_name_from_type(inst);
1754 const char *next_name = ovs_instruction_name_from_type(next);
1757 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1758 "1.1+ compatibility", name);
1760 VLOG_WARN("invalid instruction ordering: %s must appear "
1761 "before %s, for OpenFlow 1.1+ compatibility",
1764 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
1773 /* Converting ofpacts to Nicira OpenFlow extensions. */
1776 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1779 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1781 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1782 output_reg->src.n_bits);
1783 naor->src = htonl(output_reg->src.field->nxm_header);
1784 naor->max_len = htons(output_reg->max_len);
1788 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1791 struct nx_action_resubmit *nar;
1793 if (resubmit->table_id == 0xff
1794 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1795 nar = ofputil_put_NXAST_RESUBMIT(out);
1797 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1798 nar->table = resubmit->table_id;
1800 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
1804 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1807 uint64_t tun_id = tunnel->tun_id;
1809 if (tun_id <= UINT32_MAX
1810 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1811 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1813 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1818 ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1821 struct nx_action_write_metadata *nawm;
1823 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1824 nawm->metadata = om->metadata;
1825 nawm->mask = om->mask;
1829 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1831 size_t start_ofs = out->size;
1832 struct nx_action_note *nan;
1833 unsigned int remainder;
1836 nan = ofputil_put_NXAST_NOTE(out);
1837 out->size -= sizeof nan->note;
1839 ofpbuf_put(out, note->data, note->length);
1841 len = out->size - start_ofs;
1842 remainder = len % OFP_ACTION_ALIGN;
1844 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1846 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
1847 nan->len = htons(out->size - start_ofs);
1851 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1854 struct nx_action_controller *nac;
1856 nac = ofputil_put_NXAST_CONTROLLER(out);
1857 nac->max_len = htons(oc->max_len);
1858 nac->controller_id = htons(oc->controller_id);
1859 nac->reason = oc->reason;
1863 ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1866 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1867 ofputil_put_NXAST_DEC_TTL(out);
1869 struct nx_action_cnt_ids *nac_ids =
1870 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1871 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1875 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1876 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1878 ids = ofpbuf_put_zeros(out, ids_len);
1879 for (i = 0; i < oc_ids->n_controllers; i++) {
1880 ids[i] = htons(oc_ids->cnt_ids[i]);
1886 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1889 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1890 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1891 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1895 ofpact_sample_to_nxast(const struct ofpact_sample *os,
1898 struct nx_action_sample *nas;
1900 nas = ofputil_put_NXAST_SAMPLE(out);
1901 nas->probability = htons(os->probability);
1902 nas->collector_set_id = htonl(os->collector_set_id);
1903 nas->obs_domain_id = htonl(os->obs_domain_id);
1904 nas->obs_point_id = htonl(os->obs_point_id);
1908 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1911 case OFPACT_CONTROLLER:
1912 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1915 case OFPACT_OUTPUT_REG:
1916 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1920 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1923 case OFPACT_REG_MOVE:
1924 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1927 case OFPACT_REG_LOAD:
1928 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1931 case OFPACT_STACK_PUSH:
1932 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1935 case OFPACT_STACK_POP:
1936 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1939 case OFPACT_DEC_TTL:
1940 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
1943 case OFPACT_SET_MPLS_TTL:
1944 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1945 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1948 case OFPACT_DEC_MPLS_TTL:
1949 ofputil_put_NXAST_DEC_MPLS_TTL(out);
1952 case OFPACT_SET_TUNNEL:
1953 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1956 case OFPACT_WRITE_METADATA:
1957 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1960 case OFPACT_SET_QUEUE:
1961 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1962 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1965 case OFPACT_POP_QUEUE:
1966 ofputil_put_NXAST_POP_QUEUE(out);
1969 case OFPACT_FIN_TIMEOUT:
1970 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1973 case OFPACT_RESUBMIT:
1974 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1978 learn_to_nxast(ofpact_get_LEARN(a), out);
1981 case OFPACT_MULTIPATH:
1982 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1986 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1990 ofputil_put_NXAST_EXIT(out);
1993 case OFPACT_PUSH_MPLS:
1994 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1995 ofpact_get_PUSH_MPLS(a)->ethertype;
1998 case OFPACT_POP_MPLS:
1999 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2000 ofpact_get_POP_MPLS(a)->ethertype;
2004 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2009 case OFPACT_ENQUEUE:
2010 case OFPACT_SET_VLAN_VID:
2011 case OFPACT_SET_VLAN_PCP:
2012 case OFPACT_STRIP_VLAN:
2013 case OFPACT_PUSH_VLAN:
2014 case OFPACT_SET_ETH_SRC:
2015 case OFPACT_SET_ETH_DST:
2016 case OFPACT_SET_IPV4_SRC:
2017 case OFPACT_SET_IPV4_DST:
2018 case OFPACT_SET_IP_DSCP:
2019 case OFPACT_SET_IP_ECN:
2020 case OFPACT_SET_IP_TTL:
2021 case OFPACT_SET_L4_SRC_PORT:
2022 case OFPACT_SET_L4_DST_PORT:
2023 case OFPACT_WRITE_ACTIONS:
2024 case OFPACT_CLEAR_ACTIONS:
2025 case OFPACT_GOTO_TABLE:
2031 /* Converting ofpacts to OpenFlow 1.0. */
2034 ofpact_output_to_openflow10(const struct ofpact_output *output,
2037 struct ofp10_action_output *oao;
2039 oao = ofputil_put_OFPAT10_OUTPUT(out);
2040 oao->port = htons(ofp_to_u16(output->port));
2041 oao->max_len = htons(output->max_len);
2045 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2048 struct ofp10_action_enqueue *oae;
2050 oae = ofputil_put_OFPAT10_ENQUEUE(out);
2051 oae->port = htons(ofp_to_u16(enqueue->port));
2052 oae->queue_id = htonl(enqueue->queue);
2056 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2060 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2063 case OFPACT_ENQUEUE:
2064 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2067 case OFPACT_SET_VLAN_VID:
2068 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2069 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2072 case OFPACT_SET_VLAN_PCP:
2073 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2074 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2077 case OFPACT_STRIP_VLAN:
2078 ofputil_put_OFPAT10_STRIP_VLAN(out);
2081 case OFPACT_SET_ETH_SRC:
2082 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2083 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2086 case OFPACT_SET_ETH_DST:
2087 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2088 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2091 case OFPACT_SET_IPV4_SRC:
2092 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2093 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2096 case OFPACT_SET_IPV4_DST:
2097 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2098 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2101 case OFPACT_SET_IP_DSCP:
2102 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
2103 = ofpact_get_SET_IP_DSCP(a)->dscp;
2106 case OFPACT_SET_L4_SRC_PORT:
2107 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2108 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2111 case OFPACT_SET_L4_DST_PORT:
2112 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2113 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2116 case OFPACT_PUSH_VLAN:
2117 case OFPACT_CLEAR_ACTIONS:
2118 case OFPACT_WRITE_ACTIONS:
2119 case OFPACT_GOTO_TABLE:
2127 case OFPACT_CONTROLLER:
2128 case OFPACT_OUTPUT_REG:
2130 case OFPACT_REG_MOVE:
2131 case OFPACT_REG_LOAD:
2132 case OFPACT_STACK_PUSH:
2133 case OFPACT_STACK_POP:
2134 case OFPACT_DEC_TTL:
2135 case OFPACT_SET_IP_ECN:
2136 case OFPACT_SET_IP_TTL:
2137 case OFPACT_SET_MPLS_TTL:
2138 case OFPACT_DEC_MPLS_TTL:
2139 case OFPACT_SET_TUNNEL:
2140 case OFPACT_WRITE_METADATA:
2141 case OFPACT_SET_QUEUE:
2142 case OFPACT_POP_QUEUE:
2143 case OFPACT_FIN_TIMEOUT:
2144 case OFPACT_RESUBMIT:
2146 case OFPACT_MULTIPATH:
2149 case OFPACT_PUSH_MPLS:
2150 case OFPACT_POP_MPLS:
2152 ofpact_to_nxast(a, out);
2157 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
2158 * actions in 'openflow', appending the actions to any existing data in
2161 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
2162 struct ofpbuf *openflow)
2164 const struct ofpact *a;
2166 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2167 ofpact_to_openflow10(a, openflow);
2171 /* Converting ofpacts to OpenFlow 1.1. */
2174 ofpact_output_to_openflow11(const struct ofpact_output *output,
2177 struct ofp11_action_output *oao;
2179 oao = ofputil_put_OFPAT11_OUTPUT(out);
2180 oao->port = ofputil_port_to_ofp11(output->port);
2181 oao->max_len = htons(output->max_len);
2185 ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2188 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2189 && (!dec_ttl->ofpact.compat ||
2190 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2191 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2193 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2198 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2202 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2204 case OFPACT_ENQUEUE:
2208 case OFPACT_SET_VLAN_VID:
2209 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2210 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2213 case OFPACT_SET_VLAN_PCP:
2214 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2215 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2218 case OFPACT_STRIP_VLAN:
2219 ofputil_put_OFPAT11_POP_VLAN(out);
2222 case OFPACT_PUSH_VLAN:
2223 /* XXX ETH_TYPE_VLAN_8021AD case */
2224 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2225 htons(ETH_TYPE_VLAN_8021Q);
2228 case OFPACT_SET_QUEUE:
2229 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2230 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2233 case OFPACT_SET_ETH_SRC:
2234 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2235 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2238 case OFPACT_SET_ETH_DST:
2239 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2240 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2243 case OFPACT_SET_IPV4_SRC:
2244 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2245 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2248 case OFPACT_SET_IPV4_DST:
2249 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2250 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2253 case OFPACT_SET_IP_DSCP:
2254 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
2255 = ofpact_get_SET_IP_DSCP(a)->dscp;
2258 case OFPACT_SET_IP_ECN:
2259 ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2260 = ofpact_get_SET_IP_ECN(a)->ecn;
2263 case OFPACT_SET_IP_TTL:
2264 ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2265 = ofpact_get_SET_IP_TTL(a)->ttl;
2268 case OFPACT_SET_L4_SRC_PORT:
2269 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2270 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2273 case OFPACT_SET_L4_DST_PORT:
2274 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2275 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2278 case OFPACT_DEC_TTL:
2279 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
2282 case OFPACT_SET_MPLS_TTL:
2283 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2284 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2287 case OFPACT_DEC_MPLS_TTL:
2288 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2291 case OFPACT_WRITE_METADATA:
2292 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2295 case OFPACT_PUSH_MPLS:
2296 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2297 ofpact_get_PUSH_MPLS(a)->ethertype;
2300 case OFPACT_POP_MPLS:
2301 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2302 ofpact_get_POP_MPLS(a)->ethertype;
2306 case OFPACT_CLEAR_ACTIONS:
2307 case OFPACT_WRITE_ACTIONS:
2308 case OFPACT_GOTO_TABLE:
2313 ofputil_put_OFPAT11_GROUP(out)->group_id =
2314 htonl(ofpact_get_GROUP(a)->group_id);
2317 case OFPACT_CONTROLLER:
2318 case OFPACT_OUTPUT_REG:
2320 case OFPACT_REG_MOVE:
2321 case OFPACT_REG_LOAD:
2322 case OFPACT_STACK_PUSH:
2323 case OFPACT_STACK_POP:
2324 case OFPACT_SET_TUNNEL:
2325 case OFPACT_POP_QUEUE:
2326 case OFPACT_FIN_TIMEOUT:
2327 case OFPACT_RESUBMIT:
2329 case OFPACT_MULTIPATH:
2333 ofpact_to_nxast(a, out);
2338 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
2339 * 1.1 actions in 'openflow', appending the actions to any existing data in
2342 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
2343 size_t ofpacts_len, struct ofpbuf *openflow)
2345 const struct ofpact *a;
2346 size_t start_size = openflow->size;
2348 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2349 ofpact_to_openflow11(a, openflow);
2352 return openflow->size - start_size;
2356 ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
2358 struct ofp11_instruction_actions *oia;
2360 /* Update the instruction's length (or, if it's empty, delete it). */
2361 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2362 if (openflow->size > ofs + sizeof *oia) {
2363 oia->len = htons(openflow->size - ofs);
2365 openflow->size = ofs;
2370 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
2372 struct ofpbuf *openflow)
2374 const struct ofpact *a;
2376 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2377 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2378 case OVSINST_OFPIT11_CLEAR_ACTIONS:
2379 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
2382 case OVSINST_OFPIT11_GOTO_TABLE: {
2383 struct ofp11_instruction_goto_table *oigt;
2384 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2385 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2386 memset(oigt->pad, 0, sizeof oigt->pad);
2390 case OVSINST_OFPIT11_WRITE_METADATA: {
2391 const struct ofpact_metadata *om;
2392 struct ofp11_instruction_write_metadata *oiwm;
2394 om = ofpact_get_WRITE_METADATA(a);
2395 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2396 oiwm->metadata = om->metadata;
2397 oiwm->metadata_mask = om->mask;
2401 case OVSINST_OFPIT13_METER: {
2402 const struct ofpact_meter *om;
2403 struct ofp13_instruction_meter *oim;
2405 om = ofpact_get_METER(a);
2406 oim = instruction_put_OFPIT13_METER(openflow);
2407 oim->meter_id = htonl(om->meter_id);
2411 case OVSINST_OFPIT11_APPLY_ACTIONS: {
2412 const size_t ofs = openflow->size;
2413 const size_t ofpacts_len_left =
2414 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2415 const struct ofpact *action;
2416 const struct ofpact *processed = a;
2418 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2419 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
2420 if (ovs_instruction_type_from_ofpact_type(action->type)
2421 != OVSINST_OFPIT11_APPLY_ACTIONS) {
2424 ofpact_to_openflow11(action, openflow);
2427 ofpacts_update_instruction_actions(openflow, ofs);
2432 case OVSINST_OFPIT11_WRITE_ACTIONS: {
2433 const size_t ofs = openflow->size;
2434 const struct ofpact_nest *on;
2436 on = ofpact_get_WRITE_ACTIONS(a);
2437 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2438 ofpacts_put_openflow11_actions(on->actions,
2439 ofpact_nest_get_action_len(on),
2441 ofpacts_update_instruction_actions(openflow, ofs);
2449 /* Returns true if 'action' outputs to 'port', false otherwise. */
2451 ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
2453 switch (ofpact->type) {
2455 return ofpact_get_OUTPUT(ofpact)->port == port;
2456 case OFPACT_ENQUEUE:
2457 return ofpact_get_ENQUEUE(ofpact)->port == port;
2458 case OFPACT_CONTROLLER:
2459 return port == OFPP_CONTROLLER;
2461 case OFPACT_OUTPUT_REG:
2463 case OFPACT_SET_VLAN_VID:
2464 case OFPACT_SET_VLAN_PCP:
2465 case OFPACT_STRIP_VLAN:
2466 case OFPACT_PUSH_VLAN:
2467 case OFPACT_SET_ETH_SRC:
2468 case OFPACT_SET_ETH_DST:
2469 case OFPACT_SET_IPV4_SRC:
2470 case OFPACT_SET_IPV4_DST:
2471 case OFPACT_SET_IP_DSCP:
2472 case OFPACT_SET_IP_ECN:
2473 case OFPACT_SET_IP_TTL:
2474 case OFPACT_SET_L4_SRC_PORT:
2475 case OFPACT_SET_L4_DST_PORT:
2476 case OFPACT_REG_MOVE:
2477 case OFPACT_REG_LOAD:
2478 case OFPACT_STACK_PUSH:
2479 case OFPACT_STACK_POP:
2480 case OFPACT_DEC_TTL:
2481 case OFPACT_SET_MPLS_TTL:
2482 case OFPACT_DEC_MPLS_TTL:
2483 case OFPACT_SET_TUNNEL:
2484 case OFPACT_WRITE_METADATA:
2485 case OFPACT_SET_QUEUE:
2486 case OFPACT_POP_QUEUE:
2487 case OFPACT_FIN_TIMEOUT:
2488 case OFPACT_RESUBMIT:
2490 case OFPACT_MULTIPATH:
2493 case OFPACT_PUSH_MPLS:
2494 case OFPACT_POP_MPLS:
2496 case OFPACT_CLEAR_ACTIONS:
2497 case OFPACT_WRITE_ACTIONS:
2498 case OFPACT_GOTO_TABLE:
2506 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2507 * to 'port', false otherwise. */
2509 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
2512 const struct ofpact *a;
2514 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2515 if (ofpact_outputs_to_port(a, port)) {
2523 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2524 * to 'group', false otherwise. */
2526 ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
2529 const struct ofpact *a;
2531 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2532 if (a->type == OFPACT_GROUP
2533 && ofpact_get_GROUP(a)->group_id == group_id) {
2542 ofpacts_equal(const struct ofpact *a, size_t a_len,
2543 const struct ofpact *b, size_t b_len)
2545 return a_len == b_len && !memcmp(a, b, a_len);
2548 /* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2549 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2551 * This function relies on the order of 'ofpacts' being correct (as checked by
2552 * ofpacts_verify()). */
2554 ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2556 const struct ofpact *a;
2558 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2559 enum ovs_instruction_type inst;
2561 inst = ovs_instruction_type_from_ofpact_type(a->type);
2562 if (a->type == OFPACT_METER) {
2563 return ofpact_get_METER(a)->meter_id;
2564 } else if (inst > OVSINST_OFPIT13_METER) {
2572 /* Formatting ofpacts. */
2575 print_note(const struct ofpact_note *note, struct ds *string)
2579 ds_put_cstr(string, "note:");
2580 for (i = 0; i < note->length; i++) {
2582 ds_put_char(string, '.');
2584 ds_put_format(string, "%02"PRIx8, note->data[i]);
2589 print_dec_ttl(const struct ofpact_cnt_ids *ids,
2594 ds_put_cstr(s, "dec_ttl");
2595 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2596 ds_put_cstr(s, "(");
2597 for (i = 0; i < ids->n_controllers; i++) {
2599 ds_put_cstr(s, ",");
2601 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2603 ds_put_cstr(s, ")");
2608 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2611 ds_put_cstr(s, "fin_timeout(");
2612 if (fin_timeout->fin_idle_timeout) {
2613 ds_put_format(s, "idle_timeout=%"PRIu16",",
2614 fin_timeout->fin_idle_timeout);
2616 if (fin_timeout->fin_hard_timeout) {
2617 ds_put_format(s, "hard_timeout=%"PRIu16",",
2618 fin_timeout->fin_hard_timeout);
2621 ds_put_char(s, ')');
2625 ofpact_format(const struct ofpact *a, struct ds *s)
2627 const struct ofpact_enqueue *enqueue;
2628 const struct ofpact_resubmit *resubmit;
2629 const struct ofpact_controller *controller;
2630 const struct ofpact_metadata *metadata;
2631 const struct ofpact_tunnel *tunnel;
2632 const struct ofpact_sample *sample;
2637 port = ofpact_get_OUTPUT(a)->port;
2638 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
2639 ds_put_format(s, "output:%"PRIu16, port);
2641 ofputil_format_port(port, s);
2642 if (port == OFPP_CONTROLLER) {
2643 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2648 case OFPACT_CONTROLLER:
2649 controller = ofpact_get_CONTROLLER(a);
2650 if (controller->reason == OFPR_ACTION &&
2651 controller->controller_id == 0) {
2652 ds_put_format(s, "CONTROLLER:%"PRIu16,
2653 ofpact_get_CONTROLLER(a)->max_len);
2655 enum ofp_packet_in_reason reason = controller->reason;
2657 ds_put_cstr(s, "controller(");
2658 if (reason != OFPR_ACTION) {
2659 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2661 ds_put_format(s, "reason=%s,",
2662 ofputil_packet_in_reason_to_string(
2663 reason, reasonbuf, sizeof reasonbuf));
2665 if (controller->max_len != UINT16_MAX) {
2666 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2668 if (controller->controller_id != 0) {
2669 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2672 ds_put_char(s, ')');
2676 case OFPACT_ENQUEUE:
2677 enqueue = ofpact_get_ENQUEUE(a);
2678 ds_put_format(s, "enqueue:");
2679 ofputil_format_port(enqueue->port, s);
2680 ds_put_format(s, "q%"PRIu32, enqueue->queue);
2683 case OFPACT_OUTPUT_REG:
2684 ds_put_cstr(s, "output:");
2685 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2689 bundle_format(ofpact_get_BUNDLE(a), s);
2692 case OFPACT_SET_VLAN_VID:
2693 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2694 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2697 case OFPACT_SET_VLAN_PCP:
2698 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2699 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2702 case OFPACT_STRIP_VLAN:
2703 ds_put_cstr(s, "strip_vlan");
2706 case OFPACT_PUSH_VLAN:
2707 /* XXX 802.1AD case*/
2708 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2711 case OFPACT_SET_ETH_SRC:
2712 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2713 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2716 case OFPACT_SET_ETH_DST:
2717 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2718 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2721 case OFPACT_SET_IPV4_SRC:
2722 ds_put_format(s, "mod_nw_src:"IP_FMT,
2723 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
2726 case OFPACT_SET_IPV4_DST:
2727 ds_put_format(s, "mod_nw_dst:"IP_FMT,
2728 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
2731 case OFPACT_SET_IP_DSCP:
2732 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
2735 case OFPACT_SET_IP_ECN:
2736 ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
2739 case OFPACT_SET_IP_TTL:
2740 ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
2743 case OFPACT_SET_L4_SRC_PORT:
2744 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2747 case OFPACT_SET_L4_DST_PORT:
2748 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2751 case OFPACT_REG_MOVE:
2752 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2755 case OFPACT_REG_LOAD:
2756 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2759 case OFPACT_STACK_PUSH:
2760 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2763 case OFPACT_STACK_POP:
2764 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2767 case OFPACT_DEC_TTL:
2768 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
2771 case OFPACT_SET_MPLS_TTL:
2772 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2773 ofpact_get_SET_MPLS_TTL(a)->ttl);
2776 case OFPACT_DEC_MPLS_TTL:
2777 ds_put_cstr(s, "dec_mpls_ttl");
2780 case OFPACT_SET_TUNNEL:
2781 tunnel = ofpact_get_SET_TUNNEL(a);
2782 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2783 (tunnel->tun_id > UINT32_MAX
2784 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2788 case OFPACT_SET_QUEUE:
2789 ds_put_format(s, "set_queue:%"PRIu32,
2790 ofpact_get_SET_QUEUE(a)->queue_id);
2793 case OFPACT_POP_QUEUE:
2794 ds_put_cstr(s, "pop_queue");
2797 case OFPACT_FIN_TIMEOUT:
2798 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2801 case OFPACT_RESUBMIT:
2802 resubmit = ofpact_get_RESUBMIT(a);
2803 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
2804 ds_put_cstr(s, "resubmit:");
2805 ofputil_format_port(resubmit->in_port, s);
2807 ds_put_format(s, "resubmit(");
2808 if (resubmit->in_port != OFPP_IN_PORT) {
2809 ofputil_format_port(resubmit->in_port, s);
2811 ds_put_char(s, ',');
2812 if (resubmit->table_id != 255) {
2813 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2815 ds_put_char(s, ')');
2820 learn_format(ofpact_get_LEARN(a), s);
2823 case OFPACT_MULTIPATH:
2824 multipath_format(ofpact_get_MULTIPATH(a), s);
2828 print_note(ofpact_get_NOTE(a), s);
2831 case OFPACT_PUSH_MPLS:
2832 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2833 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2836 case OFPACT_POP_MPLS:
2837 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2838 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2842 ds_put_cstr(s, "exit");
2846 sample = ofpact_get_SAMPLE(a);
2848 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2849 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2850 sample->probability, sample->collector_set_id,
2851 sample->obs_domain_id, sample->obs_point_id);
2854 case OFPACT_WRITE_ACTIONS: {
2855 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2856 ds_put_format(s, "%s(",
2857 ovs_instruction_name_from_type(
2858 OVSINST_OFPIT11_WRITE_ACTIONS));
2859 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
2860 ds_put_char(s, ')');
2864 case OFPACT_CLEAR_ACTIONS:
2865 ds_put_format(s, "%s",
2866 ovs_instruction_name_from_type(
2867 OVSINST_OFPIT11_CLEAR_ACTIONS));
2870 case OFPACT_WRITE_METADATA:
2871 metadata = ofpact_get_WRITE_METADATA(a);
2872 ds_put_format(s, "%s:%#"PRIx64,
2873 ovs_instruction_name_from_type(
2874 OVSINST_OFPIT11_WRITE_METADATA),
2875 ntohll(metadata->metadata));
2876 if (metadata->mask != OVS_BE64_MAX) {
2877 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2881 case OFPACT_GOTO_TABLE:
2882 ds_put_format(s, "%s:%"PRIu8,
2883 ovs_instruction_name_from_type(
2884 OVSINST_OFPIT11_GOTO_TABLE),
2885 ofpact_get_GOTO_TABLE(a)->table_id);
2889 ds_put_format(s, "%s:%"PRIu32,
2890 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
2891 ofpact_get_METER(a)->meter_id);
2895 ds_put_format(s, "group:%"PRIu32,
2896 ofpact_get_GROUP(a)->group_id);
2901 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2902 * 'ofpacts' to 'string'. */
2904 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2908 ds_put_cstr(string, "drop");
2910 const struct ofpact *a;
2912 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2914 ds_put_cstr(string, ",");
2917 /* XXX write-actions */
2918 ofpact_format(a, string);
2923 /* Internal use by helpers. */
2926 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2928 struct ofpact *ofpact;
2930 ofpact_pad(ofpacts);
2931 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2932 ofpact_init(ofpact, type, len);
2937 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2939 memset(ofpact, 0, len);
2940 ofpact->type = type;
2941 ofpact->compat = OFPUTIL_ACTION_INVALID;
2945 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2946 * starting at 'ofpact'.
2948 * This is the correct way to update a variable-length ofpact's length after
2949 * adding the variable-length part of the payload. (See the large comment
2950 * near the end of ofp-actions.h for more information.) */
2952 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2954 ovs_assert(ofpact == ofpacts->l2);
2955 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2958 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2959 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2960 * client must call this itself after adding the final ofpact to an array of
2963 * (The consequences of failing to call this function are probably not dire.
2964 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2965 * not dereference it. That's undefined behavior, technically, but it will not
2966 * cause a real problem on common systems. Still, it seems better to call
2969 ofpact_pad(struct ofpbuf *ofpacts)
2971 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2973 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2978 ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2981 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2982 load->dst.field = mf;
2984 load->dst.n_bits = mf->n_bits;
2985 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2986 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);