]> git.proxmox.com Git - ovs.git/blame - lib/ofp-actions.c
util: Introduce ovs_assert macro.
[ovs.git] / lib / ofp-actions.c
CommitLineData
f25d0cf3 1/*
6813ee7c 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
f25d0cf3
BP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <config.h>
18#include "ofp-actions.h"
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
32VLOG_DEFINE_THIS_MODULE(ofp_actions);
33
34static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
35\f
36/* Converting OpenFlow 1.0 to ofpacts. */
37
38static enum ofperr
d01c980f 39output_from_openflow10(const struct ofp10_action_output *oao,
f25d0cf3
BP
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
51static enum ofperr
31a9e63f 52enqueue_from_openflow10(const struct ofp10_action_enqueue *oae,
f25d0cf3
BP
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
67static void
68resubmit_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
79static enum ofperr
80resubmit_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
96static enum ofperr
97output_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
115static void
116fin_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
126static void
127controller_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
4cceacb9
JS
138static enum ofperr
139metadata_from_nxast(const struct nx_action_write_metadata *nawm,
140 struct ofpbuf *out)
141{
142 struct ofpact_metadata *om;
143
144 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
145 return OFPERR_NXBRC_MUST_BE_ZERO;
146 }
147
148 om = ofpact_put_WRITE_METADATA(out);
149 om->metadata = nawm->metadata;
150 om->mask = nawm->mask;
151
152 return 0;
153}
154
f25d0cf3
BP
155static void
156note_from_openflow(const struct nx_action_note *nan, struct ofpbuf *out)
157{
158 struct ofpact_note *note;
159 unsigned int length;
160
161 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
162 note = ofpact_put(out, OFPACT_NOTE,
163 offsetof(struct ofpact_note, data) + length);
164 note->length = length;
165 memcpy(note->data, nan->note, length);
166}
167
c2d967a5 168static enum ofperr
7bcb1506 169dec_ttl_from_openflow(struct ofpbuf *out, enum ofputil_action_code compat)
c2d967a5
MM
170{
171 uint16_t id = 0;
172 struct ofpact_cnt_ids *ids;
173 enum ofperr error = 0;
174
175 ids = ofpact_put_DEC_TTL(out);
7bcb1506 176 ids->ofpact.compat = compat;
c2d967a5
MM
177 ids->n_controllers = 1;
178 ofpbuf_put(out, &id, sizeof id);
179 ids = out->l2;
180 ofpact_update_len(out, &ids->ofpact);
181 return error;
182}
183
184static enum ofperr
185dec_ttl_cnt_ids_from_openflow(const struct nx_action_cnt_ids *nac_ids,
186 struct ofpbuf *out)
187{
188 struct ofpact_cnt_ids *ids;
189 size_t ids_size;
190 int i;
191
192 ids = ofpact_put_DEC_TTL(out);
193 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
194 ids->n_controllers = ntohs(nac_ids->n_controllers);
195 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
196
197 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
198 return OFPERR_NXBRC_MUST_BE_ZERO;
199 }
200
201 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
202 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %zu bytes "
203 "allocated for controller ids. %zu bytes are required for "
204 "%"PRIu16" controllers.", ids_size,
205 ids->n_controllers * sizeof(ovs_be16), ids->n_controllers);
206 return OFPERR_OFPBAC_BAD_LEN;
207 }
208
209 for (i = 0; i < ids->n_controllers; i++) {
210 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
211 ofpbuf_put(out, &id, sizeof id);
212 }
213
214 ids = out->l2;
215 ofpact_update_len(out, &ids->ofpact);
216
217 return 0;
218}
219
f25d0cf3
BP
220static enum ofperr
221decode_nxast_action(const union ofp_action *a, enum ofputil_action_code *code)
222{
223 const struct nx_action_header *nah = (const struct nx_action_header *) a;
224 uint16_t len = ntohs(a->header.len);
225
226 if (len < sizeof(struct nx_action_header)) {
227 return OFPERR_OFPBAC_BAD_LEN;
228 } else if (a->vendor.vendor != CONSTANT_HTONL(NX_VENDOR_ID)) {
229 return OFPERR_OFPBAC_BAD_VENDOR;
230 }
231
232 switch (nah->subtype) {
233#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
234 case CONSTANT_HTONS(ENUM): \
235 if (EXTENSIBLE \
236 ? len >= sizeof(struct STRUCT) \
237 : len == sizeof(struct STRUCT)) { \
238 *code = OFPUTIL_##ENUM; \
239 return 0; \
240 } else { \
241 return OFPERR_OFPBAC_BAD_LEN; \
242 } \
243 NOT_REACHED();
244#include "ofp-util.def"
245
246 case CONSTANT_HTONS(NXAST_SNAT__OBSOLETE):
247 case CONSTANT_HTONS(NXAST_DROP_SPOOFED_ARP__OBSOLETE):
248 default:
249 return OFPERR_OFPBAC_BAD_TYPE;
250 }
251}
252
253/* Parses 'a' to determine its type. On success stores the correct type into
254 * '*code' and returns 0. On failure returns an OFPERR_* error code and
255 * '*code' is indeterminate.
256 *
257 * The caller must have already verified that 'a''s length is potentially
258 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
259 * ofp_action) and no longer than the amount of space allocated to 'a').
260 *
261 * This function verifies that 'a''s length is correct for the type of action
262 * that it represents. */
263static enum ofperr
264decode_openflow10_action(const union ofp_action *a,
265 enum ofputil_action_code *code)
266{
267 switch (a->type) {
268 case CONSTANT_HTONS(OFPAT10_VENDOR):
269 return decode_nxast_action(a, code);
270
271#define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
272 case CONSTANT_HTONS(ENUM): \
273 if (a->header.len == htons(sizeof(struct STRUCT))) { \
274 *code = OFPUTIL_##ENUM; \
275 return 0; \
276 } else { \
277 return OFPERR_OFPBAC_BAD_LEN; \
278 } \
279 break;
280#include "ofp-util.def"
281
282 default:
283 return OFPERR_OFPBAC_BAD_TYPE;
284 }
285}
286
287static enum ofperr
d01c980f
BP
288ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
289 struct ofpbuf *out)
f25d0cf3
BP
290{
291 const struct nx_action_resubmit *nar;
292 const struct nx_action_set_tunnel *nast;
293 const struct nx_action_set_queue *nasq;
294 const struct nx_action_note *nan;
295 const struct nx_action_set_tunnel64 *nast64;
4cceacb9 296 const struct nx_action_write_metadata *nawm;
f25d0cf3 297 struct ofpact_tunnel *tunnel;
d01c980f 298 enum ofperr error = 0;
f25d0cf3
BP
299
300 switch (code) {
301 case OFPUTIL_ACTION_INVALID:
d01c980f 302#define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
3ddcaf2d 303#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
d01c980f 304#include "ofp-util.def"
f25d0cf3
BP
305 NOT_REACHED();
306
f25d0cf3
BP
307 case OFPUTIL_NXAST_RESUBMIT:
308 resubmit_from_openflow((const struct nx_action_resubmit *) a, out);
309 break;
310
311 case OFPUTIL_NXAST_SET_TUNNEL:
312 nast = (const struct nx_action_set_tunnel *) a;
313 tunnel = ofpact_put_SET_TUNNEL(out);
314 tunnel->ofpact.compat = code;
315 tunnel->tun_id = ntohl(nast->tun_id);
316 break;
317
4cceacb9
JS
318 case OFPUTIL_NXAST_WRITE_METADATA:
319 nawm = (const struct nx_action_write_metadata *) a;
320 error = metadata_from_nxast(nawm, out);
321 break;
322
f25d0cf3
BP
323 case OFPUTIL_NXAST_SET_QUEUE:
324 nasq = (const struct nx_action_set_queue *) a;
325 ofpact_put_SET_QUEUE(out)->queue_id = ntohl(nasq->queue_id);
326 break;
327
328 case OFPUTIL_NXAST_POP_QUEUE:
329 ofpact_put_POP_QUEUE(out);
330 break;
331
332 case OFPUTIL_NXAST_REG_MOVE:
333 error = nxm_reg_move_from_openflow(
334 (const struct nx_action_reg_move *) a, out);
335 break;
336
337 case OFPUTIL_NXAST_REG_LOAD:
338 error = nxm_reg_load_from_openflow(
339 (const struct nx_action_reg_load *) a, out);
340 break;
341
342 case OFPUTIL_NXAST_NOTE:
343 nan = (const struct nx_action_note *) a;
344 note_from_openflow(nan, out);
345 break;
346
347 case OFPUTIL_NXAST_SET_TUNNEL64:
348 nast64 = (const struct nx_action_set_tunnel64 *) a;
349 tunnel = ofpact_put_SET_TUNNEL(out);
350 tunnel->ofpact.compat = code;
351 tunnel->tun_id = ntohll(nast64->tun_id);
352 break;
353
354 case OFPUTIL_NXAST_MULTIPATH:
355 error = multipath_from_openflow((const struct nx_action_multipath *) a,
356 ofpact_put_MULTIPATH(out));
357 break;
358
c51c638a 359 case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
f25d0cf3
BP
360 error = autopath_from_openflow((const struct nx_action_autopath *) a,
361 ofpact_put_AUTOPATH(out));
362 break;
363
364 case OFPUTIL_NXAST_BUNDLE:
365 case OFPUTIL_NXAST_BUNDLE_LOAD:
366 error = bundle_from_openflow((const struct nx_action_bundle *) a, out);
367 break;
368
369 case OFPUTIL_NXAST_OUTPUT_REG:
370 error = output_reg_from_openflow(
371 (const struct nx_action_output_reg *) a, out);
372 break;
373
374 case OFPUTIL_NXAST_RESUBMIT_TABLE:
375 nar = (const struct nx_action_resubmit *) a;
376 error = resubmit_table_from_openflow(nar, out);
377 break;
378
379 case OFPUTIL_NXAST_LEARN:
380 error = learn_from_openflow((const struct nx_action_learn *) a, out);
381 break;
382
383 case OFPUTIL_NXAST_EXIT:
384 ofpact_put_EXIT(out);
385 break;
386
387 case OFPUTIL_NXAST_DEC_TTL:
7bcb1506 388 error = dec_ttl_from_openflow(out, code);
c2d967a5
MM
389 break;
390
391 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
392 error = dec_ttl_cnt_ids_from_openflow(
393 (const struct nx_action_cnt_ids *) a, out);
f25d0cf3
BP
394 break;
395
396 case OFPUTIL_NXAST_FIN_TIMEOUT:
397 fin_timeout_from_openflow(
398 (const struct nx_action_fin_timeout *) a, out);
399 break;
400
401 case OFPUTIL_NXAST_CONTROLLER:
402 controller_from_openflow((const struct nx_action_controller *) a, out);
403 break;
404 }
405
406 return error;
407}
408
d01c980f
BP
409static enum ofperr
410ofpact_from_openflow10(const union ofp_action *a, struct ofpbuf *out)
411{
412 enum ofputil_action_code code;
413 enum ofperr error;
414
415 error = decode_openflow10_action(a, &code);
416 if (error) {
417 return error;
418 }
419
420 switch (code) {
421 case OFPUTIL_ACTION_INVALID:
3ddcaf2d 422#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
d01c980f
BP
423#include "ofp-util.def"
424 NOT_REACHED();
425
426 case OFPUTIL_OFPAT10_OUTPUT:
427 return output_from_openflow10(&a->output10, out);
428
429 case OFPUTIL_OFPAT10_SET_VLAN_VID:
430 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
431 return OFPERR_OFPBAC_BAD_ARGUMENT;
432 }
433 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
434 break;
435
436 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
437 if (a->vlan_pcp.vlan_pcp & ~7) {
438 return OFPERR_OFPBAC_BAD_ARGUMENT;
439 }
440 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
441 break;
442
443 case OFPUTIL_OFPAT10_STRIP_VLAN:
444 ofpact_put_STRIP_VLAN(out);
445 break;
446
447 case OFPUTIL_OFPAT10_SET_DL_SRC:
448 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
449 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
450 break;
451
452 case OFPUTIL_OFPAT10_SET_DL_DST:
453 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
454 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
455 break;
456
457 case OFPUTIL_OFPAT10_SET_NW_SRC:
458 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
459 break;
460
461 case OFPUTIL_OFPAT10_SET_NW_DST:
462 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
463 break;
464
465 case OFPUTIL_OFPAT10_SET_NW_TOS:
466 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
467 return OFPERR_OFPBAC_BAD_ARGUMENT;
468 }
469 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
470 break;
471
472 case OFPUTIL_OFPAT10_SET_TP_SRC:
473 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
474 break;
475
476 case OFPUTIL_OFPAT10_SET_TP_DST:
477 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
478
479 break;
480
481 case OFPUTIL_OFPAT10_ENQUEUE:
31a9e63f 482 error = enqueue_from_openflow10((const struct ofp10_action_enqueue *) a,
d01c980f
BP
483 out);
484 break;
485
486#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
487#include "ofp-util.def"
488 return ofpact_from_nxast(a, code, out);
489 }
490
491 return error;
492}
493
f25d0cf3
BP
494static inline union ofp_action *
495action_next(const union ofp_action *a)
496{
497 return ((union ofp_action *) (void *)
498 ((uint8_t *) a + ntohs(a->header.len)));
499}
500
501static inline bool
502action_is_valid(const union ofp_action *a, size_t n_actions)
503{
504 uint16_t len = ntohs(a->header.len);
505 return (!(len % OFP_ACTION_ALIGN)
506 && len >= sizeof *a
507 && len / sizeof *a <= n_actions);
508}
509
510/* This macro is careful to check for actions with bad lengths. */
511#define ACTION_FOR_EACH(ITER, LEFT, ACTIONS, N_ACTIONS) \
512 for ((ITER) = (ACTIONS), (LEFT) = (N_ACTIONS); \
513 (LEFT) > 0 && action_is_valid(ITER, LEFT); \
514 ((LEFT) -= ntohs((ITER)->header.len) / sizeof(union ofp_action), \
515 (ITER) = action_next(ITER)))
516
699dddf1
BP
517static void
518log_bad_action(const union ofp_action *actions, size_t n_actions, size_t ofs,
519 enum ofperr error)
520{
521 if (!VLOG_DROP_WARN(&rl)) {
522 struct ds s;
523
524 ds_init(&s);
525 ds_put_hex_dump(&s, actions, n_actions * sizeof *actions, 0, false);
526 VLOG_WARN("bad action at offset %#zx (%s):\n%s",
527 ofs * sizeof *actions, ofperr_get_name(error), ds_cstr(&s));
528 ds_destroy(&s);
529 }
530}
531
f25d0cf3 532static enum ofperr
0300caaf
IY
533ofpacts_from_openflow(const union ofp_action *in, size_t n_in,
534 struct ofpbuf *out,
535 enum ofperr (*ofpact_from_openflow)(
536 const union ofp_action *a, struct ofpbuf *out))
f25d0cf3
BP
537{
538 const union ofp_action *a;
539 size_t left;
540
541 ACTION_FOR_EACH (a, left, in, n_in) {
0300caaf 542 enum ofperr error = ofpact_from_openflow(a, out);
f25d0cf3 543 if (error) {
699dddf1 544 log_bad_action(in, n_in, a - in, error);
f25d0cf3
BP
545 return error;
546 }
547 }
548 if (left) {
699dddf1
BP
549 enum ofperr error = OFPERR_OFPBAC_BAD_LEN;
550 log_bad_action(in, n_in, n_in - left, error);
551 return error;
f25d0cf3
BP
552 }
553
554 ofpact_pad(out);
555 return 0;
556}
557
0300caaf
IY
558static enum ofperr
559ofpacts_from_openflow10(const union ofp_action *in, size_t n_in,
560 struct ofpbuf *out)
561{
562 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow10);
563}
564
d01c980f
BP
565static enum ofperr
566ofpacts_pull_actions(struct ofpbuf *openflow, unsigned int actions_len,
567 struct ofpbuf *ofpacts,
568 enum ofperr (*translate)(const union ofp_action *actions,
569 size_t n_actions,
570 struct ofpbuf *ofpacts))
f25d0cf3
BP
571{
572 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
573 const union ofp_action *actions;
574 enum ofperr error;
575
576 ofpbuf_clear(ofpacts);
577
578 if (actions_len % OFP_ACTION_ALIGN != 0) {
579 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
580 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
581 return OFPERR_OFPBRC_BAD_LEN;
582 }
583
584 actions = ofpbuf_try_pull(openflow, actions_len);
585 if (actions == NULL) {
586 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
587 "remaining message length (%zu)",
588 actions_len, openflow->size);
589 return OFPERR_OFPBRC_BAD_LEN;
590 }
591
d01c980f 592 error = translate(actions, actions_len / OFP_ACTION_ALIGN, ofpacts);
4cceacb9
JS
593 if (error) {
594 ofpbuf_clear(ofpacts);
595 return error;
596 }
597
598 error = ofpacts_verify(ofpacts->data, ofpacts->size);
f25d0cf3
BP
599 if (error) {
600 ofpbuf_clear(ofpacts);
601 }
d01c980f
BP
602 return error;
603}
604
605/* Attempts to convert 'actions_len' bytes of OpenFlow 1.0 actions from the
606 * front of 'openflow' into ofpacts. On success, replaces any existing content
607 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
608 * Returns 0 if successful, otherwise an OpenFlow error.
609 *
13f894b1
BP
610 * The parsed actions are valid generically, but they may not be valid in a
611 * specific context. For example, port numbers up to OFPP_MAX are valid
612 * generically, but specific datapaths may only support port numbers in a
613 * smaller range. Use ofpacts_check() to additional check whether actions are
614 * valid in a specific context. */
d01c980f
BP
615enum ofperr
616ofpacts_pull_openflow10(struct ofpbuf *openflow, unsigned int actions_len,
617 struct ofpbuf *ofpacts)
618{
619 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
620 ofpacts_from_openflow10);
621}
622\f
623/* OpenFlow 1.1 actions. */
624
625/* Parses 'a' to determine its type. On success stores the correct type into
626 * '*code' and returns 0. On failure returns an OFPERR_* error code and
627 * '*code' is indeterminate.
628 *
629 * The caller must have already verified that 'a''s length is potentially
630 * correct (that is, a->header.len is nonzero and a multiple of sizeof(union
631 * ofp_action) and no longer than the amount of space allocated to 'a').
632 *
633 * This function verifies that 'a''s length is correct for the type of action
634 * that it represents. */
635static enum ofperr
636decode_openflow11_action(const union ofp_action *a,
637 enum ofputil_action_code *code)
638{
3ddcaf2d
BP
639 uint16_t len;
640
d01c980f
BP
641 switch (a->type) {
642 case CONSTANT_HTONS(OFPAT11_EXPERIMENTER):
643 return decode_nxast_action(a, code);
644
3ddcaf2d
BP
645#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
646 case CONSTANT_HTONS(ENUM): \
647 len = ntohs(a->header.len); \
648 if (EXTENSIBLE \
649 ? len >= sizeof(struct STRUCT) \
650 : len == sizeof(struct STRUCT)) { \
651 *code = OFPUTIL_##ENUM; \
652 return 0; \
653 } else { \
654 return OFPERR_OFPBAC_BAD_LEN; \
655 } \
656 NOT_REACHED();
d01c980f
BP
657#include "ofp-util.def"
658
659 default:
660 return OFPERR_OFPBAC_BAD_TYPE;
661 }
662}
663
664static enum ofperr
665output_from_openflow11(const struct ofp11_action_output *oao,
666 struct ofpbuf *out)
667{
668 struct ofpact_output *output;
669 enum ofperr error;
670
671 output = ofpact_put_OUTPUT(out);
672 output->max_len = ntohs(oao->max_len);
673
674 error = ofputil_port_from_ofp11(oao->port, &output->port);
675 if (error) {
676 return error;
677 }
678
679 return ofputil_check_output_port(output->port, OFPP_MAX);
680}
681
682static enum ofperr
683ofpact_from_openflow11(const union ofp_action *a, struct ofpbuf *out)
684{
685 enum ofputil_action_code code;
686 enum ofperr error;
687
688 error = decode_openflow11_action(a, &code);
689 if (error) {
690 return error;
691 }
692
693 switch (code) {
694 case OFPUTIL_ACTION_INVALID:
695#define OFPAT10_ACTION(ENUM, STRUCT, NAME) case OFPUTIL_##ENUM:
696#include "ofp-util.def"
697 NOT_REACHED();
698
699 case OFPUTIL_OFPAT11_OUTPUT:
700 return output_from_openflow11((const struct ofp11_action_output *) a,
701 out);
702
703 case OFPUTIL_OFPAT11_SET_VLAN_VID:
704 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
705 return OFPERR_OFPBAC_BAD_ARGUMENT;
706 }
707 ofpact_put_SET_VLAN_VID(out)->vlan_vid = ntohs(a->vlan_vid.vlan_vid);
708 break;
709
710 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
711 if (a->vlan_pcp.vlan_pcp & ~7) {
712 return OFPERR_OFPBAC_BAD_ARGUMENT;
713 }
714 ofpact_put_SET_VLAN_PCP(out)->vlan_pcp = a->vlan_pcp.vlan_pcp;
715 break;
716
3e34fbdd
IY
717 case OFPUTIL_OFPAT11_PUSH_VLAN:
718 if (((const struct ofp11_action_push *)a)->ethertype !=
719 htons(ETH_TYPE_VLAN_8021Q)) {
5dca28b5 720 /* XXX 802.1AD(QinQ) isn't supported at the moment */
15549878 721 return OFPERR_OFPBAC_BAD_ARGUMENT;
3e34fbdd
IY
722 }
723 ofpact_put_PUSH_VLAN(out);
724 break;
725
8e61c110
IY
726 case OFPUTIL_OFPAT11_POP_VLAN:
727 ofpact_put_STRIP_VLAN(out);
728 break;
729
276c4e7a
SH
730 case OFPUTIL_OFPAT11_SET_QUEUE:
731 ofpact_put_SET_QUEUE(out)->queue_id =
732 ntohl(((const struct ofp11_action_set_queue *)a)->queue_id);
733 break;
734
d01c980f
BP
735 case OFPUTIL_OFPAT11_SET_DL_SRC:
736 memcpy(ofpact_put_SET_ETH_SRC(out)->mac,
737 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
738 break;
739
740 case OFPUTIL_OFPAT11_SET_DL_DST:
741 memcpy(ofpact_put_SET_ETH_DST(out)->mac,
742 ((const struct ofp_action_dl_addr *) a)->dl_addr, ETH_ADDR_LEN);
743 break;
744
7bcb1506
IY
745 case OFPUTIL_OFPAT11_DEC_NW_TTL:
746 dec_ttl_from_openflow(out, code);
747 break;
748
d01c980f
BP
749 case OFPUTIL_OFPAT11_SET_NW_SRC:
750 ofpact_put_SET_IPV4_SRC(out)->ipv4 = a->nw_addr.nw_addr;
751 break;
752
753 case OFPUTIL_OFPAT11_SET_NW_DST:
754 ofpact_put_SET_IPV4_DST(out)->ipv4 = a->nw_addr.nw_addr;
755 break;
756
757 case OFPUTIL_OFPAT11_SET_NW_TOS:
758 if (a->nw_tos.nw_tos & ~IP_DSCP_MASK) {
759 return OFPERR_OFPBAC_BAD_ARGUMENT;
760 }
761 ofpact_put_SET_IPV4_DSCP(out)->dscp = a->nw_tos.nw_tos;
762 break;
763
764 case OFPUTIL_OFPAT11_SET_TP_SRC:
765 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(a->tp_port.tp_port);
766 break;
767
768 case OFPUTIL_OFPAT11_SET_TP_DST:
769 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(a->tp_port.tp_port);
770 break;
771
e9536ecb 772 case OFPUTIL_OFPAT12_SET_FIELD:
d55b18c8
IY
773 return nxm_reg_load_from_openflow12_set_field(
774 (const struct ofp12_action_set_field *)a, out);
e9536ecb 775
d01c980f
BP
776#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
777#include "ofp-util.def"
778 return ofpact_from_nxast(a, code, out);
779 }
780
781 return error;
782}
783
784static enum ofperr
785ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
786 struct ofpbuf *out)
787{
0300caaf 788 return ofpacts_from_openflow(in, n_in, out, ofpact_from_openflow11);
f25d0cf3
BP
789}
790\f
d01c980f
BP
791/* OpenFlow 1.1 instructions. */
792
d01c980f 793#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
a359d5ad
IY
794 static inline const struct STRUCT * \
795 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
796 { \
797 assert(inst->type == htons(ENUM)); \
798 return (struct STRUCT *)inst; \
799 } \
800 \
d01c980f
BP
801 static inline void \
802 instruction_init_##ENUM(struct STRUCT *s) \
803 { \
804 memset(s, 0, sizeof *s); \
805 s->type = htons(ENUM); \
806 s->len = htons(sizeof *s); \
807 } \
808 \
809 static inline struct STRUCT * \
810 instruction_put_##ENUM(struct ofpbuf *buf) \
811 { \
812 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
813 instruction_init_##ENUM(s); \
814 return s; \
815 }
816OVS_INSTRUCTIONS
817#undef DEFINE_INST
818
a359d5ad
IY
819struct instruction_type_info {
820 enum ovs_instruction_type type;
821 const char *name;
822};
823
824static const struct instruction_type_info inst_info[] = {
825#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
826OVS_INSTRUCTIONS
827#undef DEFINE_INST
828};
829
830const char *
831ofpact_instruction_name_from_type(enum ovs_instruction_type type)
832{
6ae0fd54 833 return inst_info[type].name;
a359d5ad
IY
834}
835
836int
837ofpact_instruction_type_from_name(const char *name)
838{
839 const struct instruction_type_info *p;
840 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
841 if (!strcasecmp(name, p->name)) {
842 return p->type;
843 }
844 }
845 return -1;
846}
847
d01c980f
BP
848static inline struct ofp11_instruction *
849instruction_next(const struct ofp11_instruction *inst)
850{
851 return ((struct ofp11_instruction *) (void *)
852 ((uint8_t *) inst + ntohs(inst->len)));
853}
854
855static inline bool
856instruction_is_valid(const struct ofp11_instruction *inst,
857 size_t n_instructions)
858{
859 uint16_t len = ntohs(inst->len);
860 return (!(len % OFP11_INSTRUCTION_ALIGN)
861 && len >= sizeof *inst
862 && len / sizeof *inst <= n_instructions);
863}
864
865/* This macro is careful to check for instructions with bad lengths. */
866#define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
867 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
868 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
869 ((LEFT) -= (ntohs((ITER)->len) \
870 / sizeof(struct ofp11_instruction)), \
871 (ITER) = instruction_next(ITER)))
872
873static enum ofperr
874decode_openflow11_instruction(const struct ofp11_instruction *inst,
875 enum ovs_instruction_type *type)
876{
877 uint16_t len = ntohs(inst->len);
878
879 switch (inst->type) {
880 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
881 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
882
883#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
884 case CONSTANT_HTONS(ENUM): \
885 if (EXTENSIBLE \
886 ? len >= sizeof(struct STRUCT) \
887 : len == sizeof(struct STRUCT)) { \
888 *type = OVSINST_##ENUM; \
889 return 0; \
890 } else { \
891 return OFPERR_OFPBIC_BAD_LEN; \
892 }
893OVS_INSTRUCTIONS
894#undef DEFINE_INST
895
896 default:
897 return OFPERR_OFPBIC_UNKNOWN_INST;
898 }
899}
900
901static enum ofperr
902decode_openflow11_instructions(const struct ofp11_instruction insts[],
903 size_t n_insts,
904 const struct ofp11_instruction *out[])
905{
906 const struct ofp11_instruction *inst;
907 size_t left;
908
909 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
910 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
911 enum ovs_instruction_type type;
912 enum ofperr error;
913
914 error = decode_openflow11_instruction(inst, &type);
915 if (error) {
916 return error;
917 }
918
919 if (out[type]) {
15549878
JR
920 return OFPERR_OFPBAC_UNSUPPORTED_ORDER; /* No specific code for
921 * a duplicate instruction
922 * exist */
d01c980f
BP
923 }
924 out[type] = inst;
925 }
926
927 if (left) {
928 VLOG_WARN_RL(&rl, "bad instruction format at offset %zu",
929 (n_insts - left) * sizeof *inst);
930 return OFPERR_OFPBIC_BAD_LEN;
931 }
932 return 0;
933}
934
935static void
936get_actions_from_instruction(const struct ofp11_instruction *inst,
937 const union ofp_action **actions,
938 size_t *n_actions)
939{
940 *actions = (const union ofp_action *) (inst + 1);
941 *n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
942}
943
944/* Attempts to convert 'actions_len' bytes of OpenFlow 1.1 actions from the
945 * front of 'openflow' into ofpacts. On success, replaces any existing content
946 * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
947 * Returns 0 if successful, otherwise an OpenFlow error.
948 *
949 * In most places in OpenFlow 1.1 and 1.2, actions appear encapsulated in
950 * instructions, so you should call ofpacts_pull_openflow11_instructions()
951 * instead of this function.
952 *
13f894b1
BP
953 * The parsed actions are valid generically, but they may not be valid in a
954 * specific context. For example, port numbers up to OFPP_MAX are valid
955 * generically, but specific datapaths may only support port numbers in a
956 * smaller range. Use ofpacts_check() to additional check whether actions are
957 * valid in a specific context. */
d01c980f
BP
958enum ofperr
959ofpacts_pull_openflow11_actions(struct ofpbuf *openflow,
960 unsigned int actions_len,
961 struct ofpbuf *ofpacts)
962{
0300caaf
IY
963 return ofpacts_pull_actions(openflow, actions_len, ofpacts,
964 ofpacts_from_openflow11);
d01c980f
BP
965}
966
967enum ofperr
968ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
969 unsigned int instructions_len,
970 struct ofpbuf *ofpacts)
971{
972 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
973 const struct ofp11_instruction *instructions;
974 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
975 enum ofperr error;
976
977 ofpbuf_clear(ofpacts);
978
979 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
980 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
981 "multiple of %d",
982 instructions_len, OFP11_INSTRUCTION_ALIGN);
983 error = OFPERR_OFPBIC_BAD_LEN;
984 goto exit;
985 }
986
987 instructions = ofpbuf_try_pull(openflow, instructions_len);
988 if (instructions == NULL) {
989 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
990 "remaining message length (%zu)",
991 instructions_len, openflow->size);
992 error = OFPERR_OFPBIC_BAD_LEN;
993 goto exit;
994 }
995
996 error = decode_openflow11_instructions(
997 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
998 insts);
999 if (error) {
1000 goto exit;
1001 }
1002
1003 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
1004 const union ofp_action *actions;
1005 size_t n_actions;
1006
1007 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
1008 &actions, &n_actions);
1009 error = ofpacts_from_openflow11(actions, n_actions, ofpacts);
1010 if (error) {
1011 goto exit;
1012 }
1013 }
b19e8793
IY
1014 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
1015 instruction_get_OFPIT11_CLEAR_ACTIONS(
1016 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
1017 ofpact_put_CLEAR_ACTIONS(ofpacts);
1018 }
5dca28b5 1019 /* XXX Write-Actions */
4cceacb9
JS
1020 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
1021 const struct ofp11_instruction_write_metadata *oiwm;
1022 struct ofpact_metadata *om;
1023
1024 oiwm = (const struct ofp11_instruction_write_metadata *)
1025 insts[OVSINST_OFPIT11_WRITE_METADATA];
1026
1027 om = ofpact_put_WRITE_METADATA(ofpacts);
1028 om->metadata = oiwm->metadata;
1029 om->mask = oiwm->metadata_mask;
1030 }
8dd54666
IY
1031 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
1032 const struct ofp11_instruction_goto_table *oigt;
1033 struct ofpact_goto_table *ogt;
1034
1035 oigt = instruction_get_OFPIT11_GOTO_TABLE(
1036 insts[OVSINST_OFPIT11_GOTO_TABLE]);
1037 ogt = ofpact_put_GOTO_TABLE(ofpacts);
1038 ogt->table_id = oigt->table_id;
1039 }
d01c980f 1040
4cceacb9 1041 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
d01c980f
BP
1042 error = OFPERR_OFPBIC_UNSUP_INST;
1043 goto exit;
1044 }
1045
4cceacb9 1046 error = ofpacts_verify(ofpacts->data, ofpacts->size);
d01c980f
BP
1047exit:
1048 if (error) {
1049 ofpbuf_clear(ofpacts);
1050 }
1051 return error;
1052}
1053\f
f25d0cf3
BP
1054static enum ofperr
1055ofpact_check__(const struct ofpact *a, const struct flow *flow, int max_ports)
1056{
1057 const struct ofpact_enqueue *enqueue;
1058
1059 switch (a->type) {
1060 case OFPACT_OUTPUT:
1061 return ofputil_check_output_port(ofpact_get_OUTPUT(a)->port,
1062 max_ports);
1063
1064 case OFPACT_CONTROLLER:
1065 return 0;
1066
1067 case OFPACT_ENQUEUE:
1068 enqueue = ofpact_get_ENQUEUE(a);
1069 if (enqueue->port >= max_ports && enqueue->port != OFPP_IN_PORT
1070 && enqueue->port != OFPP_LOCAL) {
1071 return OFPERR_OFPBAC_BAD_OUT_PORT;
1072 }
1073 return 0;
1074
1075 case OFPACT_OUTPUT_REG:
1076 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
1077
1078 case OFPACT_BUNDLE:
1079 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
1080
1081 case OFPACT_SET_VLAN_VID:
1082 case OFPACT_SET_VLAN_PCP:
1083 case OFPACT_STRIP_VLAN:
3e34fbdd 1084 case OFPACT_PUSH_VLAN:
f25d0cf3
BP
1085 case OFPACT_SET_ETH_SRC:
1086 case OFPACT_SET_ETH_DST:
1087 case OFPACT_SET_IPV4_SRC:
1088 case OFPACT_SET_IPV4_DST:
1089 case OFPACT_SET_IPV4_DSCP:
1090 case OFPACT_SET_L4_SRC_PORT:
1091 case OFPACT_SET_L4_DST_PORT:
1092 return 0;
1093
1094 case OFPACT_REG_MOVE:
1095 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
1096
1097 case OFPACT_REG_LOAD:
1098 return nxm_reg_load_check(ofpact_get_REG_LOAD(a), flow);
1099
1100 case OFPACT_DEC_TTL:
1101 case OFPACT_SET_TUNNEL:
1102 case OFPACT_SET_QUEUE:
1103 case OFPACT_POP_QUEUE:
1104 case OFPACT_FIN_TIMEOUT:
1105 case OFPACT_RESUBMIT:
1106 return 0;
1107
1108 case OFPACT_LEARN:
1109 return learn_check(ofpact_get_LEARN(a), flow);
1110
1111 case OFPACT_MULTIPATH:
1112 return multipath_check(ofpact_get_MULTIPATH(a), flow);
1113
1114 case OFPACT_AUTOPATH:
1115 return autopath_check(ofpact_get_AUTOPATH(a), flow);
1116
1117 case OFPACT_NOTE:
1118 case OFPACT_EXIT:
1119 return 0;
1120
b19e8793 1121 case OFPACT_CLEAR_ACTIONS:
4cceacb9 1122 case OFPACT_WRITE_METADATA:
8dd54666
IY
1123 case OFPACT_GOTO_TABLE:
1124 return 0;
1125
f25d0cf3
BP
1126 default:
1127 NOT_REACHED();
1128 }
1129}
1130
1131/* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1132 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
1133 * switch with no more than 'max_ports' ports. */
1134enum ofperr
1135ofpacts_check(const struct ofpact ofpacts[], size_t ofpacts_len,
1136 const struct flow *flow, int max_ports)
1137{
1138 const struct ofpact *a;
1139
1140 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1141 enum ofperr error = ofpact_check__(a, flow, max_ports);
1142 if (error) {
1143 return error;
1144 }
1145 }
1146
1147 return 0;
1148}
4cceacb9
JS
1149
1150/* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are
1151 * in the appropriate order as defined by the OpenFlow spec. */
1152enum ofperr
1153ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len)
1154{
1155 const struct ofpact *a;
6813ee7c 1156 enum ovs_instruction_type inst;
4cceacb9 1157
6813ee7c 1158 inst = OVSINST_OFPIT11_APPLY_ACTIONS;
4cceacb9 1159 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6813ee7c
BP
1160 enum ovs_instruction_type next;
1161
1162 if (a->type == OFPACT_CLEAR_ACTIONS) {
1163 next = OVSINST_OFPIT11_CLEAR_ACTIONS;
1164 } else if (a->type == OFPACT_WRITE_METADATA) {
1165 next = OVSINST_OFPIT11_WRITE_METADATA;
1166 } else if (a->type == OFPACT_GOTO_TABLE) {
1167 next = OVSINST_OFPIT11_GOTO_TABLE;
1168 } else {
1169 next = OVSINST_OFPIT11_APPLY_ACTIONS;
1170 }
1171
1172 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS && next <= inst) {
1173 const char *name = ofpact_instruction_name_from_type(inst);
1174 const char *next_name = ofpact_instruction_name_from_type(next);
1175
1176 if (next == inst) {
1177 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
1178 "1.1+ compatibility", name);
4cceacb9 1179 } else {
6813ee7c
BP
1180 VLOG_WARN("invalid instruction ordering: %s must appear "
1181 "before %s, for OpenFlow 1.1+ compatibility",
1182 next_name, name);
4cceacb9 1183 }
6813ee7c 1184 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
4cceacb9
JS
1185 }
1186
6813ee7c 1187 inst = next;
4cceacb9
JS
1188 }
1189
1190 return 0;
1191}
f25d0cf3
BP
1192\f
1193/* Converting ofpacts to Nicira OpenFlow extensions. */
1194
1195static void
1196ofpact_output_reg_to_nxast(const struct ofpact_output_reg *output_reg,
1197 struct ofpbuf *out)
1198{
1199 struct nx_action_output_reg *naor = ofputil_put_NXAST_OUTPUT_REG(out);
1200
1201 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1202 output_reg->src.n_bits);
1203 naor->src = htonl(output_reg->src.field->nxm_header);
1204 naor->max_len = htons(output_reg->max_len);
1205}
1206
1207static void
1208ofpact_resubmit_to_nxast(const struct ofpact_resubmit *resubmit,
1209 struct ofpbuf *out)
1210{
1211 struct nx_action_resubmit *nar;
1212
1213 if (resubmit->table_id == 0xff
1214 && resubmit->ofpact.compat != OFPUTIL_NXAST_RESUBMIT_TABLE) {
1215 nar = ofputil_put_NXAST_RESUBMIT(out);
1216 } else {
1217 nar = ofputil_put_NXAST_RESUBMIT_TABLE(out);
1218 nar->table = resubmit->table_id;
1219 }
1220 nar->in_port = htons(resubmit->in_port);
1221}
1222
1223static void
1224ofpact_set_tunnel_to_nxast(const struct ofpact_tunnel *tunnel,
1225 struct ofpbuf *out)
1226{
1227 uint64_t tun_id = tunnel->tun_id;
1228
1229 if (tun_id <= UINT32_MAX
1230 && tunnel->ofpact.compat != OFPUTIL_NXAST_SET_TUNNEL64) {
1231 ofputil_put_NXAST_SET_TUNNEL(out)->tun_id = htonl(tun_id);
1232 } else {
1233 ofputil_put_NXAST_SET_TUNNEL64(out)->tun_id = htonll(tun_id);
1234 }
1235}
1236
4cceacb9
JS
1237static void
1238ofpact_write_metadata_to_nxast(const struct ofpact_metadata *om,
1239 struct ofpbuf *out)
1240{
1241 struct nx_action_write_metadata *nawm;
1242
1243 nawm = ofputil_put_NXAST_WRITE_METADATA(out);
1244 nawm->metadata = om->metadata;
1245 nawm->mask = om->mask;
1246}
1247
f25d0cf3
BP
1248static void
1249ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
1250{
1251 size_t start_ofs = out->size;
1252 struct nx_action_note *nan;
1253 unsigned int remainder;
1254 unsigned int len;
1255
1256 nan = ofputil_put_NXAST_NOTE(out);
1257 out->size -= sizeof nan->note;
1258
1259 ofpbuf_put(out, note->data, note->length);
1260
1261 len = out->size - start_ofs;
1262 remainder = len % OFP_ACTION_ALIGN;
1263 if (remainder) {
1264 ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
1265 }
1266 nan = (struct nx_action_note *)((char *)out->data + start_ofs);
1267 nan->len = htons(out->size - start_ofs);
1268}
1269
1270static void
1271ofpact_controller_to_nxast(const struct ofpact_controller *oc,
1272 struct ofpbuf *out)
1273{
1274 struct nx_action_controller *nac;
1275
1276 nac = ofputil_put_NXAST_CONTROLLER(out);
1277 nac->max_len = htons(oc->max_len);
1278 nac->controller_id = htons(oc->controller_id);
1279 nac->reason = oc->reason;
1280}
1281
c2d967a5
MM
1282static void
1283ofpact_dec_ttl_to_nxast(const struct ofpact_cnt_ids *oc_ids,
1284 struct ofpbuf *out)
1285{
1286 if (oc_ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL) {
1287 ofputil_put_NXAST_DEC_TTL(out);
1288 } else {
1289 struct nx_action_cnt_ids *nac_ids =
1290 ofputil_put_NXAST_DEC_TTL_CNT_IDS(out);
1291 int ids_len = ROUND_UP(2 * oc_ids->n_controllers, OFP_ACTION_ALIGN);
1292 ovs_be16 *ids;
1293 size_t i;
1294
1295 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
1296 nac_ids->n_controllers = htons(oc_ids->n_controllers);
1297
1298 ids = ofpbuf_put_zeros(out, ids_len);
1299 for (i = 0; i < oc_ids->n_controllers; i++) {
1300 ids[i] = htons(oc_ids->cnt_ids[i]);
1301 }
1302 }
1303}
1304
f25d0cf3
BP
1305static void
1306ofpact_fin_timeout_to_nxast(const struct ofpact_fin_timeout *fin_timeout,
1307 struct ofpbuf *out)
1308{
1309 struct nx_action_fin_timeout *naft = ofputil_put_NXAST_FIN_TIMEOUT(out);
1310 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
1311 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
1312}
1313
1314static void
1315ofpact_to_nxast(const struct ofpact *a, struct ofpbuf *out)
1316{
1317 switch (a->type) {
1318 case OFPACT_CONTROLLER:
1319 ofpact_controller_to_nxast(ofpact_get_CONTROLLER(a), out);
1320 break;
1321
1322 case OFPACT_OUTPUT_REG:
1323 ofpact_output_reg_to_nxast(ofpact_get_OUTPUT_REG(a), out);
1324 break;
1325
1326 case OFPACT_BUNDLE:
1327 bundle_to_nxast(ofpact_get_BUNDLE(a), out);
1328 break;
1329
1330 case OFPACT_REG_MOVE:
1331 nxm_reg_move_to_nxast(ofpact_get_REG_MOVE(a), out);
1332 break;
1333
1334 case OFPACT_REG_LOAD:
1335 nxm_reg_load_to_nxast(ofpact_get_REG_LOAD(a), out);
1336 break;
1337
1338 case OFPACT_DEC_TTL:
c2d967a5 1339 ofpact_dec_ttl_to_nxast(ofpact_get_DEC_TTL(a), out);
f25d0cf3
BP
1340 break;
1341
1342 case OFPACT_SET_TUNNEL:
1343 ofpact_set_tunnel_to_nxast(ofpact_get_SET_TUNNEL(a), out);
1344 break;
1345
4cceacb9
JS
1346 case OFPACT_WRITE_METADATA:
1347 ofpact_write_metadata_to_nxast(ofpact_get_WRITE_METADATA(a), out);
1348 break;
1349
f25d0cf3
BP
1350 case OFPACT_SET_QUEUE:
1351 ofputil_put_NXAST_SET_QUEUE(out)->queue_id
1352 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1353 break;
1354
1355 case OFPACT_POP_QUEUE:
1356 ofputil_put_NXAST_POP_QUEUE(out);
1357 break;
1358
1359 case OFPACT_FIN_TIMEOUT:
1360 ofpact_fin_timeout_to_nxast(ofpact_get_FIN_TIMEOUT(a), out);
1361 break;
1362
1363 case OFPACT_RESUBMIT:
1364 ofpact_resubmit_to_nxast(ofpact_get_RESUBMIT(a), out);
1365 break;
1366
1367 case OFPACT_LEARN:
1368 learn_to_nxast(ofpact_get_LEARN(a), out);
1369 break;
1370
1371 case OFPACT_MULTIPATH:
1372 multipath_to_nxast(ofpact_get_MULTIPATH(a), out);
1373 break;
1374
1375 case OFPACT_AUTOPATH:
1376 autopath_to_nxast(ofpact_get_AUTOPATH(a), out);
1377 break;
1378
1379 case OFPACT_NOTE:
1380 ofpact_note_to_nxast(ofpact_get_NOTE(a), out);
1381 break;
1382
1383 case OFPACT_EXIT:
1384 ofputil_put_NXAST_EXIT(out);
1385 break;
1386
1387 case OFPACT_OUTPUT:
1388 case OFPACT_ENQUEUE:
1389 case OFPACT_SET_VLAN_VID:
1390 case OFPACT_SET_VLAN_PCP:
1391 case OFPACT_STRIP_VLAN:
3e34fbdd 1392 case OFPACT_PUSH_VLAN:
f25d0cf3
BP
1393 case OFPACT_SET_ETH_SRC:
1394 case OFPACT_SET_ETH_DST:
1395 case OFPACT_SET_IPV4_SRC:
1396 case OFPACT_SET_IPV4_DST:
1397 case OFPACT_SET_IPV4_DSCP:
1398 case OFPACT_SET_L4_SRC_PORT:
1399 case OFPACT_SET_L4_DST_PORT:
b19e8793 1400 case OFPACT_CLEAR_ACTIONS:
8dd54666 1401 case OFPACT_GOTO_TABLE:
f25d0cf3
BP
1402 NOT_REACHED();
1403 }
1404}
1405\f
1406/* Converting ofpacts to OpenFlow 1.0. */
1407
1408static void
1409ofpact_output_to_openflow10(const struct ofpact_output *output,
1410 struct ofpbuf *out)
1411{
d01c980f 1412 struct ofp10_action_output *oao;
f25d0cf3
BP
1413
1414 oao = ofputil_put_OFPAT10_OUTPUT(out);
1415 oao->port = htons(output->port);
1416 oao->max_len = htons(output->max_len);
1417}
1418
1419static void
1420ofpact_enqueue_to_openflow10(const struct ofpact_enqueue *enqueue,
1421 struct ofpbuf *out)
1422{
31a9e63f 1423 struct ofp10_action_enqueue *oae;
f25d0cf3
BP
1424
1425 oae = ofputil_put_OFPAT10_ENQUEUE(out);
1426 oae->port = htons(enqueue->port);
1427 oae->queue_id = htonl(enqueue->queue);
1428}
1429
1430static void
1431ofpact_to_openflow10(const struct ofpact *a, struct ofpbuf *out)
1432{
1433 switch (a->type) {
1434 case OFPACT_OUTPUT:
1435 ofpact_output_to_openflow10(ofpact_get_OUTPUT(a), out);
1436 break;
1437
1438 case OFPACT_ENQUEUE:
1439 ofpact_enqueue_to_openflow10(ofpact_get_ENQUEUE(a), out);
1440 break;
1441
1442 case OFPACT_SET_VLAN_VID:
1443 ofputil_put_OFPAT10_SET_VLAN_VID(out)->vlan_vid
1444 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1445 break;
1446
1447 case OFPACT_SET_VLAN_PCP:
1448 ofputil_put_OFPAT10_SET_VLAN_PCP(out)->vlan_pcp
1449 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1450 break;
1451
1452 case OFPACT_STRIP_VLAN:
1453 ofputil_put_OFPAT10_STRIP_VLAN(out);
1454 break;
1455
1456 case OFPACT_SET_ETH_SRC:
1457 memcpy(ofputil_put_OFPAT10_SET_DL_SRC(out)->dl_addr,
1458 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1459 break;
1460
1461 case OFPACT_SET_ETH_DST:
1462 memcpy(ofputil_put_OFPAT10_SET_DL_DST(out)->dl_addr,
1463 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1464 break;
1465
1466 case OFPACT_SET_IPV4_SRC:
1467 ofputil_put_OFPAT10_SET_NW_SRC(out)->nw_addr
1468 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1469 break;
1470
1471 case OFPACT_SET_IPV4_DST:
1472 ofputil_put_OFPAT10_SET_NW_DST(out)->nw_addr
1473 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1474 break;
1475
1476 case OFPACT_SET_IPV4_DSCP:
1477 ofputil_put_OFPAT10_SET_NW_TOS(out)->nw_tos
1478 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1479 break;
1480
1481 case OFPACT_SET_L4_SRC_PORT:
1482 ofputil_put_OFPAT10_SET_TP_SRC(out)->tp_port
1483 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1484 break;
1485
1486 case OFPACT_SET_L4_DST_PORT:
1487 ofputil_put_OFPAT10_SET_TP_DST(out)->tp_port
1488 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1489 break;
1490
3e34fbdd 1491 case OFPACT_PUSH_VLAN:
b19e8793 1492 case OFPACT_CLEAR_ACTIONS:
8dd54666 1493 case OFPACT_GOTO_TABLE:
5dca28b5 1494 /* XXX */
8dd54666
IY
1495 break;
1496
f25d0cf3
BP
1497 case OFPACT_CONTROLLER:
1498 case OFPACT_OUTPUT_REG:
1499 case OFPACT_BUNDLE:
1500 case OFPACT_REG_MOVE:
1501 case OFPACT_REG_LOAD:
1502 case OFPACT_DEC_TTL:
1503 case OFPACT_SET_TUNNEL:
4cceacb9 1504 case OFPACT_WRITE_METADATA:
f25d0cf3
BP
1505 case OFPACT_SET_QUEUE:
1506 case OFPACT_POP_QUEUE:
1507 case OFPACT_FIN_TIMEOUT:
1508 case OFPACT_RESUBMIT:
1509 case OFPACT_LEARN:
1510 case OFPACT_MULTIPATH:
1511 case OFPACT_AUTOPATH:
1512 case OFPACT_NOTE:
1513 case OFPACT_EXIT:
1514 ofpact_to_nxast(a, out);
1515 break;
1516 }
1517}
1518
d01c980f 1519/* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow 1.0
f25d0cf3
BP
1520 * actions in 'openflow', appending the actions to any existing data in
1521 * 'openflow'. */
1522void
d01c980f
BP
1523ofpacts_put_openflow10(const struct ofpact ofpacts[], size_t ofpacts_len,
1524 struct ofpbuf *openflow)
f25d0cf3
BP
1525{
1526 const struct ofpact *a;
1527
1528 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1529 ofpact_to_openflow10(a, openflow);
1530 }
1531}
1532\f
d01c980f
BP
1533/* Converting ofpacts to OpenFlow 1.1. */
1534
1535static void
1536ofpact_output_to_openflow11(const struct ofpact_output *output,
1537 struct ofpbuf *out)
1538{
1539 struct ofp11_action_output *oao;
1540
1541 oao = ofputil_put_OFPAT11_OUTPUT(out);
1542 oao->port = ofputil_port_to_ofp11(output->port);
1543 oao->max_len = htons(output->max_len);
1544}
1545
99086062
BP
1546static void
1547ofpact_dec_ttl_to_openflow11(const struct ofpact_cnt_ids *dec_ttl,
1548 struct ofpbuf *out)
1549{
1550 if (dec_ttl->n_controllers == 1 && dec_ttl->cnt_ids[0] == 0
1551 && (!dec_ttl->ofpact.compat ||
1552 dec_ttl->ofpact.compat == OFPUTIL_OFPAT11_DEC_NW_TTL)) {
1553 ofputil_put_OFPAT11_DEC_NW_TTL(out);
1554 } else {
1555 ofpact_dec_ttl_to_nxast(dec_ttl, out);
1556 }
1557}
1558
d01c980f
BP
1559static void
1560ofpact_to_openflow11(const struct ofpact *a, struct ofpbuf *out)
1561{
1562 switch (a->type) {
1563 case OFPACT_OUTPUT:
1564 return ofpact_output_to_openflow11(ofpact_get_OUTPUT(a), out);
1565
1566 case OFPACT_ENQUEUE:
1567 /* XXX */
1568 break;
1569
1570 case OFPACT_SET_VLAN_VID:
1571 ofputil_put_OFPAT11_SET_VLAN_VID(out)->vlan_vid
1572 = htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1573 break;
1574
1575 case OFPACT_SET_VLAN_PCP:
1576 ofputil_put_OFPAT11_SET_VLAN_PCP(out)->vlan_pcp
1577 = ofpact_get_SET_VLAN_PCP(a)->vlan_pcp;
1578 break;
1579
1580 case OFPACT_STRIP_VLAN:
8e61c110 1581 ofputil_put_OFPAT11_POP_VLAN(out);
d01c980f
BP
1582 break;
1583
3e34fbdd 1584 case OFPACT_PUSH_VLAN:
5dca28b5 1585 /* XXX ETH_TYPE_VLAN_8021AD case */
3e34fbdd
IY
1586 ofputil_put_OFPAT11_PUSH_VLAN(out)->ethertype =
1587 htons(ETH_TYPE_VLAN_8021Q);
1588 break;
1589
276c4e7a
SH
1590 case OFPACT_SET_QUEUE:
1591 ofputil_put_OFPAT11_SET_QUEUE(out)->queue_id
1592 = htonl(ofpact_get_SET_QUEUE(a)->queue_id);
1593 break;
1594
d01c980f
BP
1595 case OFPACT_SET_ETH_SRC:
1596 memcpy(ofputil_put_OFPAT11_SET_DL_SRC(out)->dl_addr,
1597 ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
1598 break;
1599
1600 case OFPACT_SET_ETH_DST:
1601 memcpy(ofputil_put_OFPAT11_SET_DL_DST(out)->dl_addr,
1602 ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
1603 break;
1604
1605 case OFPACT_SET_IPV4_SRC:
1606 ofputil_put_OFPAT11_SET_NW_SRC(out)->nw_addr
1607 = ofpact_get_SET_IPV4_SRC(a)->ipv4;
1608 break;
1609
1610 case OFPACT_SET_IPV4_DST:
1611 ofputil_put_OFPAT11_SET_NW_DST(out)->nw_addr
1612 = ofpact_get_SET_IPV4_DST(a)->ipv4;
1613 break;
1614
1615 case OFPACT_SET_IPV4_DSCP:
1616 ofputil_put_OFPAT11_SET_NW_TOS(out)->nw_tos
1617 = ofpact_get_SET_IPV4_DSCP(a)->dscp;
1618 break;
1619
1620 case OFPACT_SET_L4_SRC_PORT:
1621 ofputil_put_OFPAT11_SET_TP_SRC(out)->tp_port
1622 = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
1623 break;
1624
1625 case OFPACT_SET_L4_DST_PORT:
1626 ofputil_put_OFPAT11_SET_TP_DST(out)->tp_port
1627 = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
1628 break;
1629
7bcb1506 1630 case OFPACT_DEC_TTL:
99086062 1631 ofpact_dec_ttl_to_openflow11(ofpact_get_DEC_TTL(a), out);
7bcb1506
IY
1632 break;
1633
4cceacb9
JS
1634 case OFPACT_WRITE_METADATA:
1635 /* OpenFlow 1.1 uses OFPIT_WRITE_METADATA to express this action. */
1636 break;
1637
b19e8793 1638 case OFPACT_CLEAR_ACTIONS:
8dd54666
IY
1639 case OFPACT_GOTO_TABLE:
1640 NOT_REACHED();
1641
d01c980f
BP
1642 case OFPACT_CONTROLLER:
1643 case OFPACT_OUTPUT_REG:
1644 case OFPACT_BUNDLE:
1645 case OFPACT_REG_MOVE:
1646 case OFPACT_REG_LOAD:
d01c980f 1647 case OFPACT_SET_TUNNEL:
d01c980f
BP
1648 case OFPACT_POP_QUEUE:
1649 case OFPACT_FIN_TIMEOUT:
1650 case OFPACT_RESUBMIT:
1651 case OFPACT_LEARN:
1652 case OFPACT_MULTIPATH:
1653 case OFPACT_AUTOPATH:
1654 case OFPACT_NOTE:
1655 case OFPACT_EXIT:
1656 ofpact_to_nxast(a, out);
1657 break;
1658 }
1659}
1660
1661/* Converts the ofpacts in 'ofpacts' (terminated by OFPACT_END) into OpenFlow
1662 * 1.1 actions in 'openflow', appending the actions to any existing data in
1663 * 'openflow'. */
a07c15bc 1664size_t
d01c980f
BP
1665ofpacts_put_openflow11_actions(const struct ofpact ofpacts[],
1666 size_t ofpacts_len, struct ofpbuf *openflow)
1667{
1668 const struct ofpact *a;
a07c15bc 1669 size_t start_size = openflow->size;
d01c980f
BP
1670
1671 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1672 ofpact_to_openflow11(a, openflow);
1673 }
a07c15bc
SH
1674
1675 return openflow->size - start_size;
d01c980f
BP
1676}
1677
8dd54666
IY
1678static void
1679ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
d01c980f
BP
1680{
1681 struct ofp11_instruction_actions *oia;
d01c980f
BP
1682
1683 /* Update the instruction's length (or, if it's empty, delete it). */
1684 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
1685 if (openflow->size > ofs + sizeof *oia) {
1686 oia->len = htons(openflow->size - ofs);
1687 } else {
1688 openflow->size = ofs;
1689 }
1690}
8dd54666
IY
1691
1692void
1693ofpacts_put_openflow11_instructions(const struct ofpact ofpacts[],
1694 size_t ofpacts_len,
1695 struct ofpbuf *openflow)
1696{
1697 const struct ofpact *a;
1698
1699 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5dca28b5 1700 /* XXX Write-Actions */
4cceacb9 1701
b19e8793 1702 if (a->type == OFPACT_CLEAR_ACTIONS) {
f606c7e9 1703 instruction_put_OFPIT11_CLEAR_ACTIONS(openflow);
b19e8793 1704 } else if (a->type == OFPACT_GOTO_TABLE) {
8dd54666
IY
1705 struct ofp11_instruction_goto_table *oigt;
1706
1707 oigt = instruction_put_OFPIT11_GOTO_TABLE(openflow);
1708 oigt->table_id = ofpact_get_GOTO_TABLE(a)->table_id;
1709 memset(oigt->pad, 0, sizeof oigt->pad);
4cceacb9
JS
1710 } else if (a->type == OFPACT_WRITE_METADATA) {
1711 const struct ofpact_metadata *om;
1712 struct ofp11_instruction_write_metadata *oiwm;
1713
1714 om = ofpact_get_WRITE_METADATA(a);
1715 oiwm = instruction_put_OFPIT11_WRITE_METADATA(openflow);
1716 oiwm->metadata = om->metadata;
1717 oiwm->metadata_mask = om->mask;
8dd54666
IY
1718 } else if (!ofpact_is_instruction(a)) {
1719 /* Apply-actions */
1720 const size_t ofs = openflow->size;
1721 const size_t ofpacts_len_left =
1722 (uint8_t*)ofpact_end(ofpacts, ofpacts_len) - (uint8_t*)a;
1723 const struct ofpact *action;
1724 const struct ofpact *processed = a;
1725
1726 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
1727 OFPACT_FOR_EACH(action, a, ofpacts_len_left) {
1728 if (ofpact_is_instruction(action)) {
1729 break;
1730 }
1731 ofpact_to_openflow11(action, openflow);
1732 processed = action;
1733 }
1734 ofpacts_update_instruction_actions(openflow, ofs);
1735 a = processed;
1736 }
1737 }
1738}
d01c980f 1739\f
f25d0cf3
BP
1740/* Returns true if 'action' outputs to 'port', false otherwise. */
1741static bool
1742ofpact_outputs_to_port(const struct ofpact *ofpact, uint16_t port)
1743{
1744 switch (ofpact->type) {
1745 case OFPACT_OUTPUT:
1746 return ofpact_get_OUTPUT(ofpact)->port == port;
1747 case OFPACT_ENQUEUE:
1748 return ofpact_get_ENQUEUE(ofpact)->port == port;
1749 case OFPACT_CONTROLLER:
1750 return port == OFPP_CONTROLLER;
1751
1752 case OFPACT_OUTPUT_REG:
1753 case OFPACT_BUNDLE:
1754 case OFPACT_SET_VLAN_VID:
1755 case OFPACT_SET_VLAN_PCP:
1756 case OFPACT_STRIP_VLAN:
3e34fbdd 1757 case OFPACT_PUSH_VLAN:
f25d0cf3
BP
1758 case OFPACT_SET_ETH_SRC:
1759 case OFPACT_SET_ETH_DST:
1760 case OFPACT_SET_IPV4_SRC:
1761 case OFPACT_SET_IPV4_DST:
1762 case OFPACT_SET_IPV4_DSCP:
1763 case OFPACT_SET_L4_SRC_PORT:
1764 case OFPACT_SET_L4_DST_PORT:
1765 case OFPACT_REG_MOVE:
1766 case OFPACT_REG_LOAD:
1767 case OFPACT_DEC_TTL:
1768 case OFPACT_SET_TUNNEL:
4cceacb9 1769 case OFPACT_WRITE_METADATA:
f25d0cf3
BP
1770 case OFPACT_SET_QUEUE:
1771 case OFPACT_POP_QUEUE:
1772 case OFPACT_FIN_TIMEOUT:
1773 case OFPACT_RESUBMIT:
1774 case OFPACT_LEARN:
1775 case OFPACT_MULTIPATH:
1776 case OFPACT_AUTOPATH:
1777 case OFPACT_NOTE:
1778 case OFPACT_EXIT:
b19e8793 1779 case OFPACT_CLEAR_ACTIONS:
8dd54666 1780 case OFPACT_GOTO_TABLE:
f25d0cf3
BP
1781 default:
1782 return false;
1783 }
1784}
1785
1786/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
1787 * to 'port', false otherwise. */
1788bool
1789ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
1790 uint16_t port)
1791{
1792 const struct ofpact *a;
1793
1794 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
1795 if (ofpact_outputs_to_port(a, port)) {
1796 return true;
1797 }
1798 }
1799
1800 return false;
1801}
1802
1803bool
1804ofpacts_equal(const struct ofpact *a, size_t a_len,
1805 const struct ofpact *b, size_t b_len)
1806{
1807 return a_len == b_len && !memcmp(a, b, a_len);
1808}
1809\f
1810/* Formatting ofpacts. */
1811
1812static void
1813print_note(const struct ofpact_note *note, struct ds *string)
1814{
1815 size_t i;
1816
1817 ds_put_cstr(string, "note:");
1818 for (i = 0; i < note->length; i++) {
1819 if (i) {
1820 ds_put_char(string, '.');
1821 }
1822 ds_put_format(string, "%02"PRIx8, note->data[i]);
1823 }
1824}
1825
c2d967a5
MM
1826static void
1827print_dec_ttl(const struct ofpact_cnt_ids *ids,
1828 struct ds *s)
1829{
1830 size_t i;
1831
1832 ds_put_cstr(s, "dec_ttl");
1833 if (ids->ofpact.compat == OFPUTIL_NXAST_DEC_TTL_CNT_IDS) {
1834 ds_put_cstr(s, "(");
1835 for (i = 0; i < ids->n_controllers; i++) {
1836 if (i) {
1837 ds_put_cstr(s, ",");
1838 }
1839 ds_put_format(s, "%"PRIu16, ids->cnt_ids[i]);
1840 }
1841 ds_put_cstr(s, ")");
1842 }
1843}
1844
f25d0cf3
BP
1845static void
1846print_fin_timeout(const struct ofpact_fin_timeout *fin_timeout,
1847 struct ds *s)
1848{
1849 ds_put_cstr(s, "fin_timeout(");
1850 if (fin_timeout->fin_idle_timeout) {
1851 ds_put_format(s, "idle_timeout=%"PRIu16",",
1852 fin_timeout->fin_idle_timeout);
1853 }
1854 if (fin_timeout->fin_hard_timeout) {
1855 ds_put_format(s, "hard_timeout=%"PRIu16",",
1856 fin_timeout->fin_hard_timeout);
1857 }
1858 ds_chomp(s, ',');
1859 ds_put_char(s, ')');
1860}
1861
1862static void
1863ofpact_format(const struct ofpact *a, struct ds *s)
1864{
1865 const struct ofpact_enqueue *enqueue;
1866 const struct ofpact_resubmit *resubmit;
1867 const struct ofpact_autopath *autopath;
1868 const struct ofpact_controller *controller;
4cceacb9 1869 const struct ofpact_metadata *metadata;
f25d0cf3
BP
1870 const struct ofpact_tunnel *tunnel;
1871 uint16_t port;
1872
1873 switch (a->type) {
1874 case OFPACT_OUTPUT:
1875 port = ofpact_get_OUTPUT(a)->port;
1876 if (port < OFPP_MAX) {
1877 ds_put_format(s, "output:%"PRIu16, port);
1878 } else {
1879 ofputil_format_port(port, s);
1880 if (port == OFPP_CONTROLLER) {
1881 ds_put_format(s, ":%"PRIu16, ofpact_get_OUTPUT(a)->max_len);
1882 }
1883 }
1884 break;
1885
1886 case OFPACT_CONTROLLER:
1887 controller = ofpact_get_CONTROLLER(a);
1888 if (controller->reason == OFPR_ACTION &&
1889 controller->controller_id == 0) {
1890 ds_put_format(s, "CONTROLLER:%"PRIu16,
1891 ofpact_get_CONTROLLER(a)->max_len);
1892 } else {
1893 enum ofp_packet_in_reason reason = controller->reason;
1894
1895 ds_put_cstr(s, "controller(");
1896 if (reason != OFPR_ACTION) {
1897 ds_put_format(s, "reason=%s,",
1898 ofputil_packet_in_reason_to_string(reason));
1899 }
1900 if (controller->max_len != UINT16_MAX) {
1901 ds_put_format(s, "max_len=%"PRIu16",", controller->max_len);
1902 }
1903 if (controller->controller_id != 0) {
1904 ds_put_format(s, "id=%"PRIu16",", controller->controller_id);
1905 }
1906 ds_chomp(s, ',');
1907 ds_put_char(s, ')');
1908 }
1909 break;
1910
1911 case OFPACT_ENQUEUE:
1912 enqueue = ofpact_get_ENQUEUE(a);
1913 ds_put_format(s, "enqueue:");
1914 ofputil_format_port(enqueue->port, s);
1915 ds_put_format(s, "q%"PRIu32, enqueue->queue);
1916 break;
1917
1918 case OFPACT_OUTPUT_REG:
1919 ds_put_cstr(s, "output:");
1920 mf_format_subfield(&ofpact_get_OUTPUT_REG(a)->src, s);
1921 break;
1922
1923 case OFPACT_BUNDLE:
1924 bundle_format(ofpact_get_BUNDLE(a), s);
1925 break;
1926
1927 case OFPACT_SET_VLAN_VID:
1928 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
1929 ofpact_get_SET_VLAN_VID(a)->vlan_vid);
1930 break;
1931
1932 case OFPACT_SET_VLAN_PCP:
1933 ds_put_format(s, "mod_vlan_pcp:%"PRIu8,
1934 ofpact_get_SET_VLAN_PCP(a)->vlan_pcp);
1935 break;
1936
1937 case OFPACT_STRIP_VLAN:
1938 ds_put_cstr(s, "strip_vlan");
1939 break;
1940
3e34fbdd 1941 case OFPACT_PUSH_VLAN:
5dca28b5 1942 /* XXX 802.1AD case*/
3e34fbdd
IY
1943 ds_put_format(s, "push_vlan:%#"PRIx16, ETH_TYPE_VLAN_8021Q);
1944 break;
1945
f25d0cf3
BP
1946 case OFPACT_SET_ETH_SRC:
1947 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
1948 ETH_ADDR_ARGS(ofpact_get_SET_ETH_SRC(a)->mac));
1949 break;
1950
1951 case OFPACT_SET_ETH_DST:
1952 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
1953 ETH_ADDR_ARGS(ofpact_get_SET_ETH_DST(a)->mac));
1954 break;
1955
1956 case OFPACT_SET_IPV4_SRC:
1957 ds_put_format(s, "mod_nw_src:"IP_FMT,
ed36537e 1958 IP_ARGS(ofpact_get_SET_IPV4_SRC(a)->ipv4));
f25d0cf3
BP
1959 break;
1960
1961 case OFPACT_SET_IPV4_DST:
1962 ds_put_format(s, "mod_nw_dst:"IP_FMT,
ed36537e 1963 IP_ARGS(ofpact_get_SET_IPV4_DST(a)->ipv4));
f25d0cf3
BP
1964 break;
1965
1966 case OFPACT_SET_IPV4_DSCP:
1967 ds_put_format(s, "mod_nw_tos:%d", ofpact_get_SET_IPV4_DSCP(a)->dscp);
1968 break;
1969
1970 case OFPACT_SET_L4_SRC_PORT:
1971 ds_put_format(s, "mod_tp_src:%d", ofpact_get_SET_L4_SRC_PORT(a)->port);
1972 break;
1973
1974 case OFPACT_SET_L4_DST_PORT:
1975 ds_put_format(s, "mod_tp_dst:%d", ofpact_get_SET_L4_DST_PORT(a)->port);
1976 break;
1977
1978 case OFPACT_REG_MOVE:
1979 nxm_format_reg_move(ofpact_get_REG_MOVE(a), s);
1980 break;
1981
1982 case OFPACT_REG_LOAD:
1983 nxm_format_reg_load(ofpact_get_REG_LOAD(a), s);
1984 break;
1985
1986 case OFPACT_DEC_TTL:
c2d967a5 1987 print_dec_ttl(ofpact_get_DEC_TTL(a), s);
f25d0cf3
BP
1988 break;
1989
1990 case OFPACT_SET_TUNNEL:
1991 tunnel = ofpact_get_SET_TUNNEL(a);
1992 ds_put_format(s, "set_tunnel%s:%#"PRIx64,
1993 (tunnel->tun_id > UINT32_MAX
1994 || a->compat == OFPUTIL_NXAST_SET_TUNNEL64 ? "64" : ""),
1995 tunnel->tun_id);
1996 break;
1997
1998 case OFPACT_SET_QUEUE:
1999 ds_put_format(s, "set_queue:%"PRIu32,
2000 ofpact_get_SET_QUEUE(a)->queue_id);
2001 break;
2002
2003 case OFPACT_POP_QUEUE:
2004 ds_put_cstr(s, "pop_queue");
2005 break;
2006
2007 case OFPACT_FIN_TIMEOUT:
2008 print_fin_timeout(ofpact_get_FIN_TIMEOUT(a), s);
2009 break;
2010
2011 case OFPACT_RESUBMIT:
2012 resubmit = ofpact_get_RESUBMIT(a);
2013 if (resubmit->in_port != OFPP_IN_PORT && resubmit->table_id == 255) {
c6100d92
BP
2014 ds_put_cstr(s, "resubmit:");
2015 ofputil_format_port(resubmit->in_port, s);
f25d0cf3
BP
2016 } else {
2017 ds_put_format(s, "resubmit(");
2018 if (resubmit->in_port != OFPP_IN_PORT) {
2019 ofputil_format_port(resubmit->in_port, s);
2020 }
2021 ds_put_char(s, ',');
2022 if (resubmit->table_id != 255) {
2023 ds_put_format(s, "%"PRIu8, resubmit->table_id);
2024 }
2025 ds_put_char(s, ')');
2026 }
2027 break;
2028
2029 case OFPACT_LEARN:
2030 learn_format(ofpact_get_LEARN(a), s);
2031 break;
2032
2033 case OFPACT_MULTIPATH:
2034 multipath_format(ofpact_get_MULTIPATH(a), s);
2035 break;
2036
2037 case OFPACT_AUTOPATH:
2038 autopath = ofpact_get_AUTOPATH(a);
c6100d92
BP
2039 ds_put_cstr(s, "autopath(");
2040 ofputil_format_port(autopath->port, s);
2041 ds_put_char(s, ',');
f25d0cf3
BP
2042 mf_format_subfield(&autopath->dst, s);
2043 ds_put_char(s, ')');
2044 break;
2045
2046 case OFPACT_NOTE:
2047 print_note(ofpact_get_NOTE(a), s);
2048 break;
2049
2050 case OFPACT_EXIT:
2051 ds_put_cstr(s, "exit");
2052 break;
8dd54666 2053
b19e8793
IY
2054 case OFPACT_CLEAR_ACTIONS:
2055 ds_put_format(s, "%s",
2056 ofpact_instruction_name_from_type(
2057 OVSINST_OFPIT11_CLEAR_ACTIONS));
2058 break;
2059
4cceacb9
JS
2060 case OFPACT_WRITE_METADATA:
2061 metadata = ofpact_get_WRITE_METADATA(a);
2062 ds_put_format(s, "%s:%#"PRIx64,
2063 ofpact_instruction_name_from_type(
2064 OVSINST_OFPIT11_WRITE_METADATA),
2065 ntohll(metadata->metadata));
2066 if (metadata->mask != htonll(UINT64_MAX)) {
2067 ds_put_format(s, "/%#"PRIx64, ntohll(metadata->mask));
2068 }
2069 break;
2070
8dd54666
IY
2071 case OFPACT_GOTO_TABLE:
2072 ds_put_format(s, "%s:%"PRIu8,
2073 ofpact_instruction_name_from_type(
2074 OVSINST_OFPIT11_GOTO_TABLE),
2075 ofpact_get_GOTO_TABLE(a)->table_id);
2076 break;
f25d0cf3
BP
2077 }
2078}
2079
2080/* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
2081 * 'ofpacts' to 'string'. */
2082void
2083ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
2084 struct ds *string)
2085{
2086 ds_put_cstr(string, "actions=");
2087 if (!ofpacts_len) {
2088 ds_put_cstr(string, "drop");
2089 } else {
2090 const struct ofpact *a;
2091
2092 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2093 if (a != ofpacts) {
2094 ds_put_cstr(string, ",");
2095 }
8dd54666 2096
5dca28b5 2097 /* XXX write-actions */
f25d0cf3
BP
2098 ofpact_format(a, string);
2099 }
2100 }
2101}
2102\f
2103/* Internal use by helpers. */
2104
2105void *
2106ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
2107{
2108 struct ofpact *ofpact;
2109
2110 ofpact_pad(ofpacts);
2111 ofpact = ofpacts->l2 = ofpbuf_put_uninit(ofpacts, len);
2112 ofpact_init(ofpact, type, len);
2113 return ofpact;
2114}
2115
2116void
2117ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
2118{
2119 memset(ofpact, 0, len);
2120 ofpact->type = type;
2121 ofpact->compat = OFPUTIL_ACTION_INVALID;
2122 ofpact->len = len;
2123}
2124\f
2125/* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
2126 * starting at 'ofpact'.
2127 *
2128 * This is the correct way to update a variable-length ofpact's length after
2129 * adding the variable-length part of the payload. (See the large comment
2130 * near the end of ofp-actions.h for more information.) */
2131void
2132ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
2133{
2134 assert(ofpact == ofpacts->l2);
2135 ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
2136}
2137
2138/* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
2139 * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
2140 * client must call this itself after adding the final ofpact to an array of
2141 * them.
2142 *
2143 * (The consequences of failing to call this function are probably not dire.
2144 * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
2145 * not dereference it. That's undefined behavior, technically, but it will not
2146 * cause a real problem on common systems. Still, it seems better to call
2147 * it.) */
2148void
2149ofpact_pad(struct ofpbuf *ofpacts)
2150{
2151 unsigned int rem = ofpacts->size % OFPACT_ALIGNTO;
2152 if (rem) {
2153 ofpbuf_put_zeros(ofpacts, OFPACT_ALIGNTO - rem);
2154 }
2155}
f5c45121
SH
2156
2157void
2158ofpact_set_field_init(struct ofpact_reg_load *load, const struct mf_field *mf,
2159 const void *src)
2160{
2161 load->ofpact.compat = OFPUTIL_OFPAT12_SET_FIELD;
2162 load->dst.field = mf;
2163 load->dst.ofs = 0;
2164 load->dst.n_bits = mf->n_bits;
2165 bitwise_copy(src, mf->n_bytes, load->dst.ofs,
2166 &load->subvalue, sizeof load->subvalue, 0, mf->n_bits);
2167}