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