]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-actions.c
93f6bf77b43d59c0d93fcb998c5f04e55ade6ec7
[mirror_ovs.git] / lib / ofp-actions.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
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 "autopath.h"
20 #include "bundle.h"
21 #include "byte-order.h"
22 #include "compiler.h"
23 #include "dynamic-string.h"
24 #include "learn.h"
25 #include "meta-flow.h"
26 #include "multipath.h"
27 #include "nx-match.h"
28 #include "ofp-util.h"
29 #include "ofpbuf.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 static enum ofperr
39 output_from_openflow10(const struct ofp10_action_output *oao,
40 struct ofpbuf *out)
41 {
42 struct ofpact_output *output;
43
44 output = ofpact_put_OUTPUT(out);
45 output->port = ntohs(oao->port);
46 output->max_len = ntohs(oao->max_len);
47
48 return ofputil_check_output_port(output->port, OFPP_MAX);
49 }
50
51 static enum ofperr
52 enqueue_from_openflow10(const struct ofp_action_enqueue *oae,
53 struct ofpbuf *out)
54 {
55 struct ofpact_enqueue *enqueue;
56
57 enqueue = ofpact_put_ENQUEUE(out);
58 enqueue->port = ntohs(oae->port);
59 enqueue->queue = ntohl(oae->queue_id);
60 if (enqueue->port >= OFPP_MAX && enqueue->port != OFPP_IN_PORT
61 && enqueue->port != OFPP_LOCAL) {
62 return OFPERR_OFPBAC_BAD_OUT_PORT;
63 }
64 return 0;
65 }
66
67 static void
68 resubmit_from_openflow(const struct nx_action_resubmit *nar,
69 struct ofpbuf *out)
70 {
71 struct ofpact_resubmit *resubmit;
72
73 resubmit = ofpact_put_RESUBMIT(out);
74 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT;
75 resubmit->in_port = ntohs(nar->in_port);
76 resubmit->table_id = 0xff;
77 }
78
79 static enum ofperr
80 resubmit_table_from_openflow(const struct nx_action_resubmit *nar,
81 struct ofpbuf *out)
82 {
83 struct ofpact_resubmit *resubmit;
84
85 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
86 return OFPERR_OFPBAC_BAD_ARGUMENT;
87 }
88
89 resubmit = ofpact_put_RESUBMIT(out);
90 resubmit->ofpact.compat = OFPUTIL_NXAST_RESUBMIT_TABLE;
91 resubmit->in_port = ntohs(nar->in_port);
92 resubmit->table_id = nar->table;
93 return 0;
94 }
95
96 static enum ofperr
97 output_reg_from_openflow(const struct nx_action_output_reg *naor,
98 struct ofpbuf *out)
99 {
100 struct ofpact_output_reg *output_reg;
101
102 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
103 return OFPERR_OFPBAC_BAD_ARGUMENT;
104 }
105
106 output_reg = ofpact_put_OUTPUT_REG(out);
107 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
108 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
109 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
110 output_reg->max_len = ntohs(naor->max_len);
111
112 return mf_check_src(&output_reg->src, NULL);
113 }
114
115 static void
116 fin_timeout_from_openflow(const struct nx_action_fin_timeout *naft,
117 struct ofpbuf *out)
118 {
119 struct ofpact_fin_timeout *oft;
120
121 oft = ofpact_put_FIN_TIMEOUT(out);
122 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
123 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
124 }
125
126 static void
127 controller_from_openflow(const struct nx_action_controller *nac,
128 struct ofpbuf *out)
129 {
130 struct ofpact_controller *oc;
131
132 oc = ofpact_put_CONTROLLER(out);
133 oc->max_len = ntohs(nac->max_len);
134 oc->controller_id = ntohs(nac->controller_id);
135 oc->reason = nac->reason;
136 }
137
138 static void
139 note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
140 {
141 struct ofpact_note *note;
142 unsigned int length;
143
144 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
145 note = ofpact_put(out, OFPACT_NOTE,
146 offsetof(struct ofpact_note, data) + length);
147 note->length = length;
148 memcpy(note->data, nan->note, length);
149 }
150
151 static enum ofperr
152 decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
153 {
154 const struct nx_action_header *nah = (const struct nx_action_header *) a;
155 uint16_t len = ntohs(a->header.len);
156
157 if (len < sizeof(struct nx_action_header)) {
158 return OFPERR_OFPBAC_BAD_LEN;
159 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
160 return OFPERR_OFPBAC_BAD_VENDOR;
161 }
162
163 switch (nah->subtype) {
164 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
165 case CONSTANT_HTONS(ENUM): \
166 if (EXTENSIBLE \
167 ? len >= sizeof(struct STRUCT) \
168 : len == sizeof(struct STRUCT)) { \
169 *code = OFPUTIL_##ENUM; \
170 return 0; \
171 } else { \
172 return OFPERR_OFPBAC_BAD_LEN; \
173 } \
174 NOT_REACHED();
175 #include "ofp-util.def"
176
177 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
178 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
179 default:
180 return OFPERR_OFPBAC_BAD_TYPE;
181 }
182 }
183
184 /* Parses 'a' to determine its type. On success stores the correct type into
185 * '*code' and returns 0. On failure returns an OFPERR_* error code and
186 * '*code' is indeterminate.
187 *
188 * The caller must have already verified that 'a''s length is potentially
189 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
190 * ofp_action) and no longer than the amount of space allocated to 'a').
191 *
192 * This function verifies that 'a''s length is correct for the type of action
193 * that it represents. */
194 static enum ofperr
195 decode_openflow10_action(const union ofp_action *a,
196 enum ofputil_action_code *code)
197 {
198 switch (a->type) {
199 case CONSTANT_HTONS(OFPAT10_VENDOR):
200 return decode_nxast_action(a, code);
201
202 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
203 case CONSTANT_HTONS(ENUM): \
204 if (a->header.len == htons(sizeof(struct STRUCT))) { \
205 *code = OFPUTIL_##ENUM; \
206 return 0; \
207 } else { \
208 return OFPERR_OFPBAC_BAD_LEN; \
209 } \
210 break;
211 #include "ofp-util.def"
212
213 default:
214 return OFPERR_OFPBAC_BAD_TYPE;
215 }
216 }
217
218 static enum ofperr
219 ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
220 struct ofpbuf *out)
221 {
222 const struct nx_action_resubmit *nar;
223 const struct nx_action_set_tunnel *nast;
224 const struct nx_action_set_queue *nasq;
225 const struct nx_action_note *nan;
226 const struct nx_action_set_tunnel64 *nast64;
227 struct ofpact_tunnel *tunnel;
228 enum ofperr error = 0;
229
230 switch (code) {
231 case OFPUTIL_ACTION_INVALID:
232 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
233 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
234 #include "ofp-util.def"
235 NOT_REACHED();
236
237 case OFPUTIL_NXAST_RESUBMIT:
238 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
239 break;
240
241 case OFPUTIL_NXAST_SET_TUNNEL:
242 nast = (const struct nx_action_set_tunnel *) a;
243 tunnel = ofpact_put_SET_TUNNEL(out);
244 tunnel->ofpact.compat = code;
245 tunnel->tun_id = ntohl(nast->tun_id);
246 break;
247
248 case OFPUTIL_NXAST_SET_QUEUE:
249 nasq = (const struct nx_action_set_queue *) a;
250 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
251 break;
252
253 case OFPUTIL_NXAST_POP_QUEUE:
254 ofpact_put_POP_QUEUE(out);
255 break;
256
257 case OFPUTIL_NXAST_REG_MOVE:
258 error = nxm_reg_move_from_openflow(
259 (const struct nx_action_reg_move *) a, out);
260 break;
261
262 case OFPUTIL_NXAST_REG_LOAD:
263 error = nxm_reg_load_from_openflow(
264 (const struct nx_action_reg_load *) a, out);
265 break;
266
267 case OFPUTIL_NXAST_NOTE:
268 nan = (const struct nx_action_note *) a;
269 note_from_openflow(nan, out);
270 break;
271
272 case OFPUTIL_NXAST_SET_TUNNEL64:
273 nast64 = (const struct nx_action_set_tunnel64 *) a;
274 tunnel = ofpact_put_SET_TUNNEL(out);
275 tunnel->ofpact.compat = code;
276 tunnel->tun_id = ntohll(nast64->tun_id);
277 break;
278
279 case OFPUTIL_NXAST_MULTIPATH:
280 error = multipath_from_openflow((const struct nx_action_multipath *) a,
281 ofpact_put_MULTIPATH(out));
282 break;
283
284 case OFPUTIL_NXAST_AUTOPATH:
285 error = autopath_from_openflow((const struct nx_action_autopath *) a,
286 ofpact_put_AUTOPATH(out));
287 break;
288
289 case OFPUTIL_NXAST_BUNDLE:
290 case OFPUTIL_NXAST_BUNDLE_LOAD:
291 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
292 break;
293
294 case OFPUTIL_NXAST_OUTPUT_REG:
295 error = output_reg_from_openflow(
296 (const struct nx_action_output_reg *) a, out);
297 break;
298
299 case OFPUTIL_NXAST_RESUBMIT_TABLE:
300 nar = (const struct nx_action_resubmit *) a;
301 error = resubmit_table_from_openflow(nar, out);
302 break;
303
304 case OFPUTIL_NXAST_LEARN:
305 error = learn_from_openflow((const struct nx_action_learn *) a, out);
306 break;
307
308 case OFPUTIL_NXAST_EXIT:
309 ofpact_put_EXIT(out);
310 break;
311
312 case OFPUTIL_NXAST_DEC_TTL:
313 ofpact_put_DEC_TTL(out);
314 break;
315
316 case OFPUTIL_NXAST_FIN_TIMEOUT:
317 fin_timeout_from_openflow(
318 (const struct nx_action_fin_timeout *) a, out);
319 break;
320
321 case OFPUTIL_NXAST_CONTROLLER:
322 controller_from_openflow((const struct nx_action_controller *) a, out);
323 break;
324 }
325
326 return error;
327 }
328
329 static enum ofperr
330 ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
331 {
332 enum ofputil_action_code code;
333 enum ofperr error;
334
335 error = decode_openflow10_action(a, &code);
336 if (error) {
337 return error;
338 }
339
340 switch (code) {
341 case OFPUTIL_ACTION_INVALID:
342 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
343 #include "ofp-util.def"
344 NOT_REACHED();
345
346 case OFPUTIL_OFPAT10_OUTPUT:
347 return output_from_openflow10(&a->output10, out);
348
349 case OFPUTIL_OFPAT10_SET_VLAN_VID:
350 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
351 return OFPERR_OFPBAC_BAD_ARGUMENT;
352 }
353 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
354 break;
355
356 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
357 if (a->vlan_pcp.vlan_pcp & ~7) {
358 return OFPERR_OFPBAC_BAD_ARGUMENT;
359 }
360 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
361 break;
362
363 case OFPUTIL_OFPAT10_STRIP_VLAN:
364 ofpact_put_STRIP_VLAN(out);
365 break;
366
367 case OFPUTIL_OFPAT10_SET_DL_SRC:
368 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
369 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
370 break;
371
372 case OFPUTIL_OFPAT10_SET_DL_DST:
373 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
374 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
375 break;
376
377 case OFPUTIL_OFPAT10_SET_NW_SRC:
378 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
379 break;
380
381 case OFPUTIL_OFPAT10_SET_NW_DST:
382 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
383 break;
384
385 case OFPUTIL_OFPAT10_SET_NW_TOS:
386 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
387 return OFPERR_OFPBAC_BAD_ARGUMENT;
388 }
389 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
390 break;
391
392 case OFPUTIL_OFPAT10_SET_TP_SRC:
393 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
394 break;
395
396 case OFPUTIL_OFPAT10_SET_TP_DST:
397 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
398
399 break;
400
401 case OFPUTIL_OFPAT10_ENQUEUE:
402 error = enqueue_from_openflow10((const struct ofp_action_enqueue *) a,
403 out);
404 break;
405
406 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
407 #include "ofp-util.def"
408 return ofpact_from_nxast(a, code, out);
409 }
410
411 return error;
412 }
413
414 static inline union ofp_action *
415 action_next(const union ofp_action *a)
416 {
417 return ((union ofp_action *) (void *)
418 ((uint8_t *) a + ntohs(a->header.len)));
419 }
420
421 static inline bool
422 action_is_valid(const union ofp_action *a, size_t n_actions)
423 {
424 uint16_t len = ntohs(a->header.len);
425 return (!(len % OFP_ACTION_ALIGN)
426 && len >= sizeof *a
427 && len / sizeof *a <= n_actions);
428 }
429
430 /* This macro is careful to check for actions with bad lengths. */
431 #define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
432 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
433 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
434 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
435 (ITER) = action_next(ITER)))
436
437 static enum ofperr
438 ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
439 struct ofpbuf *out)
440 {
441 const union ofp_action *a;
442 size_t left;
443
444 ACTION_FOR_EACH (a, left, in, n_in) {
445 enum ofperr error = ofpact_from_openflow10(a, out);
446 if (error) {
447 VLOG_WARN_RL(&rl, "bad action at offset %td (%s)",
448 (a - in) * sizeof *a, ofperr_get_name(error));
449 return error;
450 }
451 }
452 if (left) {
453 if (!VLOG_DROP_WARN(&rl)) {
454 struct ds s;
455
456 ds_init(&s);
457 ds_put_hex_dump(&s, in, n_in * sizeof *a, 0, false);
458 VLOG_WARN("bad action format at offset %#x:\n%s",
459 (n_in - left) * sizeof *a, ds_cstr(&s));
460 ds_destroy(&s);
461 }
462 return OFPERR_OFPBAC_BAD_LEN;
463 }
464
465 ofpact_pad(out);
466 return 0;
467 }
468
469 static enum ofperr
470 ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
471 struct ofpbuf *ofpacts,
472 enum ofperr (*translate)(const union ofp_action *actions,
473 size_t n_actions,
474 struct ofpbuf *ofpacts))
475 {
476 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
477 const union ofp_action *actions;
478 enum ofperr error;
479
480 ofpbuf_clear(ofpacts);
481
482 if (actions_len % OFP_ACTION_ALIGN != 0) {
483 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
484 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
485 return OFPERR_OFPBRC_BAD_LEN;
486 }
487
488 actions = ofpbuf_try_pull(openflow, actions_len);
489 if (actions == NULL) {
490 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
491 "remaining message length (%zu)",
492 actions_len, openflow->size);
493 return OFPERR_OFPBRC_BAD_LEN;
494 }
495
496 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
497 if (error) {
498 ofpbuf_clear(ofpacts);
499 }
500 return error;
501 }
502
503 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
504 * front of 'openflow' into ofpacts. On success, replaces any existing content
505 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
506 * Returns 0 if successful, otherwise an OpenFlow error.
507 *
508 * This function does not check that the actions are valid in a given context.
509 * The caller should do so, with ofpacts_check(). */
510 enum ofperr
511 ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
512 struct ofpbuf *ofpacts)
513 {
514 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
515 ofpacts_from_openflow10);
516 }
517 \f
518 /* OpenFlow 1.1 actions. */
519
520 /* Parses 'a' to determine its type. On success stores the correct type into
521 * '*code' and returns 0. On failure returns an OFPERR_* error code and
522 * '*code' is indeterminate.
523 *
524 * The caller must have already verified that 'a''s length is potentially
525 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
526 * ofp_action) and no longer than the amount of space allocated to 'a').
527 *
528 * This function verifies that 'a''s length is correct for the type of action
529 * that it represents. */
530 static enum ofperr
531 decode_openflow11_action(const union ofp_action *a,
532 enum ofputil_action_code *code)
533 {
534 switch (a->type) {
535 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
536 return decode_nxast_action(a, code);
537
538 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) \
539 case CONSTANT_HTONS(ENUM): \
540 if (a->header.len == htons(sizeof(struct STRUCT))) { \
541 *code = OFPUTIL_##ENUM; \
542 return 0; \
543 } else { \
544 return OFPERR_OFPBAC_BAD_LEN; \
545 } \
546 break;
547 #include "ofp-util.def"
548
549 default:
550 return OFPERR_OFPBAC_BAD_TYPE;
551 }
552 }
553
554 static enum ofperr
555 output_from_openflow11(const struct ofp11_action_output *oao,
556 struct ofpbuf *out)
557 {
558 struct ofpact_output *output;
559 enum ofperr error;
560
561 output = ofpact_put_OUTPUT(out);
562 output->max_len = ntohs(oao->max_len);
563
564 error = ofputil_port_from_ofp11(oao->port, &output->port);
565 if (error) {
566 return error;
567 }
568
569 return ofputil_check_output_port(output->port, OFPP_MAX);
570 }
571
572 static enum ofperr
573 ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
574 {
575 enum ofputil_action_code code;
576 enum ofperr error;
577
578 error = decode_openflow11_action(a, &code);
579 if (error) {
580 return error;
581 }
582
583 switch (code) {
584 case OFPUTIL_ACTION_INVALID:
585 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
586 #include "ofp-util.def"
587 NOT_REACHED();
588
589 case OFPUTIL_OFPAT11_OUTPUT:
590 return output_from_openflow11((const struct ofp11_action_output *) a,
591 out);
592
593 case OFPUTIL_OFPAT11_SET_VLAN_VID:
594 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
595 return OFPERR_OFPBAC_BAD_ARGUMENT;
596 }
597 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
598 break;
599
600 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
601 if (a->vlan_pcp.vlan_pcp & ~7) {
602 return OFPERR_OFPBAC_BAD_ARGUMENT;
603 }
604 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
605 break;
606
607 case OFPUTIL_OFPAT11_SET_DL_SRC:
608 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
609 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
610 break;
611
612 case OFPUTIL_OFPAT11_SET_DL_DST:
613 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
614 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
615 break;
616
617 case OFPUTIL_OFPAT11_SET_NW_SRC:
618 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
619 break;
620
621 case OFPUTIL_OFPAT11_SET_NW_DST:
622 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
623 break;
624
625 case OFPUTIL_OFPAT11_SET_NW_TOS:
626 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
627 return OFPERR_OFPBAC_BAD_ARGUMENT;
628 }
629 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
630 break;
631
632 case OFPUTIL_OFPAT11_SET_TP_SRC:
633 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
634 break;
635
636 case OFPUTIL_OFPAT11_SET_TP_DST:
637 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
638 break;
639
640 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
641 #include "ofp-util.def"
642 return ofpact_from_nxast(a, code, out);
643 }
644
645 return error;
646 }
647
648 static enum ofperr
649 ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
650 struct ofpbuf *out)
651 {
652 const union ofp_action *a;
653 size_t left;
654
655 ACTION_FOR_EACH (a, left, in, n_in) {
656 enum ofperr error = ofpact_from_openflow11(a, out);
657 if (error) {
658 VLOG_WARN_RL(&rl, "bad action at offset %td (%s)",
659 (a - in) * sizeof *a, ofperr_get_name(error));
660 return error;
661 }
662 }
663 if (left) {
664 VLOG_WARN_RL(&rl, "bad action format at offset %zu",
665 (n_in - left) * sizeof *a);
666 return OFPERR_OFPBAC_BAD_LEN;
667 }
668
669 return 0;
670 }
671 \f
672 /* OpenFlow 1.1 instructions. */
673
674 #define OVS_INSTRUCTIONS \
675 DEFINE_INST(OFPIT11_GOTO_TABLE, \
676 ofp11_instruction_goto_table, false, \
677 "goto_table") \
678 \
679 DEFINE_INST(OFPIT11_WRITE_METADATA, \
680 ofp11_instruction_write_metadata, false, \
681 "write_metadata") \
682 \
683 DEFINE_INST(OFPIT11_WRITE_ACTIONS, \
684 ofp11_instruction_actions, true, \
685 "write_actions") \
686 \
687 DEFINE_INST(OFPIT11_APPLY_ACTIONS, \
688 ofp11_instruction_actions, true, \
689 "apply_actions") \
690 \
691 DEFINE_INST(OFPIT11_CLEAR_ACTIONS, \
692 ofp11_instruction, false, \
693 "clear_actions")
694
695 enum ovs_instruction_type {
696 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) OVSINST_##ENUM,
697 OVS_INSTRUCTIONS
698 #undef DEFINE_INST
699 };
700
701 enum {
702 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
703 N_OVS_INSTRUCTIONS = OVS_INSTRUCTIONS
704 #undef DEFINE_INST
705 };
706
707 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
708 static inline void \
709 instruction_init_##ENUM(struct STRUCT *s) \
710 { \
711 memset(s, 0, sizeof *s); \
712 s->type = htons(ENUM); \
713 s->len = htons(sizeof *s); \
714 } \
715 \
716 static inline struct STRUCT * \
717 instruction_put_##ENUM(struct ofpbuf *buf) \
718 { \
719 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
720 instruction_init_##ENUM(s); \
721 return s; \
722 }
723 OVS_INSTRUCTIONS
724 #undef DEFINE_INST
725
726 static inline struct ofp11_instruction *
727 instruction_next(const struct ofp11_instruction *inst)
728 {
729 return ((struct ofp11_instruction *) (void *)
730 ((uint8_t *) inst + ntohs(inst->len)));
731 }
732
733 static inline bool
734 instruction_is_valid(const struct ofp11_instruction *inst,
735 size_t n_instructions)
736 {
737 uint16_t len = ntohs(inst->len);
738 return (!(len % OFP11_INSTRUCTION_ALIGN)
739 && len >= sizeof *inst
740 && len / sizeof *inst <= n_instructions);
741 }
742
743 /* This macro is careful to check for instructions with bad lengths. */
744 #define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
745 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
746 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
747 ((LEFT) -= (ntohs((ITER)->len) \
748 / sizeof(struct ofp11_instruction)), \
749 (ITER) = instruction_next(ITER)))
750
751 static enum ofperr
752 decode_openflow11_instruction(const struct ofp11_instruction *inst,
753 enum ovs_instruction_type *type)
754 {
755 uint16_t len = ntohs(inst->len);
756
757 switch (inst->type) {
758 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
759 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
760
761 #define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
762 case CONSTANT_HTONS(ENUM): \
763 if (EXTENSIBLE \
764 ? len >= sizeof(struct STRUCT) \
765 : len == sizeof(struct STRUCT)) { \
766 *type = OVSINST_##ENUM; \
767 return 0; \
768 } else { \
769 return OFPERR_OFPBIC_BAD_LEN; \
770 }
771 OVS_INSTRUCTIONS
772 #undef DEFINE_INST
773
774 default:
775 return OFPERR_OFPBIC_UNKNOWN_INST;
776 }
777 }
778
779 static enum ofperr
780 decode_openflow11_instructions(const struct ofp11_instruction insts[],
781 size_t n_insts,
782 const struct ofp11_instruction *out[])
783 {
784 const struct ofp11_instruction *inst;
785 size_t left;
786
787 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
788 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
789 enum ovs_instruction_type type;
790 enum ofperr error;
791
792 error = decode_openflow11_instruction(inst, &type);
793 if (error) {
794 return error;
795 }
796
797 if (out[type]) {
798 return OFPERR_NXBIC_DUP_TYPE;
799 }
800 out[type] = inst;
801 }
802
803 if (left) {
804 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
805 (n_insts - left) * sizeof *inst);
806 return OFPERR_OFPBIC_BAD_LEN;
807 }
808 return 0;
809 }
810
811 static void
812 get_actions_from_instruction(const struct ofp11_instruction *inst,
813 const union ofp_action **actions,
814 size_t *n_actions)
815 {
816 *actions = (const union ofp_action *) (inst + 1);
817 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
818 }
819
820 /* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
821 * front of 'openflow' into ofpacts. On success, replaces any existing content
822 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
823 * Returns 0 if successful, otherwise an OpenFlow error.
824 *
825 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
826 * instructions, so you should call ofpacts_pull_openflow11_instructions()
827 * instead of this function.
828 *
829 * This function does not check that the actions are valid in a given context.
830 * The caller should do so, with ofpacts_check(). */
831 enum ofperr
832 ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
833 unsigned int actions_len,
834 struct ofpbuf *ofpacts)
835 {
836 enum ofperr error;
837
838 error = ofpacts_pull_actions(openflow, actions_len, ofpacts,
839 ofpacts_from_openflow11);
840 if (!error) {
841 ofpact_pad(ofpacts);
842 }
843 return error;
844 }
845
846 enum ofperr
847 ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
848 unsigned int instructions_len,
849 struct ofpbuf *ofpacts)
850 {
851 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
852 const struct ofp11_instruction *instructions;
853 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
854 enum ofperr error;
855
856 ofpbuf_clear(ofpacts);
857
858 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
859 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
860 "multiple of %d",
861 instructions_len, OFP11_INSTRUCTION_ALIGN);
862 error = OFPERR_OFPBIC_BAD_LEN;
863 goto exit;
864 }
865
866 instructions = ofpbuf_try_pull(openflow, instructions_len);
867 if (instructions == NULL) {
868 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
869 "remaining message length (%zu)",
870 instructions_len, openflow->size);
871 error = OFPERR_OFPBIC_BAD_LEN;
872 goto exit;
873 }
874
875 error = decode_openflow11_instructions(
876 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
877 insts);
878 if (error) {
879 goto exit;
880 }
881
882 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
883 const union ofp_action *actions;
884 size_t n_actions;
885
886 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
887 &actions, &n_actions);
888 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
889 if (error) {
890 goto exit;
891 }
892 }
893
894 ofpact_pad(ofpacts);
895
896 if (insts[OVSINST_OFPIT11_GOTO_TABLE] ||
897 insts[OVSINST_OFPIT11_WRITE_METADATA] ||
898 insts[OVSINST_OFPIT11_WRITE_ACTIONS] ||
899 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
900 error = OFPERR_OFPBIC_UNSUP_INST;
901 goto exit;
902 }
903
904 exit:
905 if (error) {
906 ofpbuf_clear(ofpacts);
907 }
908 return error;
909 }
910 \f
911 static enum ofperr
912 ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
913 {
914 const struct ofpact_enqueue *enqueue;
915
916 switch (a->type) {
917 case OFPACT_OUTPUT:
918 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
919 max_ports);
920
921 case OFPACT_CONTROLLER:
922 return 0;
923
924 case OFPACT_ENQUEUE:
925 enqueue = ofpact_get_ENQUEUE(a);
926 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
927 && enqueue->port != OFPP_LOCAL) {
928 return OFPERR_OFPBAC_BAD_OUT_PORT;
929 }
930 return 0;
931
932 case OFPACT_OUTPUT_REG:
933 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
934
935 case OFPACT_BUNDLE:
936 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
937
938 case OFPACT_SET_VLAN_VID:
939 case OFPACT_SET_VLAN_PCP:
940 case OFPACT_STRIP_VLAN:
941 case OFPACT_SET_ETH_SRC:
942 case OFPACT_SET_ETH_DST:
943 case OFPACT_SET_IPV4_SRC:
944 case OFPACT_SET_IPV4_DST:
945 case OFPACT_SET_IPV4_DSCP:
946 case OFPACT_SET_L4_SRC_PORT:
947 case OFPACT_SET_L4_DST_PORT:
948 return 0;
949
950 case OFPACT_REG_MOVE:
951 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
952
953 case OFPACT_REG_LOAD:
954 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
955
956 case OFPACT_DEC_TTL:
957 case OFPACT_SET_TUNNEL:
958 case OFPACT_SET_QUEUE:
959 case OFPACT_POP_QUEUE:
960 case OFPACT_FIN_TIMEOUT:
961 case OFPACT_RESUBMIT:
962 return 0;
963
964 case OFPACT_LEARN:
965 return learn_check(ofpact_get_LEARN(a), flow);
966
967 case OFPACT_MULTIPATH:
968 return multipath_check(ofpact_get_MULTIPATH(a), flow);
969
970 case OFPACT_AUTOPATH:
971 return autopath_check(ofpact_get_AUTOPATH(a), flow);
972
973 case OFPACT_NOTE:
974 case OFPACT_EXIT:
975 return 0;
976
977 default:
978 NOT_REACHED();
979 }
980 }
981
982 /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
983 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
984 * switch with no more than 'max_ports' ports. */
985 enum ofperr
986 ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
987 const struct flow *flow, int max_ports)
988 {
989 const struct ofpact *a;
990
991 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
992 enum ofperr error = ofpact_check__(a, flow, max_ports);
993 if (error) {
994 return error;
995 }
996 }
997
998 return 0;
999 }
1000 \f
1001 /* Converting ofpacts to Nicira OpenFlow extensions. */
1002
1003 static void
1004 ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1005 struct ofpbuf *out)
1006 {
1007 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1008
1009 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1010 output_reg->src.n_bits);
1011 naor->src = htonl(output_reg->src.field->nxm_header);
1012 naor->max_len = htons(output_reg->max_len);
1013 }
1014
1015 static void
1016 ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1017 struct ofpbuf *out)
1018 {
1019 struct nx_action_resubmit *nar;
1020
1021 if (resubmit->table_id == 0xff
1022 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1023 nar = ofputil_put_NXAST_RESUBMIT(out);
1024 } else {
1025 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1026 nar->table = resubmit->table_id;
1027 }
1028 nar->in_port = htons(resubmit->in_port);
1029 }
1030
1031 static void
1032 ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1033 struct ofpbuf *out)
1034 {
1035 uint64_t tun_id = tunnel->tun_id;
1036
1037 if (tun_id <= UINT32_MAX
1038 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1039 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1040 } else {
1041 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1042 }
1043 }
1044
1045 static void
1046 ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1047 {
1048 size_t start_ofs = out->size;
1049 struct nx_action_note *nan;
1050 unsigned int remainder;
1051 unsigned int len;
1052
1053 nan = ofputil_put_NXAST_NOTE(out);
1054 out->size -= sizeof nan->note;
1055
1056 ofpbuf_put(out, note->data, note->length);
1057
1058 len = out->size - start_ofs;
1059 remainder = len % OFP_ACTION_ALIGN;
1060 if (remainder) {
1061 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1062 }
1063 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1064 nan->len = htons(out->size - start_ofs);
1065 }
1066
1067 static void
1068 ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1069 struct ofpbuf *out)
1070 {
1071 struct nx_action_controller *nac;
1072
1073 nac = ofputil_put_NXAST_CONTROLLER(out);
1074 nac->max_len = htons(oc->max_len);
1075 nac->controller_id = htons(oc->controller_id);
1076 nac->reason = oc->reason;
1077 }
1078
1079 static void
1080 ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1081 struct ofpbuf *out)
1082 {
1083 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1084 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1085 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1086 }
1087
1088 static void
1089 ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1090 {
1091 switch (a->type) {
1092 case OFPACT_CONTROLLER:
1093 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1094 break;
1095
1096 case OFPACT_OUTPUT_REG:
1097 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1098 break;
1099
1100 case OFPACT_BUNDLE:
1101 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1102 break;
1103
1104 case OFPACT_REG_MOVE:
1105 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1106 break;
1107
1108 case OFPACT_REG_LOAD:
1109 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1110 break;
1111
1112 case OFPACT_DEC_TTL:
1113 ofputil_put_NXAST_DEC_TTL(out);
1114 break;
1115
1116 case OFPACT_SET_TUNNEL:
1117 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1118 break;
1119
1120 case OFPACT_SET_QUEUE:
1121 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1122 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1123 break;
1124
1125 case OFPACT_POP_QUEUE:
1126 ofputil_put_NXAST_POP_QUEUE(out);
1127 break;
1128
1129 case OFPACT_FIN_TIMEOUT:
1130 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1131 break;
1132
1133 case OFPACT_RESUBMIT:
1134 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1135 break;
1136
1137 case OFPACT_LEARN:
1138 learn_to_nxast(ofpact_get_LEARN(a), out);
1139 break;
1140
1141 case OFPACT_MULTIPATH:
1142 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1143 break;
1144
1145 case OFPACT_AUTOPATH:
1146 autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1147 break;
1148
1149 case OFPACT_NOTE:
1150 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1151 break;
1152
1153 case OFPACT_EXIT:
1154 ofputil_put_NXAST_EXIT(out);
1155 break;
1156
1157 case OFPACT_OUTPUT:
1158 case OFPACT_ENQUEUE:
1159 case OFPACT_SET_VLAN_VID:
1160 case OFPACT_SET_VLAN_PCP:
1161 case OFPACT_STRIP_VLAN:
1162 case OFPACT_SET_ETH_SRC:
1163 case OFPACT_SET_ETH_DST:
1164 case OFPACT_SET_IPV4_SRC:
1165 case OFPACT_SET_IPV4_DST:
1166 case OFPACT_SET_IPV4_DSCP:
1167 case OFPACT_SET_L4_SRC_PORT:
1168 case OFPACT_SET_L4_DST_PORT:
1169 NOT_REACHED();
1170 }
1171 }
1172 \f
1173 /* Converting ofpacts to OpenFlow 1.0. */
1174
1175 static void
1176 ofpact_output_to_openflow10(const struct ofpact_output *output,
1177 struct ofpbuf *out)
1178 {
1179 struct ofp10_action_output *oao;
1180
1181 oao = ofputil_put_OFPAT10_OUTPUT(out);
1182 oao->port = htons(output->port);
1183 oao->max_len = htons(output->max_len);
1184 }
1185
1186 static void
1187 ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1188 struct ofpbuf *out)
1189 {
1190 struct ofp_action_enqueue *oae;
1191
1192 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1193 oae->port = htons(enqueue->port);
1194 oae->queue_id = htonl(enqueue->queue);
1195 }
1196
1197 static void
1198 ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1199 {
1200 switch (a->type) {
1201 case OFPACT_OUTPUT:
1202 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1203 break;
1204
1205 case OFPACT_ENQUEUE:
1206 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1207 break;
1208
1209 case OFPACT_SET_VLAN_VID:
1210 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1211 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1212 break;
1213
1214 case OFPACT_SET_VLAN_PCP:
1215 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1216 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1217 break;
1218
1219 case OFPACT_STRIP_VLAN:
1220 ofputil_put_OFPAT10_STRIP_VLAN(out);
1221 break;
1222
1223 case OFPACT_SET_ETH_SRC:
1224 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1225 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1226 break;
1227
1228 case OFPACT_SET_ETH_DST:
1229 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1230 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1231 break;
1232
1233 case OFPACT_SET_IPV4_SRC:
1234 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1235 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1236 break;
1237
1238 case OFPACT_SET_IPV4_DST:
1239 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1240 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1241 break;
1242
1243 case OFPACT_SET_IPV4_DSCP:
1244 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1245 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1246 break;
1247
1248 case OFPACT_SET_L4_SRC_PORT:
1249 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1250 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1251 break;
1252
1253 case OFPACT_SET_L4_DST_PORT:
1254 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1255 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1256 break;
1257
1258 case OFPACT_CONTROLLER:
1259 case OFPACT_OUTPUT_REG:
1260 case OFPACT_BUNDLE:
1261 case OFPACT_REG_MOVE:
1262 case OFPACT_REG_LOAD:
1263 case OFPACT_DEC_TTL:
1264 case OFPACT_SET_TUNNEL:
1265 case OFPACT_SET_QUEUE:
1266 case OFPACT_POP_QUEUE:
1267 case OFPACT_FIN_TIMEOUT:
1268 case OFPACT_RESUBMIT:
1269 case OFPACT_LEARN:
1270 case OFPACT_MULTIPATH:
1271 case OFPACT_AUTOPATH:
1272 case OFPACT_NOTE:
1273 case OFPACT_EXIT:
1274 ofpact_to_nxast(a, out);
1275 break;
1276 }
1277 }
1278
1279 /* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
1280 * actions in 'openflow', appending the actions to any existing data in
1281 * 'openflow'. */
1282 void
1283 ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1284 struct ofpbuf *openflow)
1285 {
1286 const struct ofpact *a;
1287
1288 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1289 ofpact_to_openflow10(a, openflow);
1290 }
1291 }
1292 \f
1293 /* Converting ofpacts to OpenFlow 1.1. */
1294
1295 static void
1296 ofpact_output_to_openflow11(const struct ofpact_output *output,
1297 struct ofpbuf *out)
1298 {
1299 struct ofp11_action_output *oao;
1300
1301 oao = ofputil_put_OFPAT11_OUTPUT(out);
1302 oao->port = ofputil_port_to_ofp11(output->port);
1303 oao->max_len = htons(output->max_len);
1304 }
1305
1306 static void
1307 ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1308 {
1309 switch (a->type) {
1310 case OFPACT_OUTPUT:
1311 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1312
1313 case OFPACT_ENQUEUE:
1314 /* XXX */
1315 break;
1316
1317 case OFPACT_SET_VLAN_VID:
1318 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1319 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1320 break;
1321
1322 case OFPACT_SET_VLAN_PCP:
1323 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1324 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1325 break;
1326
1327 case OFPACT_STRIP_VLAN:
1328 /* XXX */
1329 break;
1330
1331 case OFPACT_SET_ETH_SRC:
1332 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1333 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1334 break;
1335
1336 case OFPACT_SET_ETH_DST:
1337 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1338 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1339 break;
1340
1341 case OFPACT_SET_IPV4_SRC:
1342 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1343 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1344 break;
1345
1346 case OFPACT_SET_IPV4_DST:
1347 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1348 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1349 break;
1350
1351 case OFPACT_SET_IPV4_DSCP:
1352 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1353 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1354 break;
1355
1356 case OFPACT_SET_L4_SRC_PORT:
1357 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1358 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1359 break;
1360
1361 case OFPACT_SET_L4_DST_PORT:
1362 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1363 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1364 break;
1365
1366 case OFPACT_CONTROLLER:
1367 case OFPACT_OUTPUT_REG:
1368 case OFPACT_BUNDLE:
1369 case OFPACT_REG_MOVE:
1370 case OFPACT_REG_LOAD:
1371 case OFPACT_DEC_TTL:
1372 case OFPACT_SET_TUNNEL:
1373 case OFPACT_SET_QUEUE:
1374 case OFPACT_POP_QUEUE:
1375 case OFPACT_FIN_TIMEOUT:
1376 case OFPACT_RESUBMIT:
1377 case OFPACT_LEARN:
1378 case OFPACT_MULTIPATH:
1379 case OFPACT_AUTOPATH:
1380 case OFPACT_NOTE:
1381 case OFPACT_EXIT:
1382 ofpact_to_nxast(a, out);
1383 break;
1384 }
1385 }
1386
1387 /* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1388 * 1.1 actions in 'openflow', appending the actions to any existing data in
1389 * 'openflow'. */
1390 void
1391 ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1392 size_t ofpacts_len, struct ofpbuf *openflow)
1393 {
1394 const struct ofpact *a;
1395
1396 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1397 ofpact_to_openflow11(a, openflow);
1398 }
1399 }
1400
1401 void
1402 ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1403 size_t ofpacts_len,
1404 struct ofpbuf *openflow)
1405 {
1406 struct ofp11_instruction_actions *oia;
1407 size_t ofs;
1408
1409 /* Put an OFPIT11_APPLY_ACTIONS instruction and fill it in. */
1410 ofs = openflow->size;
1411 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1412 ofpacts_put_openflow11_actions(ofpacts, ofpacts_len, openflow);
1413
1414 /* Update the instruction's length (or, if it's empty, delete it). */
1415 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1416 if (openflow->size > ofs + sizeof *oia) {
1417 oia->len = htons(openflow->size - ofs);
1418 } else {
1419 openflow->size = ofs;
1420 }
1421 }
1422 \f
1423 /* Returns true if 'action' outputs to 'port', false otherwise. */
1424 static bool
1425 ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1426 {
1427 switch (ofpact->type) {
1428 case OFPACT_OUTPUT:
1429 return ofpact_get_OUTPUT(ofpact)->port == port;
1430 case OFPACT_ENQUEUE:
1431 return ofpact_get_ENQUEUE(ofpact)->port == port;
1432 case OFPACT_CONTROLLER:
1433 return port == OFPP_CONTROLLER;
1434
1435 case OFPACT_OUTPUT_REG:
1436 case OFPACT_BUNDLE:
1437 case OFPACT_SET_VLAN_VID:
1438 case OFPACT_SET_VLAN_PCP:
1439 case OFPACT_STRIP_VLAN:
1440 case OFPACT_SET_ETH_SRC:
1441 case OFPACT_SET_ETH_DST:
1442 case OFPACT_SET_IPV4_SRC:
1443 case OFPACT_SET_IPV4_DST:
1444 case OFPACT_SET_IPV4_DSCP:
1445 case OFPACT_SET_L4_SRC_PORT:
1446 case OFPACT_SET_L4_DST_PORT:
1447 case OFPACT_REG_MOVE:
1448 case OFPACT_REG_LOAD:
1449 case OFPACT_DEC_TTL:
1450 case OFPACT_SET_TUNNEL:
1451 case OFPACT_SET_QUEUE:
1452 case OFPACT_POP_QUEUE:
1453 case OFPACT_FIN_TIMEOUT:
1454 case OFPACT_RESUBMIT:
1455 case OFPACT_LEARN:
1456 case OFPACT_MULTIPATH:
1457 case OFPACT_AUTOPATH:
1458 case OFPACT_NOTE:
1459 case OFPACT_EXIT:
1460 default:
1461 return false;
1462 }
1463 }
1464
1465 /* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1466 * to 'port', false otherwise. */
1467 bool
1468 ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1469 uint16_t port)
1470 {
1471 const struct ofpact *a;
1472
1473 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1474 if (ofpact_outputs_to_port(a, port)) {
1475 return true;
1476 }
1477 }
1478
1479 return false;
1480 }
1481
1482 bool
1483 ofpacts_equal(const struct ofpact *a, size_t a_len,
1484 const struct ofpact *b, size_t b_len)
1485 {
1486 return a_len == b_len && !memcmp(a, b, a_len);
1487 }
1488 \f
1489 /* Formatting ofpacts. */
1490
1491 static void
1492 print_note(const struct ofpact_note *note, struct ds *string)
1493 {
1494 size_t i;
1495
1496 ds_put_cstr(string, "note:");
1497 for (i = 0; i < note->length; i++) {
1498 if (i) {
1499 ds_put_char(string, '.');
1500 }
1501 ds_put_format(string, "%02"PRIx8, note->data[i]);
1502 }
1503 }
1504
1505 static void
1506 print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1507 struct ds *s)
1508 {
1509 ds_put_cstr(s, "fin_timeout(");
1510 if (fin_timeout->fin_idle_timeout) {
1511 ds_put_format(s, "idle_timeout=%"PRIu16",",
1512 fin_timeout->fin_idle_timeout);
1513 }
1514 if (fin_timeout->fin_hard_timeout) {
1515 ds_put_format(s, "hard_timeout=%"PRIu16",",
1516 fin_timeout->fin_hard_timeout);
1517 }
1518 ds_chomp(s, ',');
1519 ds_put_char(s, ')');
1520 }
1521
1522 static void
1523 ofpact_format(const struct ofpact *a, struct ds *s)
1524 {
1525 const struct ofpact_enqueue *enqueue;
1526 const struct ofpact_resubmit *resubmit;
1527 const struct ofpact_autopath *autopath;
1528 const struct ofpact_controller *controller;
1529 const struct ofpact_tunnel *tunnel;
1530 uint16_t port;
1531
1532 switch (a->type) {
1533 case OFPACT_OUTPUT:
1534 port = ofpact_get_OUTPUT(a)->port;
1535 if (port < OFPP_MAX) {
1536 ds_put_format(s, "output:%"PRIu16, port);
1537 } else {
1538 ofputil_format_port(port, s);
1539 if (port == OFPP_CONTROLLER) {
1540 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1541 }
1542 }
1543 break;
1544
1545 case OFPACT_CONTROLLER:
1546 controller = ofpact_get_CONTROLLER(a);
1547 if (controller->reason == OFPR_ACTION &&
1548 controller->controller_id == 0) {
1549 ds_put_format(s, "CONTROLLER:%"PRIu16,
1550 ofpact_get_CONTROLLER(a)->max_len);
1551 } else {
1552 enum ofp_packet_in_reason reason = controller->reason;
1553
1554 ds_put_cstr(s, "controller(");
1555 if (reason != OFPR_ACTION) {
1556 ds_put_format(s, "reason=%s,",
1557 ofputil_packet_in_reason_to_string(reason));
1558 }
1559 if (controller->max_len != UINT16_MAX) {
1560 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1561 }
1562 if (controller->controller_id != 0) {
1563 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1564 }
1565 ds_chomp(s, ',');
1566 ds_put_char(s, ')');
1567 }
1568 break;
1569
1570 case OFPACT_ENQUEUE:
1571 enqueue = ofpact_get_ENQUEUE(a);
1572 ds_put_format(s, "enqueue:");
1573 ofputil_format_port(enqueue->port, s);
1574 ds_put_format(s, "q%"PRIu32, enqueue->queue);
1575 break;
1576
1577 case OFPACT_OUTPUT_REG:
1578 ds_put_cstr(s, "output:");
1579 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1580 break;
1581
1582 case OFPACT_BUNDLE:
1583 bundle_format(ofpact_get_BUNDLE(a), s);
1584 break;
1585
1586 case OFPACT_SET_VLAN_VID:
1587 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1588 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1589 break;
1590
1591 case OFPACT_SET_VLAN_PCP:
1592 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1593 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1594 break;
1595
1596 case OFPACT_STRIP_VLAN:
1597 ds_put_cstr(s, "strip_vlan");
1598 break;
1599
1600 case OFPACT_SET_ETH_SRC:
1601 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1602 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1603 break;
1604
1605 case OFPACT_SET_ETH_DST:
1606 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1607 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1608 break;
1609
1610 case OFPACT_SET_IPV4_SRC:
1611 ds_put_format(s, "mod_nw_src:"IP_FMT,
1612 IP_ARGS(&ofpact_get_SET_IPV4_SRC(a)->ipv4));
1613 break;
1614
1615 case OFPACT_SET_IPV4_DST:
1616 ds_put_format(s, "mod_nw_dst:"IP_FMT,
1617 IP_ARGS(&ofpact_get_SET_IPV4_DST(a)->ipv4));
1618 break;
1619
1620 case OFPACT_SET_IPV4_DSCP:
1621 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1622 break;
1623
1624 case OFPACT_SET_L4_SRC_PORT:
1625 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1626 break;
1627
1628 case OFPACT_SET_L4_DST_PORT:
1629 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1630 break;
1631
1632 case OFPACT_REG_MOVE:
1633 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1634 break;
1635
1636 case OFPACT_REG_LOAD:
1637 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1638 break;
1639
1640 case OFPACT_DEC_TTL:
1641 ds_put_cstr(s, "dec_ttl");
1642 break;
1643
1644 case OFPACT_SET_TUNNEL:
1645 tunnel = ofpact_get_SET_TUNNEL(a);
1646 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1647 (tunnel->tun_id > UINT32_MAX
1648 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1649 tunnel->tun_id);
1650 break;
1651
1652 case OFPACT_SET_QUEUE:
1653 ds_put_format(s, "set_queue:%"PRIu32,
1654 ofpact_get_SET_QUEUE(a)->queue_id);
1655 break;
1656
1657 case OFPACT_POP_QUEUE:
1658 ds_put_cstr(s, "pop_queue");
1659 break;
1660
1661 case OFPACT_FIN_TIMEOUT:
1662 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
1663 break;
1664
1665 case OFPACT_RESUBMIT:
1666 resubmit = ofpact_get_RESUBMIT(a);
1667 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
1668 ds_put_format(s, "resubmit:%"PRIu16, resubmit->in_port);
1669 } else {
1670 ds_put_format(s, "resubmit(");
1671 if (resubmit->in_port != OFPP_IN_PORT) {
1672 ofputil_format_port(resubmit->in_port, s);
1673 }
1674 ds_put_char(s, ',');
1675 if (resubmit->table_id != 255) {
1676 ds_put_format(s, "%"PRIu8, resubmit->table_id);
1677 }
1678 ds_put_char(s, ')');
1679 }
1680 break;
1681
1682 case OFPACT_LEARN:
1683 learn_format(ofpact_get_LEARN(a), s);
1684 break;
1685
1686 case OFPACT_MULTIPATH:
1687 multipath_format(ofpact_get_MULTIPATH(a), s);
1688 break;
1689
1690 case OFPACT_AUTOPATH:
1691 autopath = ofpact_get_AUTOPATH(a);
1692 ds_put_format(s, "autopath(%u,", autopath->port);
1693 mf_format_subfield(&autopath->dst, s);
1694 ds_put_char(s, ')');
1695 break;
1696
1697 case OFPACT_NOTE:
1698 print_note(ofpact_get_NOTE(a), s);
1699 break;
1700
1701 case OFPACT_EXIT:
1702 ds_put_cstr(s, "exit");
1703 break;
1704 }
1705 }
1706
1707 /* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
1708 * 'ofpacts' to 'string'. */
1709 void
1710 ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
1711 struct ds *string)
1712 {
1713 ds_put_cstr(string, "actions=");
1714 if (!ofpacts_len) {
1715 ds_put_cstr(string, "drop");
1716 } else {
1717 const struct ofpact *a;
1718
1719 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1720 if (a != ofpacts) {
1721 ds_put_cstr(string, ",");
1722 }
1723 ofpact_format(a, string);
1724 }
1725 }
1726 }
1727 \f
1728 /* Internal use by helpers. */
1729
1730 void *
1731 ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
1732 {
1733 struct ofpact *ofpact;
1734
1735 ofpact_pad(ofpacts);
1736 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
1737 ofpact_init(ofpact, type, len);
1738 return ofpact;
1739 }
1740
1741 void
1742 ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
1743 {
1744 memset(ofpact, 0, len);
1745 ofpact->type = type;
1746 ofpact->compat = OFPUTIL_ACTION_INVALID;
1747 ofpact->len = len;
1748 }
1749 \f
1750 /* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
1751 * starting at 'ofpact'.
1752 *
1753 * This is the correct way to update a variable-length ofpact's length after
1754 * adding the variable-length part of the payload. (See the large comment
1755 * near the end of ofp-actions.h for more information.) */
1756 void
1757 ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
1758 {
1759 assert(ofpact == ofpacts->l2);
1760 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
1761 }
1762
1763 /* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
1764 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
1765 * client must call this itself after adding the final ofpact to an array of
1766 * them.
1767 *
1768 * (The consequences of failing to call this function are probably not dire.
1769 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
1770 * not dereference it. That's undefined behavior, technically, but it will not
1771 * cause a real problem on common systems. Still, it seems better to call
1772 * it.) */
1773 void
1774 ofpact_pad(struct ofpbuf *ofpacts)
1775 {
1776 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
1777 if (rem) {
1778 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
1779 }
1780 }