]> git.proxmox.com Git - ovs.git/blame - lib/ofp-actions.c
Fix a bug in conversion between flow/mask and flow key
[ovs.git] / lib / ofp-actions.c
CommitLineData
f25d0cf3 1/*
6813ee7c 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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"
f25d0cf3
BP
19#include "bundle.h"
20#include "byte-order.h"
21#include "compiler.h"
22#include "dynamic-string.h"
23#include "learn.h"
24#include "meta-flow.h"
25#include "multipath.h"
26#include "nx-match.h"
27#include "ofp-util.h"
28#include "ofpbuf.h"
cb22974d 29#include "util.h"
f25d0cf3
BP
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);
4e022ec0 45 output->port = u16_to_ofp(ntohs(oao->port));
f25d0cf3
BP
46 output->max_len = ntohs(oao->max_len);
47
48 return ofputil_check_output_port(output->port, OFPP_MAX);
49}
50
51static enum ofperr
31a9e63f 52enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
f25d0cf3
BP
53 struct ofpbuf *out)
54{
55 struct ofpact_enqueue *enqueue;
56
57 enqueue = ofpact_put_ENQUEUE(out);
4e022ec0 58 enqueue->port = u16_to_ofp(ntohs(oae->port));
f25d0cf3 59 enqueue->queue = ntohl(oae->queue_id);
4e022ec0
AW
60 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
61 && enqueue->port != OFPP_IN_PORT
f25d0cf3
BP
62 && enqueue->port != OFPP_LOCAL) {
63 return OFPERR_OFPBAC_BAD_OUT_PORT;
64 }
65 return 0;
66}
67
68static void
69resubmit_from_openflow(const struct nx_action_resubmit *nar,
70 struct ofpbuf *out)
71{
72 struct ofpact_resubmit *resubmit;
73
74 resubmit = ofpact_put_RESUBMIT(out);
75 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
4e022ec0 76 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
f25d0cf3
BP
77 resubmit->table_id = 0xff;
78}
79
80static enum ofperr
81resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
82 struct ofpbuf *out)
83{
84 struct ofpact_resubmit *resubmit;
85
86 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
87 return OFPERR_OFPBAC_BAD_ARGUMENT;
88 }
89
90 resubmit = ofpact_put_RESUBMIT(out);
91 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
4e022ec0 92 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
f25d0cf3
BP
93 resubmit->table_id = nar->table;
94 return 0;
95}
96
97static enum ofperr
98output_reg_from_openflow(const struct nx_action_output_reg *naor,
99 struct ofpbuf *out)
100{
101 struct ofpact_output_reg *output_reg;
102
103 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
104 return OFPERR_OFPBAC_BAD_ARGUMENT;
105 }
106
107 output_reg = ofpact_put_OUTPUT_REG(out);
108 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
109 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
110 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
111 output_reg->max_len = ntohs(naor->max_len);
112
113 return mf_check_src(&output_reg->src, NULL);
114}
115
116static void
117fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
118 struct ofpbuf *out)
119{
120 struct ofpact_fin_timeout *oft;
121
122 oft = ofpact_put_FIN_TIMEOUT(out);
123 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
124 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
125}
126
127static void
128controller_from_openflow(const struct nx_action_controller *nac,
129 struct ofpbuf *out)
130{
131 struct ofpact_controller *oc;
132
133 oc = ofpact_put_CONTROLLER(out);
134 oc->max_len = ntohs(nac->max_len);
135 oc->controller_id = ntohs(nac->controller_id);
136 oc->reason = nac->reason;
137}
138
4cceacb9
JS
139static enum ofperr
140metadata_from_nxast(const struct nx_action_write_metadata *nawm,
141 struct ofpbuf *out)
142{
143 struct ofpact_metadata *om;
144
145 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
146 return OFPERR_NXBRC_MUST_BE_ZERO;
147 }
148
149 om = ofpact_put_WRITE_METADATA(out);
150 om->metadata = nawm->metadata;
151 om->mask = nawm->mask;
152
153 return 0;
154}
155
f25d0cf3
BP
156static void
157note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
158{
159 struct ofpact_note *note;
160 unsigned int length;
161
162 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
163 note = ofpact_put(out, OFPACT_NOTE,
164 offsetof(struct ofpact_note, data) + length);
165 note->length = length;
166 memcpy(note->data, nan->note, length);
167}
168
c2d967a5 169static enum ofperr
7bcb1506 170dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
c2d967a5
MM
171{
172 uint16_t id = 0;
173 struct ofpact_cnt_ids *ids;
174 enum ofperr error = 0;
175
176 ids = ofpact_put_DEC_TTL(out);
7bcb1506 177 ids->ofpact.compat = compat;
c2d967a5
MM
178 ids->n_controllers = 1;
179 ofpbuf_put(out, &id, sizeof id);
180 ids = out->l2;
181 ofpact_update_len(out, &ids->ofpact);
182 return error;
183}
184
185static enum ofperr
186dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
187 struct ofpbuf *out)
188{
189 struct ofpact_cnt_ids *ids;
190 size_t ids_size;
191 int i;
192
193 ids = ofpact_put_DEC_TTL(out);
194 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
195 ids->n_controllers = ntohs(nac_ids->n_controllers);
196 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
197
198 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
199 return OFPERR_NXBRC_MUST_BE_ZERO;
200 }
201
202 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
203 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
204 "allocated for controller ids. %zu bytes are required for "
205 "%"PRIu16" controllers.", ids_size,
206 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
207 return OFPERR_OFPBAC_BAD_LEN;
208 }
209
210 for (i = 0; i < ids->n_controllers; i++) {
211 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
212 ofpbuf_put(out, &id, sizeof id);
e655542c 213 ids = out->l2;
c2d967a5
MM
214 }
215
c2d967a5
MM
216 ofpact_update_len(out, &ids->ofpact);
217
218 return 0;
219}
220
29089a54
RL
221static enum ofperr
222sample_from_openflow(const struct nx_action_sample *nas,
223 struct ofpbuf *out)
224{
225 struct ofpact_sample *sample;
226
227 sample = ofpact_put_SAMPLE(out);
228 sample->probability = ntohs(nas->probability);
229 sample->collector_set_id = ntohl(nas->collector_set_id);
230 sample->obs_domain_id = ntohl(nas->obs_domain_id);
231 sample->obs_point_id = ntohl(nas->obs_point_id);
232
233 if (sample->probability == 0) {
234 return OFPERR_OFPBAC_BAD_ARGUMENT;
235 }
236
237 return 0;
238}
239
f25d0cf3
BP
240static enum ofperr
241decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
242{
243 const struct nx_action_header *nah = (const struct nx_action_header *) a;
244 uint16_t len = ntohs(a->header.len);
245
246 if (len < sizeof(struct nx_action_header)) {
247 return OFPERR_OFPBAC_BAD_LEN;
248 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
249 return OFPERR_OFPBAC_BAD_VENDOR;
250 }
251
252 switch (nah->subtype) {
253#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
254 case CONSTANT_HTONS(ENUM): \
255 if (EXTENSIBLE \
256 ? len >= sizeof(struct STRUCT) \
257 : len == sizeof(struct STRUCT)) { \
258 *code = OFPUTIL_##ENUM; \
259 return 0; \
260 } else { \
261 return OFPERR_OFPBAC_BAD_LEN; \
262 } \
263 NOT_REACHED();
264#include "ofp-util.def"
265
266 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
267 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
268 default:
269 return OFPERR_OFPBAC_BAD_TYPE;
270 }
271}
272
273/* Parses 'a' to determine its type. On success stores the correct type into
274 * '*code' and returns 0. On failure returns an OFPERR_* error code and
275 * '*code' is indeterminate.
276 *
277 * The caller must have already verified that 'a''s length is potentially
278 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
279 * ofp_action) and no longer than the amount of space allocated to 'a').
280 *
281 * This function verifies that 'a''s length is correct for the type of action
282 * that it represents. */
283static enum ofperr
284decode_openflow10_action(const union ofp_action *a,
285 enum ofputil_action_code *code)
286{
287 switch (a->type) {
288 case CONSTANT_HTONS(OFPAT10_VENDOR):
289 return decode_nxast_action(a, code);
290
291#define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
292 case CONSTANT_HTONS(ENUM): \
293 if (a->header.len == htons(sizeof(struct STRUCT))) { \
294 *code = OFPUTIL_##ENUM; \
295 return 0; \
296 } else { \
297 return OFPERR_OFPBAC_BAD_LEN; \
298 } \
299 break;
300#include "ofp-util.def"
301
302 default:
303 return OFPERR_OFPBAC_BAD_TYPE;
304 }
305}
306
307static enum ofperr
d01c980f
BP
308ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
309 struct ofpbuf *out)
f25d0cf3
BP
310{
311 const struct nx_action_resubmit *nar;
312 const struct nx_action_set_tunnel *nast;
313 const struct nx_action_set_queue *nasq;
314 const struct nx_action_note *nan;
315 const struct nx_action_set_tunnel64 *nast64;
4cceacb9 316 const struct nx_action_write_metadata *nawm;
f25d0cf3 317 struct ofpact_tunnel *tunnel;
d01c980f 318 enum ofperr error = 0;
f25d0cf3
BP
319
320 switch (code) {
321 case OFPUTIL_ACTION_INVALID:
d01c980f 322#define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
3ddcaf2d 323#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
d01c980f 324#include "ofp-util.def"
f25d0cf3
BP
325 NOT_REACHED();
326
f25d0cf3
BP
327 case OFPUTIL_NXAST_RESUBMIT:
328 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
329 break;
330
331 case OFPUTIL_NXAST_SET_TUNNEL:
332 nast = (const struct nx_action_set_tunnel *) a;
333 tunnel = ofpact_put_SET_TUNNEL(out);
334 tunnel->ofpact.compat = code;
335 tunnel->tun_id = ntohl(nast->tun_id);
336 break;
337
4cceacb9 338 case OFPUTIL_NXAST_WRITE_METADATA:
db5a1019 339 nawm = ALIGNED_CAST(const struct nx_action_write_metadata *, a);
4cceacb9
JS
340 error = metadata_from_nxast(nawm, out);
341 break;
342
f25d0cf3
BP
343 case OFPUTIL_NXAST_SET_QUEUE:
344 nasq = (const struct nx_action_set_queue *) a;
345 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
346 break;
347
348 case OFPUTIL_NXAST_POP_QUEUE:
349 ofpact_put_POP_QUEUE(out);
350 break;
351
352 case OFPUTIL_NXAST_REG_MOVE:
353 error = nxm_reg_move_from_openflow(
354 (const struct nx_action_reg_move *) a, out);
355 break;
356
357 case OFPUTIL_NXAST_REG_LOAD:
358 error = nxm_reg_load_from_openflow(
db5a1019 359 ALIGNED_CAST(const struct nx_action_reg_load *, a), out);
f25d0cf3
BP
360 break;
361
bd85dac1
AZ
362 case OFPUTIL_NXAST_STACK_PUSH:
363 error = nxm_stack_push_from_openflow(
364 (const struct nx_action_stack *) a, out);
365 break;
366
367 case OFPUTIL_NXAST_STACK_POP:
368 error = nxm_stack_pop_from_openflow(
369 (const struct nx_action_stack *) a, out);
370 break;
371
f25d0cf3
BP
372 case OFPUTIL_NXAST_NOTE:
373 nan = (const struct nx_action_note *) a;
374 note_from_openflow(nan, out);
375 break;
376
377 case OFPUTIL_NXAST_SET_TUNNEL64:
db5a1019 378 nast64 = ALIGNED_CAST(const struct nx_action_set_tunnel64 *, a);
f25d0cf3
BP
379 tunnel = ofpact_put_SET_TUNNEL(out);
380 tunnel->ofpact.compat = code;
381 tunnel->tun_id = ntohll(nast64->tun_id);
382 break;
383
384 case OFPUTIL_NXAST_MULTIPATH:
385 error = multipath_from_openflow((const struct nx_action_multipath *) a,
386 ofpact_put_MULTIPATH(out));
387 break;
388
f25d0cf3
BP
389 case OFPUTIL_NXAST_BUNDLE:
390 case OFPUTIL_NXAST_BUNDLE_LOAD:
391 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
392 break;
393
394 case OFPUTIL_NXAST_OUTPUT_REG:
395 error = output_reg_from_openflow(
396 (const struct nx_action_output_reg *) a, out);
397 break;
398
399 case OFPUTIL_NXAST_RESUBMIT_TABLE:
400 nar = (const struct nx_action_resubmit *) a;
401 error = resubmit_table_from_openflow(nar, out);
402 break;
403
404 case OFPUTIL_NXAST_LEARN:
db5a1019
AW
405 error = learn_from_openflow(
406 ALIGNED_CAST(const struct nx_action_learn *, a), out);
f25d0cf3
BP
407 break;
408
409 case OFPUTIL_NXAST_EXIT:
410 ofpact_put_EXIT(out);
411 break;
412
413 case OFPUTIL_NXAST_DEC_TTL:
7bcb1506 414 error = dec_ttl_from_openflow(out, code);
c2d967a5
MM
415 break;
416
417 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
418 error = dec_ttl_cnt_ids_from_openflow(
419 (const struct nx_action_cnt_ids *) a, out);
f25d0cf3
BP
420 break;
421
422 case OFPUTIL_NXAST_FIN_TIMEOUT:
423 fin_timeout_from_openflow(
424 (const struct nx_action_fin_timeout *) a, out);
425 break;
426
427 case OFPUTIL_NXAST_CONTROLLER:
428 controller_from_openflow((const struct nx_action_controller *) a, out);
429 break;
b02475c5
SH
430
431 case OFPUTIL_NXAST_PUSH_MPLS: {
432 struct nx_action_push_mpls *nxapm = (struct nx_action_push_mpls *)a;
433 if (!eth_type_mpls(nxapm->ethertype)) {
434 return OFPERR_OFPBAC_BAD_ARGUMENT;
435 }
436 ofpact_put_PUSH_MPLS(out)->ethertype = nxapm->ethertype;
437 break;
438 }
439
0f3f3c3d
SH
440 case OFPUTIL_NXAST_SET_MPLS_TTL: {
441 struct nx_action_mpls_ttl *nxamt = (struct nx_action_mpls_ttl *)a;
442 ofpact_put_SET_MPLS_TTL(out)->ttl = nxamt->ttl;
443 break;
444 }
445
b676167a
SH
446 case OFPUTIL_NXAST_DEC_MPLS_TTL:
447 ofpact_put_DEC_MPLS_TTL(out);
448 break;
449
b02475c5
SH
450 case OFPUTIL_NXAST_POP_MPLS: {
451 struct nx_action_pop_mpls *nxapm = (struct nx_action_pop_mpls *)a;
452 if (eth_type_mpls(nxapm->ethertype)) {
453 return OFPERR_OFPBAC_BAD_ARGUMENT;
454 }
455 ofpact_put_POP_MPLS(out)->ethertype = nxapm->ethertype;
456 break;
457 }
29089a54
RL
458
459 case OFPUTIL_NXAST_SAMPLE:
460 error = sample_from_openflow(
461 (const struct nx_action_sample *) a, out);
462 break;
f25d0cf3
BP
463 }
464
465 return error;
466}
467
d01c980f
BP
468static enum ofperr
469ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
470{
471 enum ofputil_action_code code;
472 enum ofperr error;
473
474 error = decode_openflow10_action(a, &code);
475 if (error) {
476 return error;
477 }
478
479 switch (code) {
480 case OFPUTIL_ACTION_INVALID:
3ddcaf2d 481#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
d01c980f
BP
482#include "ofp-util.def"
483 NOT_REACHED();
484
485 case OFPUTIL_OFPAT10_OUTPUT:
486 return output_from_openflow10(&a->output10, out);
487
488 case OFPUTIL_OFPAT10_SET_VLAN_VID:
489 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
490 return OFPERR_OFPBAC_BAD_ARGUMENT;
491 }
492 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
493 break;
494
495 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
496 if (a->vlan_pcp.vlan_pcp & ~7) {
497 return OFPERR_OFPBAC_BAD_ARGUMENT;
498 }
499 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
500 break;
501
502 case OFPUTIL_OFPAT10_STRIP_VLAN:
503 ofpact_put_STRIP_VLAN(out);
504 break;
505
506 case OFPUTIL_OFPAT10_SET_DL_SRC:
507 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
508 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
509 break;
510
511 case OFPUTIL_OFPAT10_SET_DL_DST:
512 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
513 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
514 break;
515
516 case OFPUTIL_OFPAT10_SET_NW_SRC:
517 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
518 break;
519
520 case OFPUTIL_OFPAT10_SET_NW_DST:
521 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
522 break;
523
524 case OFPUTIL_OFPAT10_SET_NW_TOS:
525 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
526 return OFPERR_OFPBAC_BAD_ARGUMENT;
527 }
528 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
529 break;
530
531 case OFPUTIL_OFPAT10_SET_TP_SRC:
532 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
533 break;
534
535 case OFPUTIL_OFPAT10_SET_TP_DST:
536 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
537
538 break;
539
540 case OFPUTIL_OFPAT10_ENQUEUE:
31a9e63f 541 error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
d01c980f
BP
542 out);
543 break;
544
545#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
546#include "ofp-util.def"
547 return ofpact_from_nxast(a, code, out);
548 }
549
550 return error;
551}
552
f25d0cf3
BP
553static inline union ofp_action *
554action_next(const union ofp_action *a)
555{
556 return ((union ofp_action *) (void *)
557 ((uint8_t *) a + ntohs(a->header.len)));
558}
559
560static inline bool
561action_is_valid(const union ofp_action *a, size_t n_actions)
562{
563 uint16_t len = ntohs(a->header.len);
564 return (!(len % OFP_ACTION_ALIGN)
565 && len >= sizeof *a
566 && len / sizeof *a <= n_actions);
567}
568
569/* This macro is careful to check for actions with bad lengths. */
570#define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
571 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
572 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
573 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
574 (ITER) = action_next(ITER)))
575
699dddf1
BP
576static void
577log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
578 enum ofperr error)
579{
580 if (!VLOG_DROP_WARN(&rl)) {
581 struct ds s;
582
583 ds_init(&s);
584 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
585 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
586 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
587 ds_destroy(&s);
588 }
589}
590
f25d0cf3 591static enum ofperr
0300caaf
IY
592ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
593 struct ofpbuf *out,
594 enum ofperr (*ofpact_from_openflow)(
595 const union ofp_action *a, struct ofpbuf *out))
f25d0cf3
BP
596{
597 const union ofp_action *a;
598 size_t left;
599
600 ACTION_FOR_EACH (a, left, in, n_in) {
0300caaf 601 enum ofperr error = ofpact_from_openflow(a, out);
f25d0cf3 602 if (error) {
699dddf1 603 log_bad_action(in, n_in, a - in, error);
f25d0cf3
BP
604 return error;
605 }
606 }
607 if (left) {
699dddf1
BP
608 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
609 log_bad_action(in, n_in, n_in - left, error);
610 return error;
f25d0cf3
BP
611 }
612
613 ofpact_pad(out);
614 return 0;
615}
616
0300caaf
IY
617static enum ofperr
618ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
619 struct ofpbuf *out)
620{
621 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
622}
623
d01c980f
BP
624static enum ofperr
625ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
626 struct ofpbuf *ofpacts,
627 enum ofperr (*translate)(const union ofp_action *actions,
628 size_t n_actions,
629 struct ofpbuf *ofpacts))
f25d0cf3
BP
630{
631 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
632 const union ofp_action *actions;
633 enum ofperr error;
634
635 ofpbuf_clear(ofpacts);
636
637 if (actions_len % OFP_ACTION_ALIGN != 0) {
638 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
639 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
640 return OFPERR_OFPBRC_BAD_LEN;
641 }
642
643 actions = ofpbuf_try_pull(openflow, actions_len);
644 if (actions == NULL) {
645 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
646 "remaining message length (%zu)",
647 actions_len, openflow->size);
648 return OFPERR_OFPBRC_BAD_LEN;
649 }
650
d01c980f 651 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
4cceacb9
JS
652 if (error) {
653 ofpbuf_clear(ofpacts);
654 return error;
655 }
656
657 error = ofpacts_verify(ofpacts->data, ofpacts->size);
f25d0cf3
BP
658 if (error) {
659 ofpbuf_clear(ofpacts);
660 }
d01c980f
BP
661 return error;
662}
663
664/* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
665 * front of 'openflow' into ofpacts. On success, replaces any existing content
666 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
667 * Returns 0 if successful, otherwise an OpenFlow error.
668 *
13f894b1
BP
669 * The parsed actions are valid generically, but they may not be valid in a
670 * specific context. For example, port numbers up to OFPP_MAX are valid
671 * generically, but specific datapaths may only support port numbers in a
672 * smaller range. Use ofpacts_check() to additional check whether actions are
673 * valid in a specific context. */
d01c980f
BP
674enum ofperr
675ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
676 struct ofpbuf *ofpacts)
677{
678 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
679 ofpacts_from_openflow10);
680}
681\f
682/* OpenFlow 1.1 actions. */
683
684/* Parses 'a' to determine its type. On success stores the correct type into
685 * '*code' and returns 0. On failure returns an OFPERR_* error code and
686 * '*code' is indeterminate.
687 *
688 * The caller must have already verified that 'a''s length is potentially
689 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
690 * ofp_action) and no longer than the amount of space allocated to 'a').
691 *
692 * This function verifies that 'a''s length is correct for the type of action
693 * that it represents. */
694static enum ofperr
695decode_openflow11_action(const union ofp_action *a,
696 enum ofputil_action_code *code)
697{
3ddcaf2d
BP
698 uint16_t len;
699
d01c980f
BP
700 switch (a->type) {
701 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
702 return decode_nxast_action(a, code);
703
3ddcaf2d
BP
704#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
705 case CONSTANT_HTONS(ENUM): \
706 len = ntohs(a->header.len); \
707 if (EXTENSIBLE \
708 ? len >= sizeof(struct STRUCT) \
709 : len == sizeof(struct STRUCT)) { \
710 *code = OFPUTIL_##ENUM; \
711 return 0; \
712 } else { \
713 return OFPERR_OFPBAC_BAD_LEN; \
714 } \
715 NOT_REACHED();
d01c980f
BP
716#include "ofp-util.def"
717
718 default:
719 return OFPERR_OFPBAC_BAD_TYPE;
720 }
721}
722
723static enum ofperr
724output_from_openflow11(const struct ofp11_action_output *oao,
725 struct ofpbuf *out)
726{
727 struct ofpact_output *output;
728 enum ofperr error;
729
730 output = ofpact_put_OUTPUT(out);
731 output->max_len = ntohs(oao->max_len);
732
733 error = ofputil_port_from_ofp11(oao->port, &output->port);
734 if (error) {
735 return error;
736 }
737
738 return ofputil_check_output_port(output->port, OFPP_MAX);
739}
740
741static enum ofperr
742ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
743{
744 enum ofputil_action_code code;
745 enum ofperr error;
746
747 error = decode_openflow11_action(a, &code);
748 if (error) {
749 return error;
750 }
751
752 switch (code) {
753 case OFPUTIL_ACTION_INVALID:
754#define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
755#include "ofp-util.def"
756 NOT_REACHED();
757
758 case OFPUTIL_OFPAT11_OUTPUT:
759 return output_from_openflow11((const struct ofp11_action_output *) a,
760 out);
761
762 case OFPUTIL_OFPAT11_SET_VLAN_VID:
763 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
764 return OFPERR_OFPBAC_BAD_ARGUMENT;
765 }
766 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
767 break;
768
769 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
770 if (a->vlan_pcp.vlan_pcp & ~7) {
771 return OFPERR_OFPBAC_BAD_ARGUMENT;
772 }
773 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
774 break;
775
3e34fbdd
IY
776 case OFPUTIL_OFPAT11_PUSH_VLAN:
777 if (((const struct ofp11_action_push *)a)->ethertype !=
778 htons(ETH_TYPE_VLAN_8021Q)) {
5dca28b5 779 /* XXX 802.1AD(QinQ) isn't supported at the moment */
15549878 780 return OFPERR_OFPBAC_BAD_ARGUMENT;
3e34fbdd
IY
781 }
782 ofpact_put_PUSH_VLAN(out);
783 break;
784
8e61c110
IY
785 case OFPUTIL_OFPAT11_POP_VLAN:
786 ofpact_put_STRIP_VLAN(out);
787 break;
788
276c4e7a
SH
789 case OFPUTIL_OFPAT11_SET_QUEUE:
790 ofpact_put_SET_QUEUE(out)->queue_id =
791 ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
792 break;
793
d01c980f
BP
794 case OFPUTIL_OFPAT11_SET_DL_SRC:
795 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
796 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
797 break;
798
799 case OFPUTIL_OFPAT11_SET_DL_DST:
800 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
801 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
802 break;
803
7bcb1506
IY
804 case OFPUTIL_OFPAT11_DEC_NW_TTL:
805 dec_ttl_from_openflow(out, code);
806 break;
807
d01c980f
BP
808 case OFPUTIL_OFPAT11_SET_NW_SRC:
809 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
810 break;
811
812 case OFPUTIL_OFPAT11_SET_NW_DST:
813 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
814 break;
815
816 case OFPUTIL_OFPAT11_SET_NW_TOS:
817 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
818 return OFPERR_OFPBAC_BAD_ARGUMENT;
819 }
820 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
821 break;
822
823 case OFPUTIL_OFPAT11_SET_TP_SRC:
824 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
825 break;
826
827 case OFPUTIL_OFPAT11_SET_TP_DST:
828 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
829 break;
830
e9536ecb 831 case OFPUTIL_OFPAT12_SET_FIELD:
d55b18c8
IY
832 return nxm_reg_load_from_openflow12_set_field(
833 (const struct ofp12_action_set_field *)a, out);
e9536ecb 834
0f3f3c3d
SH
835 case OFPUTIL_OFPAT11_SET_MPLS_TTL: {
836 struct ofp11_action_mpls_ttl *oamt = (struct ofp11_action_mpls_ttl *)a;
837 ofpact_put_SET_MPLS_TTL(out)->ttl = oamt->mpls_ttl;
838 break;
839 }
840
b676167a
SH
841 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
842 ofpact_put_DEC_MPLS_TTL(out);
843 break;
844
b02475c5
SH
845 case OFPUTIL_OFPAT11_PUSH_MPLS: {
846 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
847 if (!eth_type_mpls(oap->ethertype)) {
848 return OFPERR_OFPBAC_BAD_ARGUMENT;
849 }
850 ofpact_put_PUSH_MPLS(out)->ethertype = oap->ethertype;
851 break;
852 }
853
854 case OFPUTIL_OFPAT11_POP_MPLS: {
855 struct ofp11_action_pop_mpls *oapm = (struct ofp11_action_pop_mpls *)a;
856 if (eth_type_mpls(oapm->ethertype)) {
857 return OFPERR_OFPBAC_BAD_ARGUMENT;
858 }
859 ofpact_put_POP_MPLS(out)->ethertype = oapm->ethertype;
860 break;
861 }
862
d01c980f
BP
863#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
864#include "ofp-util.def"
865 return ofpact_from_nxast(a, code, out);
866 }
867
868 return error;
869}
870
871static enum ofperr
872ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
873 struct ofpbuf *out)
874{
0300caaf 875 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
f25d0cf3
BP
876}
877\f
d01c980f
BP
878/* OpenFlow 1.1 instructions. */
879
d01c980f 880#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
a359d5ad
IY
881 static inline const struct STRUCT * \
882 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
883 { \
cb22974d 884 ovs_assert(inst->type == htons(ENUM)); \
db5a1019 885 return ALIGNED_CAST(struct STRUCT *, inst); \
a359d5ad
IY
886 } \
887 \
d01c980f
BP
888 static inline void \
889 instruction_init_##ENUM(struct STRUCT *s) \
890 { \
891 memset(s, 0, sizeof *s); \
892 s->type = htons(ENUM); \
893 s->len = htons(sizeof *s); \
894 } \
895 \
896 static inline struct STRUCT * \
897 instruction_put_##ENUM(struct ofpbuf *buf) \
898 { \
899 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
900 instruction_init_##ENUM(s); \
901 return s; \
902 }
903OVS_INSTRUCTIONS
904#undef DEFINE_INST
905
a359d5ad
IY
906struct instruction_type_info {
907 enum ovs_instruction_type type;
908 const char *name;
909};
910
911static const struct instruction_type_info inst_info[] = {
912#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
913OVS_INSTRUCTIONS
914#undef DEFINE_INST
915};
916
917const char *
ba0bc9b0 918ovs_instruction_name_from_type(enum ovs_instruction_type type)
a359d5ad 919{
6ae0fd54 920 return inst_info[type].name;
a359d5ad
IY
921}
922
923int
ba0bc9b0 924ovs_instruction_type_from_name(const char *name)
a359d5ad
IY
925{
926 const struct instruction_type_info *p;
927 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
928 if (!strcasecmp(name, p->name)) {
929 return p->type;
930 }
931 }
932 return -1;
933}
934
084c53de
BP
935enum ovs_instruction_type
936ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
937{
938 switch (type) {
939 case OFPACT_METER:
940 return OVSINST_OFPIT13_METER;
941 case OFPACT_CLEAR_ACTIONS:
942 return OVSINST_OFPIT11_CLEAR_ACTIONS;
943 case OFPACT_WRITE_METADATA:
944 return OVSINST_OFPIT11_WRITE_METADATA;
945 case OFPACT_GOTO_TABLE:
946 return OVSINST_OFPIT11_GOTO_TABLE;
947 case OFPACT_OUTPUT:
948 case OFPACT_CONTROLLER:
949 case OFPACT_ENQUEUE:
950 case OFPACT_OUTPUT_REG:
951 case OFPACT_BUNDLE:
952 case OFPACT_SET_VLAN_VID:
953 case OFPACT_SET_VLAN_PCP:
954 case OFPACT_STRIP_VLAN:
955 case OFPACT_PUSH_VLAN:
956 case OFPACT_SET_ETH_SRC:
957 case OFPACT_SET_ETH_DST:
958 case OFPACT_SET_IPV4_SRC:
959 case OFPACT_SET_IPV4_DST:
960 case OFPACT_SET_IPV4_DSCP:
961 case OFPACT_SET_L4_SRC_PORT:
962 case OFPACT_SET_L4_DST_PORT:
963 case OFPACT_REG_MOVE:
964 case OFPACT_REG_LOAD:
965 case OFPACT_STACK_PUSH:
966 case OFPACT_STACK_POP:
967 case OFPACT_DEC_TTL:
968 case OFPACT_SET_MPLS_TTL:
969 case OFPACT_DEC_MPLS_TTL:
970 case OFPACT_PUSH_MPLS:
971 case OFPACT_POP_MPLS:
972 case OFPACT_SET_TUNNEL:
973 case OFPACT_SET_QUEUE:
974 case OFPACT_POP_QUEUE:
975 case OFPACT_FIN_TIMEOUT:
976 case OFPACT_RESUBMIT:
977 case OFPACT_LEARN:
978 case OFPACT_MULTIPATH:
979 case OFPACT_NOTE:
980 case OFPACT_EXIT:
981 case OFPACT_SAMPLE:
982 default:
983 return OVSINST_OFPIT11_APPLY_ACTIONS;
984 }
985}
986
d01c980f
BP
987static inline struct ofp11_instruction *
988instruction_next(const struct ofp11_instruction *inst)
989{
990 return ((struct ofp11_instruction *) (void *)
991 ((uint8_t *) inst + ntohs(inst->len)));
992}
993
994static inline bool
995instruction_is_valid(const struct ofp11_instruction *inst,
996 size_t n_instructions)
997{
998 uint16_t len = ntohs(inst->len);
999 return (!(len % OFP11_INSTRUCTION_ALIGN)
1000 && len >= sizeof *inst
1001 && len / sizeof *inst <= n_instructions);
1002}
1003
1004/* This macro is careful to check for instructions with bad lengths. */
1005#define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1006 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1007 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1008 ((LEFT) -= (ntohs((ITER)->len) \
1009 / sizeof(struct ofp11_instruction)), \
1010 (ITER) = instruction_next(ITER)))
1011
1012static enum ofperr
1013decode_openflow11_instruction(const struct ofp11_instruction *inst,
1014 enum ovs_instruction_type *type)
1015{
1016 uint16_t len = ntohs(inst->len);
1017
1018 switch (inst->type) {
1019 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1020 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1021
1022#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1023 case CONSTANT_HTONS(ENUM): \
1024 if (EXTENSIBLE \
1025 ? len >= sizeof(struct STRUCT) \
1026 : len == sizeof(struct STRUCT)) { \
1027 *type = OVSINST_##ENUM; \
1028 return 0; \
1029 } else { \
1030 return OFPERR_OFPBIC_BAD_LEN; \
1031 }
1032OVS_INSTRUCTIONS
1033#undef DEFINE_INST
1034
1035 default:
1036 return OFPERR_OFPBIC_UNKNOWN_INST;
1037 }
1038}
1039
1040static enum ofperr
1041decode_openflow11_instructions(const struct ofp11_instruction insts[],
1042 size_t n_insts,
1043 const struct ofp11_instruction *out[])
1044{
1045 const struct ofp11_instruction *inst;
1046 size_t left;
1047
1048 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1049 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1050 enum ovs_instruction_type type;
1051 enum ofperr error;
1052
1053 error = decode_openflow11_instruction(inst, &type);
1054 if (error) {
1055 return error;
1056 }
1057
1058 if (out[type]) {
f4104c68 1059 return OFPERR_ONFBIC_DUP_INSTRUCTION;
d01c980f
BP
1060 }
1061 out[type] = inst;
1062 }
1063
1064 if (left) {
1065 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1066 (n_insts - left) * sizeof *inst);
1067 return OFPERR_OFPBIC_BAD_LEN;
1068 }
1069 return 0;
1070}
1071
1072static void
1073get_actions_from_instruction(const struct ofp11_instruction *inst,
db5a1019
AW
1074 const union ofp_action **actions,
1075 size_t *n_actions)
d01c980f 1076{
db5a1019 1077 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
d01c980f
BP
1078 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
1079}
1080
1081/* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
1082 * front of 'openflow' into ofpacts. On success, replaces any existing content
1083 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1084 * Returns 0 if successful, otherwise an OpenFlow error.
1085 *
1086 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1087 * instructions, so you should call ofpacts_pull_openflow11_instructions()
1088 * instead of this function.
1089 *
13f894b1
BP
1090 * The parsed actions are valid generically, but they may not be valid in a
1091 * specific context. For example, port numbers up to OFPP_MAX are valid
1092 * generically, but specific datapaths may only support port numbers in a
1093 * smaller range. Use ofpacts_check() to additional check whether actions are
1094 * valid in a specific context. */
d01c980f
BP
1095enum ofperr
1096ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
1097 unsigned int actions_len,
1098 struct ofpbuf *ofpacts)
1099{
0300caaf
IY
1100 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1101 ofpacts_from_openflow11);
d01c980f
BP
1102}
1103
1104enum ofperr
1105ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
1106 unsigned int instructions_len,
1107 struct ofpbuf *ofpacts)
1108{
1109 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1110 const struct ofp11_instruction *instructions;
1111 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1112 enum ofperr error;
1113
1114 ofpbuf_clear(ofpacts);
1115
1116 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1117 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1118 "multiple of %d",
1119 instructions_len, OFP11_INSTRUCTION_ALIGN);
1120 error = OFPERR_OFPBIC_BAD_LEN;
1121 goto exit;
1122 }
1123
1124 instructions = ofpbuf_try_pull(openflow, instructions_len);
1125 if (instructions == NULL) {
1126 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1127 "remaining message length (%zu)",
1128 instructions_len, openflow->size);
1129 error = OFPERR_OFPBIC_BAD_LEN;
1130 goto exit;
1131 }
1132
1133 error = decode_openflow11_instructions(
1134 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1135 insts);
1136 if (error) {
1137 goto exit;
1138 }
1139
638a19b0
JR
1140 if (insts[OVSINST_OFPIT13_METER]) {
1141 const struct ofp13_instruction_meter *oim;
1142 struct ofpact_meter *om;
1143
db5a1019
AW
1144 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1145 insts[OVSINST_OFPIT13_METER]);
638a19b0
JR
1146
1147 om = ofpact_put_METER(ofpacts);
1148 om->meter_id = ntohl(oim->meter_id);
1149 }
d01c980f
BP
1150 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1151 const union ofp_action *actions;
1152 size_t n_actions;
1153
1154 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1155 &actions, &n_actions);
1156 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1157 if (error) {
1158 goto exit;
1159 }
1160 }
b19e8793
IY
1161 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1162 instruction_get_OFPIT11_CLEAR_ACTIONS(
1163 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1164 ofpact_put_CLEAR_ACTIONS(ofpacts);
1165 }
5dca28b5 1166 /* XXX Write-Actions */
4cceacb9
JS
1167 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1168 const struct ofp11_instruction_write_metadata *oiwm;
1169 struct ofpact_metadata *om;
1170
db5a1019
AW
1171 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1172 insts[OVSINST_OFPIT11_WRITE_METADATA]);
4cceacb9
JS
1173
1174 om = ofpact_put_WRITE_METADATA(ofpacts);
1175 om->metadata = oiwm->metadata;
1176 om->mask = oiwm->metadata_mask;
1177 }
8dd54666
IY
1178 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1179 const struct ofp11_instruction_goto_table *oigt;
1180 struct ofpact_goto_table *ogt;
1181
1182 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1183 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1184 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1185 ogt->table_id = oigt->table_id;
1186 }
d01c980f 1187
4cceacb9 1188 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
d01c980f
BP
1189 error = OFPERR_OFPBIC_UNSUP_INST;
1190 goto exit;
1191 }
1192
4cceacb9 1193 error = ofpacts_verify(ofpacts->data, ofpacts->size);
d01c980f
BP
1194exit:
1195 if (error) {
1196 ofpbuf_clear(ofpacts);
1197 }
1198 return error;
1199}
1200\f
eca39bce 1201/* May modify flow->dl_type, caller must restore it. */
f25d0cf3 1202static enum ofperr
e3b56933
JR
1203ofpact_check__(const struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
1204 uint8_t table_id)
f25d0cf3
BP
1205{
1206 const struct ofpact_enqueue *enqueue;
1207
1208 switch (a->type) {
1209 case OFPACT_OUTPUT:
1210 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1211 max_ports);
1212
1213 case OFPACT_CONTROLLER:
1214 return 0;
1215
1216 case OFPACT_ENQUEUE:
1217 enqueue = ofpact_get_ENQUEUE(a);
4e022ec0
AW
1218 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1219 && enqueue->port != OFPP_IN_PORT
f25d0cf3
BP
1220 && enqueue->port != OFPP_LOCAL) {
1221 return OFPERR_OFPBAC_BAD_OUT_PORT;
1222 }
1223 return 0;
1224
1225 case OFPACT_OUTPUT_REG:
1226 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1227
1228 case OFPACT_BUNDLE:
1229 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1230
1231 case OFPACT_SET_VLAN_VID:
1232 case OFPACT_SET_VLAN_PCP:
1233 case OFPACT_STRIP_VLAN:
3e34fbdd 1234 case OFPACT_PUSH_VLAN:
f25d0cf3
BP
1235 case OFPACT_SET_ETH_SRC:
1236 case OFPACT_SET_ETH_DST:
1237 case OFPACT_SET_IPV4_SRC:
1238 case OFPACT_SET_IPV4_DST:
1239 case OFPACT_SET_IPV4_DSCP:
1240 case OFPACT_SET_L4_SRC_PORT:
1241 case OFPACT_SET_L4_DST_PORT:
1242 return 0;
1243
1244 case OFPACT_REG_MOVE:
1245 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1246
1247 case OFPACT_REG_LOAD:
500d243d 1248 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
f25d0cf3 1249
bd85dac1
AZ
1250 case OFPACT_STACK_PUSH:
1251 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1252
1253 case OFPACT_STACK_POP:
1254 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1255
f25d0cf3 1256 case OFPACT_DEC_TTL:
0f3f3c3d 1257 case OFPACT_SET_MPLS_TTL:
b676167a 1258 case OFPACT_DEC_MPLS_TTL:
f25d0cf3
BP
1259 case OFPACT_SET_TUNNEL:
1260 case OFPACT_SET_QUEUE:
1261 case OFPACT_POP_QUEUE:
1262 case OFPACT_FIN_TIMEOUT:
1263 case OFPACT_RESUBMIT:
1264 return 0;
1265
1266 case OFPACT_LEARN:
1267 return learn_check(ofpact_get_LEARN(a), flow);
1268
1269 case OFPACT_MULTIPATH:
1270 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1271
f25d0cf3
BP
1272 case OFPACT_NOTE:
1273 case OFPACT_EXIT:
1274 return 0;
1275
b02475c5 1276 case OFPACT_PUSH_MPLS:
eca39bce 1277 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
b02475c5
SH
1278 return 0;
1279
1280 case OFPACT_POP_MPLS:
eca39bce 1281 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
b02475c5
SH
1282 return 0;
1283
29089a54
RL
1284 case OFPACT_SAMPLE:
1285 return 0;
1286
b19e8793 1287 case OFPACT_CLEAR_ACTIONS:
4cceacb9 1288 case OFPACT_WRITE_METADATA:
8dd54666
IY
1289 return 0;
1290
9cae45dc
JR
1291 case OFPACT_METER: {
1292 uint32_t mid = ofpact_get_METER(a)->meter_id;
1293 if (mid == 0 || mid > OFPM13_MAX) {
1294 return OFPERR_OFPMMFC_INVALID_METER;
1295 }
1296 return 0;
1297 }
e3b56933
JR
1298
1299 case OFPACT_GOTO_TABLE:
1300 if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
1301 return OFPERR_OFPBRC_BAD_TABLE_ID;
1302 }
1303 return 0;
1304
f25d0cf3
BP
1305 default:
1306 NOT_REACHED();
1307 }
1308}
1309
1310/* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1311 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
eca39bce
JR
1312 * switch with no more than 'max_ports' ports.
1313 *
1314 * May temporarily modify 'flow', but restores the changes before returning. */
f25d0cf3
BP
1315enum ofperr
1316ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
e3b56933 1317 struct flow *flow, ofp_port_t max_ports, uint8_t table_id)
f25d0cf3
BP
1318{
1319 const struct ofpact *a;
b02475c5 1320 ovs_be16 dl_type = flow->dl_type;
eca39bce 1321 enum ofperr error = 0;
f25d0cf3
BP
1322
1323 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
e3b56933 1324 error = ofpact_check__(a, flow, max_ports, table_id);
f25d0cf3 1325 if (error) {
eca39bce 1326 break;
f25d0cf3
BP
1327 }
1328 }
eca39bce
JR
1329 flow->dl_type = dl_type; /* Restore. */
1330 return error;
f25d0cf3 1331}
4cceacb9
JS
1332
1333/* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1334 * in the appropriate order as defined by the OpenFlow spec. */
1335enum ofperr
1336ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1337{
1338 const struct ofpact *a;
6813ee7c 1339 enum ovs_instruction_type inst;
4cceacb9 1340
6813ee7c 1341 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
4cceacb9 1342 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6813ee7c
BP
1343 enum ovs_instruction_type next;
1344
084c53de 1345 next = ovs_instruction_type_from_ofpact_type(a->type);
6813ee7c 1346 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS && next <= inst) {
ba0bc9b0
BP
1347 const char *name = ovs_instruction_name_from_type(inst);
1348 const char *next_name = ovs_instruction_name_from_type(next);
6813ee7c
BP
1349
1350 if (next == inst) {
1351 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1352 "1.1+ compatibility", name);
4cceacb9 1353 } else {
6813ee7c
BP
1354 VLOG_WARN("invalid instruction ordering: %s must appear "
1355 "before %s, for OpenFlow 1.1+ compatibility",
1356 next_name, name);
4cceacb9 1357 }
6813ee7c 1358 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
4cceacb9
JS
1359 }
1360
6813ee7c 1361 inst = next;
4cceacb9
JS
1362 }
1363
1364 return 0;
1365}
f25d0cf3
BP
1366\f
1367/* Converting ofpacts to Nicira OpenFlow extensions. */
1368
1369static void
1370ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1371 struct ofpbuf *out)
1372{
1373 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1374
1375 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1376 output_reg->src.n_bits);
1377 naor->src = htonl(output_reg->src.field->nxm_header);
1378 naor->max_len = htons(output_reg->max_len);
1379}
1380
1381static void
1382ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1383 struct ofpbuf *out)
1384{
1385 struct nx_action_resubmit *nar;
1386
1387 if (resubmit->table_id == 0xff
1388 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1389 nar = ofputil_put_NXAST_RESUBMIT(out);
1390 } else {
1391 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1392 nar->table = resubmit->table_id;
1393 }
4e022ec0 1394 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
f25d0cf3
BP
1395}
1396
1397static void
1398ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1399 struct ofpbuf *out)
1400{
1401 uint64_t tun_id = tunnel->tun_id;
1402
1403 if (tun_id <= UINT32_MAX
1404 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1405 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1406 } else {
1407 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1408 }
1409}
1410
4cceacb9
JS
1411static void
1412ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1413 struct ofpbuf *out)
1414{
1415 struct nx_action_write_metadata *nawm;
1416
1417 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1418 nawm->metadata = om->metadata;
1419 nawm->mask = om->mask;
1420}
1421
f25d0cf3
BP
1422static void
1423ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1424{
1425 size_t start_ofs = out->size;
1426 struct nx_action_note *nan;
1427 unsigned int remainder;
1428 unsigned int len;
1429
1430 nan = ofputil_put_NXAST_NOTE(out);
1431 out->size -= sizeof nan->note;
1432
1433 ofpbuf_put(out, note->data, note->length);
1434
1435 len = out->size - start_ofs;
1436 remainder = len % OFP_ACTION_ALIGN;
1437 if (remainder) {
1438 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1439 }
db5a1019 1440 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
f25d0cf3
BP
1441 nan->len = htons(out->size - start_ofs);
1442}
1443
1444static void
1445ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1446 struct ofpbuf *out)
1447{
1448 struct nx_action_controller *nac;
1449
1450 nac = ofputil_put_NXAST_CONTROLLER(out);
1451 nac->max_len = htons(oc->max_len);
1452 nac->controller_id = htons(oc->controller_id);
1453 nac->reason = oc->reason;
1454}
1455
c2d967a5
MM
1456static void
1457ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1458 struct ofpbuf *out)
1459{
1460 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1461 ofputil_put_NXAST_DEC_TTL(out);
1462 } else {
1463 struct nx_action_cnt_ids *nac_ids =
1464 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1465 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1466 ovs_be16 *ids;
1467 size_t i;
1468
1469 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1470 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1471
1472 ids = ofpbuf_put_zeros(out, ids_len);
1473 for (i = 0; i < oc_ids->n_controllers; i++) {
1474 ids[i] = htons(oc_ids->cnt_ids[i]);
1475 }
1476 }
1477}
1478
f25d0cf3
BP
1479static void
1480ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1481 struct ofpbuf *out)
1482{
1483 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1484 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1485 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1486}
1487
29089a54
RL
1488static void
1489ofpact_sample_to_nxast(const struct ofpact_sample *os,
1490 struct ofpbuf *out)
1491{
1492 struct nx_action_sample *nas;
1493
1494 nas = ofputil_put_NXAST_SAMPLE(out);
1495 nas->probability = htons(os->probability);
1496 nas->collector_set_id = htonl(os->collector_set_id);
1497 nas->obs_domain_id = htonl(os->obs_domain_id);
1498 nas->obs_point_id = htonl(os->obs_point_id);
1499}
1500
f25d0cf3
BP
1501static void
1502ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1503{
1504 switch (a->type) {
1505 case OFPACT_CONTROLLER:
1506 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1507 break;
1508
1509 case OFPACT_OUTPUT_REG:
1510 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1511 break;
1512
1513 case OFPACT_BUNDLE:
1514 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1515 break;
1516
1517 case OFPACT_REG_MOVE:
1518 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1519 break;
1520
1521 case OFPACT_REG_LOAD:
1522 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1523 break;
1524
bd85dac1
AZ
1525 case OFPACT_STACK_PUSH:
1526 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1527 break;
1528
1529 case OFPACT_STACK_POP:
1530 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1531 break;
1532
f25d0cf3 1533 case OFPACT_DEC_TTL:
c2d967a5 1534 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
f25d0cf3
BP
1535 break;
1536
0f3f3c3d
SH
1537 case OFPACT_SET_MPLS_TTL:
1538 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1539 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1540 break;
1541
b676167a
SH
1542 case OFPACT_DEC_MPLS_TTL:
1543 ofputil_put_NXAST_DEC_MPLS_TTL(out);
1544 break;
1545
f25d0cf3
BP
1546 case OFPACT_SET_TUNNEL:
1547 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1548 break;
1549
4cceacb9
JS
1550 case OFPACT_WRITE_METADATA:
1551 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1552 break;
1553
f25d0cf3
BP
1554 case OFPACT_SET_QUEUE:
1555 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1556 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1557 break;
1558
1559 case OFPACT_POP_QUEUE:
1560 ofputil_put_NXAST_POP_QUEUE(out);
1561 break;
1562
1563 case OFPACT_FIN_TIMEOUT:
1564 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1565 break;
1566
1567 case OFPACT_RESUBMIT:
1568 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1569 break;
1570
1571 case OFPACT_LEARN:
1572 learn_to_nxast(ofpact_get_LEARN(a), out);
1573 break;
1574
1575 case OFPACT_MULTIPATH:
1576 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1577 break;
1578
f25d0cf3
BP
1579 case OFPACT_NOTE:
1580 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1581 break;
1582
1583 case OFPACT_EXIT:
1584 ofputil_put_NXAST_EXIT(out);
1585 break;
1586
b02475c5
SH
1587 case OFPACT_PUSH_MPLS:
1588 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
1589 ofpact_get_PUSH_MPLS(a)->ethertype;
1590 break;
1591
1592 case OFPACT_POP_MPLS:
1593 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
1594 ofpact_get_POP_MPLS(a)->ethertype;
1595 break;
1596
29089a54
RL
1597 case OFPACT_SAMPLE:
1598 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
1599 break;
1600
f25d0cf3
BP
1601 case OFPACT_OUTPUT:
1602 case OFPACT_ENQUEUE:
1603 case OFPACT_SET_VLAN_VID:
1604 case OFPACT_SET_VLAN_PCP:
1605 case OFPACT_STRIP_VLAN:
3e34fbdd 1606 case OFPACT_PUSH_VLAN:
f25d0cf3
BP
1607 case OFPACT_SET_ETH_SRC:
1608 case OFPACT_SET_ETH_DST:
1609 case OFPACT_SET_IPV4_SRC:
1610 case OFPACT_SET_IPV4_DST:
1611 case OFPACT_SET_IPV4_DSCP:
1612 case OFPACT_SET_L4_SRC_PORT:
1613 case OFPACT_SET_L4_DST_PORT:
b19e8793 1614 case OFPACT_CLEAR_ACTIONS:
8dd54666 1615 case OFPACT_GOTO_TABLE:
638a19b0 1616 case OFPACT_METER:
f25d0cf3
BP
1617 NOT_REACHED();
1618 }
1619}
1620\f
1621/* Converting ofpacts to OpenFlow 1.0. */
1622
1623static void
1624ofpact_output_to_openflow10(const struct ofpact_output *output,
1625 struct ofpbuf *out)
1626{
d01c980f 1627 struct ofp10_action_output *oao;
f25d0cf3
BP
1628
1629 oao = ofputil_put_OFPAT10_OUTPUT(out);
4e022ec0 1630 oao->port = htons(ofp_to_u16(output->port));
f25d0cf3
BP
1631 oao->max_len = htons(output->max_len);
1632}
1633
1634static void
1635ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1636 struct ofpbuf *out)
1637{
31a9e63f 1638 struct ofp10_action_enqueue *oae;
f25d0cf3
BP
1639
1640 oae = ofputil_put_OFPAT10_ENQUEUE(out);
4e022ec0 1641 oae->port = htons(ofp_to_u16(enqueue->port));
f25d0cf3
BP
1642 oae->queue_id = htonl(enqueue->queue);
1643}
1644
1645static void
1646ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1647{
1648 switch (a->type) {
1649 case OFPACT_OUTPUT:
1650 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1651 break;
1652
1653 case OFPACT_ENQUEUE:
1654 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1655 break;
1656
1657 case OFPACT_SET_VLAN_VID:
1658 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1659 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1660 break;
1661
1662 case OFPACT_SET_VLAN_PCP:
1663 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1664 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1665 break;
1666
1667 case OFPACT_STRIP_VLAN:
1668 ofputil_put_OFPAT10_STRIP_VLAN(out);
1669 break;
1670
1671 case OFPACT_SET_ETH_SRC:
1672 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1673 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1674 break;
1675
1676 case OFPACT_SET_ETH_DST:
1677 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1678 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1679 break;
1680
1681 case OFPACT_SET_IPV4_SRC:
1682 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1683 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1684 break;
1685
1686 case OFPACT_SET_IPV4_DST:
1687 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1688 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1689 break;
1690
1691 case OFPACT_SET_IPV4_DSCP:
1692 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1693 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1694 break;
1695
1696 case OFPACT_SET_L4_SRC_PORT:
1697 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1698 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1699 break;
1700
1701 case OFPACT_SET_L4_DST_PORT:
1702 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1703 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1704 break;
1705
3e34fbdd 1706 case OFPACT_PUSH_VLAN:
b19e8793 1707 case OFPACT_CLEAR_ACTIONS:
8dd54666 1708 case OFPACT_GOTO_TABLE:
638a19b0 1709 case OFPACT_METER:
5dca28b5 1710 /* XXX */
8dd54666
IY
1711 break;
1712
f25d0cf3
BP
1713 case OFPACT_CONTROLLER:
1714 case OFPACT_OUTPUT_REG:
1715 case OFPACT_BUNDLE:
1716 case OFPACT_REG_MOVE:
1717 case OFPACT_REG_LOAD:
bd85dac1
AZ
1718 case OFPACT_STACK_PUSH:
1719 case OFPACT_STACK_POP:
f25d0cf3 1720 case OFPACT_DEC_TTL:
0f3f3c3d 1721 case OFPACT_SET_MPLS_TTL:
b676167a 1722 case OFPACT_DEC_MPLS_TTL:
f25d0cf3 1723 case OFPACT_SET_TUNNEL:
4cceacb9 1724 case OFPACT_WRITE_METADATA:
f25d0cf3
BP
1725 case OFPACT_SET_QUEUE:
1726 case OFPACT_POP_QUEUE:
1727 case OFPACT_FIN_TIMEOUT:
1728 case OFPACT_RESUBMIT:
1729 case OFPACT_LEARN:
1730 case OFPACT_MULTIPATH:
f25d0cf3
BP
1731 case OFPACT_NOTE:
1732 case OFPACT_EXIT:
b02475c5
SH
1733 case OFPACT_PUSH_MPLS:
1734 case OFPACT_POP_MPLS:
29089a54 1735 case OFPACT_SAMPLE:
f25d0cf3
BP
1736 ofpact_to_nxast(a, out);
1737 break;
1738 }
1739}
1740
d01c980f 1741/* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
f25d0cf3
BP
1742 * actions in 'openflow', appending the actions to any existing data in
1743 * 'openflow'. */
1744void
d01c980f
BP
1745ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1746 struct ofpbuf *openflow)
f25d0cf3
BP
1747{
1748 const struct ofpact *a;
1749
1750 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1751 ofpact_to_openflow10(a, openflow);
1752 }
1753}
1754\f
d01c980f
BP
1755/* Converting ofpacts to OpenFlow 1.1. */
1756
1757static void
1758ofpact_output_to_openflow11(const struct ofpact_output *output,
1759 struct ofpbuf *out)
1760{
1761 struct ofp11_action_output *oao;
1762
1763 oao = ofputil_put_OFPAT11_OUTPUT(out);
1764 oao->port = ofputil_port_to_ofp11(output->port);
1765 oao->max_len = htons(output->max_len);
1766}
1767
99086062
BP
1768static void
1769ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
1770 struct ofpbuf *out)
1771{
1772 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
1773 && (!dec_ttl->ofpact.compat ||
1774 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
1775 ofputil_put_OFPAT11_DEC_NW_TTL(out);
1776 } else {
1777 ofpact_dec_ttl_to_nxast(dec_ttl, out);
1778 }
1779}
1780
d01c980f
BP
1781static void
1782ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1783{
1784 switch (a->type) {
1785 case OFPACT_OUTPUT:
1786 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1787
1788 case OFPACT_ENQUEUE:
1789 /* XXX */
1790 break;
1791
1792 case OFPACT_SET_VLAN_VID:
1793 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1794 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1795 break;
1796
1797 case OFPACT_SET_VLAN_PCP:
1798 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1799 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1800 break;
1801
1802 case OFPACT_STRIP_VLAN:
8e61c110 1803 ofputil_put_OFPAT11_POP_VLAN(out);
d01c980f
BP
1804 break;
1805
3e34fbdd 1806 case OFPACT_PUSH_VLAN:
5dca28b5 1807 /* XXX ETH_TYPE_VLAN_8021AD case */
3e34fbdd
IY
1808 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
1809 htons(ETH_TYPE_VLAN_8021Q);
1810 break;
1811
276c4e7a
SH
1812 case OFPACT_SET_QUEUE:
1813 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
1814 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1815 break;
1816
d01c980f
BP
1817 case OFPACT_SET_ETH_SRC:
1818 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1819 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1820 break;
1821
1822 case OFPACT_SET_ETH_DST:
1823 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1824 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1825 break;
1826
1827 case OFPACT_SET_IPV4_SRC:
1828 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1829 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1830 break;
1831
1832 case OFPACT_SET_IPV4_DST:
1833 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1834 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1835 break;
1836
1837 case OFPACT_SET_IPV4_DSCP:
1838 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1839 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1840 break;
1841
1842 case OFPACT_SET_L4_SRC_PORT:
1843 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1844 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1845 break;
1846
1847 case OFPACT_SET_L4_DST_PORT:
1848 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1849 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1850 break;
1851
7bcb1506 1852 case OFPACT_DEC_TTL:
99086062 1853 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
7bcb1506
IY
1854 break;
1855
0f3f3c3d
SH
1856 case OFPACT_SET_MPLS_TTL:
1857 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
1858 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1859 break;
1860
b676167a
SH
1861 case OFPACT_DEC_MPLS_TTL:
1862 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
1863 break;
1864
4cceacb9
JS
1865 case OFPACT_WRITE_METADATA:
1866 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
1867 break;
1868
b02475c5
SH
1869 case OFPACT_PUSH_MPLS:
1870 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
1871 ofpact_get_PUSH_MPLS(a)->ethertype;
1872 break;
1873
1874 case OFPACT_POP_MPLS:
1875 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
1876 ofpact_get_POP_MPLS(a)->ethertype;
1877
1878 break;
1879
b19e8793 1880 case OFPACT_CLEAR_ACTIONS:
8dd54666 1881 case OFPACT_GOTO_TABLE:
638a19b0 1882 case OFPACT_METER:
8dd54666
IY
1883 NOT_REACHED();
1884
d01c980f
BP
1885 case OFPACT_CONTROLLER:
1886 case OFPACT_OUTPUT_REG:
1887 case OFPACT_BUNDLE:
1888 case OFPACT_REG_MOVE:
1889 case OFPACT_REG_LOAD:
bd85dac1
AZ
1890 case OFPACT_STACK_PUSH:
1891 case OFPACT_STACK_POP:
d01c980f 1892 case OFPACT_SET_TUNNEL:
d01c980f
BP
1893 case OFPACT_POP_QUEUE:
1894 case OFPACT_FIN_TIMEOUT:
1895 case OFPACT_RESUBMIT:
1896 case OFPACT_LEARN:
1897 case OFPACT_MULTIPATH:
d01c980f
BP
1898 case OFPACT_NOTE:
1899 case OFPACT_EXIT:
29089a54 1900 case OFPACT_SAMPLE:
d01c980f
BP
1901 ofpact_to_nxast(a, out);
1902 break;
1903 }
1904}
1905
1906/* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1907 * 1.1 actions in 'openflow', appending the actions to any existing data in
1908 * 'openflow'. */
a07c15bc 1909size_t
d01c980f
BP
1910ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1911 size_t ofpacts_len, struct ofpbuf *openflow)
1912{
1913 const struct ofpact *a;
a07c15bc 1914 size_t start_size = openflow->size;
d01c980f
BP
1915
1916 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1917 ofpact_to_openflow11(a, openflow);
1918 }
a07c15bc
SH
1919
1920 return openflow->size - start_size;
d01c980f
BP
1921}
1922
8dd54666
IY
1923static void
1924ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
d01c980f
BP
1925{
1926 struct ofp11_instruction_actions *oia;
d01c980f
BP
1927
1928 /* Update the instruction's length (or, if it's empty, delete it). */
1929 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1930 if (openflow->size > ofs + sizeof *oia) {
1931 oia->len = htons(openflow->size - ofs);
1932 } else {
1933 openflow->size = ofs;
1934 }
1935}
8dd54666
IY
1936
1937void
1938ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1939 size_t ofpacts_len,
1940 struct ofpbuf *openflow)
1941{
1942 const struct ofpact *a;
1943
1944 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
084c53de
BP
1945 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
1946 case OVSINST_OFPIT11_CLEAR_ACTIONS:
f606c7e9 1947 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
084c53de 1948 break;
8dd54666 1949
084c53de
BP
1950 case OVSINST_OFPIT11_GOTO_TABLE: {
1951 struct ofp11_instruction_goto_table *oigt;
8dd54666
IY
1952 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1953 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1954 memset(oigt->pad, 0, sizeof oigt->pad);
084c53de
BP
1955 break;
1956 }
1957
1958 case OVSINST_OFPIT11_WRITE_METADATA: {
4cceacb9
JS
1959 const struct ofpact_metadata *om;
1960 struct ofp11_instruction_write_metadata *oiwm;
1961
1962 om = ofpact_get_WRITE_METADATA(a);
1963 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
1964 oiwm->metadata = om->metadata;
1965 oiwm->metadata_mask = om->mask;
084c53de
BP
1966 break;
1967 }
1968
1969 case OVSINST_OFPIT13_METER: {
638a19b0
JR
1970 const struct ofpact_meter *om;
1971 struct ofp13_instruction_meter *oim;
1972
1973 om = ofpact_get_METER(a);
1974 oim = instruction_put_OFPIT13_METER(openflow);
1975 oim->meter_id = htonl(om->meter_id);
084c53de
BP
1976 break;
1977 }
1978
1979 case OVSINST_OFPIT11_APPLY_ACTIONS: {
8dd54666
IY
1980 const size_t ofs = openflow->size;
1981 const size_t ofpacts_len_left =
1982 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1983 const struct ofpact *action;
1984 const struct ofpact *processed = a;
1985
1986 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1987 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
084c53de
BP
1988 if (ovs_instruction_type_from_ofpact_type(action->type)
1989 != OVSINST_OFPIT11_APPLY_ACTIONS) {
8dd54666
IY
1990 break;
1991 }
1992 ofpact_to_openflow11(action, openflow);
1993 processed = action;
1994 }
1995 ofpacts_update_instruction_actions(openflow, ofs);
1996 a = processed;
084c53de
BP
1997 break;
1998 }
1999
2000 case OVSINST_OFPIT11_WRITE_ACTIONS:
2001 NOT_REACHED();
8dd54666
IY
2002 }
2003 }
2004}
d01c980f 2005\f
f25d0cf3
BP
2006/* Returns true if 'action' outputs to 'port', false otherwise. */
2007static bool
4e022ec0 2008ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
f25d0cf3
BP
2009{
2010 switch (ofpact->type) {
2011 case OFPACT_OUTPUT:
2012 return ofpact_get_OUTPUT(ofpact)->port == port;
2013 case OFPACT_ENQUEUE:
2014 return ofpact_get_ENQUEUE(ofpact)->port == port;
2015 case OFPACT_CONTROLLER:
2016 return port == OFPP_CONTROLLER;
2017
2018 case OFPACT_OUTPUT_REG:
2019 case OFPACT_BUNDLE:
2020 case OFPACT_SET_VLAN_VID:
2021 case OFPACT_SET_VLAN_PCP:
2022 case OFPACT_STRIP_VLAN:
3e34fbdd 2023 case OFPACT_PUSH_VLAN:
f25d0cf3
BP
2024 case OFPACT_SET_ETH_SRC:
2025 case OFPACT_SET_ETH_DST:
2026 case OFPACT_SET_IPV4_SRC:
2027 case OFPACT_SET_IPV4_DST:
2028 case OFPACT_SET_IPV4_DSCP:
2029 case OFPACT_SET_L4_SRC_PORT:
2030 case OFPACT_SET_L4_DST_PORT:
2031 case OFPACT_REG_MOVE:
2032 case OFPACT_REG_LOAD:
bd85dac1
AZ
2033 case OFPACT_STACK_PUSH:
2034 case OFPACT_STACK_POP:
f25d0cf3 2035 case OFPACT_DEC_TTL:
0f3f3c3d 2036 case OFPACT_SET_MPLS_TTL:
b676167a 2037 case OFPACT_DEC_MPLS_TTL:
f25d0cf3 2038 case OFPACT_SET_TUNNEL:
4cceacb9 2039 case OFPACT_WRITE_METADATA:
f25d0cf3
BP
2040 case OFPACT_SET_QUEUE:
2041 case OFPACT_POP_QUEUE:
2042 case OFPACT_FIN_TIMEOUT:
2043 case OFPACT_RESUBMIT:
2044 case OFPACT_LEARN:
2045 case OFPACT_MULTIPATH:
f25d0cf3
BP
2046 case OFPACT_NOTE:
2047 case OFPACT_EXIT:
b02475c5
SH
2048 case OFPACT_PUSH_MPLS:
2049 case OFPACT_POP_MPLS:
29089a54 2050 case OFPACT_SAMPLE:
b19e8793 2051 case OFPACT_CLEAR_ACTIONS:
8dd54666 2052 case OFPACT_GOTO_TABLE:
638a19b0 2053 case OFPACT_METER:
f25d0cf3
BP
2054 default:
2055 return false;
2056 }
2057}
2058
2059/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2060 * to 'port', false otherwise. */
2061bool
2062ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
4e022ec0 2063 ofp_port_t port)
f25d0cf3
BP
2064{
2065 const struct ofpact *a;
2066
2067 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2068 if (ofpact_outputs_to_port(a, port)) {
2069 return true;
2070 }
2071 }
2072
2073 return false;
2074}
2075
2076bool
2077ofpacts_equal(const struct ofpact *a, size_t a_len,
2078 const struct ofpact *b, size_t b_len)
2079{
2080 return a_len == b_len && !memcmp(a, b, a_len);
2081}
2082\f
2083/* Formatting ofpacts. */
2084
2085static void
2086print_note(const struct ofpact_note *note, struct ds *string)
2087{
2088 size_t i;
2089
2090 ds_put_cstr(string, "note:");
2091 for (i = 0; i < note->length; i++) {
2092 if (i) {
2093 ds_put_char(string, '.');
2094 }
2095 ds_put_format(string, "%02"PRIx8, note->data[i]);
2096 }
2097}
2098
c2d967a5
MM
2099static void
2100print_dec_ttl(const struct ofpact_cnt_ids *ids,
2101 struct ds *s)
2102{
2103 size_t i;
2104
2105 ds_put_cstr(s, "dec_ttl");
2106 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2107 ds_put_cstr(s, "(");
2108 for (i = 0; i < ids->n_controllers; i++) {
2109 if (i) {
2110 ds_put_cstr(s, ",");
2111 }
2112 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2113 }
2114 ds_put_cstr(s, ")");
2115 }
2116}
2117
f25d0cf3
BP
2118static void
2119print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2120 struct ds *s)
2121{
2122 ds_put_cstr(s, "fin_timeout(");
2123 if (fin_timeout->fin_idle_timeout) {
2124 ds_put_format(s, "idle_timeout=%"PRIu16",",
2125 fin_timeout->fin_idle_timeout);
2126 }
2127 if (fin_timeout->fin_hard_timeout) {
2128 ds_put_format(s, "hard_timeout=%"PRIu16",",
2129 fin_timeout->fin_hard_timeout);
2130 }
2131 ds_chomp(s, ',');
2132 ds_put_char(s, ')');
2133}
2134
2135static void
2136ofpact_format(const struct ofpact *a, struct ds *s)
2137{
2138 const struct ofpact_enqueue *enqueue;
2139 const struct ofpact_resubmit *resubmit;
f25d0cf3 2140 const struct ofpact_controller *controller;
4cceacb9 2141 const struct ofpact_metadata *metadata;
f25d0cf3 2142 const struct ofpact_tunnel *tunnel;
29089a54 2143 const struct ofpact_sample *sample;
4e022ec0 2144 ofp_port_t port;
f25d0cf3
BP
2145
2146 switch (a->type) {
2147 case OFPACT_OUTPUT:
2148 port = ofpact_get_OUTPUT(a)->port;
4e022ec0 2149 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
f25d0cf3
BP
2150 ds_put_format(s, "output:%"PRIu16, port);
2151 } else {
2152 ofputil_format_port(port, s);
2153 if (port == OFPP_CONTROLLER) {
2154 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2155 }
2156 }
2157 break;
2158
2159 case OFPACT_CONTROLLER:
2160 controller = ofpact_get_CONTROLLER(a);
2161 if (controller->reason == OFPR_ACTION &&
2162 controller->controller_id == 0) {
2163 ds_put_format(s, "CONTROLLER:%"PRIu16,
2164 ofpact_get_CONTROLLER(a)->max_len);
2165 } else {
2166 enum ofp_packet_in_reason reason = controller->reason;
2167
2168 ds_put_cstr(s, "controller(");
2169 if (reason != OFPR_ACTION) {
5014a89d
BP
2170 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2171
f25d0cf3 2172 ds_put_format(s, "reason=%s,",
5014a89d
BP
2173 ofputil_packet_in_reason_to_string(
2174 reason, reasonbuf, sizeof reasonbuf));
f25d0cf3
BP
2175 }
2176 if (controller->max_len != UINT16_MAX) {
2177 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2178 }
2179 if (controller->controller_id != 0) {
2180 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2181 }
2182 ds_chomp(s, ',');
2183 ds_put_char(s, ')');
2184 }
2185 break;
2186
2187 case OFPACT_ENQUEUE:
2188 enqueue = ofpact_get_ENQUEUE(a);
2189 ds_put_format(s, "enqueue:");
2190 ofputil_format_port(enqueue->port, s);
2191 ds_put_format(s, "q%"PRIu32, enqueue->queue);
2192 break;
2193
2194 case OFPACT_OUTPUT_REG:
2195 ds_put_cstr(s, "output:");
2196 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2197 break;
2198
2199 case OFPACT_BUNDLE:
2200 bundle_format(ofpact_get_BUNDLE(a), s);
2201 break;
2202
2203 case OFPACT_SET_VLAN_VID:
2204 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
2205 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2206 break;
2207
2208 case OFPACT_SET_VLAN_PCP:
2209 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
2210 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2211 break;
2212
2213 case OFPACT_STRIP_VLAN:
2214 ds_put_cstr(s, "strip_vlan");
2215 break;
2216
3e34fbdd 2217 case OFPACT_PUSH_VLAN:
5dca28b5 2218 /* XXX 802.1AD case*/
3e34fbdd
IY
2219 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2220 break;
2221
f25d0cf3
BP
2222 case OFPACT_SET_ETH_SRC:
2223 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2224 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2225 break;
2226
2227 case OFPACT_SET_ETH_DST:
2228 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2229 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2230 break;
2231
2232 case OFPACT_SET_IPV4_SRC:
2233 ds_put_format(s, "mod_nw_src:"IP_FMT,
ed36537e 2234 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
f25d0cf3
BP
2235 break;
2236
2237 case OFPACT_SET_IPV4_DST:
2238 ds_put_format(s, "mod_nw_dst:"IP_FMT,
ed36537e 2239 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
f25d0cf3
BP
2240 break;
2241
2242 case OFPACT_SET_IPV4_DSCP:
2243 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
2244 break;
2245
2246 case OFPACT_SET_L4_SRC_PORT:
2247 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2248 break;
2249
2250 case OFPACT_SET_L4_DST_PORT:
2251 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2252 break;
2253
2254 case OFPACT_REG_MOVE:
2255 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2256 break;
2257
2258 case OFPACT_REG_LOAD:
2259 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2260 break;
2261
bd85dac1
AZ
2262 case OFPACT_STACK_PUSH:
2263 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2264 break;
2265
2266 case OFPACT_STACK_POP:
2267 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2268 break;
2269
f25d0cf3 2270 case OFPACT_DEC_TTL:
c2d967a5 2271 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
f25d0cf3
BP
2272 break;
2273
0f3f3c3d
SH
2274 case OFPACT_SET_MPLS_TTL:
2275 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2276 ofpact_get_SET_MPLS_TTL(a)->ttl);
2277 break;
2278
b676167a
SH
2279 case OFPACT_DEC_MPLS_TTL:
2280 ds_put_cstr(s, "dec_mpls_ttl");
2281 break;
2282
f25d0cf3
BP
2283 case OFPACT_SET_TUNNEL:
2284 tunnel = ofpact_get_SET_TUNNEL(a);
2285 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2286 (tunnel->tun_id > UINT32_MAX
2287 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2288 tunnel->tun_id);
2289 break;
2290
2291 case OFPACT_SET_QUEUE:
2292 ds_put_format(s, "set_queue:%"PRIu32,
2293 ofpact_get_SET_QUEUE(a)->queue_id);
2294 break;
2295
2296 case OFPACT_POP_QUEUE:
2297 ds_put_cstr(s, "pop_queue");
2298 break;
2299
2300 case OFPACT_FIN_TIMEOUT:
2301 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2302 break;
2303
2304 case OFPACT_RESUBMIT:
2305 resubmit = ofpact_get_RESUBMIT(a);
2306 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
c6100d92
BP
2307 ds_put_cstr(s, "resubmit:");
2308 ofputil_format_port(resubmit->in_port, s);
f25d0cf3
BP
2309 } else {
2310 ds_put_format(s, "resubmit(");
2311 if (resubmit->in_port != OFPP_IN_PORT) {
2312 ofputil_format_port(resubmit->in_port, s);
2313 }
2314 ds_put_char(s, ',');
2315 if (resubmit->table_id != 255) {
2316 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2317 }
2318 ds_put_char(s, ')');
2319 }
2320 break;
2321
2322 case OFPACT_LEARN:
2323 learn_format(ofpact_get_LEARN(a), s);
2324 break;
2325
2326 case OFPACT_MULTIPATH:
2327 multipath_format(ofpact_get_MULTIPATH(a), s);
2328 break;
2329
f25d0cf3
BP
2330 case OFPACT_NOTE:
2331 print_note(ofpact_get_NOTE(a), s);
2332 break;
2333
b02475c5
SH
2334 case OFPACT_PUSH_MPLS:
2335 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2336 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2337 break;
2338
2339 case OFPACT_POP_MPLS:
2340 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2341 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2342 break;
2343
f25d0cf3
BP
2344 case OFPACT_EXIT:
2345 ds_put_cstr(s, "exit");
2346 break;
8dd54666 2347
29089a54
RL
2348 case OFPACT_SAMPLE:
2349 sample = ofpact_get_SAMPLE(a);
2350 ds_put_format(
2351 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2352 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2353 sample->probability, sample->collector_set_id,
2354 sample->obs_domain_id, sample->obs_point_id);
2355 break;
2356
b19e8793
IY
2357 case OFPACT_CLEAR_ACTIONS:
2358 ds_put_format(s, "%s",
ba0bc9b0 2359 ovs_instruction_name_from_type(
b19e8793
IY
2360 OVSINST_OFPIT11_CLEAR_ACTIONS));
2361 break;
2362
4cceacb9
JS
2363 case OFPACT_WRITE_METADATA:
2364 metadata = ofpact_get_WRITE_METADATA(a);
2365 ds_put_format(s, "%s:%#"PRIx64,
ba0bc9b0 2366 ovs_instruction_name_from_type(
4cceacb9
JS
2367 OVSINST_OFPIT11_WRITE_METADATA),
2368 ntohll(metadata->metadata));
2369 if (metadata->mask != htonll(UINT64_MAX)) {
2370 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2371 }
2372 break;
2373
8dd54666
IY
2374 case OFPACT_GOTO_TABLE:
2375 ds_put_format(s, "%s:%"PRIu8,
ba0bc9b0 2376 ovs_instruction_name_from_type(
8dd54666
IY
2377 OVSINST_OFPIT11_GOTO_TABLE),
2378 ofpact_get_GOTO_TABLE(a)->table_id);
2379 break;
638a19b0
JR
2380
2381 case OFPACT_METER:
2382 ds_put_format(s, "%s:%"PRIu32,
ba0bc9b0 2383 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
638a19b0
JR
2384 ofpact_get_METER(a)->meter_id);
2385 break;
f25d0cf3
BP
2386 }
2387}
2388
2389/* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2390 * 'ofpacts' to 'string'. */
2391void
2392ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2393 struct ds *string)
2394{
2395 ds_put_cstr(string, "actions=");
2396 if (!ofpacts_len) {
2397 ds_put_cstr(string, "drop");
2398 } else {
2399 const struct ofpact *a;
2400
2401 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2402 if (a != ofpacts) {
2403 ds_put_cstr(string, ",");
2404 }
8dd54666 2405
5dca28b5 2406 /* XXX write-actions */
f25d0cf3
BP
2407 ofpact_format(a, string);
2408 }
2409 }
2410}
2411\f
2412/* Internal use by helpers. */
2413
2414void *
2415ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2416{
2417 struct ofpact *ofpact;
2418
2419 ofpact_pad(ofpacts);
2420 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2421 ofpact_init(ofpact, type, len);
2422 return ofpact;
2423}
2424
2425void
2426ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2427{
2428 memset(ofpact, 0, len);
2429 ofpact->type = type;
2430 ofpact->compat = OFPUTIL_ACTION_INVALID;
2431 ofpact->len = len;
2432}
2433\f
2434/* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2435 * starting at 'ofpact'.
2436 *
2437 * This is the correct way to update a variable-length ofpact's length after
2438 * adding the variable-length part of the payload. (See the large comment
2439 * near the end of ofp-actions.h for more information.) */
2440void
2441ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2442{
cb22974d 2443 ovs_assert(ofpact == ofpacts->l2);
f25d0cf3
BP
2444 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2445}
2446
2447/* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2448 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2449 * client must call this itself after adding the final ofpact to an array of
2450 * them.
2451 *
2452 * (The consequences of failing to call this function are probably not dire.
2453 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2454 * not dereference it. That's undefined behavior, technically, but it will not
2455 * cause a real problem on common systems. Still, it seems better to call
2456 * it.) */
2457void
2458ofpact_pad(struct ofpbuf *ofpacts)
2459{
2460 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2461 if (rem) {
2462 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2463 }
2464}
f5c45121
SH
2465
2466void
2467ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2468 const void *src)
2469{
2470 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2471 load->dst.field = mf;
2472 load->dst.ofs = 0;
2473 load->dst.n_bits = mf->n_bits;
2474 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2475 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);
2476}