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