]> git.proxmox.com Git - ovs.git/blame - lib/ofp-actions.c
ofproto-dpif-xlate: Remove obsolete comment.
[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
e45e72f1
JR
38union ofp_action {
39 ovs_be16 type;
40 struct ofp_action_header header;
41 struct ofp_action_vendor_header vendor;
42 struct ofp10_action_output output10;
43 struct ofp_action_vlan_vid vlan_vid;
44 struct ofp_action_vlan_pcp vlan_pcp;
45 struct ofp_action_nw_addr nw_addr;
46 struct ofp_action_nw_tos nw_tos;
ff14eb7a 47 struct ofp11_action_nw_ecn nw_ecn;
0c20dbe4 48 struct ofp11_action_nw_ttl nw_ttl;
e45e72f1 49 struct ofp_action_tp_port tp_port;
dba70df0
BP
50 struct ofp_action_dl_addr dl_addr;
51 struct ofp10_action_enqueue enqueue;
52 struct ofp11_action_output ofp11_output;
53 struct ofp11_action_push push;
54 struct ofp11_action_pop_mpls ofp11_pop_mpls;
55 struct ofp11_action_set_queue ofp11_set_queue;
56 struct ofp11_action_mpls_ttl ofp11_mpls_ttl;
57 struct ofp11_action_group group;
58 struct ofp12_action_set_field set_field;
59 struct nx_action_header nxa_header;
60 struct nx_action_resubmit resubmit;
61 struct nx_action_set_tunnel set_tunnel;
62 struct nx_action_set_tunnel64 set_tunnel64;
63 struct nx_action_write_metadata write_metadata;
64 struct nx_action_set_queue set_queue;
65 struct nx_action_reg_move reg_move;
66 struct nx_action_reg_load reg_load;
67 struct nx_action_stack stack;
68 struct nx_action_note note;
69 struct nx_action_multipath multipath;
70 struct nx_action_bundle bundle;
71 struct nx_action_output_reg output_reg;
72 struct nx_action_cnt_ids cnt_ids;
73 struct nx_action_fin_timeout fin_timeout;
74 struct nx_action_controller controller;
75 struct nx_action_push_mpls push_mpls;
76 struct nx_action_mpls_ttl mpls_ttl;
77 struct nx_action_pop_mpls pop_mpls;
78 struct nx_action_sample sample;
79 struct nx_action_learn learn;
e45e72f1 80};
e45e72f1 81
f25d0cf3 82static enum ofperr
d01c980f 83output_from_openflow10(const struct ofp10_action_output *oao,
f25d0cf3
BP
84 struct ofpbuf *out)
85{
86 struct ofpact_output *output;
87
88 output = ofpact_put_OUTPUT(out);
4e022ec0 89 output->port = u16_to_ofp(ntohs(oao->port));
f25d0cf3
BP
90 output->max_len = ntohs(oao->max_len);
91
92 return ofputil_check_output_port(output->port, OFPP_MAX);
93}
94
95static enum ofperr
31a9e63f 96enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
f25d0cf3
BP
97 struct ofpbuf *out)
98{
99 struct ofpact_enqueue *enqueue;
100
101 enqueue = ofpact_put_ENQUEUE(out);
4e022ec0 102 enqueue->port = u16_to_ofp(ntohs(oae->port));
f25d0cf3 103 enqueue->queue = ntohl(oae->queue_id);
4e022ec0
AW
104 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
105 && enqueue->port != OFPP_IN_PORT
f25d0cf3
BP
106 && enqueue->port != OFPP_LOCAL) {
107 return OFPERR_OFPBAC_BAD_OUT_PORT;
108 }
109 return 0;
110}
111
112static void
113resubmit_from_openflow(const struct nx_action_resubmit *nar,
114 struct ofpbuf *out)
115{
116 struct ofpact_resubmit *resubmit;
117
118 resubmit = ofpact_put_RESUBMIT(out);
119 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
4e022ec0 120 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
f25d0cf3
BP
121 resubmit->table_id = 0xff;
122}
123
124static enum ofperr
125resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
126 struct ofpbuf *out)
127{
128 struct ofpact_resubmit *resubmit;
129
130 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
131 return OFPERR_OFPBAC_BAD_ARGUMENT;
132 }
133
134 resubmit = ofpact_put_RESUBMIT(out);
135 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
4e022ec0 136 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
f25d0cf3
BP
137 resubmit->table_id = nar->table;
138 return 0;
139}
140
141static enum ofperr
142output_reg_from_openflow(const struct nx_action_output_reg *naor,
143 struct ofpbuf *out)
144{
145 struct ofpact_output_reg *output_reg;
146
147 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
148 return OFPERR_OFPBAC_BAD_ARGUMENT;
149 }
150
151 output_reg = ofpact_put_OUTPUT_REG(out);
152 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
153 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
154 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
155 output_reg->max_len = ntohs(naor->max_len);
156
157 return mf_check_src(&output_reg->src, NULL);
158}
159
160static void
161fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
162 struct ofpbuf *out)
163{
164 struct ofpact_fin_timeout *oft;
165
166 oft = ofpact_put_FIN_TIMEOUT(out);
167 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
168 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
169}
170
171static void
172controller_from_openflow(const struct nx_action_controller *nac,
173 struct ofpbuf *out)
174{
175 struct ofpact_controller *oc;
176
177 oc = ofpact_put_CONTROLLER(out);
178 oc->max_len = ntohs(nac->max_len);
179 oc->controller_id = ntohs(nac->controller_id);
180 oc->reason = nac->reason;
181}
182
4cceacb9
JS
183static enum ofperr
184metadata_from_nxast(const struct nx_action_write_metadata *nawm,
185 struct ofpbuf *out)
186{
187 struct ofpact_metadata *om;
188
189 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
190 return OFPERR_NXBRC_MUST_BE_ZERO;
191 }
192
193 om = ofpact_put_WRITE_METADATA(out);
194 om->metadata = nawm->metadata;
195 om->mask = nawm->mask;
196
197 return 0;
198}
199
f25d0cf3
BP
200static void
201note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
202{
203 struct ofpact_note *note;
204 unsigned int length;
205
206 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
207 note = ofpact_put(out, OFPACT_NOTE,
208 offsetof(struct ofpact_note, data) + length);
209 note->length = length;
210 memcpy(note->data, nan->note, length);
211}
212
c2d967a5 213static enum ofperr
7bcb1506 214dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
c2d967a5
MM
215{
216 uint16_t id = 0;
217 struct ofpact_cnt_ids *ids;
218 enum ofperr error = 0;
219
220 ids = ofpact_put_DEC_TTL(out);
7bcb1506 221 ids->ofpact.compat = compat;
c2d967a5
MM
222 ids->n_controllers = 1;
223 ofpbuf_put(out, &id, sizeof id);
224 ids = out->l2;
225 ofpact_update_len(out, &ids->ofpact);
226 return error;
227}
228
229static enum ofperr
230dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
231 struct ofpbuf *out)
232{
233 struct ofpact_cnt_ids *ids;
234 size_t ids_size;
235 int i;
236
237 ids = ofpact_put_DEC_TTL(out);
238 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
239 ids->n_controllers = ntohs(nac_ids->n_controllers);
240 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
241
242 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
243 return OFPERR_NXBRC_MUST_BE_ZERO;
244 }
245
246 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
247 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
248 "allocated for controller ids. %zu bytes are required for "
249 "%"PRIu16" controllers.", ids_size,
250 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
251 return OFPERR_OFPBAC_BAD_LEN;
252 }
253
254 for (i = 0; i < ids->n_controllers; i++) {
255 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
256 ofpbuf_put(out, &id, sizeof id);
e655542c 257 ids = out->l2;
c2d967a5
MM
258 }
259
c2d967a5
MM
260 ofpact_update_len(out, &ids->ofpact);
261
262 return 0;
263}
264
29089a54
RL
265static enum ofperr
266sample_from_openflow(const struct nx_action_sample *nas,
267 struct ofpbuf *out)
268{
269 struct ofpact_sample *sample;
270
271 sample = ofpact_put_SAMPLE(out);
272 sample->probability = ntohs(nas->probability);
273 sample->collector_set_id = ntohl(nas->collector_set_id);
274 sample->obs_domain_id = ntohl(nas->obs_domain_id);
275 sample->obs_point_id = ntohl(nas->obs_point_id);
276
277 if (sample->probability == 0) {
278 return OFPERR_OFPBAC_BAD_ARGUMENT;
279 }
280
281 return 0;
282}
283
a7a2d006
JS
284static enum ofperr
285push_mpls_from_openflow(ovs_be16 ethertype, enum ofpact_mpls_position position,
286 struct ofpbuf *out)
287{
288 struct ofpact_push_mpls *oam;
289
290 if (!eth_type_mpls(ethertype)) {
291 return OFPERR_OFPBAC_BAD_ARGUMENT;
292 }
293 oam = ofpact_put_PUSH_MPLS(out);
294 oam->ethertype = ethertype;
295 oam->position = position;
296
297 return 0;
298}
299
f25d0cf3
BP
300static enum ofperr
301decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
302{
dba70df0 303 const struct nx_action_header *nah = &a->nxa_header;
f25d0cf3
BP
304 uint16_t len = ntohs(a->header.len);
305
306 if (len < sizeof(struct nx_action_header)) {
307 return OFPERR_OFPBAC_BAD_LEN;
308 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
309 return OFPERR_OFPBAC_BAD_VENDOR;
310 }
311
312 switch (nah->subtype) {
313#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
314 case CONSTANT_HTONS(ENUM): \
315 if (EXTENSIBLE \
316 ? len >= sizeof(struct STRUCT) \
317 : len == sizeof(struct STRUCT)) { \
318 *code = OFPUTIL_##ENUM; \
319 return 0; \
320 } else { \
321 return OFPERR_OFPBAC_BAD_LEN; \
322 } \
323 NOT_REACHED();
324#include "ofp-util.def"
325
326 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
327 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
328 default:
329 return OFPERR_OFPBAC_BAD_TYPE;
330 }
331}
332
333/* Parses 'a' to determine its type. On success stores the correct type into
334 * '*code' and returns 0. On failure returns an OFPERR_* error code and
335 * '*code' is indeterminate.
336 *
337 * The caller must have already verified that 'a''s length is potentially
dba70df0
BP
338 * correct (that is, a->header.len is nonzero and a multiple of
339 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
f25d0cf3
BP
340 *
341 * This function verifies that 'a''s length is correct for the type of action
342 * that it represents. */
343static enum ofperr
344decode_openflow10_action(const union ofp_action *a,
345 enum ofputil_action_code *code)
346{
347 switch (a->type) {
348 case CONSTANT_HTONS(OFPAT10_VENDOR):
349 return decode_nxast_action(a, code);
350
351#define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
352 case CONSTANT_HTONS(ENUM): \
353 if (a->header.len == htons(sizeof(struct STRUCT))) { \
354 *code = OFPUTIL_##ENUM; \
355 return 0; \
356 } else { \
357 return OFPERR_OFPBAC_BAD_LEN; \
358 } \
359 break;
360#include "ofp-util.def"
361
362 default:
363 return OFPERR_OFPBAC_BAD_TYPE;
364 }
365}
366
367static enum ofperr
d01c980f
BP
368ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
369 struct ofpbuf *out)
f25d0cf3 370{
f25d0cf3 371 struct ofpact_tunnel *tunnel;
d01c980f 372 enum ofperr error = 0;
f25d0cf3
BP
373
374 switch (code) {
375 case OFPUTIL_ACTION_INVALID:
d01c980f 376#define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
3ddcaf2d 377#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
d01c980f 378#include "ofp-util.def"
f25d0cf3
BP
379 NOT_REACHED();
380
f25d0cf3 381 case OFPUTIL_NXAST_RESUBMIT:
dba70df0 382 resubmit_from_openflow(&a->resubmit, out);
f25d0cf3
BP
383 break;
384
385 case OFPUTIL_NXAST_SET_TUNNEL:
f25d0cf3
BP
386 tunnel = ofpact_put_SET_TUNNEL(out);
387 tunnel->ofpact.compat = code;
dba70df0 388 tunnel->tun_id = ntohl(a->set_tunnel.tun_id);
f25d0cf3
BP
389 break;
390
4cceacb9 391 case OFPUTIL_NXAST_WRITE_METADATA:
dba70df0 392 error = metadata_from_nxast(&a->write_metadata, out);
4cceacb9
JS
393 break;
394
f25d0cf3 395 case OFPUTIL_NXAST_SET_QUEUE:
dba70df0 396 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(a->set_queue.queue_id);
f25d0cf3
BP
397 break;
398
399 case OFPUTIL_NXAST_POP_QUEUE:
400 ofpact_put_POP_QUEUE(out);
401 break;
402
403 case OFPUTIL_NXAST_REG_MOVE:
dba70df0 404 error = nxm_reg_move_from_openflow(&a->reg_move, out);
f25d0cf3
BP
405 break;
406
407 case OFPUTIL_NXAST_REG_LOAD:
dba70df0 408 error = nxm_reg_load_from_openflow(&a->reg_load, out);
f25d0cf3
BP
409 break;
410
bd85dac1 411 case OFPUTIL_NXAST_STACK_PUSH:
dba70df0 412 error = nxm_stack_push_from_openflow(&a->stack, out);
bd85dac1
AZ
413 break;
414
415 case OFPUTIL_NXAST_STACK_POP:
dba70df0 416 error = nxm_stack_pop_from_openflow(&a->stack, out);
bd85dac1
AZ
417 break;
418
f25d0cf3 419 case OFPUTIL_NXAST_NOTE:
dba70df0 420 note_from_openflow(&a->note, out);
f25d0cf3
BP
421 break;
422
423 case OFPUTIL_NXAST_SET_TUNNEL64:
f25d0cf3
BP
424 tunnel = ofpact_put_SET_TUNNEL(out);
425 tunnel->ofpact.compat = code;
dba70df0 426 tunnel->tun_id = ntohll(a->set_tunnel64.tun_id);
f25d0cf3
BP
427 break;
428
429 case OFPUTIL_NXAST_MULTIPATH:
dba70df0 430 error = multipath_from_openflow(&a->multipath,
f25d0cf3
BP
431 ofpact_put_MULTIPATH(out));
432 break;
433
f25d0cf3
BP
434 case OFPUTIL_NXAST_BUNDLE:
435 case OFPUTIL_NXAST_BUNDLE_LOAD:
dba70df0 436 error = bundle_from_openflow(&a->bundle, out);
f25d0cf3
BP
437 break;
438
439 case OFPUTIL_NXAST_OUTPUT_REG:
dba70df0 440 error = output_reg_from_openflow(&a->output_reg, out);
f25d0cf3
BP
441 break;
442
443 case OFPUTIL_NXAST_RESUBMIT_TABLE:
dba70df0 444 error = resubmit_table_from_openflow(&a->resubmit, out);
f25d0cf3
BP
445 break;
446
447 case OFPUTIL_NXAST_LEARN:
dba70df0 448 error = learn_from_openflow(&a->learn, out);
f25d0cf3
BP
449 break;
450
451 case OFPUTIL_NXAST_EXIT:
452 ofpact_put_EXIT(out);
453 break;
454
455 case OFPUTIL_NXAST_DEC_TTL:
7bcb1506 456 error = dec_ttl_from_openflow(out, code);
c2d967a5
MM
457 break;
458
459 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
dba70df0 460 error = dec_ttl_cnt_ids_from_openflow(&a->cnt_ids, out);
f25d0cf3
BP
461 break;
462
463 case OFPUTIL_NXAST_FIN_TIMEOUT:
dba70df0 464 fin_timeout_from_openflow(&a->fin_timeout, out);
f25d0cf3
BP
465 break;
466
467 case OFPUTIL_NXAST_CONTROLLER:
dba70df0 468 controller_from_openflow(&a->controller, out);
f25d0cf3 469 break;
b02475c5 470
dba70df0
BP
471 case OFPUTIL_NXAST_PUSH_MPLS:
472 error = push_mpls_from_openflow(a->push_mpls.ethertype,
a7a2d006 473 OFPACT_MPLS_AFTER_VLAN, out);
b02475c5 474 break;
b02475c5 475
dba70df0
BP
476 case OFPUTIL_NXAST_SET_MPLS_TTL:
477 ofpact_put_SET_MPLS_TTL(out)->ttl = a->mpls_ttl.ttl;
0f3f3c3d 478 break;
0f3f3c3d 479
b676167a
SH
480 case OFPUTIL_NXAST_DEC_MPLS_TTL:
481 ofpact_put_DEC_MPLS_TTL(out);
482 break;
483
dba70df0
BP
484 case OFPUTIL_NXAST_POP_MPLS:
485 if (eth_type_mpls(a->pop_mpls.ethertype)) {
b02475c5
SH
486 return OFPERR_OFPBAC_BAD_ARGUMENT;
487 }
dba70df0 488 ofpact_put_POP_MPLS(out)->ethertype = a->pop_mpls.ethertype;
b02475c5 489 break;
29089a54
RL
490
491 case OFPUTIL_NXAST_SAMPLE:
dba70df0 492 error = sample_from_openflow(&a->sample, out);
29089a54 493 break;
f25d0cf3
BP
494 }
495
496 return error;
497}
498
d01c980f
BP
499static enum ofperr
500ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
501{
502 enum ofputil_action_code code;
503 enum ofperr error;
ca287d20
JR
504 struct ofpact_vlan_vid *vlan_vid;
505 struct ofpact_vlan_pcp *vlan_pcp;
d01c980f
BP
506
507 error = decode_openflow10_action(a, &code);
508 if (error) {
509 return error;
510 }
511
512 switch (code) {
513 case OFPUTIL_ACTION_INVALID:
3ddcaf2d 514#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
d01c980f
BP
515#include "ofp-util.def"
516 NOT_REACHED();
517
518 case OFPUTIL_OFPAT10_OUTPUT:
519 return output_from_openflow10(&a->output10, out);
520
521 case OFPUTIL_OFPAT10_SET_VLAN_VID:
522 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
523 return OFPERR_OFPBAC_BAD_ARGUMENT;
524 }
ca287d20
JR
525 vlan_vid = ofpact_put_SET_VLAN_VID(out);
526 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
527 vlan_vid->push_vlan_if_needed = true;
528 vlan_vid->ofpact.compat = code;
d01c980f
BP
529 break;
530
531 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
532 if (a->vlan_pcp.vlan_pcp & ~7) {
533 return OFPERR_OFPBAC_BAD_ARGUMENT;
534 }
ca287d20
JR
535 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
536 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
537 vlan_pcp->push_vlan_if_needed = true;
538 vlan_pcp->ofpact.compat = code;
d01c980f
BP
539 break;
540
541 case OFPUTIL_OFPAT10_STRIP_VLAN:
542 ofpact_put_STRIP_VLAN(out);
543 break;
544
545 case OFPUTIL_OFPAT10_SET_DL_SRC:
dba70df0
BP
546 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
547 ETH_ADDR_LEN);
d01c980f
BP
548 break;
549
550 case OFPUTIL_OFPAT10_SET_DL_DST:
dba70df0
BP
551 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
552 ETH_ADDR_LEN);
d01c980f
BP
553 break;
554
555 case OFPUTIL_OFPAT10_SET_NW_SRC:
556 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
557 break;
558
559 case OFPUTIL_OFPAT10_SET_NW_DST:
560 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
561 break;
562
563 case OFPUTIL_OFPAT10_SET_NW_TOS:
564 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
565 return OFPERR_OFPBAC_BAD_ARGUMENT;
566 }
04f01c24 567 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
d01c980f
BP
568 break;
569
570 case OFPUTIL_OFPAT10_SET_TP_SRC:
571 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
572 break;
573
574 case OFPUTIL_OFPAT10_SET_TP_DST:
575 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
576
577 break;
578
579 case OFPUTIL_OFPAT10_ENQUEUE:
dba70df0 580 error = enqueue_from_openflow10(&a->enqueue, out);
d01c980f
BP
581 break;
582
583#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
584#include "ofp-util.def"
585 return ofpact_from_nxast(a, code, out);
586 }
587
588 return error;
589}
590
f25d0cf3
BP
591static inline union ofp_action *
592action_next(const union ofp_action *a)
593{
594 return ((union ofp_action *) (void *)
595 ((uint8_t *) a + ntohs(a->header.len)));
596}
597
598static inline bool
dba70df0 599action_is_valid(const union ofp_action *a, size_t max_actions)
f25d0cf3
BP
600{
601 uint16_t len = ntohs(a->header.len);
602 return (!(len % OFP_ACTION_ALIGN)
dba70df0
BP
603 && len >= OFP_ACTION_ALIGN
604 && len / OFP_ACTION_ALIGN <= max_actions);
f25d0cf3
BP
605}
606
607/* This macro is careful to check for actions with bad lengths. */
dba70df0
BP
608#define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, MAX_ACTIONS) \
609 for ((ITER) = (ACTIONS), (LEFT) = (MAX_ACTIONS); \
f25d0cf3 610 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
dba70df0 611 ((LEFT) -= ntohs((ITER)->header.len) / OFP_ACTION_ALIGN, \
f25d0cf3
BP
612 (ITER) = action_next(ITER)))
613
699dddf1 614static void
dba70df0
BP
615log_bad_action(const union ofp_action *actions, size_t max_actions,
616 const union ofp_action *bad_action, enum ofperr error)
699dddf1
BP
617{
618 if (!VLOG_DROP_WARN(&rl)) {
619 struct ds s;
620
621 ds_init(&s);
dba70df0
BP
622 ds_put_hex_dump(&s, actions, max_actions * OFP_ACTION_ALIGN, 0, false);
623 VLOG_WARN("bad action at offset %#tx (%s):\n%s",
624 (char *)bad_action - (char *)actions,
625 ofperr_get_name(error), ds_cstr(&s));
699dddf1
BP
626 ds_destroy(&s);
627 }
628}
629
f25d0cf3 630static enum ofperr
0300caaf
IY
631ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
632 struct ofpbuf *out,
633 enum ofperr (*ofpact_from_openflow)(
634 const union ofp_action *a, struct ofpbuf *out))
f25d0cf3
BP
635{
636 const union ofp_action *a;
637 size_t left;
638
639 ACTION_FOR_EACH (a, left, in, n_in) {
0300caaf 640 enum ofperr error = ofpact_from_openflow(a, out);
f25d0cf3 641 if (error) {
dba70df0 642 log_bad_action(in, n_in, a, error);
f25d0cf3
BP
643 return error;
644 }
645 }
646 if (left) {
699dddf1 647 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
dba70df0 648 log_bad_action(in, n_in, a, error);
699dddf1 649 return error;
f25d0cf3
BP
650 }
651
652 ofpact_pad(out);
653 return 0;
654}
655
0300caaf
IY
656static enum ofperr
657ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
658 struct ofpbuf *out)
659{
660 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
661}
662
d01c980f
BP
663static enum ofperr
664ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
665 struct ofpbuf *ofpacts,
666 enum ofperr (*translate)(const union ofp_action *actions,
dba70df0 667 size_t max_actions,
d01c980f 668 struct ofpbuf *ofpacts))
f25d0cf3
BP
669{
670 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
671 const union ofp_action *actions;
672 enum ofperr error;
673
674 ofpbuf_clear(ofpacts);
675
676 if (actions_len % OFP_ACTION_ALIGN != 0) {
677 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
678 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
679 return OFPERR_OFPBRC_BAD_LEN;
680 }
681
682 actions = ofpbuf_try_pull(openflow, actions_len);
683 if (actions == NULL) {
684 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
685 "remaining message length (%zu)",
686 actions_len, openflow->size);
687 return OFPERR_OFPBRC_BAD_LEN;
688 }
689
d01c980f 690 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
4cceacb9
JS
691 if (error) {
692 ofpbuf_clear(ofpacts);
693 return error;
694 }
695
696 error = ofpacts_verify(ofpacts->data, ofpacts->size);
f25d0cf3
BP
697 if (error) {
698 ofpbuf_clear(ofpacts);
699 }
d01c980f
BP
700 return error;
701}
702
703/* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
704 * front of 'openflow' into ofpacts. On success, replaces any existing content
705 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
706 * Returns 0 if successful, otherwise an OpenFlow error.
707 *
13f894b1
BP
708 * The parsed actions are valid generically, but they may not be valid in a
709 * specific context. For example, port numbers up to OFPP_MAX are valid
710 * generically, but specific datapaths may only support port numbers in a
711 * smaller range. Use ofpacts_check() to additional check whether actions are
712 * valid in a specific context. */
d01c980f
BP
713enum ofperr
714ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
715 struct ofpbuf *ofpacts)
716{
717 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
718 ofpacts_from_openflow10);
719}
720\f
721/* OpenFlow 1.1 actions. */
722
723/* Parses 'a' to determine its type. On success stores the correct type into
724 * '*code' and returns 0. On failure returns an OFPERR_* error code and
725 * '*code' is indeterminate.
726 *
727 * The caller must have already verified that 'a''s length is potentially
dba70df0
BP
728 * correct (that is, a->header.len is nonzero and a multiple of
729 * OFP_ACTION_ALIGN and no longer than the amount of space allocated to 'a').
d01c980f
BP
730 *
731 * This function verifies that 'a''s length is correct for the type of action
732 * that it represents. */
733static enum ofperr
734decode_openflow11_action(const union ofp_action *a,
735 enum ofputil_action_code *code)
736{
3ddcaf2d
BP
737 uint16_t len;
738
d01c980f
BP
739 switch (a->type) {
740 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
741 return decode_nxast_action(a, code);
742
3ddcaf2d
BP
743#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
744 case CONSTANT_HTONS(ENUM): \
745 len = ntohs(a->header.len); \
746 if (EXTENSIBLE \
747 ? len >= sizeof(struct STRUCT) \
748 : len == sizeof(struct STRUCT)) { \
749 *code = OFPUTIL_##ENUM; \
750 return 0; \
751 } else { \
752 return OFPERR_OFPBAC_BAD_LEN; \
753 } \
754 NOT_REACHED();
d01c980f
BP
755#include "ofp-util.def"
756
757 default:
758 return OFPERR_OFPBAC_BAD_TYPE;
759 }
760}
761
762static enum ofperr
763output_from_openflow11(const struct ofp11_action_output *oao,
764 struct ofpbuf *out)
765{
766 struct ofpact_output *output;
767 enum ofperr error;
768
769 output = ofpact_put_OUTPUT(out);
770 output->max_len = ntohs(oao->max_len);
771
772 error = ofputil_port_from_ofp11(oao->port, &output->port);
773 if (error) {
774 return error;
775 }
776
777 return ofputil_check_output_port(output->port, OFPP_MAX);
778}
779
780static enum ofperr
781ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
782{
783 enum ofputil_action_code code;
784 enum ofperr error;
ca287d20
JR
785 struct ofpact_vlan_vid *vlan_vid;
786 struct ofpact_vlan_pcp *vlan_pcp;
d01c980f
BP
787
788 error = decode_openflow11_action(a, &code);
789 if (error) {
790 return error;
791 }
792
793 switch (code) {
794 case OFPUTIL_ACTION_INVALID:
795#define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
796#include "ofp-util.def"
797 NOT_REACHED();
798
799 case OFPUTIL_OFPAT11_OUTPUT:
dba70df0 800 return output_from_openflow11(&a->ofp11_output, out);
d01c980f
BP
801
802 case OFPUTIL_OFPAT11_SET_VLAN_VID:
803 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
804 return OFPERR_OFPBAC_BAD_ARGUMENT;
805 }
ca287d20
JR
806 vlan_vid = ofpact_put_SET_VLAN_VID(out);
807 vlan_vid->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
808 vlan_vid->push_vlan_if_needed = false;
809 vlan_vid->ofpact.compat = code;
d01c980f
BP
810 break;
811
812 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
813 if (a->vlan_pcp.vlan_pcp & ~7) {
814 return OFPERR_OFPBAC_BAD_ARGUMENT;
815 }
ca287d20
JR
816 vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
817 vlan_pcp->vlan_pcp = a->vlan_pcp.vlan_pcp;
818 vlan_pcp->push_vlan_if_needed = false;
819 vlan_pcp->ofpact.compat = code;
d01c980f
BP
820 break;
821
3e34fbdd 822 case OFPUTIL_OFPAT11_PUSH_VLAN:
dba70df0 823 if (a->push.ethertype != htons(ETH_TYPE_VLAN_8021Q)) {
5dca28b5 824 /* XXX 802.1AD(QinQ) isn't supported at the moment */
15549878 825 return OFPERR_OFPBAC_BAD_ARGUMENT;
3e34fbdd
IY
826 }
827 ofpact_put_PUSH_VLAN(out);
828 break;
829
8e61c110
IY
830 case OFPUTIL_OFPAT11_POP_VLAN:
831 ofpact_put_STRIP_VLAN(out);
832 break;
833
276c4e7a
SH
834 case OFPUTIL_OFPAT11_SET_QUEUE:
835 ofpact_put_SET_QUEUE(out)->queue_id =
dba70df0 836 ntohl(a->ofp11_set_queue.queue_id);
276c4e7a
SH
837 break;
838
d01c980f 839 case OFPUTIL_OFPAT11_SET_DL_SRC:
dba70df0
BP
840 memcpy(ofpact_put_SET_ETH_SRC(out)->mac, a->dl_addr.dl_addr,
841 ETH_ADDR_LEN);
d01c980f
BP
842 break;
843
844 case OFPUTIL_OFPAT11_SET_DL_DST:
dba70df0
BP
845 memcpy(ofpact_put_SET_ETH_DST(out)->mac, a->dl_addr.dl_addr,
846 ETH_ADDR_LEN);
d01c980f
BP
847 break;
848
7bcb1506
IY
849 case OFPUTIL_OFPAT11_DEC_NW_TTL:
850 dec_ttl_from_openflow(out, code);
851 break;
852
d01c980f
BP
853 case OFPUTIL_OFPAT11_SET_NW_SRC:
854 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
855 break;
856
857 case OFPUTIL_OFPAT11_SET_NW_DST:
858 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
859 break;
860
861 case OFPUTIL_OFPAT11_SET_NW_TOS:
862 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
863 return OFPERR_OFPBAC_BAD_ARGUMENT;
864 }
04f01c24 865 ofpact_put_SET_IP_DSCP(out)->dscp = a->nw_tos.nw_tos;
d01c980f
BP
866 break;
867
ff14eb7a
JR
868 case OFPUTIL_OFPAT11_SET_NW_ECN:
869 if (a->nw_ecn.nw_ecn & ~IP_ECN_MASK) {
870 return OFPERR_OFPBAC_BAD_ARGUMENT;
871 }
872 ofpact_put_SET_IP_ECN(out)->ecn = a->nw_ecn.nw_ecn;
873 break;
874
0c20dbe4
JR
875 case OFPUTIL_OFPAT11_SET_NW_TTL:
876 ofpact_put_SET_IP_TTL(out)->ttl = a->nw_ttl.nw_ttl;
877 break;
878
d01c980f
BP
879 case OFPUTIL_OFPAT11_SET_TP_SRC:
880 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
881 break;
882
883 case OFPUTIL_OFPAT11_SET_TP_DST:
884 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
885 break;
886
e9536ecb 887 case OFPUTIL_OFPAT12_SET_FIELD:
dba70df0 888 return nxm_reg_load_from_openflow12_set_field(&a->set_field, out);
e9536ecb 889
dba70df0
BP
890 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
891 ofpact_put_SET_MPLS_TTL(out)->ttl = a->ofp11_mpls_ttl.mpls_ttl;
0f3f3c3d 892 break;
0f3f3c3d 893
b676167a
SH
894 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
895 ofpact_put_DEC_MPLS_TTL(out);
896 break;
897
dba70df0
BP
898 case OFPUTIL_OFPAT11_PUSH_MPLS:
899 error = push_mpls_from_openflow(a->push.ethertype,
a7a2d006 900 OFPACT_MPLS_AFTER_VLAN, out);
b02475c5 901 break;
b02475c5 902
dba70df0
BP
903 case OFPUTIL_OFPAT11_POP_MPLS:
904 if (eth_type_mpls(a->ofp11_pop_mpls.ethertype)) {
b02475c5
SH
905 return OFPERR_OFPBAC_BAD_ARGUMENT;
906 }
dba70df0 907 ofpact_put_POP_MPLS(out)->ethertype = a->ofp11_pop_mpls.ethertype;
b02475c5 908 break;
b02475c5 909
dba70df0
BP
910 case OFPUTIL_OFPAT11_GROUP:
911 ofpact_put_GROUP(out)->group_id = ntohl(a->group.group_id);
7395c052 912 break;
7395c052 913
d01c980f
BP
914#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
915#include "ofp-util.def"
916 return ofpact_from_nxast(a, code, out);
917 }
918
919 return error;
920}
921
922static enum ofperr
923ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
924 struct ofpbuf *out)
925{
0300caaf 926 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
f25d0cf3 927}
7fdb60a7
SH
928
929/* True if an action sets the value of a field
930 * in a way that is compatibile with the action set.
931 * False otherwise. */
932static bool
933ofpact_is_set_action(const struct ofpact *a)
934{
935 switch (a->type) {
936 case OFPACT_REG_LOAD:
937 case OFPACT_SET_ETH_DST:
938 case OFPACT_SET_ETH_SRC:
04f01c24 939 case OFPACT_SET_IP_DSCP:
ff14eb7a 940 case OFPACT_SET_IP_ECN:
0c20dbe4 941 case OFPACT_SET_IP_TTL:
7fdb60a7
SH
942 case OFPACT_SET_IPV4_DST:
943 case OFPACT_SET_IPV4_SRC:
944 case OFPACT_SET_L4_DST_PORT:
945 case OFPACT_SET_L4_SRC_PORT:
946 case OFPACT_SET_MPLS_TTL:
947 case OFPACT_SET_QUEUE:
948 case OFPACT_SET_TUNNEL:
949 case OFPACT_SET_VLAN_PCP:
950 case OFPACT_SET_VLAN_VID:
951 return true;
952 case OFPACT_BUNDLE:
953 case OFPACT_CLEAR_ACTIONS:
954 case OFPACT_CONTROLLER:
955 case OFPACT_DEC_MPLS_TTL:
956 case OFPACT_DEC_TTL:
957 case OFPACT_ENQUEUE:
958 case OFPACT_EXIT:
959 case OFPACT_FIN_TIMEOUT:
960 case OFPACT_GOTO_TABLE:
961 case OFPACT_GROUP:
962 case OFPACT_LEARN:
963 case OFPACT_METER:
964 case OFPACT_MULTIPATH:
965 case OFPACT_NOTE:
966 case OFPACT_OUTPUT:
967 case OFPACT_OUTPUT_REG:
968 case OFPACT_POP_MPLS:
969 case OFPACT_POP_QUEUE:
970 case OFPACT_PUSH_MPLS:
971 case OFPACT_PUSH_VLAN:
972 case OFPACT_REG_MOVE:
973 case OFPACT_RESUBMIT:
974 case OFPACT_SAMPLE:
975 case OFPACT_STACK_POP:
976 case OFPACT_STACK_PUSH:
977 case OFPACT_STRIP_VLAN:
978 case OFPACT_WRITE_ACTIONS:
979 case OFPACT_WRITE_METADATA:
980 return false;
981 default:
982 NOT_REACHED();
983 }
984}
985
986/* True if an action is allowed in the action set.
987 * False otherwise. */
988static bool
989ofpact_is_allowed_in_actions_set(const struct ofpact *a)
990{
991 switch (a->type) {
992 case OFPACT_DEC_MPLS_TTL:
993 case OFPACT_DEC_TTL:
994 case OFPACT_GROUP:
995 case OFPACT_OUTPUT:
996 case OFPACT_POP_MPLS:
997 case OFPACT_PUSH_MPLS:
998 case OFPACT_PUSH_VLAN:
999 case OFPACT_REG_LOAD:
1000 case OFPACT_SET_ETH_DST:
1001 case OFPACT_SET_ETH_SRC:
04f01c24 1002 case OFPACT_SET_IP_DSCP:
ff14eb7a 1003 case OFPACT_SET_IP_ECN:
0c20dbe4 1004 case OFPACT_SET_IP_TTL:
7fdb60a7
SH
1005 case OFPACT_SET_IPV4_DST:
1006 case OFPACT_SET_IPV4_SRC:
1007 case OFPACT_SET_L4_DST_PORT:
1008 case OFPACT_SET_L4_SRC_PORT:
1009 case OFPACT_SET_MPLS_TTL:
1010 case OFPACT_SET_QUEUE:
1011 case OFPACT_SET_TUNNEL:
1012 case OFPACT_SET_VLAN_PCP:
1013 case OFPACT_SET_VLAN_VID:
1014 case OFPACT_STRIP_VLAN:
1015 return true;
1016
1017 /* In general these actions are excluded because they are not part of
1018 * the OpenFlow specification nor map to actions that are defined in
1019 * the specification. Thus the order in which they should be applied
1020 * in the action set is undefined. */
1021 case OFPACT_BUNDLE:
1022 case OFPACT_CONTROLLER:
1023 case OFPACT_ENQUEUE:
1024 case OFPACT_EXIT:
1025 case OFPACT_FIN_TIMEOUT:
1026 case OFPACT_LEARN:
1027 case OFPACT_MULTIPATH:
1028 case OFPACT_NOTE:
1029 case OFPACT_OUTPUT_REG:
1030 case OFPACT_POP_QUEUE:
1031 case OFPACT_REG_MOVE:
1032 case OFPACT_RESUBMIT:
1033 case OFPACT_SAMPLE:
1034 case OFPACT_STACK_POP:
1035 case OFPACT_STACK_PUSH:
1036
1037 /* The action set may only include actions and thus
1038 * may not include any instructions */
1039 case OFPACT_CLEAR_ACTIONS:
1040 case OFPACT_GOTO_TABLE:
1041 case OFPACT_METER:
1042 case OFPACT_WRITE_ACTIONS:
1043 case OFPACT_WRITE_METADATA:
1044 return false;
1045 default:
1046 NOT_REACHED();
1047 }
1048}
1049
1050/* Append ofpact 'a' onto the tail of 'out' */
1051static void
1052ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
1053{
1054 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
1055}
1056
1057/* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
1058static bool
1059ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
1060 enum ofpact_type filter)
1061{
1062 const struct ofpact *target;
1063 const struct ofpact *a;
1064
1065 target = NULL;
1066 OFPACT_FOR_EACH (a, in->data, in->size) {
1067 if (a->type == filter) {
1068 target = a;
1069 }
1070 }
1071 if (target) {
1072 ofpact_copy(out, target);
1073 }
1074 return target != NULL;
1075}
1076
1077/* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
1078 * The order of appended ofpacts is preserved between 'in' and 'out' */
1079static void
1080ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
1081 bool (*filter)(const struct ofpact *))
1082{
1083 const struct ofpact *a;
1084
1085 OFPACT_FOR_EACH (a, in->data, in->size) {
1086 if (filter(a)) {
1087 ofpact_copy(out, a);
1088 }
1089 }
1090}
1091
1092/* Reads 'action_set', which contains ofpacts accumulated by
1093 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
1094 * executed directly into 'action_list'. (These names correspond to the
1095 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
1096 *
1097 * In general this involves appending the last instance of each action that is
1098 * adimissible in the action set in the order described in the OpenFlow
1099 * specification.
1100 *
1101 * Exceptions:
1102 * + output action is only appended if no group action was present in 'in'.
1103 * + As a simplification all set actions are copied in the order the are
1104 * provided in 'in' as many set actions applied to a field has the same
1105 * affect as only applying the last action that sets a field and
1106 * duplicates are removed by do_xlate_actions().
1107 * This has an unwanted side-effect of compsoting multiple
1108 * LOAD_REG actions that touch different regions of the same field. */
1109void
1110ofpacts_execute_action_set(struct ofpbuf *action_list,
1111 const struct ofpbuf *action_set)
1112{
1113 /* The OpenFlow spec "Action Set" section specifies this order. */
1114 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
1115 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
1116 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
1117 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
1118 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
1119 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1120 ofpacts_copy_all(action_list, action_set, ofpact_is_set_action);
1121 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
1122
1123 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
1124 * we should execute only OFPACT_GROUP.
1125 *
1126 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
1127 * all the actions because there's no point in modifying a packet that will
1128 * not be sent anywhere. */
1129 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
1130 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT)) {
1131 ofpbuf_clear(action_list);
1132 }
1133}
1134
1135
1136static enum ofperr
1137ofpacts_from_openflow11_for_action_set(const union ofp_action *in,
1138 size_t n_in, struct ofpbuf *out)
1139{
1140 enum ofperr error;
1141 struct ofpact *a;
1142 size_t start = out->size;
1143
3d7e1e94 1144 error = ofpacts_from_openflow11(in, n_in, out);
7fdb60a7
SH
1145 if (error) {
1146 return error;
1147 }
1148
1149 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
1150 if (!ofpact_is_allowed_in_actions_set(a)) {
1151 VLOG_WARN_RL(&rl, "disallowed action in action set");
1152 return OFPERR_OFPBAC_BAD_TYPE;
1153 }
1154 }
1155
1156 return 0;
1157}
1158
a7a2d006
JS
1159\f
1160static enum ofperr
1161ofpact_from_openflow13(const union ofp_action *a, struct ofpbuf *out)
1162{
1163 enum ofputil_action_code code;
1164 enum ofperr error;
1165
1166 error = decode_openflow11_action(a, &code);
1167 if (error) {
1168 return error;
1169 }
1170
1171 if (code == OFPUTIL_OFPAT11_PUSH_MPLS) {
1172 struct ofp11_action_push *oap = (struct ofp11_action_push *)a;
1173 error = push_mpls_from_openflow(oap->ethertype,
1174 OFPACT_MPLS_BEFORE_VLAN, out);
1175 } else {
1176 error = ofpact_from_openflow11(a, out);
1177 }
1178
1179 return error;
1180}
1181
1182static enum ofperr
1183ofpacts_from_openflow13(const union ofp_action *in, size_t n_in,
1184 struct ofpbuf *out)
1185{
1186 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow13);
1187}
f25d0cf3 1188\f
d01c980f
BP
1189/* OpenFlow 1.1 instructions. */
1190
d01c980f 1191#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6a8a8528 1192 static inline const struct STRUCT * OVS_UNUSED \
a359d5ad
IY
1193 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
1194 { \
cb22974d 1195 ovs_assert(inst->type == htons(ENUM)); \
db5a1019 1196 return ALIGNED_CAST(struct STRUCT *, inst); \
a359d5ad
IY
1197 } \
1198 \
6a8a8528 1199 static inline void OVS_UNUSED \
d01c980f
BP
1200 instruction_init_##ENUM(struct STRUCT *s) \
1201 { \
1202 memset(s, 0, sizeof *s); \
1203 s->type = htons(ENUM); \
1204 s->len = htons(sizeof *s); \
1205 } \
1206 \
6a8a8528 1207 static inline struct STRUCT * OVS_UNUSED \
d01c980f
BP
1208 instruction_put_##ENUM(struct ofpbuf *buf) \
1209 { \
1210 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
1211 instruction_init_##ENUM(s); \
1212 return s; \
1213 }
1214OVS_INSTRUCTIONS
1215#undef DEFINE_INST
1216
a359d5ad
IY
1217struct instruction_type_info {
1218 enum ovs_instruction_type type;
1219 const char *name;
1220};
1221
1222static const struct instruction_type_info inst_info[] = {
1223#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
1224OVS_INSTRUCTIONS
1225#undef DEFINE_INST
1226};
1227
1228const char *
ba0bc9b0 1229ovs_instruction_name_from_type(enum ovs_instruction_type type)
a359d5ad 1230{
6ae0fd54 1231 return inst_info[type].name;
a359d5ad
IY
1232}
1233
1234int
ba0bc9b0 1235ovs_instruction_type_from_name(const char *name)
a359d5ad
IY
1236{
1237 const struct instruction_type_info *p;
1238 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
1239 if (!strcasecmp(name, p->name)) {
1240 return p->type;
1241 }
1242 }
1243 return -1;
1244}
1245
084c53de
BP
1246enum ovs_instruction_type
1247ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
1248{
1249 switch (type) {
1250 case OFPACT_METER:
1251 return OVSINST_OFPIT13_METER;
1252 case OFPACT_CLEAR_ACTIONS:
1253 return OVSINST_OFPIT11_CLEAR_ACTIONS;
7fdb60a7
SH
1254 case OFPACT_WRITE_ACTIONS:
1255 return OVSINST_OFPIT11_WRITE_ACTIONS;
084c53de
BP
1256 case OFPACT_WRITE_METADATA:
1257 return OVSINST_OFPIT11_WRITE_METADATA;
1258 case OFPACT_GOTO_TABLE:
1259 return OVSINST_OFPIT11_GOTO_TABLE;
1260 case OFPACT_OUTPUT:
7395c052 1261 case OFPACT_GROUP:
084c53de
BP
1262 case OFPACT_CONTROLLER:
1263 case OFPACT_ENQUEUE:
1264 case OFPACT_OUTPUT_REG:
1265 case OFPACT_BUNDLE:
1266 case OFPACT_SET_VLAN_VID:
1267 case OFPACT_SET_VLAN_PCP:
1268 case OFPACT_STRIP_VLAN:
1269 case OFPACT_PUSH_VLAN:
1270 case OFPACT_SET_ETH_SRC:
1271 case OFPACT_SET_ETH_DST:
1272 case OFPACT_SET_IPV4_SRC:
1273 case OFPACT_SET_IPV4_DST:
04f01c24 1274 case OFPACT_SET_IP_DSCP:
ff14eb7a 1275 case OFPACT_SET_IP_ECN:
0c20dbe4 1276 case OFPACT_SET_IP_TTL:
084c53de
BP
1277 case OFPACT_SET_L4_SRC_PORT:
1278 case OFPACT_SET_L4_DST_PORT:
1279 case OFPACT_REG_MOVE:
1280 case OFPACT_REG_LOAD:
1281 case OFPACT_STACK_PUSH:
1282 case OFPACT_STACK_POP:
1283 case OFPACT_DEC_TTL:
1284 case OFPACT_SET_MPLS_TTL:
1285 case OFPACT_DEC_MPLS_TTL:
1286 case OFPACT_PUSH_MPLS:
1287 case OFPACT_POP_MPLS:
1288 case OFPACT_SET_TUNNEL:
1289 case OFPACT_SET_QUEUE:
1290 case OFPACT_POP_QUEUE:
1291 case OFPACT_FIN_TIMEOUT:
1292 case OFPACT_RESUBMIT:
1293 case OFPACT_LEARN:
1294 case OFPACT_MULTIPATH:
1295 case OFPACT_NOTE:
1296 case OFPACT_EXIT:
1297 case OFPACT_SAMPLE:
1298 default:
1299 return OVSINST_OFPIT11_APPLY_ACTIONS;
1300 }
1301}
1302
d01c980f
BP
1303static inline struct ofp11_instruction *
1304instruction_next(const struct ofp11_instruction *inst)
1305{
1306 return ((struct ofp11_instruction *) (void *)
1307 ((uint8_t *) inst + ntohs(inst->len)));
1308}
1309
1310static inline bool
1311instruction_is_valid(const struct ofp11_instruction *inst,
1312 size_t n_instructions)
1313{
1314 uint16_t len = ntohs(inst->len);
1315 return (!(len % OFP11_INSTRUCTION_ALIGN)
1316 && len >= sizeof *inst
1317 && len / sizeof *inst <= n_instructions);
1318}
1319
1320/* This macro is careful to check for instructions with bad lengths. */
1321#define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
1322 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
1323 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
1324 ((LEFT) -= (ntohs((ITER)->len) \
1325 / sizeof(struct ofp11_instruction)), \
1326 (ITER) = instruction_next(ITER)))
1327
1328static enum ofperr
1329decode_openflow11_instruction(const struct ofp11_instruction *inst,
1330 enum ovs_instruction_type *type)
1331{
1332 uint16_t len = ntohs(inst->len);
1333
1334 switch (inst->type) {
1335 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
1336 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
1337
1338#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
1339 case CONSTANT_HTONS(ENUM): \
1340 if (EXTENSIBLE \
1341 ? len >= sizeof(struct STRUCT) \
1342 : len == sizeof(struct STRUCT)) { \
1343 *type = OVSINST_##ENUM; \
1344 return 0; \
1345 } else { \
1346 return OFPERR_OFPBIC_BAD_LEN; \
1347 }
1348OVS_INSTRUCTIONS
1349#undef DEFINE_INST
1350
1351 default:
1352 return OFPERR_OFPBIC_UNKNOWN_INST;
1353 }
1354}
1355
1356static enum ofperr
1357decode_openflow11_instructions(const struct ofp11_instruction insts[],
1358 size_t n_insts,
1359 const struct ofp11_instruction *out[])
1360{
1361 const struct ofp11_instruction *inst;
1362 size_t left;
1363
1364 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
1365 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
1366 enum ovs_instruction_type type;
1367 enum ofperr error;
1368
1369 error = decode_openflow11_instruction(inst, &type);
1370 if (error) {
1371 return error;
1372 }
1373
1374 if (out[type]) {
f4104c68 1375 return OFPERR_ONFBIC_DUP_INSTRUCTION;
d01c980f
BP
1376 }
1377 out[type] = inst;
1378 }
1379
1380 if (left) {
1381 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
1382 (n_insts - left) * sizeof *inst);
1383 return OFPERR_OFPBIC_BAD_LEN;
1384 }
1385 return 0;
1386}
1387
1388static void
1389get_actions_from_instruction(const struct ofp11_instruction *inst,
db5a1019 1390 const union ofp_action **actions,
dba70df0 1391 size_t *max_actions)
d01c980f 1392{
db5a1019 1393 *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
dba70df0 1394 *max_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
d01c980f
BP
1395}
1396
a7a2d006 1397/* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
d01c980f
BP
1398 * front of 'openflow' into ofpacts. On success, replaces any existing content
1399 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
1400 * Returns 0 if successful, otherwise an OpenFlow error.
1401 *
a7a2d006
JS
1402 * Actions are processed according to their OpenFlow version which
1403 * is provided in the 'version' parameter.
1404 *
d01c980f
BP
1405 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
1406 * instructions, so you should call ofpacts_pull_openflow11_instructions()
1407 * instead of this function.
1408 *
13f894b1
BP
1409 * The parsed actions are valid generically, but they may not be valid in a
1410 * specific context. For example, port numbers up to OFPP_MAX are valid
1411 * generically, but specific datapaths may only support port numbers in a
1412 * smaller range. Use ofpacts_check() to additional check whether actions are
1413 * valid in a specific context. */
d01c980f
BP
1414enum ofperr
1415ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
a7a2d006 1416 enum ofp_version version,
d01c980f
BP
1417 unsigned int actions_len,
1418 struct ofpbuf *ofpacts)
1419{
a7a2d006
JS
1420 switch (version) {
1421 case OFP10_VERSION:
1422 case OFP11_VERSION:
1423 case OFP12_VERSION:
1424 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1425 ofpacts_from_openflow11);
1426 case OFP13_VERSION:
1427 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
1428 ofpacts_from_openflow13);
1429 default:
1430 NOT_REACHED();
1431 }
d01c980f
BP
1432}
1433
1434enum ofperr
1435ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
a7a2d006 1436 enum ofp_version version,
d01c980f
BP
1437 unsigned int instructions_len,
1438 struct ofpbuf *ofpacts)
1439{
1440 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1441 const struct ofp11_instruction *instructions;
1442 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
1443 enum ofperr error;
1444
1445 ofpbuf_clear(ofpacts);
1446
1447 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
1448 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
1449 "multiple of %d",
1450 instructions_len, OFP11_INSTRUCTION_ALIGN);
1451 error = OFPERR_OFPBIC_BAD_LEN;
1452 goto exit;
1453 }
1454
1455 instructions = ofpbuf_try_pull(openflow, instructions_len);
1456 if (instructions == NULL) {
1457 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
1458 "remaining message length (%zu)",
1459 instructions_len, openflow->size);
1460 error = OFPERR_OFPBIC_BAD_LEN;
1461 goto exit;
1462 }
1463
1464 error = decode_openflow11_instructions(
1465 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
1466 insts);
1467 if (error) {
1468 goto exit;
1469 }
1470
638a19b0
JR
1471 if (insts[OVSINST_OFPIT13_METER]) {
1472 const struct ofp13_instruction_meter *oim;
1473 struct ofpact_meter *om;
1474
db5a1019
AW
1475 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
1476 insts[OVSINST_OFPIT13_METER]);
638a19b0
JR
1477
1478 om = ofpact_put_METER(ofpacts);
1479 om->meter_id = ntohl(oim->meter_id);
1480 }
d01c980f
BP
1481 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1482 const union ofp_action *actions;
dba70df0 1483 size_t max_actions;
d01c980f
BP
1484
1485 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
dba70df0 1486 &actions, &max_actions);
a7a2d006
JS
1487 switch (version) {
1488 case OFP10_VERSION:
1489 case OFP11_VERSION:
1490 case OFP12_VERSION:
dba70df0 1491 error = ofpacts_from_openflow11(actions, max_actions, ofpacts);
a7a2d006
JS
1492 break;
1493 case OFP13_VERSION:
dba70df0 1494 error = ofpacts_from_openflow13(actions, max_actions, ofpacts);
a7a2d006
JS
1495 break;
1496 default:
1497 NOT_REACHED();
1498 }
d01c980f
BP
1499 if (error) {
1500 goto exit;
1501 }
1502 }
b19e8793
IY
1503 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1504 instruction_get_OFPIT11_CLEAR_ACTIONS(
1505 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1506 ofpact_put_CLEAR_ACTIONS(ofpacts);
1507 }
7fdb60a7
SH
1508 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
1509 struct ofpact_nest *on;
1510 const union ofp_action *actions;
dba70df0 1511 size_t max_actions;
2b11907b 1512 size_t start;
7fdb60a7 1513
2b11907b
YT
1514 ofpact_pad(ofpacts);
1515 start = ofpacts->size;
7fdb60a7
SH
1516 on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1517 offsetof(struct ofpact_nest, actions));
1518 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
dba70df0
BP
1519 &actions, &max_actions);
1520 error = ofpacts_from_openflow11_for_action_set(actions, max_actions,
7fdb60a7
SH
1521 ofpacts);
1522 if (error) {
1523 goto exit;
1524 }
2b11907b 1525 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
7fdb60a7
SH
1526 on->ofpact.len = ofpacts->size - start;
1527 }
4cceacb9
JS
1528 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1529 const struct ofp11_instruction_write_metadata *oiwm;
1530 struct ofpact_metadata *om;
1531
db5a1019
AW
1532 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
1533 insts[OVSINST_OFPIT11_WRITE_METADATA]);
4cceacb9
JS
1534
1535 om = ofpact_put_WRITE_METADATA(ofpacts);
1536 om->metadata = oiwm->metadata;
1537 om->mask = oiwm->metadata_mask;
1538 }
8dd54666
IY
1539 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1540 const struct ofp11_instruction_goto_table *oigt;
1541 struct ofpact_goto_table *ogt;
1542
1543 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1544 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1545 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1546 ogt->table_id = oigt->table_id;
1547 }
d01c980f 1548
4cceacb9 1549 error = ofpacts_verify(ofpacts->data, ofpacts->size);
d01c980f
BP
1550exit:
1551 if (error) {
1552 ofpbuf_clear(ofpacts);
1553 }
1554 return error;
1555}
1556\f
ca287d20
JR
1557/* May modify flow->dl_type and flow->vlan_tci, caller must restore them.
1558 *
1559 * Modifies some actions, filling in fields that could not be properly set
1560 * without context. */
f25d0cf3 1561static enum ofperr
ca287d20 1562ofpact_check__(struct ofpact *a, struct flow *flow, ofp_port_t max_ports,
8621547c 1563 uint8_t table_id, bool enforce_consistency)
f25d0cf3
BP
1564{
1565 const struct ofpact_enqueue *enqueue;
1566
1567 switch (a->type) {
1568 case OFPACT_OUTPUT:
1569 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1570 max_ports);
1571
1572 case OFPACT_CONTROLLER:
1573 return 0;
1574
1575 case OFPACT_ENQUEUE:
1576 enqueue = ofpact_get_ENQUEUE(a);
4e022ec0
AW
1577 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
1578 && enqueue->port != OFPP_IN_PORT
f25d0cf3
BP
1579 && enqueue->port != OFPP_LOCAL) {
1580 return OFPERR_OFPBAC_BAD_OUT_PORT;
1581 }
1582 return 0;
1583
1584 case OFPACT_OUTPUT_REG:
1585 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1586
1587 case OFPACT_BUNDLE:
1588 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1589
1590 case OFPACT_SET_VLAN_VID:
ca287d20
JR
1591 /* Remember if we saw a vlan tag in the flow to aid translating to
1592 * OpenFlow 1.1+ if need be. */
1593 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
1594 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1595 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1596 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
1597 goto inconsistent;
1598 }
1599 /* Temporary mark that we have a vlan tag. */
1600 flow->vlan_tci |= htons(VLAN_CFI);
1601 return 0;
1602
f25d0cf3 1603 case OFPACT_SET_VLAN_PCP:
ca287d20
JR
1604 /* Remember if we saw a vlan tag in the flow to aid translating to
1605 * OpenFlow 1.1+ if need be. */
1606 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
1607 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
1608 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
1609 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
1610 goto inconsistent;
1611 }
1612 /* Temporary mark that we have a vlan tag. */
1613 flow->vlan_tci |= htons(VLAN_CFI);
8621547c
JR
1614 return 0;
1615
f25d0cf3 1616 case OFPACT_STRIP_VLAN:
8621547c
JR
1617 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
1618 goto inconsistent;
1619 }
ca287d20
JR
1620 /* Temporary mark that we have no vlan tag. */
1621 flow->vlan_tci = htons(0);
8621547c
JR
1622 return 0;
1623
3e34fbdd 1624 case OFPACT_PUSH_VLAN:
8621547c
JR
1625 if (flow->vlan_tci & htons(VLAN_CFI)) {
1626 /* Multiple VLAN headers not supported. */
1627 return OFPERR_OFPBAC_BAD_TAG;
1628 }
ca287d20
JR
1629 /* Temporary mark that we have a vlan tag. */
1630 flow->vlan_tci |= htons(VLAN_CFI);
8621547c
JR
1631 return 0;
1632
f25d0cf3
BP
1633 case OFPACT_SET_ETH_SRC:
1634 case OFPACT_SET_ETH_DST:
8621547c
JR
1635 return 0;
1636
f25d0cf3
BP
1637 case OFPACT_SET_IPV4_SRC:
1638 case OFPACT_SET_IPV4_DST:
8621547c
JR
1639 if (flow->dl_type != htons(ETH_TYPE_IP)) {
1640 goto inconsistent;
1641 }
1642 return 0;
1643
04f01c24 1644 case OFPACT_SET_IP_DSCP:
ff14eb7a 1645 case OFPACT_SET_IP_ECN:
0c20dbe4 1646 case OFPACT_SET_IP_TTL:
8621547c
JR
1647 case OFPACT_DEC_TTL:
1648 if (!is_ip_any(flow)) {
1649 goto inconsistent;
1650 }
1651 return 0;
1652
f25d0cf3
BP
1653 case OFPACT_SET_L4_SRC_PORT:
1654 case OFPACT_SET_L4_DST_PORT:
8621547c
JR
1655 if (!is_ip_any(flow) ||
1656 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1657 && flow->nw_proto != IPPROTO_SCTP)) {
1658 goto inconsistent;
1659 }
f25d0cf3
BP
1660 return 0;
1661
1662 case OFPACT_REG_MOVE:
1663 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1664
1665 case OFPACT_REG_LOAD:
500d243d 1666 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
f25d0cf3 1667
bd85dac1
AZ
1668 case OFPACT_STACK_PUSH:
1669 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
1670
1671 case OFPACT_STACK_POP:
1672 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
1673
0f3f3c3d 1674 case OFPACT_SET_MPLS_TTL:
b676167a 1675 case OFPACT_DEC_MPLS_TTL:
8621547c
JR
1676 if (!eth_type_mpls(flow->dl_type)) {
1677 goto inconsistent;
1678 }
1679 return 0;
1680
f25d0cf3
BP
1681 case OFPACT_SET_TUNNEL:
1682 case OFPACT_SET_QUEUE:
1683 case OFPACT_POP_QUEUE:
f25d0cf3
BP
1684 case OFPACT_RESUBMIT:
1685 return 0;
1686
8621547c
JR
1687 case OFPACT_FIN_TIMEOUT:
1688 if (flow->nw_proto != IPPROTO_TCP) {
1689 goto inconsistent;
1690 }
1691 return 0;
1692
f25d0cf3
BP
1693 case OFPACT_LEARN:
1694 return learn_check(ofpact_get_LEARN(a), flow);
1695
1696 case OFPACT_MULTIPATH:
1697 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1698
f25d0cf3
BP
1699 case OFPACT_NOTE:
1700 case OFPACT_EXIT:
1701 return 0;
1702
b02475c5 1703 case OFPACT_PUSH_MPLS:
eca39bce 1704 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
b02475c5
SH
1705 return 0;
1706
1707 case OFPACT_POP_MPLS:
eca39bce 1708 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
8621547c
JR
1709 if (!eth_type_mpls(flow->dl_type)) {
1710 goto inconsistent;
1711 }
b02475c5
SH
1712 return 0;
1713
29089a54
RL
1714 case OFPACT_SAMPLE:
1715 return 0;
1716
b19e8793 1717 case OFPACT_CLEAR_ACTIONS:
7fdb60a7
SH
1718 return 0;
1719
1720 case OFPACT_WRITE_ACTIONS: {
1721 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
1722 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
8621547c 1723 flow, max_ports, table_id, false);
7fdb60a7
SH
1724 }
1725
4cceacb9 1726 case OFPACT_WRITE_METADATA:
8dd54666
IY
1727 return 0;
1728
9cae45dc
JR
1729 case OFPACT_METER: {
1730 uint32_t mid = ofpact_get_METER(a)->meter_id;
1731 if (mid == 0 || mid > OFPM13_MAX) {
1732 return OFPERR_OFPMMFC_INVALID_METER;
1733 }
1734 return 0;
1735 }
e3b56933
JR
1736
1737 case OFPACT_GOTO_TABLE:
1738 if (ofpact_get_GOTO_TABLE(a)->table_id <= table_id) {
1739 return OFPERR_OFPBRC_BAD_TABLE_ID;
1740 }
1741 return 0;
1742
7395c052
NZ
1743 case OFPACT_GROUP:
1744 return 0;
1745
f25d0cf3
BP
1746 default:
1747 NOT_REACHED();
1748 }
8621547c
JR
1749
1750 inconsistent:
1751 if (enforce_consistency) {
1752 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
1753 }
1754 return 0;
f25d0cf3
BP
1755}
1756
1757/* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1758 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
eca39bce
JR
1759 * switch with no more than 'max_ports' ports.
1760 *
ca287d20
JR
1761 * May annotate ofpacts with information gathered from the 'flow'.
1762 *
eca39bce 1763 * May temporarily modify 'flow', but restores the changes before returning. */
f25d0cf3 1764enum ofperr
ca287d20 1765ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
8621547c
JR
1766 struct flow *flow, ofp_port_t max_ports, uint8_t table_id,
1767 bool enforce_consistency)
f25d0cf3 1768{
ca287d20 1769 struct ofpact *a;
b02475c5 1770 ovs_be16 dl_type = flow->dl_type;
ca287d20 1771 ovs_be16 vlan_tci = flow->vlan_tci;
eca39bce 1772 enum ofperr error = 0;
f25d0cf3
BP
1773
1774 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
8621547c
JR
1775 error = ofpact_check__(a, flow, max_ports, table_id,
1776 enforce_consistency);
f25d0cf3 1777 if (error) {
eca39bce 1778 break;
f25d0cf3
BP
1779 }
1780 }
ca287d20
JR
1781 /* Restore fields that may have been modified. */
1782 flow->dl_type = dl_type;
1783 flow->vlan_tci = vlan_tci;
eca39bce 1784 return error;
f25d0cf3 1785}
4cceacb9
JS
1786
1787/* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1788 * in the appropriate order as defined by the OpenFlow spec. */
1789enum ofperr
1790ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1791{
1792 const struct ofpact *a;
6813ee7c 1793 enum ovs_instruction_type inst;
4cceacb9 1794
6813ee7c 1795 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
4cceacb9 1796 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6813ee7c
BP
1797 enum ovs_instruction_type next;
1798
084c53de 1799 next = ovs_instruction_type_from_ofpact_type(a->type);
7fdb60a7
SH
1800 if (inst == OVSINST_OFPIT11_APPLY_ACTIONS
1801 ? next < inst
1802 : next <= inst) {
ba0bc9b0
BP
1803 const char *name = ovs_instruction_name_from_type(inst);
1804 const char *next_name = ovs_instruction_name_from_type(next);
6813ee7c
BP
1805
1806 if (next == inst) {
1807 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1808 "1.1+ compatibility", name);
4cceacb9 1809 } else {
6813ee7c
BP
1810 VLOG_WARN("invalid instruction ordering: %s must appear "
1811 "before %s, for OpenFlow 1.1+ compatibility",
1812 next_name, name);
4cceacb9 1813 }
6813ee7c 1814 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
4cceacb9
JS
1815 }
1816
6813ee7c 1817 inst = next;
4cceacb9
JS
1818 }
1819
1820 return 0;
1821}
f25d0cf3
BP
1822\f
1823/* Converting ofpacts to Nicira OpenFlow extensions. */
1824
1825static void
1826ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1827 struct ofpbuf *out)
1828{
1829 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1830
1831 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1832 output_reg->src.n_bits);
1833 naor->src = htonl(output_reg->src.field->nxm_header);
1834 naor->max_len = htons(output_reg->max_len);
1835}
1836
1837static void
1838ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1839 struct ofpbuf *out)
1840{
1841 struct nx_action_resubmit *nar;
1842
1843 if (resubmit->table_id == 0xff
1844 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1845 nar = ofputil_put_NXAST_RESUBMIT(out);
1846 } else {
1847 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1848 nar->table = resubmit->table_id;
1849 }
4e022ec0 1850 nar->in_port = htons(ofp_to_u16(resubmit->in_port));
f25d0cf3
BP
1851}
1852
1853static void
1854ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1855 struct ofpbuf *out)
1856{
1857 uint64_t tun_id = tunnel->tun_id;
1858
1859 if (tun_id <= UINT32_MAX
1860 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1861 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1862 } else {
1863 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1864 }
1865}
1866
4cceacb9
JS
1867static void
1868ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1869 struct ofpbuf *out)
1870{
1871 struct nx_action_write_metadata *nawm;
1872
1873 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1874 nawm->metadata = om->metadata;
1875 nawm->mask = om->mask;
1876}
1877
f25d0cf3
BP
1878static void
1879ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1880{
1881 size_t start_ofs = out->size;
1882 struct nx_action_note *nan;
1883 unsigned int remainder;
1884 unsigned int len;
1885
1886 nan = ofputil_put_NXAST_NOTE(out);
1887 out->size -= sizeof nan->note;
1888
1889 ofpbuf_put(out, note->data, note->length);
1890
1891 len = out->size - start_ofs;
1892 remainder = len % OFP_ACTION_ALIGN;
1893 if (remainder) {
1894 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1895 }
db5a1019 1896 nan = ofpbuf_at(out, start_ofs, sizeof *nan);
f25d0cf3
BP
1897 nan->len = htons(out->size - start_ofs);
1898}
1899
1900static void
1901ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1902 struct ofpbuf *out)
1903{
1904 struct nx_action_controller *nac;
1905
1906 nac = ofputil_put_NXAST_CONTROLLER(out);
1907 nac->max_len = htons(oc->max_len);
1908 nac->controller_id = htons(oc->controller_id);
1909 nac->reason = oc->reason;
1910}
1911
c2d967a5
MM
1912static void
1913ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1914 struct ofpbuf *out)
1915{
1916 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1917 ofputil_put_NXAST_DEC_TTL(out);
1918 } else {
1919 struct nx_action_cnt_ids *nac_ids =
1920 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1921 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1922 ovs_be16 *ids;
1923 size_t i;
1924
1925 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1926 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1927
1928 ids = ofpbuf_put_zeros(out, ids_len);
1929 for (i = 0; i < oc_ids->n_controllers; i++) {
1930 ids[i] = htons(oc_ids->cnt_ids[i]);
1931 }
1932 }
1933}
1934
f25d0cf3
BP
1935static void
1936ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1937 struct ofpbuf *out)
1938{
1939 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1940 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1941 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1942}
1943
29089a54
RL
1944static void
1945ofpact_sample_to_nxast(const struct ofpact_sample *os,
1946 struct ofpbuf *out)
1947{
1948 struct nx_action_sample *nas;
1949
1950 nas = ofputil_put_NXAST_SAMPLE(out);
1951 nas->probability = htons(os->probability);
1952 nas->collector_set_id = htonl(os->collector_set_id);
1953 nas->obs_domain_id = htonl(os->obs_domain_id);
1954 nas->obs_point_id = htonl(os->obs_point_id);
1955}
1956
f25d0cf3
BP
1957static void
1958ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1959{
1960 switch (a->type) {
1961 case OFPACT_CONTROLLER:
1962 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1963 break;
1964
1965 case OFPACT_OUTPUT_REG:
1966 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1967 break;
1968
1969 case OFPACT_BUNDLE:
1970 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1971 break;
1972
1973 case OFPACT_REG_MOVE:
1974 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1975 break;
1976
1977 case OFPACT_REG_LOAD:
1978 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1979 break;
1980
bd85dac1
AZ
1981 case OFPACT_STACK_PUSH:
1982 nxm_stack_push_to_nxast(ofpact_get_STACK_PUSH(a), out);
1983 break;
1984
1985 case OFPACT_STACK_POP:
1986 nxm_stack_pop_to_nxast(ofpact_get_STACK_POP(a), out);
1987 break;
1988
f25d0cf3 1989 case OFPACT_DEC_TTL:
c2d967a5 1990 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
f25d0cf3
BP
1991 break;
1992
0f3f3c3d
SH
1993 case OFPACT_SET_MPLS_TTL:
1994 ofputil_put_NXAST_SET_MPLS_TTL(out)->ttl
1995 = ofpact_get_SET_MPLS_TTL(a)->ttl;
1996 break;
1997
b676167a
SH
1998 case OFPACT_DEC_MPLS_TTL:
1999 ofputil_put_NXAST_DEC_MPLS_TTL(out);
2000 break;
2001
f25d0cf3
BP
2002 case OFPACT_SET_TUNNEL:
2003 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
2004 break;
2005
4cceacb9
JS
2006 case OFPACT_WRITE_METADATA:
2007 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
2008 break;
2009
f25d0cf3
BP
2010 case OFPACT_SET_QUEUE:
2011 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
2012 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2013 break;
2014
2015 case OFPACT_POP_QUEUE:
2016 ofputil_put_NXAST_POP_QUEUE(out);
2017 break;
2018
2019 case OFPACT_FIN_TIMEOUT:
2020 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
2021 break;
2022
2023 case OFPACT_RESUBMIT:
2024 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
2025 break;
2026
2027 case OFPACT_LEARN:
2028 learn_to_nxast(ofpact_get_LEARN(a), out);
2029 break;
2030
2031 case OFPACT_MULTIPATH:
2032 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
2033 break;
2034
f25d0cf3
BP
2035 case OFPACT_NOTE:
2036 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
2037 break;
2038
2039 case OFPACT_EXIT:
2040 ofputil_put_NXAST_EXIT(out);
2041 break;
2042
b02475c5
SH
2043 case OFPACT_PUSH_MPLS:
2044 ofputil_put_NXAST_PUSH_MPLS(out)->ethertype =
2045 ofpact_get_PUSH_MPLS(a)->ethertype;
2046 break;
2047
2048 case OFPACT_POP_MPLS:
2049 ofputil_put_NXAST_POP_MPLS(out)->ethertype =
2050 ofpact_get_POP_MPLS(a)->ethertype;
2051 break;
2052
29089a54
RL
2053 case OFPACT_SAMPLE:
2054 ofpact_sample_to_nxast(ofpact_get_SAMPLE(a), out);
2055 break;
2056
7395c052 2057 case OFPACT_GROUP:
f25d0cf3
BP
2058 case OFPACT_OUTPUT:
2059 case OFPACT_ENQUEUE:
2060 case OFPACT_SET_VLAN_VID:
2061 case OFPACT_SET_VLAN_PCP:
2062 case OFPACT_STRIP_VLAN:
3e34fbdd 2063 case OFPACT_PUSH_VLAN:
f25d0cf3
BP
2064 case OFPACT_SET_ETH_SRC:
2065 case OFPACT_SET_ETH_DST:
2066 case OFPACT_SET_IPV4_SRC:
2067 case OFPACT_SET_IPV4_DST:
04f01c24 2068 case OFPACT_SET_IP_DSCP:
ff14eb7a 2069 case OFPACT_SET_IP_ECN:
0c20dbe4 2070 case OFPACT_SET_IP_TTL:
f25d0cf3
BP
2071 case OFPACT_SET_L4_SRC_PORT:
2072 case OFPACT_SET_L4_DST_PORT:
7fdb60a7 2073 case OFPACT_WRITE_ACTIONS:
b19e8793 2074 case OFPACT_CLEAR_ACTIONS:
8dd54666 2075 case OFPACT_GOTO_TABLE:
638a19b0 2076 case OFPACT_METER:
f25d0cf3
BP
2077 NOT_REACHED();
2078 }
2079}
2080\f
2081/* Converting ofpacts to OpenFlow 1.0. */
2082
2083static void
2084ofpact_output_to_openflow10(const struct ofpact_output *output,
2085 struct ofpbuf *out)
2086{
d01c980f 2087 struct ofp10_action_output *oao;
f25d0cf3
BP
2088
2089 oao = ofputil_put_OFPAT10_OUTPUT(out);
4e022ec0 2090 oao->port = htons(ofp_to_u16(output->port));
f25d0cf3
BP
2091 oao->max_len = htons(output->max_len);
2092}
2093
2094static void
2095ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
2096 struct ofpbuf *out)
2097{
31a9e63f 2098 struct ofp10_action_enqueue *oae;
f25d0cf3
BP
2099
2100 oae = ofputil_put_OFPAT10_ENQUEUE(out);
4e022ec0 2101 oae->port = htons(ofp_to_u16(enqueue->port));
f25d0cf3
BP
2102 oae->queue_id = htonl(enqueue->queue);
2103}
2104
2105static void
2106ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
2107{
2108 switch (a->type) {
2109 case OFPACT_OUTPUT:
2110 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
2111 break;
2112
2113 case OFPACT_ENQUEUE:
2114 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
2115 break;
2116
2117 case OFPACT_SET_VLAN_VID:
2118 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
2119 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2120 break;
2121
2122 case OFPACT_SET_VLAN_PCP:
2123 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
2124 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2125 break;
2126
2127 case OFPACT_STRIP_VLAN:
2128 ofputil_put_OFPAT10_STRIP_VLAN(out);
2129 break;
2130
2131 case OFPACT_SET_ETH_SRC:
2132 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
2133 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2134 break;
2135
2136 case OFPACT_SET_ETH_DST:
2137 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
2138 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2139 break;
2140
2141 case OFPACT_SET_IPV4_SRC:
2142 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
2143 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2144 break;
2145
2146 case OFPACT_SET_IPV4_DST:
2147 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
2148 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2149 break;
2150
04f01c24 2151 case OFPACT_SET_IP_DSCP:
f25d0cf3 2152 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
04f01c24 2153 = ofpact_get_SET_IP_DSCP(a)->dscp;
f25d0cf3
BP
2154 break;
2155
2156 case OFPACT_SET_L4_SRC_PORT:
2157 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
2158 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2159 break;
2160
2161 case OFPACT_SET_L4_DST_PORT:
2162 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
2163 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2164 break;
2165
3e34fbdd 2166 case OFPACT_PUSH_VLAN:
ca287d20
JR
2167 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
2168 * follow this action. */
2169 break;
2170
b19e8793 2171 case OFPACT_CLEAR_ACTIONS:
7fdb60a7 2172 case OFPACT_WRITE_ACTIONS:
8dd54666 2173 case OFPACT_GOTO_TABLE:
638a19b0 2174 case OFPACT_METER:
5dca28b5 2175 /* XXX */
8dd54666
IY
2176 break;
2177
7395c052
NZ
2178 case OFPACT_GROUP:
2179 break;
2180
f25d0cf3
BP
2181 case OFPACT_CONTROLLER:
2182 case OFPACT_OUTPUT_REG:
2183 case OFPACT_BUNDLE:
2184 case OFPACT_REG_MOVE:
2185 case OFPACT_REG_LOAD:
bd85dac1
AZ
2186 case OFPACT_STACK_PUSH:
2187 case OFPACT_STACK_POP:
f25d0cf3 2188 case OFPACT_DEC_TTL:
ff14eb7a 2189 case OFPACT_SET_IP_ECN:
0c20dbe4 2190 case OFPACT_SET_IP_TTL:
0f3f3c3d 2191 case OFPACT_SET_MPLS_TTL:
b676167a 2192 case OFPACT_DEC_MPLS_TTL:
f25d0cf3 2193 case OFPACT_SET_TUNNEL:
4cceacb9 2194 case OFPACT_WRITE_METADATA:
f25d0cf3
BP
2195 case OFPACT_SET_QUEUE:
2196 case OFPACT_POP_QUEUE:
2197 case OFPACT_FIN_TIMEOUT:
2198 case OFPACT_RESUBMIT:
2199 case OFPACT_LEARN:
2200 case OFPACT_MULTIPATH:
f25d0cf3
BP
2201 case OFPACT_NOTE:
2202 case OFPACT_EXIT:
b02475c5
SH
2203 case OFPACT_PUSH_MPLS:
2204 case OFPACT_POP_MPLS:
29089a54 2205 case OFPACT_SAMPLE:
f25d0cf3
BP
2206 ofpact_to_nxast(a, out);
2207 break;
2208 }
2209}
2210
d01c980f 2211/* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
f25d0cf3
BP
2212 * actions in 'openflow', appending the actions to any existing data in
2213 * 'openflow'. */
2214void
d01c980f
BP
2215ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
2216 struct ofpbuf *openflow)
f25d0cf3
BP
2217{
2218 const struct ofpact *a;
2219
2220 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2221 ofpact_to_openflow10(a, openflow);
2222 }
2223}
2224\f
d01c980f
BP
2225/* Converting ofpacts to OpenFlow 1.1. */
2226
2227static void
2228ofpact_output_to_openflow11(const struct ofpact_output *output,
2229 struct ofpbuf *out)
2230{
2231 struct ofp11_action_output *oao;
2232
2233 oao = ofputil_put_OFPAT11_OUTPUT(out);
2234 oao->port = ofputil_port_to_ofp11(output->port);
2235 oao->max_len = htons(output->max_len);
2236}
2237
99086062
BP
2238static void
2239ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
2240 struct ofpbuf *out)
2241{
2242 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
2243 && (!dec_ttl->ofpact.compat ||
2244 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
2245 ofputil_put_OFPAT11_DEC_NW_TTL(out);
2246 } else {
2247 ofpact_dec_ttl_to_nxast(dec_ttl, out);
2248 }
2249}
2250
d01c980f
BP
2251static void
2252ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
2253{
2254 switch (a->type) {
2255 case OFPACT_OUTPUT:
2256 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
2257
2258 case OFPACT_ENQUEUE:
2259 /* XXX */
2260 break;
2261
2262 case OFPACT_SET_VLAN_VID:
ca287d20
JR
2263 /* Push a VLAN tag, if one was not seen at action validation time. */
2264 if (!ofpact_get_SET_VLAN_VID(a)->flow_has_vlan
2265 && ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2266 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2267 = htons(ETH_TYPE_VLAN_8021Q);
2268 }
d01c980f
BP
2269 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
2270 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2271 break;
2272
2273 case OFPACT_SET_VLAN_PCP:
ca287d20
JR
2274 /* Push a VLAN tag, if one was not seen at action validation time. */
2275 if (!ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan
2276 && ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2277 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype
2278 = htons(ETH_TYPE_VLAN_8021Q);
2279 }
d01c980f
BP
2280 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
2281 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
2282 break;
2283
2284 case OFPACT_STRIP_VLAN:
8e61c110 2285 ofputil_put_OFPAT11_POP_VLAN(out);
d01c980f
BP
2286 break;
2287
3e34fbdd 2288 case OFPACT_PUSH_VLAN:
5dca28b5 2289 /* XXX ETH_TYPE_VLAN_8021AD case */
3e34fbdd
IY
2290 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
2291 htons(ETH_TYPE_VLAN_8021Q);
2292 break;
2293
276c4e7a
SH
2294 case OFPACT_SET_QUEUE:
2295 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
2296 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
2297 break;
2298
d01c980f
BP
2299 case OFPACT_SET_ETH_SRC:
2300 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
2301 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2302 break;
2303
2304 case OFPACT_SET_ETH_DST:
2305 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
2306 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2307 break;
2308
2309 case OFPACT_SET_IPV4_SRC:
2310 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
2311 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2312 break;
2313
2314 case OFPACT_SET_IPV4_DST:
2315 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
2316 = ofpact_get_SET_IPV4_DST(a)->ipv4;
2317 break;
2318
04f01c24 2319 case OFPACT_SET_IP_DSCP:
d01c980f 2320 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
04f01c24 2321 = ofpact_get_SET_IP_DSCP(a)->dscp;
d01c980f
BP
2322 break;
2323
ff14eb7a
JR
2324 case OFPACT_SET_IP_ECN:
2325 ofputil_put_OFPAT11_SET_NW_ECN(out)->nw_ecn
2326 = ofpact_get_SET_IP_ECN(a)->ecn;
2327 break;
2328
0c20dbe4
JR
2329 case OFPACT_SET_IP_TTL:
2330 ofputil_put_OFPAT11_SET_NW_TTL(out)->nw_ttl
2331 = ofpact_get_SET_IP_TTL(a)->ttl;
2332 break;
2333
d01c980f
BP
2334 case OFPACT_SET_L4_SRC_PORT:
2335 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
2336 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2337 break;
2338
2339 case OFPACT_SET_L4_DST_PORT:
2340 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
2341 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2342 break;
2343
7bcb1506 2344 case OFPACT_DEC_TTL:
99086062 2345 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
7bcb1506
IY
2346 break;
2347
0f3f3c3d
SH
2348 case OFPACT_SET_MPLS_TTL:
2349 ofputil_put_OFPAT11_SET_MPLS_TTL(out)->mpls_ttl
2350 = ofpact_get_SET_MPLS_TTL(a)->ttl;
2351 break;
2352
b676167a
SH
2353 case OFPACT_DEC_MPLS_TTL:
2354 ofputil_put_OFPAT11_DEC_MPLS_TTL(out);
2355 break;
2356
4cceacb9
JS
2357 case OFPACT_WRITE_METADATA:
2358 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
2359 break;
2360
b02475c5
SH
2361 case OFPACT_PUSH_MPLS:
2362 ofputil_put_OFPAT11_PUSH_MPLS(out)->ethertype =
2363 ofpact_get_PUSH_MPLS(a)->ethertype;
2364 break;
2365
2366 case OFPACT_POP_MPLS:
2367 ofputil_put_OFPAT11_POP_MPLS(out)->ethertype =
2368 ofpact_get_POP_MPLS(a)->ethertype;
2369
2370 break;
2371
b19e8793 2372 case OFPACT_CLEAR_ACTIONS:
7fdb60a7 2373 case OFPACT_WRITE_ACTIONS:
8dd54666 2374 case OFPACT_GOTO_TABLE:
638a19b0 2375 case OFPACT_METER:
8dd54666
IY
2376 NOT_REACHED();
2377
7395c052
NZ
2378 case OFPACT_GROUP:
2379 ofputil_put_OFPAT11_GROUP(out)->group_id =
2380 htonl(ofpact_get_GROUP(a)->group_id);
2381 break;
2382
d01c980f
BP
2383 case OFPACT_CONTROLLER:
2384 case OFPACT_OUTPUT_REG:
2385 case OFPACT_BUNDLE:
2386 case OFPACT_REG_MOVE:
2387 case OFPACT_REG_LOAD:
bd85dac1
AZ
2388 case OFPACT_STACK_PUSH:
2389 case OFPACT_STACK_POP:
d01c980f 2390 case OFPACT_SET_TUNNEL:
d01c980f
BP
2391 case OFPACT_POP_QUEUE:
2392 case OFPACT_FIN_TIMEOUT:
2393 case OFPACT_RESUBMIT:
2394 case OFPACT_LEARN:
2395 case OFPACT_MULTIPATH:
d01c980f
BP
2396 case OFPACT_NOTE:
2397 case OFPACT_EXIT:
29089a54 2398 case OFPACT_SAMPLE:
d01c980f
BP
2399 ofpact_to_nxast(a, out);
2400 break;
2401 }
2402}
2403
2404/* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
2405 * 1.1 actions in 'openflow', appending the actions to any existing data in
2406 * 'openflow'. */
a07c15bc 2407size_t
d01c980f
BP
2408ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
2409 size_t ofpacts_len, struct ofpbuf *openflow)
2410{
2411 const struct ofpact *a;
a07c15bc 2412 size_t start_size = openflow->size;
d01c980f
BP
2413
2414 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2415 ofpact_to_openflow11(a, openflow);
2416 }
a07c15bc
SH
2417
2418 return openflow->size - start_size;
d01c980f
BP
2419}
2420
8dd54666
IY
2421static void
2422ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
d01c980f
BP
2423{
2424 struct ofp11_instruction_actions *oia;
d01c980f
BP
2425
2426 /* Update the instruction's length (or, if it's empty, delete it). */
2427 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
2428 if (openflow->size > ofs + sizeof *oia) {
2429 oia->len = htons(openflow->size - ofs);
2430 } else {
2431 openflow->size = ofs;
2432 }
2433}
8dd54666
IY
2434
2435void
2436ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
2437 size_t ofpacts_len,
2438 struct ofpbuf *openflow)
2439{
2440 const struct ofpact *a;
2441
2442 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
084c53de
BP
2443 switch (ovs_instruction_type_from_ofpact_type(a->type)) {
2444 case OVSINST_OFPIT11_CLEAR_ACTIONS:
f606c7e9 2445 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
084c53de 2446 break;
8dd54666 2447
084c53de
BP
2448 case OVSINST_OFPIT11_GOTO_TABLE: {
2449 struct ofp11_instruction_goto_table *oigt;
8dd54666
IY
2450 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
2451 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
2452 memset(oigt->pad, 0, sizeof oigt->pad);
084c53de
BP
2453 break;
2454 }
2455
2456 case OVSINST_OFPIT11_WRITE_METADATA: {
4cceacb9
JS
2457 const struct ofpact_metadata *om;
2458 struct ofp11_instruction_write_metadata *oiwm;
2459
2460 om = ofpact_get_WRITE_METADATA(a);
2461 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
2462 oiwm->metadata = om->metadata;
2463 oiwm->metadata_mask = om->mask;
084c53de
BP
2464 break;
2465 }
2466
2467 case OVSINST_OFPIT13_METER: {
638a19b0
JR
2468 const struct ofpact_meter *om;
2469 struct ofp13_instruction_meter *oim;
2470
2471 om = ofpact_get_METER(a);
2472 oim = instruction_put_OFPIT13_METER(openflow);
2473 oim->meter_id = htonl(om->meter_id);
084c53de
BP
2474 break;
2475 }
2476
2477 case OVSINST_OFPIT11_APPLY_ACTIONS: {
8dd54666
IY
2478 const size_t ofs = openflow->size;
2479 const size_t ofpacts_len_left =
2480 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
2481 const struct ofpact *action;
2482 const struct ofpact *processed = a;
2483
2484 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
2485 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
084c53de
BP
2486 if (ovs_instruction_type_from_ofpact_type(action->type)
2487 != OVSINST_OFPIT11_APPLY_ACTIONS) {
8dd54666
IY
2488 break;
2489 }
2490 ofpact_to_openflow11(action, openflow);
2491 processed = action;
2492 }
2493 ofpacts_update_instruction_actions(openflow, ofs);
2494 a = processed;
084c53de
BP
2495 break;
2496 }
2497
7fdb60a7
SH
2498 case OVSINST_OFPIT11_WRITE_ACTIONS: {
2499 const size_t ofs = openflow->size;
2500 const struct ofpact_nest *on;
2501
2502 on = ofpact_get_WRITE_ACTIONS(a);
2503 instruction_put_OFPIT11_WRITE_ACTIONS(openflow);
2504 ofpacts_put_openflow11_actions(on->actions,
2505 ofpact_nest_get_action_len(on),
2506 openflow);
2507 ofpacts_update_instruction_actions(openflow, ofs);
2508
2509 break;
2510 }
8dd54666
IY
2511 }
2512 }
2513}
d01c980f 2514\f
f25d0cf3
BP
2515/* Returns true if 'action' outputs to 'port', false otherwise. */
2516static bool
4e022ec0 2517ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
f25d0cf3
BP
2518{
2519 switch (ofpact->type) {
2520 case OFPACT_OUTPUT:
2521 return ofpact_get_OUTPUT(ofpact)->port == port;
2522 case OFPACT_ENQUEUE:
2523 return ofpact_get_ENQUEUE(ofpact)->port == port;
2524 case OFPACT_CONTROLLER:
2525 return port == OFPP_CONTROLLER;
2526
2527 case OFPACT_OUTPUT_REG:
2528 case OFPACT_BUNDLE:
2529 case OFPACT_SET_VLAN_VID:
2530 case OFPACT_SET_VLAN_PCP:
2531 case OFPACT_STRIP_VLAN:
3e34fbdd 2532 case OFPACT_PUSH_VLAN:
f25d0cf3
BP
2533 case OFPACT_SET_ETH_SRC:
2534 case OFPACT_SET_ETH_DST:
2535 case OFPACT_SET_IPV4_SRC:
2536 case OFPACT_SET_IPV4_DST:
04f01c24 2537 case OFPACT_SET_IP_DSCP:
ff14eb7a 2538 case OFPACT_SET_IP_ECN:
0c20dbe4 2539 case OFPACT_SET_IP_TTL:
f25d0cf3
BP
2540 case OFPACT_SET_L4_SRC_PORT:
2541 case OFPACT_SET_L4_DST_PORT:
2542 case OFPACT_REG_MOVE:
2543 case OFPACT_REG_LOAD:
bd85dac1
AZ
2544 case OFPACT_STACK_PUSH:
2545 case OFPACT_STACK_POP:
f25d0cf3 2546 case OFPACT_DEC_TTL:
0f3f3c3d 2547 case OFPACT_SET_MPLS_TTL:
b676167a 2548 case OFPACT_DEC_MPLS_TTL:
f25d0cf3 2549 case OFPACT_SET_TUNNEL:
4cceacb9 2550 case OFPACT_WRITE_METADATA:
f25d0cf3
BP
2551 case OFPACT_SET_QUEUE:
2552 case OFPACT_POP_QUEUE:
2553 case OFPACT_FIN_TIMEOUT:
2554 case OFPACT_RESUBMIT:
2555 case OFPACT_LEARN:
2556 case OFPACT_MULTIPATH:
f25d0cf3
BP
2557 case OFPACT_NOTE:
2558 case OFPACT_EXIT:
b02475c5
SH
2559 case OFPACT_PUSH_MPLS:
2560 case OFPACT_POP_MPLS:
29089a54 2561 case OFPACT_SAMPLE:
b19e8793 2562 case OFPACT_CLEAR_ACTIONS:
7fdb60a7 2563 case OFPACT_WRITE_ACTIONS:
8dd54666 2564 case OFPACT_GOTO_TABLE:
638a19b0 2565 case OFPACT_METER:
7395c052 2566 case OFPACT_GROUP:
f25d0cf3
BP
2567 default:
2568 return false;
2569 }
2570}
2571
2572/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2573 * to 'port', false otherwise. */
2574bool
2575ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
4e022ec0 2576 ofp_port_t port)
f25d0cf3
BP
2577{
2578 const struct ofpact *a;
2579
2580 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2581 if (ofpact_outputs_to_port(a, port)) {
2582 return true;
2583 }
2584 }
2585
2586 return false;
2587}
2588
7395c052
NZ
2589/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
2590 * to 'group', false otherwise. */
2591bool
2592ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
2593 uint32_t group_id)
2594{
2595 const struct ofpact *a;
2596
2597 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2598 if (a->type == OFPACT_GROUP
2599 && ofpact_get_GROUP(a)->group_id == group_id) {
2600 return true;
2601 }
2602 }
2603
2604 return false;
2605}
2606
f25d0cf3
BP
2607bool
2608ofpacts_equal(const struct ofpact *a, size_t a_len,
2609 const struct ofpact *b, size_t b_len)
2610{
2611 return a_len == b_len && !memcmp(a, b, a_len);
2612}
d6fb622d
BP
2613
2614/* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
2615 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
2616 *
2617 * This function relies on the order of 'ofpacts' being correct (as checked by
2618 * ofpacts_verify()). */
2619uint32_t
2620ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
2621{
2622 const struct ofpact *a;
2623
2624 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2625 enum ovs_instruction_type inst;
2626
2627 inst = ovs_instruction_type_from_ofpact_type(a->type);
2628 if (a->type == OFPACT_METER) {
2629 return ofpact_get_METER(a)->meter_id;
2630 } else if (inst > OVSINST_OFPIT13_METER) {
2631 break;
2632 }
2633 }
2634
2635 return 0;
2636}
f25d0cf3
BP
2637\f
2638/* Formatting ofpacts. */
2639
2640static void
2641print_note(const struct ofpact_note *note, struct ds *string)
2642{
2643 size_t i;
2644
2645 ds_put_cstr(string, "note:");
2646 for (i = 0; i < note->length; i++) {
2647 if (i) {
2648 ds_put_char(string, '.');
2649 }
2650 ds_put_format(string, "%02"PRIx8, note->data[i]);
2651 }
2652}
2653
c2d967a5
MM
2654static void
2655print_dec_ttl(const struct ofpact_cnt_ids *ids,
2656 struct ds *s)
2657{
2658 size_t i;
2659
2660 ds_put_cstr(s, "dec_ttl");
2661 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
2662 ds_put_cstr(s, "(");
2663 for (i = 0; i < ids->n_controllers; i++) {
2664 if (i) {
2665 ds_put_cstr(s, ",");
2666 }
2667 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
2668 }
2669 ds_put_cstr(s, ")");
2670 }
2671}
2672
f25d0cf3
BP
2673static void
2674print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
2675 struct ds *s)
2676{
2677 ds_put_cstr(s, "fin_timeout(");
2678 if (fin_timeout->fin_idle_timeout) {
2679 ds_put_format(s, "idle_timeout=%"PRIu16",",
2680 fin_timeout->fin_idle_timeout);
2681 }
2682 if (fin_timeout->fin_hard_timeout) {
2683 ds_put_format(s, "hard_timeout=%"PRIu16",",
2684 fin_timeout->fin_hard_timeout);
2685 }
2686 ds_chomp(s, ',');
2687 ds_put_char(s, ')');
2688}
2689
2690static void
2691ofpact_format(const struct ofpact *a, struct ds *s)
2692{
2693 const struct ofpact_enqueue *enqueue;
2694 const struct ofpact_resubmit *resubmit;
f25d0cf3 2695 const struct ofpact_controller *controller;
4cceacb9 2696 const struct ofpact_metadata *metadata;
f25d0cf3 2697 const struct ofpact_tunnel *tunnel;
29089a54 2698 const struct ofpact_sample *sample;
4e022ec0 2699 ofp_port_t port;
f25d0cf3
BP
2700
2701 switch (a->type) {
2702 case OFPACT_OUTPUT:
2703 port = ofpact_get_OUTPUT(a)->port;
4e022ec0 2704 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)) {
f25d0cf3
BP
2705 ds_put_format(s, "output:%"PRIu16, port);
2706 } else {
2707 ofputil_format_port(port, s);
2708 if (port == OFPP_CONTROLLER) {
2709 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
2710 }
2711 }
2712 break;
2713
2714 case OFPACT_CONTROLLER:
2715 controller = ofpact_get_CONTROLLER(a);
2716 if (controller->reason == OFPR_ACTION &&
2717 controller->controller_id == 0) {
2718 ds_put_format(s, "CONTROLLER:%"PRIu16,
2719 ofpact_get_CONTROLLER(a)->max_len);
2720 } else {
2721 enum ofp_packet_in_reason reason = controller->reason;
2722
2723 ds_put_cstr(s, "controller(");
2724 if (reason != OFPR_ACTION) {
5014a89d
BP
2725 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2726
f25d0cf3 2727 ds_put_format(s, "reason=%s,",
5014a89d
BP
2728 ofputil_packet_in_reason_to_string(
2729 reason, reasonbuf, sizeof reasonbuf));
f25d0cf3
BP
2730 }
2731 if (controller->max_len != UINT16_MAX) {
2732 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
2733 }
2734 if (controller->controller_id != 0) {
2735 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
2736 }
2737 ds_chomp(s, ',');
2738 ds_put_char(s, ')');
2739 }
2740 break;
2741
2742 case OFPACT_ENQUEUE:
2743 enqueue = ofpact_get_ENQUEUE(a);
2744 ds_put_format(s, "enqueue:");
2745 ofputil_format_port(enqueue->port, s);
2746 ds_put_format(s, "q%"PRIu32, enqueue->queue);
2747 break;
2748
2749 case OFPACT_OUTPUT_REG:
2750 ds_put_cstr(s, "output:");
2751 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
2752 break;
2753
2754 case OFPACT_BUNDLE:
2755 bundle_format(ofpact_get_BUNDLE(a), s);
2756 break;
2757
2758 case OFPACT_SET_VLAN_VID:
ca287d20
JR
2759 ds_put_format(s, "%s:%"PRIu16,
2760 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_VID
2761 ? "set_vlan_vid"
2762 : "mod_vlan_vid"),
f25d0cf3
BP
2763 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
2764 break;
2765
2766 case OFPACT_SET_VLAN_PCP:
ca287d20
JR
2767 ds_put_format(s, "%s:%"PRIu8,
2768 (a->compat == OFPUTIL_OFPAT11_SET_VLAN_PCP
2769 ? "set_vlan_pcp"
2770 : "mod_vlan_pcp"),
f25d0cf3
BP
2771 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
2772 break;
2773
2774 case OFPACT_STRIP_VLAN:
2775 ds_put_cstr(s, "strip_vlan");
2776 break;
2777
3e34fbdd 2778 case OFPACT_PUSH_VLAN:
5dca28b5 2779 /* XXX 802.1AD case*/
3e34fbdd
IY
2780 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
2781 break;
2782
f25d0cf3
BP
2783 case OFPACT_SET_ETH_SRC:
2784 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
2785 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
2786 break;
2787
2788 case OFPACT_SET_ETH_DST:
2789 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
2790 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
2791 break;
2792
2793 case OFPACT_SET_IPV4_SRC:
2794 ds_put_format(s, "mod_nw_src:"IP_FMT,
ed36537e 2795 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
f25d0cf3
BP
2796 break;
2797
2798 case OFPACT_SET_IPV4_DST:
2799 ds_put_format(s, "mod_nw_dst:"IP_FMT,
ed36537e 2800 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
f25d0cf3
BP
2801 break;
2802
04f01c24
BP
2803 case OFPACT_SET_IP_DSCP:
2804 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IP_DSCP(a)->dscp);
f25d0cf3
BP
2805 break;
2806
ff14eb7a
JR
2807 case OFPACT_SET_IP_ECN:
2808 ds_put_format(s, "mod_nw_ecn:%d", ofpact_get_SET_IP_ECN(a)->ecn);
2809 break;
2810
0c20dbe4
JR
2811 case OFPACT_SET_IP_TTL:
2812 ds_put_format(s, "mod_nw_ttl:%d", ofpact_get_SET_IP_TTL(a)->ttl);
2813 break;
2814
f25d0cf3
BP
2815 case OFPACT_SET_L4_SRC_PORT:
2816 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
2817 break;
2818
2819 case OFPACT_SET_L4_DST_PORT:
2820 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
2821 break;
2822
2823 case OFPACT_REG_MOVE:
2824 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
2825 break;
2826
2827 case OFPACT_REG_LOAD:
2828 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
2829 break;
2830
bd85dac1
AZ
2831 case OFPACT_STACK_PUSH:
2832 nxm_format_stack_push(ofpact_get_STACK_PUSH(a), s);
2833 break;
2834
2835 case OFPACT_STACK_POP:
2836 nxm_format_stack_pop(ofpact_get_STACK_POP(a), s);
2837 break;
2838
f25d0cf3 2839 case OFPACT_DEC_TTL:
c2d967a5 2840 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
f25d0cf3
BP
2841 break;
2842
0f3f3c3d
SH
2843 case OFPACT_SET_MPLS_TTL:
2844 ds_put_format(s, "set_mpls_ttl(%"PRIu8")",
2845 ofpact_get_SET_MPLS_TTL(a)->ttl);
2846 break;
2847
b676167a
SH
2848 case OFPACT_DEC_MPLS_TTL:
2849 ds_put_cstr(s, "dec_mpls_ttl");
2850 break;
2851
f25d0cf3
BP
2852 case OFPACT_SET_TUNNEL:
2853 tunnel = ofpact_get_SET_TUNNEL(a);
2854 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
2855 (tunnel->tun_id > UINT32_MAX
2856 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
2857 tunnel->tun_id);
2858 break;
2859
2860 case OFPACT_SET_QUEUE:
2861 ds_put_format(s, "set_queue:%"PRIu32,
2862 ofpact_get_SET_QUEUE(a)->queue_id);
2863 break;
2864
2865 case OFPACT_POP_QUEUE:
2866 ds_put_cstr(s, "pop_queue");
2867 break;
2868
2869 case OFPACT_FIN_TIMEOUT:
2870 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2871 break;
2872
2873 case OFPACT_RESUBMIT:
2874 resubmit = ofpact_get_RESUBMIT(a);
2875 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
c6100d92
BP
2876 ds_put_cstr(s, "resubmit:");
2877 ofputil_format_port(resubmit->in_port, s);
f25d0cf3
BP
2878 } else {
2879 ds_put_format(s, "resubmit(");
2880 if (resubmit->in_port != OFPP_IN_PORT) {
2881 ofputil_format_port(resubmit->in_port, s);
2882 }
2883 ds_put_char(s, ',');
2884 if (resubmit->table_id != 255) {
2885 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2886 }
2887 ds_put_char(s, ')');
2888 }
2889 break;
2890
2891 case OFPACT_LEARN:
2892 learn_format(ofpact_get_LEARN(a), s);
2893 break;
2894
2895 case OFPACT_MULTIPATH:
2896 multipath_format(ofpact_get_MULTIPATH(a), s);
2897 break;
2898
f25d0cf3
BP
2899 case OFPACT_NOTE:
2900 print_note(ofpact_get_NOTE(a), s);
2901 break;
2902
b02475c5
SH
2903 case OFPACT_PUSH_MPLS:
2904 ds_put_format(s, "push_mpls:0x%04"PRIx16,
2905 ntohs(ofpact_get_PUSH_MPLS(a)->ethertype));
2906 break;
2907
2908 case OFPACT_POP_MPLS:
2909 ds_put_format(s, "pop_mpls:0x%04"PRIx16,
2910 ntohs(ofpact_get_POP_MPLS(a)->ethertype));
2911 break;
2912
f25d0cf3
BP
2913 case OFPACT_EXIT:
2914 ds_put_cstr(s, "exit");
2915 break;
8dd54666 2916
29089a54
RL
2917 case OFPACT_SAMPLE:
2918 sample = ofpact_get_SAMPLE(a);
2919 ds_put_format(
2920 s, "sample(probability=%"PRIu16",collector_set_id=%"PRIu32
2921 ",obs_domain_id=%"PRIu32",obs_point_id=%"PRIu32")",
2922 sample->probability, sample->collector_set_id,
2923 sample->obs_domain_id, sample->obs_point_id);
2924 break;
2925
7fdb60a7
SH
2926 case OFPACT_WRITE_ACTIONS: {
2927 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2928 ds_put_format(s, "%s(",
2929 ovs_instruction_name_from_type(
2930 OVSINST_OFPIT11_WRITE_ACTIONS));
2931 ofpacts_format(on->actions, ofpact_nest_get_action_len(on), s);
2932 ds_put_char(s, ')');
2933 break;
2934 }
2935
b19e8793
IY
2936 case OFPACT_CLEAR_ACTIONS:
2937 ds_put_format(s, "%s",
ba0bc9b0 2938 ovs_instruction_name_from_type(
b19e8793
IY
2939 OVSINST_OFPIT11_CLEAR_ACTIONS));
2940 break;
2941
4cceacb9
JS
2942 case OFPACT_WRITE_METADATA:
2943 metadata = ofpact_get_WRITE_METADATA(a);
2944 ds_put_format(s, "%s:%#"PRIx64,
ba0bc9b0 2945 ovs_instruction_name_from_type(
4cceacb9
JS
2946 OVSINST_OFPIT11_WRITE_METADATA),
2947 ntohll(metadata->metadata));
b8266395 2948 if (metadata->mask != OVS_BE64_MAX) {
4cceacb9
JS
2949 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2950 }
2951 break;
2952
8dd54666
IY
2953 case OFPACT_GOTO_TABLE:
2954 ds_put_format(s, "%s:%"PRIu8,
ba0bc9b0 2955 ovs_instruction_name_from_type(
8dd54666
IY
2956 OVSINST_OFPIT11_GOTO_TABLE),
2957 ofpact_get_GOTO_TABLE(a)->table_id);
2958 break;
638a19b0
JR
2959
2960 case OFPACT_METER:
2961 ds_put_format(s, "%s:%"PRIu32,
ba0bc9b0 2962 ovs_instruction_name_from_type(OVSINST_OFPIT13_METER),
638a19b0
JR
2963 ofpact_get_METER(a)->meter_id);
2964 break;
7395c052
NZ
2965
2966 case OFPACT_GROUP:
2967 ds_put_format(s, "group:%"PRIu32,
2968 ofpact_get_GROUP(a)->group_id);
2969 break;
f25d0cf3
BP
2970 }
2971}
2972
2973/* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2974 * 'ofpacts' to 'string'. */
2975void
2976ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2977 struct ds *string)
2978{
f25d0cf3
BP
2979 if (!ofpacts_len) {
2980 ds_put_cstr(string, "drop");
2981 } else {
2982 const struct ofpact *a;
2983
2984 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2985 if (a != ofpacts) {
2986 ds_put_cstr(string, ",");
2987 }
8dd54666 2988
5dca28b5 2989 /* XXX write-actions */
f25d0cf3
BP
2990 ofpact_format(a, string);
2991 }
2992 }
2993}
2994\f
2995/* Internal use by helpers. */
2996
2997void *
2998ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2999{
3000 struct ofpact *ofpact;
3001
3002 ofpact_pad(ofpacts);
3003 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
3004 ofpact_init(ofpact, type, len);
3005 return ofpact;
3006}
3007
3008void
3009ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
3010{
3011 memset(ofpact, 0, len);
3012 ofpact->type = type;
3013 ofpact->compat = OFPUTIL_ACTION_INVALID;
3014 ofpact->len = len;
3015}
3016\f
3017/* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
3018 * starting at 'ofpact'.
3019 *
3020 * This is the correct way to update a variable-length ofpact's length after
3021 * adding the variable-length part of the payload. (See the large comment
3022 * near the end of ofp-actions.h for more information.) */
3023void
3024ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
3025{
cb22974d 3026 ovs_assert(ofpact == ofpacts->l2);
f25d0cf3
BP
3027 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
3028}
3029
3030/* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
3031 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
3032 * client must call this itself after adding the final ofpact to an array of
3033 * them.
3034 *
3035 * (The consequences of failing to call this function are probably not dire.
3036 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
3037 * not dereference it. That's undefined behavior, technically, but it will not
3038 * cause a real problem on common systems. Still, it seems better to call
3039 * it.) */
3040void
3041ofpact_pad(struct ofpbuf *ofpacts)
3042{
3043 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
3044 if (rem) {
3045 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
3046 }
3047}
f5c45121
SH
3048
3049void
3050ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
3051 const void *src)
3052{
3053 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
3054 load->dst.field = mf;
3055 load->dst.ofs = 0;
3056 load->dst.n_bits = mf->n_bits;
3057 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
3058 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);
3059}