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