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