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