]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ofp-actions.c
openflow-1.2: Remove OFPAT12_* definitions that duplicate OFPAT11_* ones.
[mirror_ovs.git] / lib / ofp-actions.c
CommitLineData
f25d0cf3 1/*
9ff262f4 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
f25d0cf3
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
18#include "ofp-actions.h"
19#include "autopath.h"
20#include "bundle.h"
21#include "byte-order.h"
22#include "compiler.h"
23#include "dynamic-string.h"
24#include "learn.h"
25#include "meta-flow.h"
26#include "multipath.h"
27#include "nx-match.h"
28#include "ofp-util.h"
29#include "ofpbuf.h"
30#include "vlog.h"
31
32VLOG_DEFINE_THIS_MODULE(ofp_actions);
33
34static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
35\f
36/* Converting OpenFlow 1.0 to ofpacts. */
37
38static enum ofperr
d01c980f 39output_from_openflow10(const struct ofp10_action_output *oao,
f25d0cf3
BP
40 struct ofpbuf *out)
41{
42 struct ofpact_output *output;
43
44 output = ofpact_put_OUTPUT(out);
45 output->port = ntohs(oao->port);
46 output->max_len = ntohs(oao->max_len);
47
48 return ofputil_check_output_port(output->port, OFPP_MAX);
49}
50
51static enum ofperr
52enqueue_from_openflow10(const struct ofp_action_enqueue *oae,
53 struct ofpbuf *out)
54{
55 struct ofpact_enqueue *enqueue;
56
57 enqueue = ofpact_put_ENQUEUE(out);
58 enqueue->port = ntohs(oae->port);
59 enqueue->queue = ntohl(oae->queue_id);
60 if (enqueue->port >= OFPP_MAX && enqueue->port != OFPP_IN_PORT
61 && enqueue->port != OFPP_LOCAL) {
62 return OFPERR_OFPBAC_BAD_OUT_PORT;
63 }
64 return 0;
65}
66
67static void
68resubmit_from_openflow(const struct nx_action_resubmit *nar,
69 struct ofpbuf *out)
70{
71 struct ofpact_resubmit *resubmit;
72
73 resubmit = ofpact_put_RESUBMIT(out);
74 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
75 resubmit->in_port = ntohs(nar->in_port);
76 resubmit->table_id = 0xff;
77}
78
79static enum ofperr
80resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
81 struct ofpbuf *out)
82{
83 struct ofpact_resubmit *resubmit;
84
85 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
86 return OFPERR_OFPBAC_BAD_ARGUMENT;
87 }
88
89 resubmit = ofpact_put_RESUBMIT(out);
90 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
91 resubmit->in_port = ntohs(nar->in_port);
92 resubmit->table_id = nar->table;
93 return 0;
94}
95
96static enum ofperr
97output_reg_from_openflow(const struct nx_action_output_reg *naor,
98 struct ofpbuf *out)
99{
100 struct ofpact_output_reg *output_reg;
101
102 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
103 return OFPERR_OFPBAC_BAD_ARGUMENT;
104 }
105
106 output_reg = ofpact_put_OUTPUT_REG(out);
107 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
108 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
109 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
110 output_reg->max_len = ntohs(naor->max_len);
111
112 return mf_check_src(&output_reg->src, NULL);
113}
114
115static void
116fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
117 struct ofpbuf *out)
118{
119 struct ofpact_fin_timeout *oft;
120
121 oft = ofpact_put_FIN_TIMEOUT(out);
122 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
123 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
124}
125
126static void
127controller_from_openflow(const struct nx_action_controller *nac,
128 struct ofpbuf *out)
129{
130 struct ofpact_controller *oc;
131
132 oc = ofpact_put_CONTROLLER(out);
133 oc->max_len = ntohs(nac->max_len);
134 oc->controller_id = ntohs(nac->controller_id);
135 oc->reason = nac->reason;
136}
137
138static void
139note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
140{
141 struct ofpact_note *note;
142 unsigned int length;
143
144 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
145 note = ofpact_put(out, OFPACT_NOTE,
146 offsetof(struct ofpact_note, data) + length);
147 note->length = length;
148 memcpy(note->data, nan->note, length);
149}
150
c2d967a5
MM
151static enum ofperr
152dec_ttl_from_openflow(struct ofpbuf *out)
153{
154 uint16_t id = 0;
155 struct ofpact_cnt_ids *ids;
156 enum ofperr error = 0;
157
158 ids = ofpact_put_DEC_TTL(out);
159 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL;
160 ids->n_controllers = 1;
161 ofpbuf_put(out, &id, sizeof id);
162 ids = out->l2;
163 ofpact_update_len(out, &ids->ofpact);
164 return error;
165}
166
167static enum ofperr
168dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
169 struct ofpbuf *out)
170{
171 struct ofpact_cnt_ids *ids;
172 size_t ids_size;
173 int i;
174
175 ids = ofpact_put_DEC_TTL(out);
176 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
177 ids->n_controllers = ntohs(nac_ids->n_controllers);
178 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
179
180 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
181 return OFPERR_NXBRC_MUST_BE_ZERO;
182 }
183
184 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
185 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
186 "allocated for controller ids. %zu bytes are required for "
187 "%"PRIu16" controllers.", ids_size,
188 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
189 return OFPERR_OFPBAC_BAD_LEN;
190 }
191
192 for (i = 0; i < ids->n_controllers; i++) {
193 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
194 ofpbuf_put(out, &id, sizeof id);
195 }
196
197 ids = out->l2;
198 ofpact_update_len(out, &ids->ofpact);
199
200 return 0;
201}
202
f25d0cf3
BP
203static enum ofperr
204decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
205{
206 const struct nx_action_header *nah = (const struct nx_action_header *) a;
207 uint16_t len = ntohs(a->header.len);
208
209 if (len < sizeof(struct nx_action_header)) {
210 return OFPERR_OFPBAC_BAD_LEN;
211 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
212 return OFPERR_OFPBAC_BAD_VENDOR;
213 }
214
215 switch (nah->subtype) {
216#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
217 case CONSTANT_HTONS(ENUM): \
218 if (EXTENSIBLE \
219 ? len >= sizeof(struct STRUCT) \
220 : len == sizeof(struct STRUCT)) { \
221 *code = OFPUTIL_##ENUM; \
222 return 0; \
223 } else { \
224 return OFPERR_OFPBAC_BAD_LEN; \
225 } \
226 NOT_REACHED();
227#include "ofp-util.def"
228
229 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
230 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
231 default:
232 return OFPERR_OFPBAC_BAD_TYPE;
233 }
234}
235
236/* Parses 'a' to determine its type. On success stores the correct type into
237 * '*code' and returns 0. On failure returns an OFPERR_* error code and
238 * '*code' is indeterminate.
239 *
240 * The caller must have already verified that 'a''s length is potentially
241 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
242 * ofp_action) and no longer than the amount of space allocated to 'a').
243 *
244 * This function verifies that 'a''s length is correct for the type of action
245 * that it represents. */
246static enum ofperr
247decode_openflow10_action(const union ofp_action *a,
248 enum ofputil_action_code *code)
249{
250 switch (a->type) {
251 case CONSTANT_HTONS(OFPAT10_VENDOR):
252 return decode_nxast_action(a, code);
253
254#define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
255 case CONSTANT_HTONS(ENUM): \
256 if (a->header.len == htons(sizeof(struct STRUCT))) { \
257 *code = OFPUTIL_##ENUM; \
258 return 0; \
259 } else { \
260 return OFPERR_OFPBAC_BAD_LEN; \
261 } \
262 break;
263#include "ofp-util.def"
264
265 default:
266 return OFPERR_OFPBAC_BAD_TYPE;
267 }
268}
269
270static enum ofperr
d01c980f
BP
271ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
272 struct ofpbuf *out)
f25d0cf3
BP
273{
274 const struct nx_action_resubmit *nar;
275 const struct nx_action_set_tunnel *nast;
276 const struct nx_action_set_queue *nasq;
277 const struct nx_action_note *nan;
278 const struct nx_action_set_tunnel64 *nast64;
279 struct ofpact_tunnel *tunnel;
d01c980f 280 enum ofperr error = 0;
f25d0cf3
BP
281
282 switch (code) {
283 case OFPUTIL_ACTION_INVALID:
d01c980f
BP
284#define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
285#define OFPAT11_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
78a3fff6 286#define OFPAT12_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
d01c980f 287#include "ofp-util.def"
f25d0cf3
BP
288 NOT_REACHED();
289
f25d0cf3
BP
290 case OFPUTIL_NXAST_RESUBMIT:
291 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
292 break;
293
294 case OFPUTIL_NXAST_SET_TUNNEL:
295 nast = (const struct nx_action_set_tunnel *) a;
296 tunnel = ofpact_put_SET_TUNNEL(out);
297 tunnel->ofpact.compat = code;
298 tunnel->tun_id = ntohl(nast->tun_id);
299 break;
300
301 case OFPUTIL_NXAST_SET_QUEUE:
302 nasq = (const struct nx_action_set_queue *) a;
303 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
304 break;
305
306 case OFPUTIL_NXAST_POP_QUEUE:
307 ofpact_put_POP_QUEUE(out);
308 break;
309
310 case OFPUTIL_NXAST_REG_MOVE:
311 error = nxm_reg_move_from_openflow(
312 (const struct nx_action_reg_move *) a, out);
313 break;
314
315 case OFPUTIL_NXAST_REG_LOAD:
316 error = nxm_reg_load_from_openflow(
317 (const struct nx_action_reg_load *) a, out);
318 break;
319
320 case OFPUTIL_NXAST_NOTE:
321 nan = (const struct nx_action_note *) a;
322 note_from_openflow(nan, out);
323 break;
324
325 case OFPUTIL_NXAST_SET_TUNNEL64:
326 nast64 = (const struct nx_action_set_tunnel64 *) a;
327 tunnel = ofpact_put_SET_TUNNEL(out);
328 tunnel->ofpact.compat = code;
329 tunnel->tun_id = ntohll(nast64->tun_id);
330 break;
331
332 case OFPUTIL_NXAST_MULTIPATH:
333 error = multipath_from_openflow((const struct nx_action_multipath *) a,
334 ofpact_put_MULTIPATH(out));
335 break;
336
c51c638a 337 case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
f25d0cf3
BP
338 error = autopath_from_openflow((const struct nx_action_autopath *) a,
339 ofpact_put_AUTOPATH(out));
340 break;
341
342 case OFPUTIL_NXAST_BUNDLE:
343 case OFPUTIL_NXAST_BUNDLE_LOAD:
344 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
345 break;
346
347 case OFPUTIL_NXAST_OUTPUT_REG:
348 error = output_reg_from_openflow(
349 (const struct nx_action_output_reg *) a, out);
350 break;
351
352 case OFPUTIL_NXAST_RESUBMIT_TABLE:
353 nar = (const struct nx_action_resubmit *) a;
354 error = resubmit_table_from_openflow(nar, out);
355 break;
356
357 case OFPUTIL_NXAST_LEARN:
358 error = learn_from_openflow((const struct nx_action_learn *) a, out);
359 break;
360
361 case OFPUTIL_NXAST_EXIT:
362 ofpact_put_EXIT(out);
363 break;
364
365 case OFPUTIL_NXAST_DEC_TTL:
c2d967a5
MM
366 error = dec_ttl_from_openflow(out);
367 break;
368
369 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
370 error = dec_ttl_cnt_ids_from_openflow(
371 (const struct nx_action_cnt_ids *) a, out);
f25d0cf3
BP
372 break;
373
374 case OFPUTIL_NXAST_FIN_TIMEOUT:
375 fin_timeout_from_openflow(
376 (const struct nx_action_fin_timeout *) a, out);
377 break;
378
379 case OFPUTIL_NXAST_CONTROLLER:
380 controller_from_openflow((const struct nx_action_controller *) a, out);
381 break;
382 }
383
384 return error;
385}
386
d01c980f
BP
387static enum ofperr
388ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
389{
390 enum ofputil_action_code code;
391 enum ofperr error;
392
393 error = decode_openflow10_action(a, &code);
394 if (error) {
395 return error;
396 }
397
398 switch (code) {
399 case OFPUTIL_ACTION_INVALID:
400#define OFPAT11_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
78a3fff6 401#define OFPAT12_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
d01c980f
BP
402#include "ofp-util.def"
403 NOT_REACHED();
404
405 case OFPUTIL_OFPAT10_OUTPUT:
406 return output_from_openflow10(&a->output10, out);
407
408 case OFPUTIL_OFPAT10_SET_VLAN_VID:
409 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
410 return OFPERR_OFPBAC_BAD_ARGUMENT;
411 }
412 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
413 break;
414
415 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
416 if (a->vlan_pcp.vlan_pcp & ~7) {
417 return OFPERR_OFPBAC_BAD_ARGUMENT;
418 }
419 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
420 break;
421
422 case OFPUTIL_OFPAT10_STRIP_VLAN:
423 ofpact_put_STRIP_VLAN(out);
424 break;
425
426 case OFPUTIL_OFPAT10_SET_DL_SRC:
427 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
428 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
429 break;
430
431 case OFPUTIL_OFPAT10_SET_DL_DST:
432 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
433 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
434 break;
435
436 case OFPUTIL_OFPAT10_SET_NW_SRC:
437 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
438 break;
439
440 case OFPUTIL_OFPAT10_SET_NW_DST:
441 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
442 break;
443
444 case OFPUTIL_OFPAT10_SET_NW_TOS:
445 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
446 return OFPERR_OFPBAC_BAD_ARGUMENT;
447 }
448 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
449 break;
450
451 case OFPUTIL_OFPAT10_SET_TP_SRC:
452 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
453 break;
454
455 case OFPUTIL_OFPAT10_SET_TP_DST:
456 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
457
458 break;
459
460 case OFPUTIL_OFPAT10_ENQUEUE:
461 error = enqueue_from_openflow10((const struct ofp_action_enqueue *) a,
462 out);
463 break;
464
465#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
466#include "ofp-util.def"
467 return ofpact_from_nxast(a, code, out);
468 }
469
470 return error;
471}
472
f25d0cf3
BP
473static inline union ofp_action *
474action_next(const union ofp_action *a)
475{
476 return ((union ofp_action *) (void *)
477 ((uint8_t *) a + ntohs(a->header.len)));
478}
479
480static inline bool
481action_is_valid(const union ofp_action *a, size_t n_actions)
482{
483 uint16_t len = ntohs(a->header.len);
484 return (!(len % OFP_ACTION_ALIGN)
485 && len >= sizeof *a
486 && len / sizeof *a <= n_actions);
487}
488
489/* This macro is careful to check for actions with bad lengths. */
490#define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
491 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
492 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
493 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
494 (ITER) = action_next(ITER)))
495
699dddf1
BP
496static void
497log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
498 enum ofperr error)
499{
500 if (!VLOG_DROP_WARN(&rl)) {
501 struct ds s;
502
503 ds_init(&s);
504 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
505 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
506 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
507 ds_destroy(&s);
508 }
509}
510
f25d0cf3 511static enum ofperr
0300caaf
IY
512ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
513 struct ofpbuf *out,
514 enum ofperr (*ofpact_from_openflow)(
515 const union ofp_action *a, struct ofpbuf *out))
f25d0cf3
BP
516{
517 const union ofp_action *a;
518 size_t left;
519
520 ACTION_FOR_EACH (a, left, in, n_in) {
0300caaf 521 enum ofperr error = ofpact_from_openflow(a, out);
f25d0cf3 522 if (error) {
699dddf1 523 log_bad_action(in, n_in, a - in, error);
f25d0cf3
BP
524 return error;
525 }
526 }
527 if (left) {
699dddf1
BP
528 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
529 log_bad_action(in, n_in, n_in - left, error);
530 return error;
f25d0cf3
BP
531 }
532
533 ofpact_pad(out);
534 return 0;
535}
536
0300caaf
IY
537static enum ofperr
538ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
539 struct ofpbuf *out)
540{
541 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
542}
543
d01c980f
BP
544static enum ofperr
545ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
546 struct ofpbuf *ofpacts,
547 enum ofperr (*translate)(const union ofp_action *actions,
548 size_t n_actions,
549 struct ofpbuf *ofpacts))
f25d0cf3
BP
550{
551 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
552 const union ofp_action *actions;
553 enum ofperr error;
554
555 ofpbuf_clear(ofpacts);
556
557 if (actions_len % OFP_ACTION_ALIGN != 0) {
558 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
559 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
560 return OFPERR_OFPBRC_BAD_LEN;
561 }
562
563 actions = ofpbuf_try_pull(openflow, actions_len);
564 if (actions == NULL) {
565 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
566 "remaining message length (%zu)",
567 actions_len, openflow->size);
568 return OFPERR_OFPBRC_BAD_LEN;
569 }
570
d01c980f 571 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
f25d0cf3
BP
572 if (error) {
573 ofpbuf_clear(ofpacts);
574 }
d01c980f
BP
575 return error;
576}
577
578/* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
579 * front of 'openflow' into ofpacts. On success, replaces any existing content
580 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
581 * Returns 0 if successful, otherwise an OpenFlow error.
582 *
13f894b1
BP
583 * The parsed actions are valid generically, but they may not be valid in a
584 * specific context. For example, port numbers up to OFPP_MAX are valid
585 * generically, but specific datapaths may only support port numbers in a
586 * smaller range. Use ofpacts_check() to additional check whether actions are
587 * valid in a specific context. */
d01c980f
BP
588enum ofperr
589ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
590 struct ofpbuf *ofpacts)
591{
592 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
593 ofpacts_from_openflow10);
594}
595\f
596/* OpenFlow 1.1 actions. */
597
598/* Parses 'a' to determine its type. On success stores the correct type into
599 * '*code' and returns 0. On failure returns an OFPERR_* error code and
600 * '*code' is indeterminate.
601 *
602 * The caller must have already verified that 'a''s length is potentially
603 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
604 * ofp_action) and no longer than the amount of space allocated to 'a').
605 *
606 * This function verifies that 'a''s length is correct for the type of action
607 * that it represents. */
608static enum ofperr
609decode_openflow11_action(const union ofp_action *a,
610 enum ofputil_action_code *code)
611{
612 switch (a->type) {
613 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
614 return decode_nxast_action(a, code);
615
616#define OFPAT11_ACTION(ENUM, STRUCT, NAME) \
617 case CONSTANT_HTONS(ENUM): \
618 if (a->header.len == htons(sizeof(struct STRUCT))) { \
619 *code = OFPUTIL_##ENUM; \
620 return 0; \
621 } else { \
622 return OFPERR_OFPBAC_BAD_LEN; \
623 } \
624 break;
625#include "ofp-util.def"
626
627 default:
628 return OFPERR_OFPBAC_BAD_TYPE;
629 }
630}
631
632static enum ofperr
633output_from_openflow11(const struct ofp11_action_output *oao,
634 struct ofpbuf *out)
635{
636 struct ofpact_output *output;
637 enum ofperr error;
638
639 output = ofpact_put_OUTPUT(out);
640 output->max_len = ntohs(oao->max_len);
641
642 error = ofputil_port_from_ofp11(oao->port, &output->port);
643 if (error) {
644 return error;
645 }
646
647 return ofputil_check_output_port(output->port, OFPP_MAX);
648}
649
650static enum ofperr
651ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
652{
653 enum ofputil_action_code code;
654 enum ofperr error;
655
656 error = decode_openflow11_action(a, &code);
657 if (error) {
658 return error;
659 }
660
661 switch (code) {
662 case OFPUTIL_ACTION_INVALID:
663#define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
78a3fff6 664#define OFPAT12_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
d01c980f
BP
665#include "ofp-util.def"
666 NOT_REACHED();
667
668 case OFPUTIL_OFPAT11_OUTPUT:
669 return output_from_openflow11((const struct ofp11_action_output *) a,
670 out);
671
672 case OFPUTIL_OFPAT11_SET_VLAN_VID:
673 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
674 return OFPERR_OFPBAC_BAD_ARGUMENT;
675 }
676 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
677 break;
678
679 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
680 if (a->vlan_pcp.vlan_pcp & ~7) {
681 return OFPERR_OFPBAC_BAD_ARGUMENT;
682 }
683 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
684 break;
685
686 case OFPUTIL_OFPAT11_SET_DL_SRC:
687 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
688 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
689 break;
690
691 case OFPUTIL_OFPAT11_SET_DL_DST:
692 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
693 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
694 break;
695
696 case OFPUTIL_OFPAT11_SET_NW_SRC:
697 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
698 break;
699
700 case OFPUTIL_OFPAT11_SET_NW_DST:
701 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
702 break;
703
704 case OFPUTIL_OFPAT11_SET_NW_TOS:
705 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
706 return OFPERR_OFPBAC_BAD_ARGUMENT;
707 }
708 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
709 break;
710
711 case OFPUTIL_OFPAT11_SET_TP_SRC:
712 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
713 break;
714
715 case OFPUTIL_OFPAT11_SET_TP_DST:
716 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
717 break;
718
719#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
720#include "ofp-util.def"
721 return ofpact_from_nxast(a, code, out);
722 }
723
724 return error;
725}
726
727static enum ofperr
728ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
729 struct ofpbuf *out)
730{
0300caaf 731 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
f25d0cf3
BP
732}
733\f
d01c980f
BP
734/* OpenFlow 1.1 instructions. */
735
d01c980f 736#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
a359d5ad
IY
737 static inline const struct STRUCT * \
738 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
739 { \
740 assert(inst->type == htons(ENUM)); \
741 return (struct STRUCT *)inst; \
742 } \
743 \
d01c980f
BP
744 static inline void \
745 instruction_init_##ENUM(struct STRUCT *s) \
746 { \
747 memset(s, 0, sizeof *s); \
748 s->type = htons(ENUM); \
749 s->len = htons(sizeof *s); \
750 } \
751 \
752 static inline struct STRUCT * \
753 instruction_put_##ENUM(struct ofpbuf *buf) \
754 { \
755 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
756 instruction_init_##ENUM(s); \
757 return s; \
758 }
759OVS_INSTRUCTIONS
760#undef DEFINE_INST
761
a359d5ad
IY
762struct instruction_type_info {
763 enum ovs_instruction_type type;
764 const char *name;
765};
766
767static const struct instruction_type_info inst_info[] = {
768#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
769OVS_INSTRUCTIONS
770#undef DEFINE_INST
771};
772
773const char *
774ofpact_instruction_name_from_type(enum ovs_instruction_type type)
775{
6ae0fd54 776 return inst_info[type].name;
a359d5ad
IY
777}
778
779int
780ofpact_instruction_type_from_name(const char *name)
781{
782 const struct instruction_type_info *p;
783 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
784 if (!strcasecmp(name, p->name)) {
785 return p->type;
786 }
787 }
788 return -1;
789}
790
d01c980f
BP
791static inline struct ofp11_instruction *
792instruction_next(const struct ofp11_instruction *inst)
793{
794 return ((struct ofp11_instruction *) (void *)
795 ((uint8_t *) inst + ntohs(inst->len)));
796}
797
798static inline bool
799instruction_is_valid(const struct ofp11_instruction *inst,
800 size_t n_instructions)
801{
802 uint16_t len = ntohs(inst->len);
803 return (!(len % OFP11_INSTRUCTION_ALIGN)
804 && len >= sizeof *inst
805 && len / sizeof *inst <= n_instructions);
806}
807
808/* This macro is careful to check for instructions with bad lengths. */
809#define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
810 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
811 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
812 ((LEFT) -= (ntohs((ITER)->len) \
813 / sizeof(struct ofp11_instruction)), \
814 (ITER) = instruction_next(ITER)))
815
816static enum ofperr
817decode_openflow11_instruction(const struct ofp11_instruction *inst,
818 enum ovs_instruction_type *type)
819{
820 uint16_t len = ntohs(inst->len);
821
822 switch (inst->type) {
823 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
824 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
825
826#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
827 case CONSTANT_HTONS(ENUM): \
828 if (EXTENSIBLE \
829 ? len >= sizeof(struct STRUCT) \
830 : len == sizeof(struct STRUCT)) { \
831 *type = OVSINST_##ENUM; \
832 return 0; \
833 } else { \
834 return OFPERR_OFPBIC_BAD_LEN; \
835 }
836OVS_INSTRUCTIONS
837#undef DEFINE_INST
838
839 default:
840 return OFPERR_OFPBIC_UNKNOWN_INST;
841 }
842}
843
844static enum ofperr
845decode_openflow11_instructions(const struct ofp11_instruction insts[],
846 size_t n_insts,
847 const struct ofp11_instruction *out[])
848{
849 const struct ofp11_instruction *inst;
850 size_t left;
851
852 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
853 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
854 enum ovs_instruction_type type;
855 enum ofperr error;
856
857 error = decode_openflow11_instruction(inst, &type);
858 if (error) {
859 return error;
860 }
861
862 if (out[type]) {
e38c9de5 863 return OFPERR_OFPIT_BAD_INSTRUCTION;
d01c980f
BP
864 }
865 out[type] = inst;
866 }
867
868 if (left) {
869 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
870 (n_insts - left) * sizeof *inst);
871 return OFPERR_OFPBIC_BAD_LEN;
872 }
873 return 0;
874}
875
876static void
877get_actions_from_instruction(const struct ofp11_instruction *inst,
878 const union ofp_action **actions,
879 size_t *n_actions)
880{
881 *actions = (const union ofp_action *) (inst + 1);
882 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
883}
884
885/* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
886 * front of 'openflow' into ofpacts. On success, replaces any existing content
887 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
888 * Returns 0 if successful, otherwise an OpenFlow error.
889 *
890 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
891 * instructions, so you should call ofpacts_pull_openflow11_instructions()
892 * instead of this function.
893 *
13f894b1
BP
894 * The parsed actions are valid generically, but they may not be valid in a
895 * specific context. For example, port numbers up to OFPP_MAX are valid
896 * generically, but specific datapaths may only support port numbers in a
897 * smaller range. Use ofpacts_check() to additional check whether actions are
898 * valid in a specific context. */
d01c980f
BP
899enum ofperr
900ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
901 unsigned int actions_len,
902 struct ofpbuf *ofpacts)
903{
0300caaf
IY
904 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
905 ofpacts_from_openflow11);
d01c980f
BP
906}
907
908enum ofperr
909ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
910 unsigned int instructions_len,
911 struct ofpbuf *ofpacts)
912{
913 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
914 const struct ofp11_instruction *instructions;
915 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
916 enum ofperr error;
917
918 ofpbuf_clear(ofpacts);
919
920 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
921 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
922 "multiple of %d",
923 instructions_len, OFP11_INSTRUCTION_ALIGN);
924 error = OFPERR_OFPBIC_BAD_LEN;
925 goto exit;
926 }
927
928 instructions = ofpbuf_try_pull(openflow, instructions_len);
929 if (instructions == NULL) {
930 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
931 "remaining message length (%zu)",
932 instructions_len, openflow->size);
933 error = OFPERR_OFPBIC_BAD_LEN;
934 goto exit;
935 }
936
937 error = decode_openflow11_instructions(
938 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
939 insts);
940 if (error) {
941 goto exit;
942 }
943
944 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
945 const union ofp_action *actions;
946 size_t n_actions;
947
948 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
949 &actions, &n_actions);
950 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
951 if (error) {
952 goto exit;
953 }
954 }
955
d01c980f
BP
956 if (insts[OVSINST_OFPIT11_GOTO_TABLE] ||
957 insts[OVSINST_OFPIT11_WRITE_METADATA] ||
958 insts[OVSINST_OFPIT11_WRITE_ACTIONS] ||
959 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
960 error = OFPERR_OFPBIC_UNSUP_INST;
961 goto exit;
962 }
963
964exit:
965 if (error) {
966 ofpbuf_clear(ofpacts);
967 }
968 return error;
969}
970\f
f25d0cf3
BP
971static enum ofperr
972ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
973{
974 const struct ofpact_enqueue *enqueue;
975
976 switch (a->type) {
977 case OFPACT_OUTPUT:
978 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
979 max_ports);
980
981 case OFPACT_CONTROLLER:
982 return 0;
983
984 case OFPACT_ENQUEUE:
985 enqueue = ofpact_get_ENQUEUE(a);
986 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
987 && enqueue->port != OFPP_LOCAL) {
988 return OFPERR_OFPBAC_BAD_OUT_PORT;
989 }
990 return 0;
991
992 case OFPACT_OUTPUT_REG:
993 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
994
995 case OFPACT_BUNDLE:
996 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
997
998 case OFPACT_SET_VLAN_VID:
999 case OFPACT_SET_VLAN_PCP:
1000 case OFPACT_STRIP_VLAN:
1001 case OFPACT_SET_ETH_SRC:
1002 case OFPACT_SET_ETH_DST:
1003 case OFPACT_SET_IPV4_SRC:
1004 case OFPACT_SET_IPV4_DST:
1005 case OFPACT_SET_IPV4_DSCP:
1006 case OFPACT_SET_L4_SRC_PORT:
1007 case OFPACT_SET_L4_DST_PORT:
1008 return 0;
1009
1010 case OFPACT_REG_MOVE:
1011 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1012
1013 case OFPACT_REG_LOAD:
1014 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1015
1016 case OFPACT_DEC_TTL:
1017 case OFPACT_SET_TUNNEL:
1018 case OFPACT_SET_QUEUE:
1019 case OFPACT_POP_QUEUE:
1020 case OFPACT_FIN_TIMEOUT:
1021 case OFPACT_RESUBMIT:
1022 return 0;
1023
1024 case OFPACT_LEARN:
1025 return learn_check(ofpact_get_LEARN(a), flow);
1026
1027 case OFPACT_MULTIPATH:
1028 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1029
1030 case OFPACT_AUTOPATH:
1031 return autopath_check(ofpact_get_AUTOPATH(a), flow);
1032
1033 case OFPACT_NOTE:
1034 case OFPACT_EXIT:
1035 return 0;
1036
1037 default:
1038 NOT_REACHED();
1039 }
1040}
1041
1042/* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1043 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1044 * switch with no more than 'max_ports' ports. */
1045enum ofperr
1046ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1047 const struct flow *flow, int max_ports)
1048{
1049 const struct ofpact *a;
1050
1051 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1052 enum ofperr error = ofpact_check__(a, flow, max_ports);
1053 if (error) {
1054 return error;
1055 }
1056 }
1057
1058 return 0;
1059}
1060\f
1061/* Converting ofpacts to Nicira OpenFlow extensions. */
1062
1063static void
1064ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1065 struct ofpbuf *out)
1066{
1067 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1068
1069 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1070 output_reg->src.n_bits);
1071 naor->src = htonl(output_reg->src.field->nxm_header);
1072 naor->max_len = htons(output_reg->max_len);
1073}
1074
1075static void
1076ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1077 struct ofpbuf *out)
1078{
1079 struct nx_action_resubmit *nar;
1080
1081 if (resubmit->table_id == 0xff
1082 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1083 nar = ofputil_put_NXAST_RESUBMIT(out);
1084 } else {
1085 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1086 nar->table = resubmit->table_id;
1087 }
1088 nar->in_port = htons(resubmit->in_port);
1089}
1090
1091static void
1092ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1093 struct ofpbuf *out)
1094{
1095 uint64_t tun_id = tunnel->tun_id;
1096
1097 if (tun_id <= UINT32_MAX
1098 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1099 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1100 } else {
1101 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1102 }
1103}
1104
1105static void
1106ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1107{
1108 size_t start_ofs = out->size;
1109 struct nx_action_note *nan;
1110 unsigned int remainder;
1111 unsigned int len;
1112
1113 nan = ofputil_put_NXAST_NOTE(out);
1114 out->size -= sizeof nan->note;
1115
1116 ofpbuf_put(out, note->data, note->length);
1117
1118 len = out->size - start_ofs;
1119 remainder = len % OFP_ACTION_ALIGN;
1120 if (remainder) {
1121 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1122 }
1123 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1124 nan->len = htons(out->size - start_ofs);
1125}
1126
1127static void
1128ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1129 struct ofpbuf *out)
1130{
1131 struct nx_action_controller *nac;
1132
1133 nac = ofputil_put_NXAST_CONTROLLER(out);
1134 nac->max_len = htons(oc->max_len);
1135 nac->controller_id = htons(oc->controller_id);
1136 nac->reason = oc->reason;
1137}
1138
c2d967a5
MM
1139static void
1140ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1141 struct ofpbuf *out)
1142{
1143 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1144 ofputil_put_NXAST_DEC_TTL(out);
1145 } else {
1146 struct nx_action_cnt_ids *nac_ids =
1147 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1148 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1149 ovs_be16 *ids;
1150 size_t i;
1151
1152 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1153 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1154
1155 ids = ofpbuf_put_zeros(out, ids_len);
1156 for (i = 0; i < oc_ids->n_controllers; i++) {
1157 ids[i] = htons(oc_ids->cnt_ids[i]);
1158 }
1159 }
1160}
1161
f25d0cf3
BP
1162static void
1163ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1164 struct ofpbuf *out)
1165{
1166 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1167 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1168 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1169}
1170
1171static void
1172ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1173{
1174 switch (a->type) {
1175 case OFPACT_CONTROLLER:
1176 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1177 break;
1178
1179 case OFPACT_OUTPUT_REG:
1180 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1181 break;
1182
1183 case OFPACT_BUNDLE:
1184 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1185 break;
1186
1187 case OFPACT_REG_MOVE:
1188 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1189 break;
1190
1191 case OFPACT_REG_LOAD:
1192 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1193 break;
1194
1195 case OFPACT_DEC_TTL:
c2d967a5 1196 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
f25d0cf3
BP
1197 break;
1198
1199 case OFPACT_SET_TUNNEL:
1200 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1201 break;
1202
1203 case OFPACT_SET_QUEUE:
1204 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1205 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1206 break;
1207
1208 case OFPACT_POP_QUEUE:
1209 ofputil_put_NXAST_POP_QUEUE(out);
1210 break;
1211
1212 case OFPACT_FIN_TIMEOUT:
1213 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1214 break;
1215
1216 case OFPACT_RESUBMIT:
1217 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1218 break;
1219
1220 case OFPACT_LEARN:
1221 learn_to_nxast(ofpact_get_LEARN(a), out);
1222 break;
1223
1224 case OFPACT_MULTIPATH:
1225 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1226 break;
1227
1228 case OFPACT_AUTOPATH:
1229 autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1230 break;
1231
1232 case OFPACT_NOTE:
1233 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1234 break;
1235
1236 case OFPACT_EXIT:
1237 ofputil_put_NXAST_EXIT(out);
1238 break;
1239
1240 case OFPACT_OUTPUT:
1241 case OFPACT_ENQUEUE:
1242 case OFPACT_SET_VLAN_VID:
1243 case OFPACT_SET_VLAN_PCP:
1244 case OFPACT_STRIP_VLAN:
1245 case OFPACT_SET_ETH_SRC:
1246 case OFPACT_SET_ETH_DST:
1247 case OFPACT_SET_IPV4_SRC:
1248 case OFPACT_SET_IPV4_DST:
1249 case OFPACT_SET_IPV4_DSCP:
1250 case OFPACT_SET_L4_SRC_PORT:
1251 case OFPACT_SET_L4_DST_PORT:
1252 NOT_REACHED();
1253 }
1254}
1255\f
1256/* Converting ofpacts to OpenFlow 1.0. */
1257
1258static void
1259ofpact_output_to_openflow10(const struct ofpact_output *output,
1260 struct ofpbuf *out)
1261{
d01c980f 1262 struct ofp10_action_output *oao;
f25d0cf3
BP
1263
1264 oao = ofputil_put_OFPAT10_OUTPUT(out);
1265 oao->port = htons(output->port);
1266 oao->max_len = htons(output->max_len);
1267}
1268
1269static void
1270ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1271 struct ofpbuf *out)
1272{
1273 struct ofp_action_enqueue *oae;
1274
1275 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1276 oae->port = htons(enqueue->port);
1277 oae->queue_id = htonl(enqueue->queue);
1278}
1279
1280static void
1281ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1282{
1283 switch (a->type) {
1284 case OFPACT_OUTPUT:
1285 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1286 break;
1287
1288 case OFPACT_ENQUEUE:
1289 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1290 break;
1291
1292 case OFPACT_SET_VLAN_VID:
1293 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1294 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1295 break;
1296
1297 case OFPACT_SET_VLAN_PCP:
1298 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1299 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1300 break;
1301
1302 case OFPACT_STRIP_VLAN:
1303 ofputil_put_OFPAT10_STRIP_VLAN(out);
1304 break;
1305
1306 case OFPACT_SET_ETH_SRC:
1307 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1308 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1309 break;
1310
1311 case OFPACT_SET_ETH_DST:
1312 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1313 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1314 break;
1315
1316 case OFPACT_SET_IPV4_SRC:
1317 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1318 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1319 break;
1320
1321 case OFPACT_SET_IPV4_DST:
1322 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1323 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1324 break;
1325
1326 case OFPACT_SET_IPV4_DSCP:
1327 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1328 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1329 break;
1330
1331 case OFPACT_SET_L4_SRC_PORT:
1332 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1333 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1334 break;
1335
1336 case OFPACT_SET_L4_DST_PORT:
1337 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1338 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1339 break;
1340
1341 case OFPACT_CONTROLLER:
1342 case OFPACT_OUTPUT_REG:
1343 case OFPACT_BUNDLE:
1344 case OFPACT_REG_MOVE:
1345 case OFPACT_REG_LOAD:
1346 case OFPACT_DEC_TTL:
1347 case OFPACT_SET_TUNNEL:
1348 case OFPACT_SET_QUEUE:
1349 case OFPACT_POP_QUEUE:
1350 case OFPACT_FIN_TIMEOUT:
1351 case OFPACT_RESUBMIT:
1352 case OFPACT_LEARN:
1353 case OFPACT_MULTIPATH:
1354 case OFPACT_AUTOPATH:
1355 case OFPACT_NOTE:
1356 case OFPACT_EXIT:
1357 ofpact_to_nxast(a, out);
1358 break;
1359 }
1360}
1361
d01c980f 1362/* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
f25d0cf3
BP
1363 * actions in 'openflow', appending the actions to any existing data in
1364 * 'openflow'. */
1365void
d01c980f
BP
1366ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1367 struct ofpbuf *openflow)
f25d0cf3
BP
1368{
1369 const struct ofpact *a;
1370
1371 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1372 ofpact_to_openflow10(a, openflow);
1373 }
1374}
1375\f
d01c980f
BP
1376/* Converting ofpacts to OpenFlow 1.1. */
1377
1378static void
1379ofpact_output_to_openflow11(const struct ofpact_output *output,
1380 struct ofpbuf *out)
1381{
1382 struct ofp11_action_output *oao;
1383
1384 oao = ofputil_put_OFPAT11_OUTPUT(out);
1385 oao->port = ofputil_port_to_ofp11(output->port);
1386 oao->max_len = htons(output->max_len);
1387}
1388
1389static void
1390ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1391{
1392 switch (a->type) {
1393 case OFPACT_OUTPUT:
1394 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1395
1396 case OFPACT_ENQUEUE:
1397 /* XXX */
1398 break;
1399
1400 case OFPACT_SET_VLAN_VID:
1401 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1402 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1403 break;
1404
1405 case OFPACT_SET_VLAN_PCP:
1406 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1407 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1408 break;
1409
1410 case OFPACT_STRIP_VLAN:
1411 /* XXX */
1412 break;
1413
1414 case OFPACT_SET_ETH_SRC:
1415 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1416 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1417 break;
1418
1419 case OFPACT_SET_ETH_DST:
1420 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1421 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1422 break;
1423
1424 case OFPACT_SET_IPV4_SRC:
1425 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1426 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1427 break;
1428
1429 case OFPACT_SET_IPV4_DST:
1430 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1431 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1432 break;
1433
1434 case OFPACT_SET_IPV4_DSCP:
1435 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1436 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1437 break;
1438
1439 case OFPACT_SET_L4_SRC_PORT:
1440 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1441 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1442 break;
1443
1444 case OFPACT_SET_L4_DST_PORT:
1445 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1446 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1447 break;
1448
1449 case OFPACT_CONTROLLER:
1450 case OFPACT_OUTPUT_REG:
1451 case OFPACT_BUNDLE:
1452 case OFPACT_REG_MOVE:
1453 case OFPACT_REG_LOAD:
1454 case OFPACT_DEC_TTL:
1455 case OFPACT_SET_TUNNEL:
1456 case OFPACT_SET_QUEUE:
1457 case OFPACT_POP_QUEUE:
1458 case OFPACT_FIN_TIMEOUT:
1459 case OFPACT_RESUBMIT:
1460 case OFPACT_LEARN:
1461 case OFPACT_MULTIPATH:
1462 case OFPACT_AUTOPATH:
1463 case OFPACT_NOTE:
1464 case OFPACT_EXIT:
1465 ofpact_to_nxast(a, out);
1466 break;
1467 }
1468}
1469
1470/* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1471 * 1.1 actions in 'openflow', appending the actions to any existing data in
1472 * 'openflow'. */
a07c15bc 1473size_t
d01c980f
BP
1474ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1475 size_t ofpacts_len, struct ofpbuf *openflow)
1476{
1477 const struct ofpact *a;
a07c15bc 1478 size_t start_size = openflow->size;
d01c980f
BP
1479
1480 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1481 ofpact_to_openflow11(a, openflow);
1482 }
a07c15bc
SH
1483
1484 return openflow->size - start_size;
d01c980f
BP
1485}
1486
1487void
1488ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1489 size_t ofpacts_len,
1490 struct ofpbuf *openflow)
1491{
1492 struct ofp11_instruction_actions *oia;
1493 size_t ofs;
1494
1495 /* Put an OFPIT11_APPLY_ACTIONS instruction and fill it in. */
1496 ofs = openflow->size;
1497 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1498 ofpacts_put_openflow11_actions(ofpacts, ofpacts_len, openflow);
1499
1500 /* Update the instruction's length (or, if it's empty, delete it). */
1501 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1502 if (openflow->size > ofs + sizeof *oia) {
1503 oia->len = htons(openflow->size - ofs);
1504 } else {
1505 openflow->size = ofs;
1506 }
1507}
1508\f
f25d0cf3
BP
1509/* Returns true if 'action' outputs to 'port', false otherwise. */
1510static bool
1511ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1512{
1513 switch (ofpact->type) {
1514 case OFPACT_OUTPUT:
1515 return ofpact_get_OUTPUT(ofpact)->port == port;
1516 case OFPACT_ENQUEUE:
1517 return ofpact_get_ENQUEUE(ofpact)->port == port;
1518 case OFPACT_CONTROLLER:
1519 return port == OFPP_CONTROLLER;
1520
1521 case OFPACT_OUTPUT_REG:
1522 case OFPACT_BUNDLE:
1523 case OFPACT_SET_VLAN_VID:
1524 case OFPACT_SET_VLAN_PCP:
1525 case OFPACT_STRIP_VLAN:
1526 case OFPACT_SET_ETH_SRC:
1527 case OFPACT_SET_ETH_DST:
1528 case OFPACT_SET_IPV4_SRC:
1529 case OFPACT_SET_IPV4_DST:
1530 case OFPACT_SET_IPV4_DSCP:
1531 case OFPACT_SET_L4_SRC_PORT:
1532 case OFPACT_SET_L4_DST_PORT:
1533 case OFPACT_REG_MOVE:
1534 case OFPACT_REG_LOAD:
1535 case OFPACT_DEC_TTL:
1536 case OFPACT_SET_TUNNEL:
1537 case OFPACT_SET_QUEUE:
1538 case OFPACT_POP_QUEUE:
1539 case OFPACT_FIN_TIMEOUT:
1540 case OFPACT_RESUBMIT:
1541 case OFPACT_LEARN:
1542 case OFPACT_MULTIPATH:
1543 case OFPACT_AUTOPATH:
1544 case OFPACT_NOTE:
1545 case OFPACT_EXIT:
1546 default:
1547 return false;
1548 }
1549}
1550
1551/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1552 * to 'port', false otherwise. */
1553bool
1554ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1555 uint16_t port)
1556{
1557 const struct ofpact *a;
1558
1559 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1560 if (ofpact_outputs_to_port(a, port)) {
1561 return true;
1562 }
1563 }
1564
1565 return false;
1566}
1567
1568bool
1569ofpacts_equal(const struct ofpact *a, size_t a_len,
1570 const struct ofpact *b, size_t b_len)
1571{
1572 return a_len == b_len && !memcmp(a, b, a_len);
1573}
1574\f
1575/* Formatting ofpacts. */
1576
1577static void
1578print_note(const struct ofpact_note *note, struct ds *string)
1579{
1580 size_t i;
1581
1582 ds_put_cstr(string, "note:");
1583 for (i = 0; i < note->length; i++) {
1584 if (i) {
1585 ds_put_char(string, '.');
1586 }
1587 ds_put_format(string, "%02"PRIx8, note->data[i]);
1588 }
1589}
1590
c2d967a5
MM
1591static void
1592print_dec_ttl(const struct ofpact_cnt_ids *ids,
1593 struct ds *s)
1594{
1595 size_t i;
1596
1597 ds_put_cstr(s, "dec_ttl");
1598 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
1599 ds_put_cstr(s, "(");
1600 for (i = 0; i < ids->n_controllers; i++) {
1601 if (i) {
1602 ds_put_cstr(s, ",");
1603 }
1604 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
1605 }
1606 ds_put_cstr(s, ")");
1607 }
1608}
1609
f25d0cf3
BP
1610static void
1611print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1612 struct ds *s)
1613{
1614 ds_put_cstr(s, "fin_timeout(");
1615 if (fin_timeout->fin_idle_timeout) {
1616 ds_put_format(s, "idle_timeout=%"PRIu16",",
1617 fin_timeout->fin_idle_timeout);
1618 }
1619 if (fin_timeout->fin_hard_timeout) {
1620 ds_put_format(s, "hard_timeout=%"PRIu16",",
1621 fin_timeout->fin_hard_timeout);
1622 }
1623 ds_chomp(s, ',');
1624 ds_put_char(s, ')');
1625}
1626
1627static void
1628ofpact_format(const struct ofpact *a, struct ds *s)
1629{
1630 const struct ofpact_enqueue *enqueue;
1631 const struct ofpact_resubmit *resubmit;
1632 const struct ofpact_autopath *autopath;
1633 const struct ofpact_controller *controller;
1634 const struct ofpact_tunnel *tunnel;
1635 uint16_t port;
1636
1637 switch (a->type) {
1638 case OFPACT_OUTPUT:
1639 port = ofpact_get_OUTPUT(a)->port;
1640 if (port < OFPP_MAX) {
1641 ds_put_format(s, "output:%"PRIu16, port);
1642 } else {
1643 ofputil_format_port(port, s);
1644 if (port == OFPP_CONTROLLER) {
1645 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1646 }
1647 }
1648 break;
1649
1650 case OFPACT_CONTROLLER:
1651 controller = ofpact_get_CONTROLLER(a);
1652 if (controller->reason == OFPR_ACTION &&
1653 controller->controller_id == 0) {
1654 ds_put_format(s, "CONTROLLER:%"PRIu16,
1655 ofpact_get_CONTROLLER(a)->max_len);
1656 } else {
1657 enum ofp_packet_in_reason reason = controller->reason;
1658
1659 ds_put_cstr(s, "controller(");
1660 if (reason != OFPR_ACTION) {
1661 ds_put_format(s, "reason=%s,",
1662 ofputil_packet_in_reason_to_string(reason));
1663 }
1664 if (controller->max_len != UINT16_MAX) {
1665 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1666 }
1667 if (controller->controller_id != 0) {
1668 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1669 }
1670 ds_chomp(s, ',');
1671 ds_put_char(s, ')');
1672 }
1673 break;
1674
1675 case OFPACT_ENQUEUE:
1676 enqueue = ofpact_get_ENQUEUE(a);
1677 ds_put_format(s, "enqueue:");
1678 ofputil_format_port(enqueue->port, s);
1679 ds_put_format(s, "q%"PRIu32, enqueue->queue);
1680 break;
1681
1682 case OFPACT_OUTPUT_REG:
1683 ds_put_cstr(s, "output:");
1684 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1685 break;
1686
1687 case OFPACT_BUNDLE:
1688 bundle_format(ofpact_get_BUNDLE(a), s);
1689 break;
1690
1691 case OFPACT_SET_VLAN_VID:
1692 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1693 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1694 break;
1695
1696 case OFPACT_SET_VLAN_PCP:
1697 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1698 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1699 break;
1700
1701 case OFPACT_STRIP_VLAN:
1702 ds_put_cstr(s, "strip_vlan");
1703 break;
1704
1705 case OFPACT_SET_ETH_SRC:
1706 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1707 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1708 break;
1709
1710 case OFPACT_SET_ETH_DST:
1711 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1712 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1713 break;
1714
1715 case OFPACT_SET_IPV4_SRC:
1716 ds_put_format(s, "mod_nw_src:"IP_FMT,
1717 IP_ARGS(&ofpact_get_SET_IPV4_SRC(a)->ipv4));
1718 break;
1719
1720 case OFPACT_SET_IPV4_DST:
1721 ds_put_format(s, "mod_nw_dst:"IP_FMT,
1722 IP_ARGS(&ofpact_get_SET_IPV4_DST(a)->ipv4));
1723 break;
1724
1725 case OFPACT_SET_IPV4_DSCP:
1726 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1727 break;
1728
1729 case OFPACT_SET_L4_SRC_PORT:
1730 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1731 break;
1732
1733 case OFPACT_SET_L4_DST_PORT:
1734 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1735 break;
1736
1737 case OFPACT_REG_MOVE:
1738 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1739 break;
1740
1741 case OFPACT_REG_LOAD:
1742 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1743 break;
1744
1745 case OFPACT_DEC_TTL:
c2d967a5 1746 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
f25d0cf3
BP
1747 break;
1748
1749 case OFPACT_SET_TUNNEL:
1750 tunnel = ofpact_get_SET_TUNNEL(a);
1751 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1752 (tunnel->tun_id > UINT32_MAX
1753 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1754 tunnel->tun_id);
1755 break;
1756
1757 case OFPACT_SET_QUEUE:
1758 ds_put_format(s, "set_queue:%"PRIu32,
1759 ofpact_get_SET_QUEUE(a)->queue_id);
1760 break;
1761
1762 case OFPACT_POP_QUEUE:
1763 ds_put_cstr(s, "pop_queue");
1764 break;
1765
1766 case OFPACT_FIN_TIMEOUT:
1767 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
1768 break;
1769
1770 case OFPACT_RESUBMIT:
1771 resubmit = ofpact_get_RESUBMIT(a);
1772 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
c6100d92
BP
1773 ds_put_cstr(s, "resubmit:");
1774 ofputil_format_port(resubmit->in_port, s);
f25d0cf3
BP
1775 } else {
1776 ds_put_format(s, "resubmit(");
1777 if (resubmit->in_port != OFPP_IN_PORT) {
1778 ofputil_format_port(resubmit->in_port, s);
1779 }
1780 ds_put_char(s, ',');
1781 if (resubmit->table_id != 255) {
1782 ds_put_format(s, "%"PRIu8, resubmit->table_id);
1783 }
1784 ds_put_char(s, ')');
1785 }
1786 break;
1787
1788 case OFPACT_LEARN:
1789 learn_format(ofpact_get_LEARN(a), s);
1790 break;
1791
1792 case OFPACT_MULTIPATH:
1793 multipath_format(ofpact_get_MULTIPATH(a), s);
1794 break;
1795
1796 case OFPACT_AUTOPATH:
1797 autopath = ofpact_get_AUTOPATH(a);
c6100d92
BP
1798 ds_put_cstr(s, "autopath(");
1799 ofputil_format_port(autopath->port, s);
1800 ds_put_char(s, ',');
f25d0cf3
BP
1801 mf_format_subfield(&autopath->dst, s);
1802 ds_put_char(s, ')');
1803 break;
1804
1805 case OFPACT_NOTE:
1806 print_note(ofpact_get_NOTE(a), s);
1807 break;
1808
1809 case OFPACT_EXIT:
1810 ds_put_cstr(s, "exit");
1811 break;
1812 }
1813}
1814
1815/* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
1816 * 'ofpacts' to 'string'. */
1817void
1818ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
1819 struct ds *string)
1820{
1821 ds_put_cstr(string, "actions=");
1822 if (!ofpacts_len) {
1823 ds_put_cstr(string, "drop");
1824 } else {
1825 const struct ofpact *a;
1826
1827 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1828 if (a != ofpacts) {
1829 ds_put_cstr(string, ",");
1830 }
1831 ofpact_format(a, string);
1832 }
1833 }
1834}
1835\f
1836/* Internal use by helpers. */
1837
1838void *
1839ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
1840{
1841 struct ofpact *ofpact;
1842
1843 ofpact_pad(ofpacts);
1844 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
1845 ofpact_init(ofpact, type, len);
1846 return ofpact;
1847}
1848
1849void
1850ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
1851{
1852 memset(ofpact, 0, len);
1853 ofpact->type = type;
1854 ofpact->compat = OFPUTIL_ACTION_INVALID;
1855 ofpact->len = len;
1856}
1857\f
1858/* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
1859 * starting at 'ofpact'.
1860 *
1861 * This is the correct way to update a variable-length ofpact's length after
1862 * adding the variable-length part of the payload. (See the large comment
1863 * near the end of ofp-actions.h for more information.) */
1864void
1865ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
1866{
1867 assert(ofpact == ofpacts->l2);
1868 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
1869}
1870
1871/* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
1872 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
1873 * client must call this itself after adding the final ofpact to an array of
1874 * them.
1875 *
1876 * (The consequences of failing to call this function are probably not dire.
1877 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
1878 * not dereference it. That's undefined behavior, technically, but it will not
1879 * cause a real problem on common systems. Still, it seems better to call
1880 * it.) */
1881void
1882ofpact_pad(struct ofpbuf *ofpacts)
1883{
1884 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
1885 if (rem) {
1886 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
1887 }
1888}