]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ofp-actions.c
nx-match: Fix use-after-free parsing matches.
[mirror_ovs.git] / lib / ofp-actions.c
CommitLineData
f25d0cf3 1/*
77ab5fd2 2 * Copyright (c) 2008-2016 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>
d787ad39
JS
18#include <netinet/in.h>
19
f25d0cf3 20#include "ofp-actions.h"
f25d0cf3
BP
21#include "bundle.h"
22#include "byte-order.h"
b1c5bf1f 23#include "colors.h"
f25d0cf3 24#include "compiler.h"
d4abaff5 25#include "dummy.h"
3e8a2ad1 26#include "openvswitch/dynamic-string.h"
c2d936a4 27#include "hmap.h"
f25d0cf3
BP
28#include "learn.h"
29#include "meta-flow.h"
30#include "multipath.h"
31#include "nx-match.h"
9ac0aada 32#include "odp-netlink.h"
c2d936a4 33#include "ofp-parse.h"
bdcad671 34#include "ofp-prop.h"
f25d0cf3
BP
35#include "ofp-util.h"
36#include "ofpbuf.h"
c2d936a4 37#include "unaligned.h"
cb22974d 38#include "util.h"
e6211adc 39#include "openvswitch/vlog.h"
f25d0cf3
BP
40
41VLOG_DEFINE_THIS_MODULE(ofp_actions);
42
43static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
f25d0cf3 44
c2d936a4
BP
45struct ofp_action_header;
46
47/* Raw identifiers for OpenFlow actions.
48 *
49 * Decoding and encoding OpenFlow actions across multiple versions is difficult
50 * to do in a clean, consistent way. This enumeration lays out all of the
51 * forms of actions that Open vSwitch supports.
52 *
53 * The comments here must follow a stylized form because the
54 * "extract-ofp-actions" program parses them at build time to generate data
55 * tables.
56 *
57 * - The first part of each comment specifies the vendor, OpenFlow versions,
58 * and type for each protocol that supports the action:
59 *
60 * # The vendor is OF for standard OpenFlow actions, NX for Nicira
61 * extension actions. (Support for other vendors can be added, but
62 * it can't be done just based on a vendor ID definition alone
63 * because OpenFlow doesn't define a standard way to specify a
64 * subtype for vendor actions, so other vendors might do it different
65 * from Nicira.)
66 *
67 * # The version can specify a specific OpenFlow version, a version
68 * range delimited by "-", or an open-ended range with "+".
69 *
70 * # The type, in parentheses, is the action type number (for standard
71 * OpenFlow actions) or subtype (for vendor extension actions).
72 *
73 * # Optionally one may add "is deprecated" followed by a
74 * human-readable reason in parentheses (which will be used in log
75 * messages), if a particular action should no longer be used.
76 *
77 * Multiple such specifications may be separated by commas.
78 *
79 * - The second part describes the action's wire format. It may be:
80 *
81 * # "struct <name>": The struct fully specifies the wire format. The
82 * action is exactly the size of the struct. (Thus, the struct must
83 * be an exact multiple of 8 bytes in size.)
84 *
85 * # "struct <name>, ...": The struct specifies the beginning of the
86 * wire format. An instance of the action is either the struct's
87 * exact size, or a multiple of 8 bytes longer.
88 *
89 * # "uint<N>_t" or "ovs_be<N>": The action consists of a (standard or
90 * vendor extension) header, followed by 0 or more pad bytes to align
91 * to a multiple of <N> bits, followed by an argument of the given
92 * type, followed by 0 or more pad bytes to bring the total action up
93 * to a multiple of 8 bytes.
94 *
95 * # "void": The action is just a (standard or vendor extension)
96 * header.
97 *
98 * - Optional additional text enclosed in square brackets is commentary for
99 * the human reader.
100 */
101enum ofp_raw_action_type {
102/* ## ----------------- ## */
103/* ## Standard actions. ## */
104/* ## ----------------- ## */
105
106 /* OF1.0(0): struct ofp10_action_output. */
107 OFPAT_RAW10_OUTPUT,
108 /* OF1.1+(0): struct ofp11_action_output. */
109 OFPAT_RAW11_OUTPUT,
110
111 /* OF1.0(1): uint16_t. */
112 OFPAT_RAW10_SET_VLAN_VID,
113 /* OF1.0(2): uint8_t. */
114 OFPAT_RAW10_SET_VLAN_PCP,
115
116 /* OF1.1(1), OF1.2+(1) is deprecated (use Set-Field): uint16_t.
117 *
118 * [Semantics differ slightly between the 1.0 and 1.1 versions of the VLAN
119 * modification actions: the 1.0 versions push a VLAN header if none is
120 * present, but the 1.1 versions do not. That is the only reason that we
121 * distinguish their raw action types.] */
122 OFPAT_RAW11_SET_VLAN_VID,
123 /* OF1.1(2), OF1.2+(2) is deprecated (use Set-Field): uint8_t. */
124 OFPAT_RAW11_SET_VLAN_PCP,
125
126 /* OF1.1+(17): ovs_be16.
127 *
128 * [The argument is the Ethertype, e.g. ETH_TYPE_VLAN_8021Q, not the VID or
129 * TCI.] */
130 OFPAT_RAW11_PUSH_VLAN,
131
132 /* OF1.0(3): void. */
133 OFPAT_RAW10_STRIP_VLAN,
134 /* OF1.1+(18): void. */
135 OFPAT_RAW11_POP_VLAN,
136
137 /* OF1.0(4), OF1.1(3), OF1.2+(3) is deprecated (use Set-Field): struct
138 * ofp_action_dl_addr. */
139 OFPAT_RAW_SET_DL_SRC,
140
141 /* OF1.0(5), OF1.1(4), OF1.2+(4) is deprecated (use Set-Field): struct
142 * ofp_action_dl_addr. */
143 OFPAT_RAW_SET_DL_DST,
144
145 /* OF1.0(6), OF1.1(5), OF1.2+(5) is deprecated (use Set-Field):
146 * ovs_be32. */
147 OFPAT_RAW_SET_NW_SRC,
148
149 /* OF1.0(7), OF1.1(6), OF1.2+(6) is deprecated (use Set-Field):
150 * ovs_be32. */
151 OFPAT_RAW_SET_NW_DST,
152
153 /* OF1.0(8), OF1.1(7), OF1.2+(7) is deprecated (use Set-Field): uint8_t. */
154 OFPAT_RAW_SET_NW_TOS,
155
156 /* OF1.1(8), OF1.2+(8) is deprecated (use Set-Field): uint8_t. */
157 OFPAT_RAW11_SET_NW_ECN,
158
159 /* OF1.0(9), OF1.1(9), OF1.2+(9) is deprecated (use Set-Field):
160 * ovs_be16. */
161 OFPAT_RAW_SET_TP_SRC,
162
163 /* OF1.0(10), OF1.1(10), OF1.2+(10) is deprecated (use Set-Field):
164 * ovs_be16. */
165 OFPAT_RAW_SET_TP_DST,
166
167 /* OF1.0(11): struct ofp10_action_enqueue. */
168 OFPAT_RAW10_ENQUEUE,
169
170 /* NX1.0(30), OF1.1(13), OF1.2+(13) is deprecated (use Set-Field):
171 * ovs_be32. */
172 OFPAT_RAW_SET_MPLS_LABEL,
173
174 /* NX1.0(31), OF1.1(14), OF1.2+(14) is deprecated (use Set-Field):
175 * uint8_t. */
176 OFPAT_RAW_SET_MPLS_TC,
177
178 /* NX1.0(25), OF1.1(15), OF1.2+(15) is deprecated (use Set-Field):
179 * uint8_t. */
180 OFPAT_RAW_SET_MPLS_TTL,
181
182 /* NX1.0(26), OF1.1+(16): void. */
183 OFPAT_RAW_DEC_MPLS_TTL,
184
185 /* NX1.0(23), OF1.1+(19): ovs_be16.
186 *
187 * [The argument is the Ethertype, e.g. ETH_TYPE_MPLS, not the label.] */
188 OFPAT_RAW_PUSH_MPLS,
189
190 /* NX1.0(24), OF1.1+(20): ovs_be16.
191 *
192 * [The argument is the Ethertype, e.g. ETH_TYPE_IPV4 if at BoS or
193 * ETH_TYPE_MPLS otherwise, not the label.] */
194 OFPAT_RAW_POP_MPLS,
195
196 /* NX1.0(4), OF1.1+(21): uint32_t. */
197 OFPAT_RAW_SET_QUEUE,
198
199 /* OF1.1+(22): uint32_t. */
200 OFPAT_RAW11_GROUP,
201
202 /* OF1.1+(23): uint8_t. */
203 OFPAT_RAW11_SET_NW_TTL,
204
205 /* NX1.0(18), OF1.1+(24): void. */
206 OFPAT_RAW_DEC_NW_TTL,
207 /* NX1.0+(21): struct nx_action_cnt_ids, ... */
208 NXAST_RAW_DEC_TTL_CNT_IDS,
209
7eb4b1f1 210 /* OF1.2-1.4(25): struct ofp12_action_set_field, ... */
c2d936a4 211 OFPAT_RAW12_SET_FIELD,
7eb4b1f1
BP
212 /* OF1.5+(25): struct ofp12_action_set_field, ... */
213 OFPAT_RAW15_SET_FIELD,
214 /* NX1.0-1.4(7): struct nx_action_reg_load.
215 *
216 * [In OpenFlow 1.5, set_field is a superset of reg_load functionality, so
217 * we drop reg_load.] */
c2d936a4 218 NXAST_RAW_REG_LOAD,
bad8a439
BP
219 /* NX1.0-1.4(33): struct nx_action_reg_load2, ...
220 *
221 * [In OpenFlow 1.5, set_field is a superset of reg_load2 functionality, so
222 * we drop reg_load2.] */
223 NXAST_RAW_REG_LOAD2,
c2d936a4 224
f13cdd73 225 /* OF1.5+(28): struct ofp15_action_copy_field, ... */
73178f20 226 OFPAT_RAW15_COPY_FIELD,
914624f8
BP
227 /* ONF1.3-1.4(3200): struct onf_action_copy_field, ... */
228 ONFACT_RAW13_COPY_FIELD,
bad8a439 229 /* NX1.0-1.4(6): struct nx_action_reg_move, ... */
73178f20
BP
230 NXAST_RAW_REG_MOVE,
231
c2d936a4
BP
232/* ## ------------------------- ## */
233/* ## Nicira extension actions. ## */
234/* ## ------------------------- ## */
235
236/* Actions similar to standard actions are listed with the standard actions. */
237
238 /* NX1.0+(1): uint16_t. */
239 NXAST_RAW_RESUBMIT,
240 /* NX1.0+(14): struct nx_action_resubmit. */
241 NXAST_RAW_RESUBMIT_TABLE,
242
243 /* NX1.0+(2): uint32_t. */
244 NXAST_RAW_SET_TUNNEL,
245 /* NX1.0+(9): uint64_t. */
246 NXAST_RAW_SET_TUNNEL64,
247
248 /* NX1.0+(5): void. */
249 NXAST_RAW_POP_QUEUE,
250
c2d936a4
BP
251 /* NX1.0+(8): struct nx_action_note, ... */
252 NXAST_RAW_NOTE,
253
254 /* NX1.0+(10): struct nx_action_multipath. */
255 NXAST_RAW_MULTIPATH,
256
257 /* NX1.0+(12): struct nx_action_bundle, ... */
258 NXAST_RAW_BUNDLE,
259 /* NX1.0+(13): struct nx_action_bundle, ... */
260 NXAST_RAW_BUNDLE_LOAD,
261
262 /* NX1.0+(15): struct nx_action_output_reg. */
263 NXAST_RAW_OUTPUT_REG,
bad8a439
BP
264 /* NX1.0+(32): struct nx_action_output_reg2. */
265 NXAST_RAW_OUTPUT_REG2,
c2d936a4
BP
266
267 /* NX1.0+(16): struct nx_action_learn, ... */
268 NXAST_RAW_LEARN,
269
270 /* NX1.0+(17): void. */
271 NXAST_RAW_EXIT,
272
273 /* NX1.0+(19): struct nx_action_fin_timeout. */
274 NXAST_RAW_FIN_TIMEOUT,
275
276 /* NX1.0+(20): struct nx_action_controller. */
277 NXAST_RAW_CONTROLLER,
bdcad671
BP
278 /* NX1.0+(37): struct nx_action_controller2, ... */
279 NXAST_RAW_CONTROLLER2,
c2d936a4
BP
280
281 /* NX1.0+(22): struct nx_action_write_metadata. */
282 NXAST_RAW_WRITE_METADATA,
283
284 /* NX1.0+(27): struct nx_action_stack. */
285 NXAST_RAW_STACK_PUSH,
286
287 /* NX1.0+(28): struct nx_action_stack. */
288 NXAST_RAW_STACK_POP,
289
290 /* NX1.0+(29): struct nx_action_sample. */
291 NXAST_RAW_SAMPLE,
18080541
BP
292
293 /* NX1.0+(34): struct nx_action_conjunction. */
294 NXAST_RAW_CONJUNCTION,
d4abaff5 295
07659514
JS
296 /* NX1.0+(35): struct nx_action_conntrack, ... */
297 NXAST_RAW_CT,
298
9ac0aada
JR
299 /* NX1.0+(36): struct nx_action_nat, ... */
300 NXAST_RAW_NAT,
301
d4abaff5
BP
302/* ## ------------------ ## */
303/* ## Debugging actions. ## */
304/* ## ------------------ ## */
305
306/* These are intentionally undocumented, subject to change, and ovs-vswitchd */
307/* accepts them only if started with --enable-dummy. */
308
309 /* NX1.0+(255): void. */
310 NXAST_RAW_DEBUG_RECIRC,
c2d936a4
BP
311};
312
313/* OpenFlow actions are always a multiple of 8 bytes in length. */
314#define OFP_ACTION_ALIGN 8
315
316/* Define a few functions for working with instructions. */
317#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
318 static inline const struct STRUCT * OVS_UNUSED \
319 instruction_get_##ENUM(const struct ofp11_instruction *inst)\
320 { \
321 ovs_assert(inst->type == htons(ENUM)); \
322 return ALIGNED_CAST(struct STRUCT *, inst); \
323 } \
324 \
325 static inline void OVS_UNUSED \
326 instruction_init_##ENUM(struct STRUCT *s) \
327 { \
328 memset(s, 0, sizeof *s); \
329 s->type = htons(ENUM); \
330 s->len = htons(sizeof *s); \
331 } \
332 \
333 static inline struct STRUCT * OVS_UNUSED \
334 instruction_put_##ENUM(struct ofpbuf *buf) \
335 { \
336 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
337 instruction_init_##ENUM(s); \
338 return s; \
339 }
340OVS_INSTRUCTIONS
341#undef DEFINE_INST
342
343static void ofpacts_update_instruction_actions(struct ofpbuf *openflow,
344 size_t ofs);
178742f9 345static void pad_ofpat(struct ofpbuf *openflow, size_t start_ofs);
c2d936a4
BP
346
347static enum ofperr ofpacts_verify(const struct ofpact[], size_t ofpacts_len,
d824b5b7
JS
348 uint32_t allowed_ovsinsts,
349 enum ofpact_type outer_action);
c2d936a4
BP
350
351static void ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version,
352 enum mf_field_id, uint64_t value);
353
354static enum ofperr ofpact_pull_raw(struct ofpbuf *, enum ofp_version,
355 enum ofp_raw_action_type *, uint64_t *arg);
356static void *ofpact_put_raw(struct ofpbuf *, enum ofp_version,
357 enum ofp_raw_action_type, uint64_t arg);
358
cab50449 359static char *OVS_WARN_UNUSED_RESULT ofpacts_parse(
c2d936a4 360 char *str, struct ofpbuf *ofpacts, enum ofputil_protocol *usable_protocols,
d824b5b7 361 bool allow_instructions, enum ofpact_type outer_action);
07659514
JS
362static enum ofperr ofpacts_pull_openflow_actions__(
363 struct ofpbuf *openflow, unsigned int actions_len,
364 enum ofp_version version, uint32_t allowed_ovsinsts,
365 struct ofpbuf *ofpacts, enum ofpact_type outer_action);
8e53fe8c
JS
366static char * OVS_WARN_UNUSED_RESULT ofpacts_parse_copy(
367 const char *s_, struct ofpbuf *ofpacts,
368 enum ofputil_protocol *usable_protocols,
369 bool allow_instructions, enum ofpact_type outer_action);
d824b5b7 370
4f20179d
BP
371/* Returns the ofpact following 'ofpact', except that if 'ofpact' contains
372 * nested ofpacts it returns the first one. */
373struct ofpact *
374ofpact_next_flattened(const struct ofpact *ofpact)
375{
376 switch (ofpact->type) {
377 case OFPACT_OUTPUT:
378 case OFPACT_GROUP:
379 case OFPACT_CONTROLLER:
380 case OFPACT_ENQUEUE:
381 case OFPACT_OUTPUT_REG:
382 case OFPACT_BUNDLE:
383 case OFPACT_SET_FIELD:
384 case OFPACT_SET_VLAN_VID:
385 case OFPACT_SET_VLAN_PCP:
386 case OFPACT_STRIP_VLAN:
387 case OFPACT_PUSH_VLAN:
388 case OFPACT_SET_ETH_SRC:
389 case OFPACT_SET_ETH_DST:
390 case OFPACT_SET_IPV4_SRC:
391 case OFPACT_SET_IPV4_DST:
392 case OFPACT_SET_IP_DSCP:
393 case OFPACT_SET_IP_ECN:
394 case OFPACT_SET_IP_TTL:
395 case OFPACT_SET_L4_SRC_PORT:
396 case OFPACT_SET_L4_DST_PORT:
397 case OFPACT_REG_MOVE:
398 case OFPACT_STACK_PUSH:
399 case OFPACT_STACK_POP:
400 case OFPACT_DEC_TTL:
401 case OFPACT_SET_MPLS_LABEL:
402 case OFPACT_SET_MPLS_TC:
403 case OFPACT_SET_MPLS_TTL:
404 case OFPACT_DEC_MPLS_TTL:
405 case OFPACT_PUSH_MPLS:
406 case OFPACT_POP_MPLS:
407 case OFPACT_SET_TUNNEL:
408 case OFPACT_SET_QUEUE:
409 case OFPACT_POP_QUEUE:
410 case OFPACT_FIN_TIMEOUT:
411 case OFPACT_RESUBMIT:
412 case OFPACT_LEARN:
413 case OFPACT_CONJUNCTION:
414 case OFPACT_MULTIPATH:
415 case OFPACT_NOTE:
416 case OFPACT_EXIT:
417 case OFPACT_SAMPLE:
418 case OFPACT_UNROLL_XLATE:
419 case OFPACT_DEBUG_RECIRC:
420 case OFPACT_METER:
421 case OFPACT_CLEAR_ACTIONS:
422 case OFPACT_WRITE_METADATA:
423 case OFPACT_GOTO_TABLE:
424 case OFPACT_NAT:
425 return ofpact_next(ofpact);
426
427 case OFPACT_CT:
428 return ofpact_get_CT(ofpact)->actions;
429
430 case OFPACT_WRITE_ACTIONS:
431 return ofpact_get_WRITE_ACTIONS(ofpact)->actions;
432 }
433
434 OVS_NOT_REACHED();
435}
436
d824b5b7
JS
437/* Pull off existing actions or instructions. Used by nesting actions to keep
438 * ofpacts_parse() oblivious of actions nesting.
439 *
440 * Push the actions back on after nested parsing, e.g.:
441 *
442 * size_t ofs = ofpacts_pull(ofpacts);
443 * ...nested parsing...
444 * ofpbuf_push_uninit(ofpacts, ofs);
445 */
446static size_t
447ofpacts_pull(struct ofpbuf *ofpacts)
448{
449 size_t ofs;
450
d824b5b7
JS
451 ofs = ofpacts->size;
452 ofpbuf_pull(ofpacts, ofs);
453
454 return ofs;
455}
c2d936a4
BP
456
457#include "ofp-actions.inc1"
458\f
459/* Output actions. */
460
461/* Action structure for OFPAT10_OUTPUT, which sends packets out 'port'.
462 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
463 * number of bytes to send. A 'max_len' of zero means no bytes of the
464 * packet should be sent. */
465struct ofp10_action_output {
466 ovs_be16 type; /* OFPAT10_OUTPUT. */
467 ovs_be16 len; /* Length is 8. */
468 ovs_be16 port; /* Output port. */
469 ovs_be16 max_len; /* Max length to send to controller. */
e45e72f1 470};
c2d936a4
BP
471OFP_ASSERT(sizeof(struct ofp10_action_output) == 8);
472
473/* Action structure for OFPAT_OUTPUT, which sends packets out 'port'.
474 * When the 'port' is the OFPP_CONTROLLER, 'max_len' indicates the max
475 * number of bytes to send. A 'max_len' of zero means no bytes of the
476 * packet should be sent.*/
477struct ofp11_action_output {
478 ovs_be16 type; /* OFPAT11_OUTPUT. */
479 ovs_be16 len; /* Length is 16. */
480 ovs_be32 port; /* Output port. */
481 ovs_be16 max_len; /* Max length to send to controller. */
482 uint8_t pad[6]; /* Pad to 64 bits. */
483};
484OFP_ASSERT(sizeof(struct ofp11_action_output) == 16);
e45e72f1 485
f25d0cf3 486static enum ofperr
c2d936a4 487decode_OFPAT_RAW10_OUTPUT(const struct ofp10_action_output *oao,
f3cd3ac7 488 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4 489 struct ofpbuf *out)
f25d0cf3
BP
490{
491 struct ofpact_output *output;
492
493 output = ofpact_put_OUTPUT(out);
4e022ec0 494 output->port = u16_to_ofp(ntohs(oao->port));
f25d0cf3
BP
495 output->max_len = ntohs(oao->max_len);
496
57ad4e9e 497 return ofpact_check_output_port(output->port, OFPP_MAX);
f25d0cf3
BP
498}
499
500static enum ofperr
c2d936a4 501decode_OFPAT_RAW11_OUTPUT(const struct ofp11_action_output *oao,
f3cd3ac7
JS
502 enum ofp_version ofp_version OVS_UNUSED,
503 struct ofpbuf *out)
f25d0cf3 504{
c2d936a4
BP
505 struct ofpact_output *output;
506 enum ofperr error;
f25d0cf3 507
c2d936a4
BP
508 output = ofpact_put_OUTPUT(out);
509 output->max_len = ntohs(oao->max_len);
510
511 error = ofputil_port_from_ofp11(oao->port, &output->port);
512 if (error) {
513 return error;
f25d0cf3 514 }
c2d936a4
BP
515
516 return ofpact_check_output_port(output->port, OFPP_MAX);
f25d0cf3
BP
517}
518
519static void
c2d936a4
BP
520encode_OUTPUT(const struct ofpact_output *output,
521 enum ofp_version ofp_version, struct ofpbuf *out)
f25d0cf3 522{
c2d936a4
BP
523 if (ofp_version == OFP10_VERSION) {
524 struct ofp10_action_output *oao;
f25d0cf3 525
c2d936a4
BP
526 oao = put_OFPAT10_OUTPUT(out);
527 oao->port = htons(ofp_to_u16(output->port));
528 oao->max_len = htons(output->max_len);
529 } else {
530 struct ofp11_action_output *oao;
531
532 oao = put_OFPAT11_OUTPUT(out);
533 oao->port = ofputil_port_to_ofp11(output->port);
534 oao->max_len = htons(output->max_len);
535 }
f25d0cf3
BP
536}
537
cab50449 538static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
539parse_OUTPUT(const char *arg, struct ofpbuf *ofpacts,
540 enum ofputil_protocol *usable_protocols OVS_UNUSED)
f25d0cf3 541{
c2d936a4
BP
542 if (strchr(arg, '[')) {
543 struct ofpact_output_reg *output_reg;
f25d0cf3 544
c2d936a4
BP
545 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
546 output_reg->max_len = UINT16_MAX;
547 return mf_parse_subfield(&output_reg->src, arg);
548 } else {
549 struct ofpact_output *output;
550
551 output = ofpact_put_OUTPUT(ofpacts);
552 if (!ofputil_port_from_string(arg, &output->port)) {
553 return xasprintf("%s: output to unknown port", arg);
554 }
555 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
556 return NULL;
f25d0cf3 557 }
c2d936a4 558}
f25d0cf3 559
c2d936a4
BP
560static void
561format_OUTPUT(const struct ofpact_output *a, struct ds *s)
562{
563 if (ofp_to_u16(a->port) < ofp_to_u16(OFPP_MAX)) {
b1c5bf1f
QM
564 ds_put_format(s, "%soutput:%s%"PRIu16,
565 colors.special, colors.end, a->port);
c2d936a4
BP
566 } else {
567 ofputil_format_port(a->port, s);
568 if (a->port == OFPP_CONTROLLER) {
569 ds_put_format(s, ":%"PRIu16, a->max_len);
570 }
571 }
f25d0cf3 572}
c2d936a4
BP
573\f
574/* Group actions. */
f25d0cf3
BP
575
576static enum ofperr
f3cd3ac7
JS
577decode_OFPAT_RAW11_GROUP(uint32_t group_id,
578 enum ofp_version ofp_version OVS_UNUSED,
579 struct ofpbuf *out)
f25d0cf3 580{
c2d936a4
BP
581 ofpact_put_GROUP(out)->group_id = group_id;
582 return 0;
583}
f25d0cf3 584
c2d936a4
BP
585static void
586encode_GROUP(const struct ofpact_group *group,
587 enum ofp_version ofp_version, struct ofpbuf *out)
588{
589 if (ofp_version == OFP10_VERSION) {
590 /* XXX */
591 } else {
592 put_OFPAT11_GROUP(out, group->group_id);
f25d0cf3 593 }
c2d936a4 594}
f25d0cf3 595
cab50449 596static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
597parse_GROUP(char *arg, struct ofpbuf *ofpacts,
598 enum ofputil_protocol *usable_protocols OVS_UNUSED)
599{
600 return str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
f25d0cf3
BP
601}
602
603static void
c2d936a4 604format_GROUP(const struct ofpact_group *a, struct ds *s)
f25d0cf3 605{
b1c5bf1f
QM
606 ds_put_format(s, "%sgroup:%s%"PRIu32,
607 colors.special, colors.end, a->group_id);
f25d0cf3 608}
c2d936a4
BP
609\f
610/* Action structure for NXAST_CONTROLLER.
611 *
612 * This generalizes using OFPAT_OUTPUT to send a packet to OFPP_CONTROLLER. In
613 * addition to the 'max_len' that OFPAT_OUTPUT supports, it also allows
614 * specifying:
615 *
616 * - 'reason': The reason code to use in the ofp_packet_in or nx_packet_in.
617 *
618 * - 'controller_id': The ID of the controller connection to which the
619 * ofp_packet_in should be sent. The ofp_packet_in or nx_packet_in is
620 * sent only to controllers that have the specified controller connection
621 * ID. See "struct nx_controller_id" for more information. */
622struct nx_action_controller {
623 ovs_be16 type; /* OFPAT_VENDOR. */
624 ovs_be16 len; /* Length is 16. */
625 ovs_be32 vendor; /* NX_VENDOR_ID. */
626 ovs_be16 subtype; /* NXAST_CONTROLLER. */
627 ovs_be16 max_len; /* Maximum length to send to controller. */
628 ovs_be16 controller_id; /* Controller ID to send packet-in. */
629 uint8_t reason; /* enum ofp_packet_in_reason (OFPR_*). */
630 uint8_t zero; /* Must be zero. */
631};
632OFP_ASSERT(sizeof(struct nx_action_controller) == 16);
f25d0cf3 633
77ab5fd2
BP
634/* Properties for NXAST_CONTROLLER2.
635 *
636 * For more information on the effect of NXAC2PT_PAUSE, see the large comment
637 * on NXT_PACKET_IN2 in nicira-ext.h */
bdcad671
BP
638enum nx_action_controller2_prop_type {
639 NXAC2PT_MAX_LEN, /* ovs_be16 max bytes to send (default all). */
640 NXAC2PT_CONTROLLER_ID, /* ovs_be16 dest controller ID (default 0). */
641 NXAC2PT_REASON, /* uint8_t reason (OFPR_*), default 0. */
642 NXAC2PT_USERDATA, /* Data to copy into NXPINT_USERDATA. */
77ab5fd2 643 NXAC2PT_PAUSE, /* Flag to pause pipeline to resume later. */
bdcad671
BP
644};
645
646/* Action structure for NXAST_CONTROLLER2.
647 *
648 * This replacement for NXAST_CONTROLLER makes it extensible via properties. */
649struct nx_action_controller2 {
650 ovs_be16 type; /* OFPAT_VENDOR. */
651 ovs_be16 len; /* Length is 16 or more. */
652 ovs_be32 vendor; /* NX_VENDOR_ID. */
653 ovs_be16 subtype; /* NXAST_CONTROLLER2. */
654 uint8_t zeros[6]; /* Must be zero. */
655 /* Followed by NXAC2PT_* properties. */
656};
657OFP_ASSERT(sizeof(struct nx_action_controller2) == 16);
658
c2d936a4
BP
659static enum ofperr
660decode_NXAST_RAW_CONTROLLER(const struct nx_action_controller *nac,
f3cd3ac7 661 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4 662 struct ofpbuf *out)
f25d0cf3
BP
663{
664 struct ofpact_controller *oc;
665
666 oc = ofpact_put_CONTROLLER(out);
bdcad671 667 oc->ofpact.raw = NXAST_RAW_CONTROLLER;
f25d0cf3
BP
668 oc->max_len = ntohs(nac->max_len);
669 oc->controller_id = ntohs(nac->controller_id);
670 oc->reason = nac->reason;
bdcad671
BP
671 ofpact_finish(out, &oc->ofpact);
672
673 return 0;
674}
675
676static enum ofperr
677decode_NXAST_RAW_CONTROLLER2(const struct nx_action_controller2 *nac2,
678 enum ofp_version ofp_version OVS_UNUSED,
679 struct ofpbuf *out)
680{
681 if (!is_all_zeros(nac2->zeros, sizeof nac2->zeros)) {
682 return OFPERR_NXBRC_MUST_BE_ZERO;
683 }
684
685 size_t start_ofs = out->size;
686 struct ofpact_controller *oc = ofpact_put_CONTROLLER(out);
687 oc->ofpact.raw = NXAST_RAW_CONTROLLER2;
688 oc->max_len = UINT16_MAX;
689 oc->reason = OFPR_ACTION;
690
691 struct ofpbuf properties;
692 ofpbuf_use_const(&properties, nac2, ntohs(nac2->len));
693 ofpbuf_pull(&properties, sizeof *nac2);
694
695 while (properties.size > 0) {
696 struct ofpbuf payload;
697 uint64_t type;
698
699 enum ofperr error = ofpprop_pull(&properties, &payload, &type);
700 if (error) {
701 return error;
702 }
703
704 switch (type) {
705 case NXAC2PT_MAX_LEN:
706 error = ofpprop_parse_u16(&payload, &oc->max_len);
707 break;
708
709 case NXAC2PT_CONTROLLER_ID:
710 error = ofpprop_parse_u16(&payload, &oc->controller_id);
711 break;
712
713 case NXAC2PT_REASON: {
714 uint8_t u8;
715 error = ofpprop_parse_u8(&payload, &u8);
716 oc->reason = u8;
717 break;
718 }
719
720 case NXAC2PT_USERDATA:
721 out->size = start_ofs + OFPACT_CONTROLLER_SIZE;
722 ofpbuf_put(out, payload.msg, ofpbuf_msgsize(&payload));
723 oc = ofpbuf_at_assert(out, start_ofs, sizeof *oc);
724 oc->userdata_len = ofpbuf_msgsize(&payload);
725 break;
726
77ab5fd2
BP
727 case NXAC2PT_PAUSE:
728 oc->pause = true;
729 break;
730
bdcad671
BP
731 default:
732 error = OFPPROP_UNKNOWN(false, "NXAST_RAW_CONTROLLER2", type);
733 break;
734 }
735 if (error) {
736 return error;
737 }
738 }
739
740 ofpact_finish(out, &oc->ofpact);
741
c2d936a4 742 return 0;
f25d0cf3
BP
743}
744
c2d936a4
BP
745static void
746encode_CONTROLLER(const struct ofpact_controller *controller,
747 enum ofp_version ofp_version OVS_UNUSED,
748 struct ofpbuf *out)
4cceacb9 749{
bdcad671 750 if (controller->userdata_len
77ab5fd2 751 || controller->pause
bdcad671
BP
752 || controller->ofpact.raw == NXAST_RAW_CONTROLLER2) {
753 size_t start_ofs = out->size;
754 put_NXAST_CONTROLLER2(out);
755 if (controller->max_len != UINT16_MAX) {
756 ofpprop_put_u16(out, NXAC2PT_MAX_LEN, controller->max_len);
757 }
758 if (controller->controller_id != 0) {
759 ofpprop_put_u16(out, NXAC2PT_CONTROLLER_ID,
760 controller->controller_id);
761 }
762 if (controller->reason != OFPR_ACTION) {
763 ofpprop_put_u8(out, NXAC2PT_REASON, controller->reason);
764 }
765 if (controller->userdata_len != 0) {
766 ofpprop_put(out, NXAC2PT_USERDATA, controller->userdata,
767 controller->userdata_len);
768 }
77ab5fd2
BP
769 if (controller->pause) {
770 ofpprop_put_flag(out, NXAC2PT_PAUSE);
771 }
bdcad671
BP
772 pad_ofpat(out, start_ofs);
773 } else {
774 struct nx_action_controller *nac;
4cceacb9 775
bdcad671
BP
776 nac = put_NXAST_CONTROLLER(out);
777 nac->max_len = htons(controller->max_len);
778 nac->controller_id = htons(controller->controller_id);
779 nac->reason = controller->reason;
780 }
c2d936a4
BP
781}
782
cab50449 783static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
784parse_CONTROLLER(char *arg, struct ofpbuf *ofpacts,
785 enum ofputil_protocol *usable_protocols OVS_UNUSED)
786{
787 enum ofp_packet_in_reason reason = OFPR_ACTION;
788 uint16_t controller_id = 0;
789 uint16_t max_len = UINT16_MAX;
bdcad671 790 const char *userdata = NULL;
77ab5fd2 791 bool pause = false;
c2d936a4
BP
792
793 if (!arg[0]) {
794 /* Use defaults. */
795 } else if (strspn(arg, "0123456789") == strlen(arg)) {
796 char *error = str_to_u16(arg, "max_len", &max_len);
797 if (error) {
798 return error;
799 }
800 } else {
801 char *name, *value;
802
803 while (ofputil_parse_key_value(&arg, &name, &value)) {
804 if (!strcmp(name, "reason")) {
805 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
806 return xasprintf("unknown reason \"%s\"", value);
807 }
808 } else if (!strcmp(name, "max_len")) {
809 char *error = str_to_u16(value, "max_len", &max_len);
810 if (error) {
811 return error;
812 }
813 } else if (!strcmp(name, "id")) {
814 char *error = str_to_u16(value, "id", &controller_id);
815 if (error) {
816 return error;
817 }
bdcad671
BP
818 } else if (!strcmp(name, "userdata")) {
819 userdata = value;
77ab5fd2
BP
820 } else if (!strcmp(name, "pause")) {
821 pause = true;
c2d936a4
BP
822 } else {
823 return xasprintf("unknown key \"%s\" parsing controller "
824 "action", name);
825 }
826 }
4cceacb9
JS
827 }
828
77ab5fd2 829 if (reason == OFPR_ACTION && controller_id == 0 && !userdata && !pause) {
c2d936a4 830 struct ofpact_output *output;
4cceacb9 831
c2d936a4
BP
832 output = ofpact_put_OUTPUT(ofpacts);
833 output->port = OFPP_CONTROLLER;
834 output->max_len = max_len;
835 } else {
836 struct ofpact_controller *controller;
837
838 controller = ofpact_put_CONTROLLER(ofpacts);
839 controller->max_len = max_len;
840 controller->reason = reason;
841 controller->controller_id = controller_id;
77ab5fd2 842 controller->pause = pause;
bdcad671
BP
843
844 if (userdata) {
845 size_t start_ofs = ofpacts->size;
846 const char *end = ofpbuf_put_hex(ofpacts, userdata, NULL);
847 if (*end) {
848 return xstrdup("bad hex digit in `controller' "
849 "action `userdata'");
850 }
851 size_t userdata_len = ofpacts->size - start_ofs;
852 controller = ofpacts->header;
853 controller->userdata_len = userdata_len;
854 }
855 ofpact_finish(ofpacts, &controller->ofpact);
c2d936a4
BP
856 }
857
858 return NULL;
4cceacb9
JS
859}
860
bdcad671
BP
861static void
862format_hex_arg(struct ds *s, const uint8_t *data, size_t len)
863{
864 for (size_t i = 0; i < len; i++) {
865 if (i) {
866 ds_put_char(s, '.');
867 }
868 ds_put_format(s, "%02"PRIx8, data[i]);
869 }
870}
871
f25d0cf3 872static void
c2d936a4 873format_CONTROLLER(const struct ofpact_controller *a, struct ds *s)
f25d0cf3 874{
77ab5fd2
BP
875 if (a->reason == OFPR_ACTION && !a->controller_id && !a->userdata_len
876 && !a->pause) {
b1c5bf1f
QM
877 ds_put_format(s, "%sCONTROLLER:%s%"PRIu16,
878 colors.special, colors.end, a->max_len);
c2d936a4
BP
879 } else {
880 enum ofp_packet_in_reason reason = a->reason;
f25d0cf3 881
b1c5bf1f 882 ds_put_format(s, "%scontroller(%s", colors.paren, colors.end);
c2d936a4
BP
883 if (reason != OFPR_ACTION) {
884 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
885
b1c5bf1f 886 ds_put_format(s, "%sreason=%s%s,", colors.param, colors.end,
c2d936a4
BP
887 ofputil_packet_in_reason_to_string(
888 reason, reasonbuf, sizeof reasonbuf));
889 }
890 if (a->max_len != UINT16_MAX) {
b1c5bf1f
QM
891 ds_put_format(s, "%smax_len=%s%"PRIu16",",
892 colors.param, colors.end, a->max_len);
c2d936a4
BP
893 }
894 if (a->controller_id != 0) {
b1c5bf1f
QM
895 ds_put_format(s, "%sid=%s%"PRIu16",",
896 colors.param, colors.end, a->controller_id);
c2d936a4 897 }
bdcad671 898 if (a->userdata_len) {
b1c5bf1f 899 ds_put_format(s, "%suserdata=%s", colors.param, colors.end);
bdcad671
BP
900 format_hex_arg(s, a->userdata, a->userdata_len);
901 ds_put_char(s, ',');
902 }
77ab5fd2 903 if (a->pause) {
b1c5bf1f 904 ds_put_format(s, "%spause%s,", colors.value, colors.end);
77ab5fd2 905 }
c2d936a4 906 ds_chomp(s, ',');
b1c5bf1f 907 ds_put_format(s, "%s)%s", colors.paren, colors.end);
c2d936a4 908 }
f25d0cf3 909}
c2d936a4
BP
910\f
911/* Enqueue action. */
912struct ofp10_action_enqueue {
913 ovs_be16 type; /* OFPAT10_ENQUEUE. */
914 ovs_be16 len; /* Len is 16. */
915 ovs_be16 port; /* Port that queue belongs. Should
916 refer to a valid physical port
917 (i.e. < OFPP_MAX) or OFPP_IN_PORT. */
918 uint8_t pad[6]; /* Pad for 64-bit alignment. */
919 ovs_be32 queue_id; /* Where to enqueue the packets. */
920};
921OFP_ASSERT(sizeof(struct ofp10_action_enqueue) == 16);
f25d0cf3 922
c2d967a5 923static enum ofperr
c2d936a4 924decode_OFPAT_RAW10_ENQUEUE(const struct ofp10_action_enqueue *oae,
f3cd3ac7 925 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4 926 struct ofpbuf *out)
c2d967a5 927{
c2d936a4 928 struct ofpact_enqueue *enqueue;
c2d967a5 929
c2d936a4
BP
930 enqueue = ofpact_put_ENQUEUE(out);
931 enqueue->port = u16_to_ofp(ntohs(oae->port));
932 enqueue->queue = ntohl(oae->queue_id);
933 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(OFPP_MAX)
934 && enqueue->port != OFPP_IN_PORT
935 && enqueue->port != OFPP_LOCAL) {
936 return OFPERR_OFPBAC_BAD_OUT_PORT;
937 }
938 return 0;
c2d967a5
MM
939}
940
c2d936a4
BP
941static void
942encode_ENQUEUE(const struct ofpact_enqueue *enqueue,
943 enum ofp_version ofp_version, struct ofpbuf *out)
c2d967a5 944{
c2d936a4
BP
945 if (ofp_version == OFP10_VERSION) {
946 struct ofp10_action_enqueue *oae;
c2d967a5 947
c2d936a4
BP
948 oae = put_OFPAT10_ENQUEUE(out);
949 oae->port = htons(ofp_to_u16(enqueue->port));
950 oae->queue_id = htonl(enqueue->queue);
951 } else {
952 /* XXX */
c2d967a5 953 }
c2d936a4 954}
c2d967a5 955
cab50449 956static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
957parse_ENQUEUE(char *arg, struct ofpbuf *ofpacts,
958 enum ofputil_protocol *usable_protocols OVS_UNUSED)
959{
960 char *sp = NULL;
961 char *port = strtok_r(arg, ":q,", &sp);
962 char *queue = strtok_r(NULL, "", &sp);
963 struct ofpact_enqueue *enqueue;
c2d967a5 964
c2d936a4
BP
965 if (port == NULL || queue == NULL) {
966 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
967 "\"enqueue(PORT,QUEUE)\"");
c2d967a5
MM
968 }
969
c2d936a4
BP
970 enqueue = ofpact_put_ENQUEUE(ofpacts);
971 if (!ofputil_port_from_string(port, &enqueue->port)) {
972 return xasprintf("%s: enqueue to unknown port", port);
973 }
974 return str_to_u32(queue, &enqueue->queue);
c2d967a5
MM
975}
976
c2d936a4
BP
977static void
978format_ENQUEUE(const struct ofpact_enqueue *a, struct ds *s)
29089a54 979{
b1c5bf1f 980 ds_put_format(s, "%senqueue:%s", colors.param, colors.end);
c2d936a4
BP
981 ofputil_format_port(a->port, s);
982 ds_put_format(s, ":%"PRIu32, a->queue);
983}
984\f
985/* Action structure for NXAST_OUTPUT_REG.
986 *
987 * Outputs to the OpenFlow port number written to src[ofs:ofs+nbits].
988 *
989 * The format and semantics of 'src' and 'ofs_nbits' are similar to those for
990 * the NXAST_REG_LOAD action.
991 *
992 * The acceptable nxm_header values for 'src' are the same as the acceptable
993 * nxm_header values for the 'src' field of NXAST_REG_MOVE.
994 *
995 * The 'max_len' field indicates the number of bytes to send when the chosen
996 * port is OFPP_CONTROLLER. Its semantics are equivalent to the 'max_len'
997 * field of OFPAT_OUTPUT.
998 *
999 * The 'zero' field is required to be zeroed for forward compatibility. */
1000struct nx_action_output_reg {
1001 ovs_be16 type; /* OFPAT_VENDOR. */
1002 ovs_be16 len; /* 24. */
1003 ovs_be32 vendor; /* NX_VENDOR_ID. */
1004 ovs_be16 subtype; /* NXAST_OUTPUT_REG. */
29089a54 1005
c2d936a4
BP
1006 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1007 ovs_be32 src; /* Source. */
29089a54 1008
c2d936a4 1009 ovs_be16 max_len; /* Max length to send to controller. */
29089a54 1010
c2d936a4
BP
1011 uint8_t zero[6]; /* Reserved, must be zero. */
1012};
1013OFP_ASSERT(sizeof(struct nx_action_output_reg) == 24);
29089a54 1014
bad8a439
BP
1015/* Action structure for NXAST_OUTPUT_REG2.
1016 *
1017 * Like the NXAST_OUTPUT_REG but organized so that there is room for a 64-bit
1018 * experimenter OXM as 'src'.
1019 */
1020struct nx_action_output_reg2 {
1021 ovs_be16 type; /* OFPAT_VENDOR. */
1022 ovs_be16 len; /* 24. */
1023 ovs_be32 vendor; /* NX_VENDOR_ID. */
1024 ovs_be16 subtype; /* NXAST_OUTPUT_REG2. */
1025
1026 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1027 ovs_be16 max_len; /* Max length to send to controller. */
1028
1029 /* Followed by:
1030 * - 'src', as an OXM/NXM header (either 4 or 8 bytes).
1031 * - Enough 0-bytes to pad the action out to 24 bytes. */
1032 uint8_t pad[10];
1033};
1034OFP_ASSERT(sizeof(struct nx_action_output_reg2) == 24);
1035
a7a2d006 1036static enum ofperr
c2d936a4 1037decode_NXAST_RAW_OUTPUT_REG(const struct nx_action_output_reg *naor,
f3cd3ac7 1038 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4 1039 struct ofpbuf *out)
a7a2d006 1040{
c2d936a4 1041 struct ofpact_output_reg *output_reg;
a7a2d006 1042
c2d936a4 1043 if (!is_all_zeros(naor->zero, sizeof naor->zero)) {
a7a2d006
JS
1044 return OFPERR_OFPBAC_BAD_ARGUMENT;
1045 }
a7a2d006 1046
c2d936a4 1047 output_reg = ofpact_put_OUTPUT_REG(out);
bad8a439 1048 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG;
c2d936a4
BP
1049 output_reg->src.field = mf_from_nxm_header(ntohl(naor->src));
1050 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1051 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1052 output_reg->max_len = ntohs(naor->max_len);
1053
1054 return mf_check_src(&output_reg->src, NULL);
a7a2d006
JS
1055}
1056
bad8a439
BP
1057static enum ofperr
1058decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
f3cd3ac7
JS
1059 enum ofp_version ofp_version OVS_UNUSED,
1060 struct ofpbuf *out)
bad8a439
BP
1061{
1062 struct ofpact_output_reg *output_reg;
bad8a439
BP
1063 output_reg = ofpact_put_OUTPUT_REG(out);
1064 output_reg->ofpact.raw = NXAST_RAW_OUTPUT_REG2;
1065 output_reg->src.ofs = nxm_decode_ofs(naor->ofs_nbits);
1066 output_reg->src.n_bits = nxm_decode_n_bits(naor->ofs_nbits);
1067 output_reg->max_len = ntohs(naor->max_len);
1068
0a2869d5 1069 struct ofpbuf b = ofpbuf_const_initializer(naor, ntohs(naor->len));
bad8a439 1070 ofpbuf_pull(&b, OBJECT_OFFSETOF(naor, pad));
0a2869d5
BP
1071
1072 enum ofperr error = nx_pull_header(&b, &output_reg->src.field, NULL);
bad8a439
BP
1073 if (error) {
1074 return error;
1075 }
6fd6ed71 1076 if (!is_all_zeros(b.data, b.size)) {
bad8a439
BP
1077 return OFPERR_NXBRC_MUST_BE_ZERO;
1078 }
1079
1080 return mf_check_src(&output_reg->src, NULL);
1081}
1082
c2d936a4
BP
1083static void
1084encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
1085 enum ofp_version ofp_version OVS_UNUSED,
1086 struct ofpbuf *out)
f25d0cf3 1087{
bad8a439
BP
1088 /* If 'output_reg' came in as an NXAST_RAW_OUTPUT_REG2 action, or if it
1089 * cannot be encoded in the older form, encode it as
1090 * NXAST_RAW_OUTPUT_REG2. */
1091 if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
1092 || !mf_nxm_header(output_reg->src.field->id)) {
1093 struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
6fd6ed71 1094 size_t size = out->size;
bad8a439
BP
1095
1096 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1097 output_reg->src.n_bits);
1098 naor->max_len = htons(output_reg->max_len);
1099
6fd6ed71 1100 out->size = size - sizeof naor->pad;
bad8a439 1101 nx_put_header(out, output_reg->src.field->id, 0, false);
6fd6ed71 1102 out->size = size;
bad8a439
BP
1103 } else {
1104 struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
f25d0cf3 1105
bad8a439
BP
1106 naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
1107 output_reg->src.n_bits);
1108 naor->src = htonl(mf_nxm_header(output_reg->src.field->id));
1109 naor->max_len = htons(output_reg->max_len);
1110 }
f25d0cf3
BP
1111}
1112
cab50449 1113static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1114parse_OUTPUT_REG(const char *arg, struct ofpbuf *ofpacts,
1115 enum ofputil_protocol *usable_protocols OVS_UNUSED)
f25d0cf3 1116{
c2d936a4 1117 return parse_OUTPUT(arg, ofpacts, usable_protocols);
f25d0cf3
BP
1118}
1119
c2d936a4
BP
1120static void
1121format_OUTPUT_REG(const struct ofpact_output_reg *a, struct ds *s)
f25d0cf3 1122{
b1c5bf1f 1123 ds_put_format(s, "%soutput:%s", colors.special, colors.end);
c2d936a4
BP
1124 mf_format_subfield(&a->src, s);
1125}
1126\f
1127/* Action structure for NXAST_BUNDLE and NXAST_BUNDLE_LOAD.
1128 *
1129 * The bundle actions choose a slave from a supplied list of options.
1130 * NXAST_BUNDLE outputs to its selection. NXAST_BUNDLE_LOAD writes its
1131 * selection to a register.
1132 *
1133 * The list of possible slaves follows the nx_action_bundle structure. The size
1134 * of each slave is governed by its type as indicated by the 'slave_type'
1135 * parameter. The list of slaves should be padded at its end with zeros to make
1136 * the total length of the action a multiple of 8.
1137 *
1138 * Switches infer from the 'slave_type' parameter the size of each slave. All
1139 * implementations must support the NXM_OF_IN_PORT 'slave_type' which indicates
1140 * that the slaves are OpenFlow port numbers with NXM_LENGTH(NXM_OF_IN_PORT) ==
1141 * 2 byte width. Switches should reject actions which indicate unknown or
1142 * unsupported slave types.
1143 *
1144 * Switches use a strategy dictated by the 'algorithm' parameter to choose a
1145 * slave. If the switch does not support the specified 'algorithm' parameter,
1146 * it should reject the action.
1147 *
1148 * Several algorithms take into account liveness when selecting slaves. The
1149 * liveness of a slave is implementation defined (with one exception), but will
1150 * generally take into account things like its carrier status and the results
1151 * of any link monitoring protocols which happen to be running on it. In order
1152 * to give controllers a place-holder value, the OFPP_NONE port is always
1153 * considered live.
1154 *
1155 * Some slave selection strategies require the use of a hash function, in which
1156 * case the 'fields' and 'basis' parameters should be populated. The 'fields'
1157 * parameter (one of NX_HASH_FIELDS_*) designates which parts of the flow to
1158 * hash. Refer to the definition of "enum nx_hash_fields" for details. The
1159 * 'basis' parameter is used as a universal hash parameter. Different values
1160 * of 'basis' yield different hash results.
1161 *
1162 * The 'zero' parameter at the end of the action structure is reserved for
1163 * future use. Switches are required to reject actions which have nonzero
1164 * bytes in the 'zero' field.
1165 *
1166 * NXAST_BUNDLE actions should have 'ofs_nbits' and 'dst' zeroed. Switches
1167 * should reject actions which have nonzero bytes in either of these fields.
1168 *
1169 * NXAST_BUNDLE_LOAD stores the OpenFlow port number of the selected slave in
1170 * dst[ofs:ofs+n_bits]. The format and semantics of 'dst' and 'ofs_nbits' are
1171 * similar to those for the NXAST_REG_LOAD action. */
1172struct nx_action_bundle {
1173 ovs_be16 type; /* OFPAT_VENDOR. */
1174 ovs_be16 len; /* Length including slaves. */
1175 ovs_be32 vendor; /* NX_VENDOR_ID. */
1176 ovs_be16 subtype; /* NXAST_BUNDLE or NXAST_BUNDLE_LOAD. */
f25d0cf3 1177
c2d936a4
BP
1178 /* Slave choice algorithm to apply to hash value. */
1179 ovs_be16 algorithm; /* One of NX_BD_ALG_*. */
4cceacb9 1180
c2d936a4
BP
1181 /* What fields to hash and how. */
1182 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
1183 ovs_be16 basis; /* Universal hash parameter. */
f25d0cf3 1184
c2d936a4
BP
1185 ovs_be32 slave_type; /* NXM_OF_IN_PORT. */
1186 ovs_be16 n_slaves; /* Number of slaves. */
f25d0cf3 1187
c2d936a4
BP
1188 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
1189 ovs_be32 dst; /* Destination. */
f25d0cf3 1190
c2d936a4
BP
1191 uint8_t zero[4]; /* Reserved. Must be zero. */
1192};
1193OFP_ASSERT(sizeof(struct nx_action_bundle) == 32);
f25d0cf3 1194
c2d936a4
BP
1195static enum ofperr
1196decode_bundle(bool load, const struct nx_action_bundle *nab,
1197 struct ofpbuf *ofpacts)
1198{
1199 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1200 struct ofpact_bundle *bundle;
1201 uint32_t slave_type;
1202 size_t slaves_size, i;
1203 enum ofperr error;
bd85dac1 1204
c2d936a4
BP
1205 bundle = ofpact_put_BUNDLE(ofpacts);
1206
1207 bundle->n_slaves = ntohs(nab->n_slaves);
1208 bundle->basis = ntohs(nab->basis);
1209 bundle->fields = ntohs(nab->fields);
1210 bundle->algorithm = ntohs(nab->algorithm);
1211 slave_type = ntohl(nab->slave_type);
1212 slaves_size = ntohs(nab->len) - sizeof *nab;
1213
1214 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1215 if (!flow_hash_fields_valid(bundle->fields)) {
1216 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) bundle->fields);
1217 } else if (bundle->n_slaves > BUNDLE_MAX_SLAVES) {
1218 VLOG_WARN_RL(&rl, "too many slaves");
1219 } else if (bundle->algorithm != NX_BD_ALG_HRW
1220 && bundle->algorithm != NX_BD_ALG_ACTIVE_BACKUP) {
1221 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) bundle->algorithm);
508a9338 1222 } else if (slave_type != mf_nxm_header(MFF_IN_PORT)) {
c2d936a4
BP
1223 VLOG_WARN_RL(&rl, "unsupported slave type %"PRIu16, slave_type);
1224 } else {
1225 error = 0;
1226 }
bd85dac1 1227
c2d936a4
BP
1228 if (!is_all_zeros(nab->zero, sizeof nab->zero)) {
1229 VLOG_WARN_RL(&rl, "reserved field is nonzero");
1230 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1231 }
f25d0cf3 1232
c2d936a4
BP
1233 if (load) {
1234 bundle->dst.field = mf_from_nxm_header(ntohl(nab->dst));
1235 bundle->dst.ofs = nxm_decode_ofs(nab->ofs_nbits);
1236 bundle->dst.n_bits = nxm_decode_n_bits(nab->ofs_nbits);
f25d0cf3 1237
c2d936a4
BP
1238 if (bundle->dst.n_bits < 16) {
1239 VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
1240 "destination.");
1241 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1242 }
1243 } else {
1244 if (nab->ofs_nbits || nab->dst) {
1245 VLOG_WARN_RL(&rl, "bundle action has nonzero reserved fields");
1246 error = OFPERR_OFPBAC_BAD_ARGUMENT;
1247 }
1248 }
f25d0cf3 1249
c2d936a4
BP
1250 if (slaves_size < bundle->n_slaves * sizeof(ovs_be16)) {
1251 VLOG_WARN_RL(&rl, "Nicira action %s only has %"PRIuSIZE" bytes "
1252 "allocated for slaves. %"PRIuSIZE" bytes are required "
1253 "for %"PRIu16" slaves.",
1254 load ? "bundle_load" : "bundle", slaves_size,
1255 bundle->n_slaves * sizeof(ovs_be16), bundle->n_slaves);
1256 error = OFPERR_OFPBAC_BAD_LEN;
1257 }
f25d0cf3 1258
c2d936a4
BP
1259 for (i = 0; i < bundle->n_slaves; i++) {
1260 uint16_t ofp_port = ntohs(((ovs_be16 *)(nab + 1))[i]);
1261 ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
19b58f3c 1262 bundle = ofpacts->header;
c2d936a4 1263 }
f25d0cf3 1264
34abaa3d 1265 ofpact_finish(ofpacts, &bundle->ofpact);
f25d0cf3 1266
c2d936a4
BP
1267 if (!error) {
1268 error = bundle_check(bundle, OFPP_MAX, NULL);
1269 }
1270 return error;
1271}
f25d0cf3 1272
c2d936a4 1273static enum ofperr
f3cd3ac7
JS
1274decode_NXAST_RAW_BUNDLE(const struct nx_action_bundle *nab,
1275 enum ofp_version ofp_version OVS_UNUSED,
1276 struct ofpbuf *out)
c2d936a4
BP
1277{
1278 return decode_bundle(false, nab, out);
1279}
f25d0cf3 1280
c2d936a4
BP
1281static enum ofperr
1282decode_NXAST_RAW_BUNDLE_LOAD(const struct nx_action_bundle *nab,
f3cd3ac7 1283 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4
BP
1284 struct ofpbuf *out)
1285{
1286 return decode_bundle(true, nab, out);
1287}
c2d967a5 1288
c2d936a4
BP
1289static void
1290encode_BUNDLE(const struct ofpact_bundle *bundle,
1291 enum ofp_version ofp_version OVS_UNUSED,
1292 struct ofpbuf *out)
1293{
1294 int slaves_len = ROUND_UP(2 * bundle->n_slaves, OFP_ACTION_ALIGN);
1295 struct nx_action_bundle *nab;
1296 ovs_be16 *slaves;
1297 size_t i;
f25d0cf3 1298
c2d936a4
BP
1299 nab = (bundle->dst.field
1300 ? put_NXAST_BUNDLE_LOAD(out)
1301 : put_NXAST_BUNDLE(out));
1302 nab->len = htons(ntohs(nab->len) + slaves_len);
1303 nab->algorithm = htons(bundle->algorithm);
1304 nab->fields = htons(bundle->fields);
1305 nab->basis = htons(bundle->basis);
508a9338 1306 nab->slave_type = htonl(mf_nxm_header(MFF_IN_PORT));
c2d936a4
BP
1307 nab->n_slaves = htons(bundle->n_slaves);
1308 if (bundle->dst.field) {
1309 nab->ofs_nbits = nxm_encode_ofs_nbits(bundle->dst.ofs,
1310 bundle->dst.n_bits);
508a9338 1311 nab->dst = htonl(mf_nxm_header(bundle->dst.field->id));
c2d936a4 1312 }
f25d0cf3 1313
c2d936a4
BP
1314 slaves = ofpbuf_put_zeros(out, slaves_len);
1315 for (i = 0; i < bundle->n_slaves; i++) {
1316 slaves[i] = htons(ofp_to_u16(bundle->slaves[i]));
1317 }
1318}
b02475c5 1319
cab50449 1320static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1321parse_BUNDLE(const char *arg, struct ofpbuf *ofpacts,
1322 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1323{
1324 return bundle_parse(arg, ofpacts);
1325}
b02475c5 1326
cab50449 1327static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1328parse_bundle_load(const char *arg, struct ofpbuf *ofpacts)
1329{
1330 return bundle_parse_load(arg, ofpacts);
1331}
097d4939 1332
c2d936a4
BP
1333static void
1334format_BUNDLE(const struct ofpact_bundle *a, struct ds *s)
1335{
1336 bundle_format(a, s);
1337}
1338\f
1339/* Set VLAN actions. */
097d4939 1340
c2d936a4
BP
1341static enum ofperr
1342decode_set_vlan_vid(uint16_t vid, bool push_vlan_if_needed, struct ofpbuf *out)
1343{
1344 if (vid & ~0xfff) {
1345 return OFPERR_OFPBAC_BAD_ARGUMENT;
1346 } else {
1347 struct ofpact_vlan_vid *vlan_vid = ofpact_put_SET_VLAN_VID(out);
1348 vlan_vid->vlan_vid = vid;
1349 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1350 return 0;
1351 }
1352}
0f3f3c3d 1353
c2d936a4 1354static enum ofperr
f3cd3ac7
JS
1355decode_OFPAT_RAW10_SET_VLAN_VID(uint16_t vid,
1356 enum ofp_version ofp_version OVS_UNUSED,
1357 struct ofpbuf *out)
c2d936a4
BP
1358{
1359 return decode_set_vlan_vid(vid, true, out);
1360}
b676167a 1361
c2d936a4 1362static enum ofperr
f3cd3ac7
JS
1363decode_OFPAT_RAW11_SET_VLAN_VID(uint16_t vid,
1364 enum ofp_version ofp_version OVS_UNUSED,
1365 struct ofpbuf *out)
c2d936a4
BP
1366{
1367 return decode_set_vlan_vid(vid, false, out);
1368}
29089a54 1369
c2d936a4
BP
1370static void
1371encode_SET_VLAN_VID(const struct ofpact_vlan_vid *vlan_vid,
1372 enum ofp_version ofp_version, struct ofpbuf *out)
1373{
1374 uint16_t vid = vlan_vid->vlan_vid;
1375
1376 /* Push a VLAN tag, if none is present and this form of the action calls
1377 * for such a feature. */
1378 if (ofp_version > OFP10_VERSION
1379 && vlan_vid->push_vlan_if_needed
1380 && !vlan_vid->flow_has_vlan) {
1381 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
f25d0cf3
BP
1382 }
1383
c2d936a4
BP
1384 if (ofp_version == OFP10_VERSION) {
1385 put_OFPAT10_SET_VLAN_VID(out, vid);
1386 } else if (ofp_version == OFP11_VERSION) {
1387 put_OFPAT11_SET_VLAN_VID(out, vid);
1388 } else {
1389 ofpact_put_set_field(out, ofp_version,
1390 MFF_VLAN_VID, vid | OFPVID12_PRESENT);
1391 }
f25d0cf3
BP
1392}
1393
cab50449 1394static char * OVS_WARN_UNUSED_RESULT
c2d936a4 1395parse_set_vlan_vid(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
d01c980f 1396{
ca287d20 1397 struct ofpact_vlan_vid *vlan_vid;
c2d936a4
BP
1398 uint16_t vid;
1399 char *error;
d01c980f 1400
c2d936a4 1401 error = str_to_u16(arg, "VLAN VID", &vid);
d01c980f
BP
1402 if (error) {
1403 return error;
1404 }
1405
c2d936a4
BP
1406 if (vid & ~VLAN_VID_MASK) {
1407 return xasprintf("%s: not a valid VLAN VID", arg);
d01c980f 1408 }
c2d936a4
BP
1409 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
1410 vlan_vid->vlan_vid = vid;
1411 vlan_vid->push_vlan_if_needed = push_vlan_if_needed;
1412 return NULL;
1413}
d01c980f 1414
cab50449 1415static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1416parse_SET_VLAN_VID(char *arg, struct ofpbuf *ofpacts,
1417 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1418{
1419 return parse_set_vlan_vid(arg, ofpacts, false);
d01c980f
BP
1420}
1421
c2d936a4
BP
1422static void
1423format_SET_VLAN_VID(const struct ofpact_vlan_vid *a, struct ds *s)
1424{
b1c5bf1f 1425 ds_put_format(s, "%s%s:%s%"PRIu16, colors.param,
c2d936a4 1426 a->push_vlan_if_needed ? "mod_vlan_vid" : "set_vlan_vid",
b1c5bf1f 1427 colors.end, a->vlan_vid);
c2d936a4
BP
1428}
1429\f
1430/* Set PCP actions. */
e3f8f887 1431
c2d936a4
BP
1432static enum ofperr
1433decode_set_vlan_pcp(uint8_t pcp, bool push_vlan_if_needed, struct ofpbuf *out)
f25d0cf3 1434{
c2d936a4
BP
1435 if (pcp & ~7) {
1436 return OFPERR_OFPBAC_BAD_ARGUMENT;
1437 } else {
1438 struct ofpact_vlan_pcp *vlan_pcp = ofpact_put_SET_VLAN_PCP(out);
1439 vlan_pcp->vlan_pcp = pcp;
1440 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1441 return 0;
1442 }
f25d0cf3
BP
1443}
1444
c2d936a4 1445static enum ofperr
f3cd3ac7
JS
1446decode_OFPAT_RAW10_SET_VLAN_PCP(uint8_t pcp,
1447 enum ofp_version ofp_version OVS_UNUSED,
1448 struct ofpbuf *out)
f25d0cf3 1449{
c2d936a4 1450 return decode_set_vlan_pcp(pcp, true, out);
f25d0cf3
BP
1451}
1452
c2d936a4 1453static enum ofperr
f3cd3ac7
JS
1454decode_OFPAT_RAW11_SET_VLAN_PCP(uint8_t pcp,
1455 enum ofp_version ofp_version OVS_UNUSED,
1456 struct ofpbuf *out)
c2d936a4
BP
1457{
1458 return decode_set_vlan_pcp(pcp, false, out);
1459}
f25d0cf3 1460
699dddf1 1461static void
c2d936a4
BP
1462encode_SET_VLAN_PCP(const struct ofpact_vlan_pcp *vlan_pcp,
1463 enum ofp_version ofp_version, struct ofpbuf *out)
699dddf1 1464{
c2d936a4
BP
1465 uint8_t pcp = vlan_pcp->vlan_pcp;
1466
1467 /* Push a VLAN tag, if none is present and this form of the action calls
1468 * for such a feature. */
1469 if (ofp_version > OFP10_VERSION
1470 && vlan_pcp->push_vlan_if_needed
1471 && !vlan_pcp->flow_has_vlan) {
1472 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
1473 }
699dddf1 1474
c2d936a4
BP
1475 if (ofp_version == OFP10_VERSION) {
1476 put_OFPAT10_SET_VLAN_PCP(out, pcp);
1477 } else if (ofp_version == OFP11_VERSION) {
1478 put_OFPAT11_SET_VLAN_PCP(out, pcp);
1479 } else {
1480 ofpact_put_set_field(out, ofp_version, MFF_VLAN_PCP, pcp);
699dddf1
BP
1481 }
1482}
1483
cab50449 1484static char * OVS_WARN_UNUSED_RESULT
c2d936a4 1485parse_set_vlan_pcp(char *arg, struct ofpbuf *ofpacts, bool push_vlan_if_needed)
f25d0cf3 1486{
c2d936a4
BP
1487 struct ofpact_vlan_pcp *vlan_pcp;
1488 uint8_t pcp;
1489 char *error;
e3f8f887 1490
c2d936a4
BP
1491 error = str_to_u8(arg, "VLAN PCP", &pcp);
1492 if (error) {
699dddf1 1493 return error;
f25d0cf3
BP
1494 }
1495
c2d936a4
BP
1496 if (pcp & ~7) {
1497 return xasprintf("%s: not a valid VLAN PCP", arg);
1498 }
1499 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
1500 vlan_pcp->vlan_pcp = pcp;
1501 vlan_pcp->push_vlan_if_needed = push_vlan_if_needed;
1502 return NULL;
1503}
1504
cab50449 1505static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1506parse_SET_VLAN_PCP(char *arg, struct ofpbuf *ofpacts,
1507 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1508{
1509 return parse_set_vlan_pcp(arg, ofpacts, false);
1510}
1511
1512static void
1513format_SET_VLAN_PCP(const struct ofpact_vlan_pcp *a, struct ds *s)
1514{
b1c5bf1f 1515 ds_put_format(s, "%s%s:%s%"PRIu8, colors.param,
c2d936a4 1516 a->push_vlan_if_needed ? "mod_vlan_pcp" : "set_vlan_pcp",
b1c5bf1f 1517 colors.end, a->vlan_pcp);
c2d936a4
BP
1518}
1519\f
1520/* Strip VLAN actions. */
1521
1522static enum ofperr
1523decode_OFPAT_RAW10_STRIP_VLAN(struct ofpbuf *out)
1524{
1525 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
f25d0cf3
BP
1526 return 0;
1527}
1528
8f2cded4 1529static enum ofperr
c2d936a4 1530decode_OFPAT_RAW11_POP_VLAN(struct ofpbuf *out)
8f2cded4 1531{
c2d936a4
BP
1532 ofpact_put_STRIP_VLAN(out)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1533 return 0;
1534}
f25d0cf3 1535
c2d936a4
BP
1536static void
1537encode_STRIP_VLAN(const struct ofpact_null *null OVS_UNUSED,
1538 enum ofp_version ofp_version, struct ofpbuf *out)
1539{
1540 if (ofp_version == OFP10_VERSION) {
1541 put_OFPAT10_STRIP_VLAN(out);
1542 } else {
1543 put_OFPAT11_POP_VLAN(out);
1544 }
1545}
f25d0cf3 1546
cab50449 1547static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1548parse_STRIP_VLAN(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
1549 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1550{
1551 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW10_STRIP_VLAN;
1552 return NULL;
1553}
1554
cab50449 1555static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1556parse_pop_vlan(struct ofpbuf *ofpacts)
1557{
1558 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.raw = OFPAT_RAW11_POP_VLAN;
1559 return NULL;
1560}
1561
1562static void
1563format_STRIP_VLAN(const struct ofpact_null *a, struct ds *s)
1564{
b1c5bf1f
QM
1565 ds_put_format(s, (a->ofpact.raw == OFPAT_RAW11_POP_VLAN
1566 ? "%spop_vlan%s"
1567 : "%sstrip_vlan%s"),
1568 colors.value, colors.end);
c2d936a4
BP
1569}
1570\f
1571/* Push VLAN action. */
1572
1573static enum ofperr
f3cd3ac7
JS
1574decode_OFPAT_RAW11_PUSH_VLAN(ovs_be16 eth_type,
1575 enum ofp_version ofp_version OVS_UNUSED,
1576 struct ofpbuf *out)
c2d936a4
BP
1577{
1578 if (eth_type != htons(ETH_TYPE_VLAN_8021Q)) {
1579 /* XXX 802.1AD(QinQ) isn't supported at the moment */
1580 return OFPERR_OFPBAC_BAD_ARGUMENT;
f25d0cf3 1581 }
c2d936a4
BP
1582 ofpact_put_PUSH_VLAN(out);
1583 return 0;
1584}
f25d0cf3 1585
c2d936a4
BP
1586static void
1587encode_PUSH_VLAN(const struct ofpact_null *null OVS_UNUSED,
1588 enum ofp_version ofp_version, struct ofpbuf *out)
1589{
1590 if (ofp_version == OFP10_VERSION) {
1591 /* PUSH is a side effect of a SET_VLAN_VID/PCP, which should
1592 * follow this action. */
1593 } else {
1594 /* XXX ETH_TYPE_VLAN_8021AD case */
1595 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
f25d0cf3 1596 }
c2d936a4 1597}
f25d0cf3 1598
cab50449 1599static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1600parse_PUSH_VLAN(char *arg, struct ofpbuf *ofpacts,
1601 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1602{
1603 uint16_t ethertype;
1604 char *error;
1605
1606 *usable_protocols &= OFPUTIL_P_OF11_UP;
1607 error = str_to_u16(arg, "ethertype", &ethertype);
4cceacb9 1608 if (error) {
4cceacb9
JS
1609 return error;
1610 }
1611
c2d936a4
BP
1612 if (ethertype != ETH_TYPE_VLAN_8021Q) {
1613 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
1614 return xasprintf("%s: not a valid VLAN ethertype", arg);
f25d0cf3 1615 }
c2d936a4
BP
1616
1617 ofpact_put_PUSH_VLAN(ofpacts);
1618 return NULL;
d01c980f
BP
1619}
1620
c2d936a4
BP
1621static void
1622format_PUSH_VLAN(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
8f2cded4 1623{
c2d936a4 1624 /* XXX 802.1AD case*/
b1c5bf1f
QM
1625 ds_put_format(s, "%spush_vlan:%s%#"PRIx16,
1626 colors.param, colors.end, ETH_TYPE_VLAN_8021Q);
8f2cded4 1627}
d01c980f 1628\f
c2d936a4
BP
1629/* Action structure for OFPAT10_SET_DL_SRC/DST and OFPAT11_SET_DL_SRC/DST. */
1630struct ofp_action_dl_addr {
1631 ovs_be16 type; /* Type. */
1632 ovs_be16 len; /* Length is 16. */
74ff3298 1633 struct eth_addr dl_addr; /* Ethernet address. */
c2d936a4
BP
1634 uint8_t pad[6];
1635};
1636OFP_ASSERT(sizeof(struct ofp_action_dl_addr) == 16);
d01c980f 1637
d01c980f 1638static enum ofperr
c2d936a4 1639decode_OFPAT_RAW_SET_DL_SRC(const struct ofp_action_dl_addr *a,
f3cd3ac7 1640 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4 1641 struct ofpbuf *out)
d01c980f 1642{
74ff3298 1643 ofpact_put_SET_ETH_SRC(out)->mac = a->dl_addr;
c2d936a4
BP
1644 return 0;
1645}
3ddcaf2d 1646
c2d936a4
BP
1647static enum ofperr
1648decode_OFPAT_RAW_SET_DL_DST(const struct ofp_action_dl_addr *a,
f3cd3ac7 1649 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4
BP
1650 struct ofpbuf *out)
1651{
74ff3298 1652 ofpact_put_SET_ETH_DST(out)->mac = a->dl_addr;
c2d936a4
BP
1653 return 0;
1654}
d01c980f 1655
c2d936a4
BP
1656static void
1657encode_SET_ETH_addr(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1658 enum ofp_raw_action_type raw, enum mf_field_id field,
1659 struct ofpbuf *out)
1660{
c2d936a4
BP
1661 if (ofp_version < OFP12_VERSION) {
1662 struct ofp_action_dl_addr *oada;
1663
1664 oada = ofpact_put_raw(out, ofp_version, raw, 0);
74ff3298 1665 oada->dl_addr = mac->mac;
c2d936a4
BP
1666 } else {
1667 ofpact_put_set_field(out, ofp_version, field,
74ff3298 1668 eth_addr_to_uint64(mac->mac));
d01c980f
BP
1669 }
1670}
1671
c2d936a4
BP
1672static void
1673encode_SET_ETH_SRC(const struct ofpact_mac *mac, enum ofp_version ofp_version,
1674 struct ofpbuf *out)
b2dd70be 1675{
c2d936a4
BP
1676 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_SRC, MFF_ETH_SRC,
1677 out);
b2dd70be 1678
c2d936a4 1679}
b2dd70be 1680
c2d936a4
BP
1681static void
1682encode_SET_ETH_DST(const struct ofpact_mac *mac,
1683 enum ofp_version ofp_version,
1684 struct ofpbuf *out)
1685{
1686 encode_SET_ETH_addr(mac, ofp_version, OFPAT_RAW_SET_DL_DST, MFF_ETH_DST,
1687 out);
b2dd70be 1688
c2d936a4 1689}
b2dd70be 1690
cab50449 1691static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1692parse_SET_ETH_SRC(char *arg, struct ofpbuf *ofpacts,
1693 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1694{
74ff3298 1695 return str_to_mac(arg, &ofpact_put_SET_ETH_SRC(ofpacts)->mac);
c2d936a4
BP
1696}
1697
cab50449 1698static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1699parse_SET_ETH_DST(char *arg, struct ofpbuf *ofpacts,
1700 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1701{
74ff3298 1702 return str_to_mac(arg, &ofpact_put_SET_ETH_DST(ofpacts)->mac);
b2dd70be
JR
1703}
1704
1705static void
c2d936a4 1706format_SET_ETH_SRC(const struct ofpact_mac *a, struct ds *s)
b2dd70be 1707{
b1c5bf1f
QM
1708 ds_put_format(s, "%smod_dl_src:%s"ETH_ADDR_FMT,
1709 colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
c2d936a4 1710}
b2dd70be 1711
c2d936a4
BP
1712static void
1713format_SET_ETH_DST(const struct ofpact_mac *a, struct ds *s)
1714{
b1c5bf1f
QM
1715 ds_put_format(s, "%smod_dl_dst:%s"ETH_ADDR_FMT,
1716 colors.param, colors.end, ETH_ADDR_ARGS(a->mac));
c2d936a4
BP
1717}
1718\f
1719/* Set IPv4 address actions. */
b2dd70be 1720
c2d936a4 1721static enum ofperr
f3cd3ac7
JS
1722decode_OFPAT_RAW_SET_NW_SRC(ovs_be32 ipv4,
1723 enum ofp_version ofp_version OVS_UNUSED,
1724 struct ofpbuf *out)
c2d936a4
BP
1725{
1726 ofpact_put_SET_IPV4_SRC(out)->ipv4 = ipv4;
1727 return 0;
b2dd70be
JR
1728}
1729
c2d936a4 1730static enum ofperr
f3cd3ac7
JS
1731decode_OFPAT_RAW_SET_NW_DST(ovs_be32 ipv4,
1732 enum ofp_version ofp_version OVS_UNUSED,
1733 struct ofpbuf *out)
b2dd70be 1734{
c2d936a4
BP
1735 ofpact_put_SET_IPV4_DST(out)->ipv4 = ipv4;
1736 return 0;
1737}
b2dd70be 1738
c2d936a4
BP
1739static void
1740encode_SET_IPV4_addr(const struct ofpact_ipv4 *ipv4,
1741 enum ofp_version ofp_version,
1742 enum ofp_raw_action_type raw, enum mf_field_id field,
1743 struct ofpbuf *out)
1744{
1745 ovs_be32 addr = ipv4->ipv4;
1746 if (ofp_version < OFP12_VERSION) {
1747 ofpact_put_raw(out, ofp_version, raw, ntohl(addr));
b2dd70be 1748 } else {
c2d936a4 1749 ofpact_put_set_field(out, ofp_version, field, ntohl(addr));
b2dd70be
JR
1750 }
1751}
1752
a6fd70bb 1753static void
c2d936a4
BP
1754encode_SET_IPV4_SRC(const struct ofpact_ipv4 *ipv4,
1755 enum ofp_version ofp_version, struct ofpbuf *out)
a6fd70bb 1756{
c2d936a4
BP
1757 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_SRC, MFF_IPV4_SRC,
1758 out);
1759}
a6fd70bb 1760
c2d936a4
BP
1761static void
1762encode_SET_IPV4_DST(const struct ofpact_ipv4 *ipv4,
1763 enum ofp_version ofp_version, struct ofpbuf *out)
1764{
1765 encode_SET_IPV4_addr(ipv4, ofp_version, OFPAT_RAW_SET_NW_DST, MFF_IPV4_DST,
1766 out);
1767}
a6fd70bb 1768
cab50449 1769static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1770parse_SET_IPV4_SRC(char *arg, struct ofpbuf *ofpacts,
1771 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1772{
1773 return str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
1774}
a6fd70bb 1775
cab50449 1776static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1777parse_SET_IPV4_DST(char *arg, struct ofpbuf *ofpacts,
1778 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1779{
1780 return str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
1781}
a6fd70bb 1782
c2d936a4
BP
1783static void
1784format_SET_IPV4_SRC(const struct ofpact_ipv4 *a, struct ds *s)
1785{
b1c5bf1f
QM
1786 ds_put_format(s, "%smod_nw_src:%s"IP_FMT,
1787 colors.param, colors.end, IP_ARGS(a->ipv4));
c2d936a4 1788}
097d4939 1789
c2d936a4
BP
1790static void
1791format_SET_IPV4_DST(const struct ofpact_ipv4 *a, struct ds *s)
1792{
b1c5bf1f
QM
1793 ds_put_format(s, "%smod_nw_dst:%s"IP_FMT,
1794 colors.param, colors.end, IP_ARGS(a->ipv4));
c2d936a4
BP
1795}
1796\f
1797/* Set IPv4/v6 TOS actions. */
097d4939 1798
c2d936a4 1799static enum ofperr
f3cd3ac7
JS
1800decode_OFPAT_RAW_SET_NW_TOS(uint8_t dscp,
1801 enum ofp_version ofp_version OVS_UNUSED,
1802 struct ofpbuf *out)
c2d936a4
BP
1803{
1804 if (dscp & ~IP_DSCP_MASK) {
1805 return OFPERR_OFPBAC_BAD_ARGUMENT;
1806 } else {
1807 ofpact_put_SET_IP_DSCP(out)->dscp = dscp;
1808 return 0;
a6fd70bb
JR
1809 }
1810}
1811
a6fd70bb 1812static void
c2d936a4
BP
1813encode_SET_IP_DSCP(const struct ofpact_dscp *dscp,
1814 enum ofp_version ofp_version, struct ofpbuf *out)
a6fd70bb 1815{
c2d936a4
BP
1816 if (ofp_version < OFP12_VERSION) {
1817 put_OFPAT_SET_NW_TOS(out, ofp_version, dscp->dscp);
1818 } else {
1819 ofpact_put_set_field(out, ofp_version,
1820 MFF_IP_DSCP_SHIFTED, dscp->dscp >> 2);
1821 }
1822}
a6fd70bb 1823
cab50449 1824static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1825parse_SET_IP_DSCP(char *arg, struct ofpbuf *ofpacts,
1826 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1827{
1828 uint8_t tos;
1829 char *error;
a6fd70bb 1830
c2d936a4
BP
1831 error = str_to_u8(arg, "TOS", &tos);
1832 if (error) {
1833 return error;
1834 }
a6fd70bb 1835
c2d936a4
BP
1836 if (tos & ~IP_DSCP_MASK) {
1837 return xasprintf("%s: not a valid TOS", arg);
a6fd70bb 1838 }
c2d936a4
BP
1839 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
1840 return NULL;
a6fd70bb
JR
1841}
1842
b2dd70be 1843static void
c2d936a4 1844format_SET_IP_DSCP(const struct ofpact_dscp *a, struct ds *s)
b2dd70be 1845{
b1c5bf1f 1846 ds_put_format(s, "%smod_nw_tos:%s%d", colors.param, colors.end, a->dscp);
c2d936a4
BP
1847}
1848\f
1849/* Set IPv4/v6 ECN actions. */
b2dd70be 1850
c2d936a4 1851static enum ofperr
f3cd3ac7
JS
1852decode_OFPAT_RAW11_SET_NW_ECN(uint8_t ecn,
1853 enum ofp_version ofp_version OVS_UNUSED,
1854 struct ofpbuf *out)
c2d936a4
BP
1855{
1856 if (ecn & ~IP_ECN_MASK) {
1857 return OFPERR_OFPBAC_BAD_ARGUMENT;
b2dd70be 1858 } else {
c2d936a4
BP
1859 ofpact_put_SET_IP_ECN(out)->ecn = ecn;
1860 return 0;
b2dd70be
JR
1861 }
1862}
1863
c2d936a4
BP
1864static void
1865encode_SET_IP_ECN(const struct ofpact_ecn *ip_ecn,
1866 enum ofp_version ofp_version, struct ofpbuf *out)
d01c980f 1867{
c2d936a4
BP
1868 uint8_t ecn = ip_ecn->ecn;
1869 if (ofp_version == OFP10_VERSION) {
1870 /* XXX */
1871 } else if (ofp_version == OFP11_VERSION) {
1872 put_OFPAT11_SET_NW_ECN(out, ecn);
1873 } else {
1874 ofpact_put_set_field(out, ofp_version, MFF_IP_ECN, ecn);
1875 }
1876}
d01c980f 1877
cab50449 1878static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1879parse_SET_IP_ECN(char *arg, struct ofpbuf *ofpacts,
1880 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1881{
1882 uint8_t ecn;
1883 char *error;
d01c980f 1884
c2d936a4 1885 error = str_to_u8(arg, "ECN", &ecn);
d01c980f
BP
1886 if (error) {
1887 return error;
1888 }
1889
c2d936a4
BP
1890 if (ecn & ~IP_ECN_MASK) {
1891 return xasprintf("%s: not a valid ECN", arg);
1892 }
1893 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
1894 return NULL;
d01c980f
BP
1895}
1896
c2d936a4
BP
1897static void
1898format_SET_IP_ECN(const struct ofpact_ecn *a, struct ds *s)
1899{
b1c5bf1f
QM
1900 ds_put_format(s, "%smod_nw_ecn:%s%d",
1901 colors.param, colors.end, a->ecn);
c2d936a4
BP
1902}
1903\f
1904/* Set IPv4/v6 TTL actions. */
1905
d01c980f 1906static enum ofperr
f3cd3ac7
JS
1907decode_OFPAT_RAW11_SET_NW_TTL(uint8_t ttl,
1908 enum ofp_version ofp_version OVS_UNUSED,
1909 struct ofpbuf *out)
d01c980f 1910{
c2d936a4
BP
1911 ofpact_put_SET_IP_TTL(out)->ttl = ttl;
1912 return 0;
1913}
1914
1915static void
1916encode_SET_IP_TTL(const struct ofpact_ip_ttl *ttl,
1917 enum ofp_version ofp_version, struct ofpbuf *out)
1918{
1919 if (ofp_version >= OFP11_VERSION) {
1920 put_OFPAT11_SET_NW_TTL(out, ttl->ttl);
1921 } else {
1922 /* XXX */
1923 }
1924}
1925
cab50449 1926static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
1927parse_SET_IP_TTL(char *arg, struct ofpbuf *ofpacts,
1928 enum ofputil_protocol *usable_protocols OVS_UNUSED)
1929{
1930 uint8_t ttl;
1931 char *error;
d01c980f 1932
c2d936a4 1933 error = str_to_u8(arg, "TTL", &ttl);
d01c980f
BP
1934 if (error) {
1935 return error;
1936 }
1937
c2d936a4
BP
1938 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
1939 return NULL;
1940}
d01c980f 1941
c2d936a4
BP
1942static void
1943format_SET_IP_TTL(const struct ofpact_ip_ttl *a, struct ds *s)
1944{
b1c5bf1f 1945 ds_put_format(s, "%smod_nw_ttl:%s%d", colors.param, colors.end, a->ttl);
c2d936a4
BP
1946}
1947\f
1948/* Set TCP/UDP/SCTP port actions. */
d01c980f 1949
c2d936a4 1950static enum ofperr
f3cd3ac7
JS
1951decode_OFPAT_RAW_SET_TP_SRC(ovs_be16 port,
1952 enum ofp_version ofp_version OVS_UNUSED,
1953 struct ofpbuf *out)
c2d936a4
BP
1954{
1955 ofpact_put_SET_L4_SRC_PORT(out)->port = ntohs(port);
1956 return 0;
1957}
d01c980f 1958
c2d936a4 1959static enum ofperr
f3cd3ac7
JS
1960decode_OFPAT_RAW_SET_TP_DST(ovs_be16 port,
1961 enum ofp_version ofp_version OVS_UNUSED,
1962 struct ofpbuf *out)
c2d936a4
BP
1963{
1964 ofpact_put_SET_L4_DST_PORT(out)->port = ntohs(port);
1965 return 0;
1966}
d01c980f 1967
c2d936a4
BP
1968static void
1969encode_SET_L4_port(const struct ofpact_l4_port *l4_port,
1970 enum ofp_version ofp_version, enum ofp_raw_action_type raw,
1971 enum mf_field_id field, struct ofpbuf *out)
1972{
1973 uint16_t port = l4_port->port;
3e34fbdd 1974
c2d936a4
BP
1975 if (ofp_version >= OFP12_VERSION && field != MFF_N_IDS) {
1976 ofpact_put_set_field(out, ofp_version, field, port);
1977 } else {
1978 ofpact_put_raw(out, ofp_version, raw, port);
1979 }
1980}
8e61c110 1981
c2d936a4
BP
1982static void
1983encode_SET_L4_SRC_PORT(const struct ofpact_l4_port *l4_port,
1984 enum ofp_version ofp_version, struct ofpbuf *out)
1985{
1986 uint8_t proto = l4_port->flow_ip_proto;
1987 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_SRC
1988 : proto == IPPROTO_UDP ? MFF_UDP_SRC
1989 : proto == IPPROTO_SCTP ? MFF_SCTP_SRC
1990 : MFF_N_IDS);
276c4e7a 1991
c2d936a4
BP
1992 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_SRC, field, out);
1993}
d01c980f 1994
c2d936a4
BP
1995static void
1996encode_SET_L4_DST_PORT(const struct ofpact_l4_port *l4_port,
1997 enum ofp_version ofp_version,
1998 struct ofpbuf *out)
1999{
2000 uint8_t proto = l4_port->flow_ip_proto;
2001 enum mf_field_id field = (proto == IPPROTO_TCP ? MFF_TCP_DST
2002 : proto == IPPROTO_UDP ? MFF_UDP_DST
2003 : proto == IPPROTO_SCTP ? MFF_SCTP_DST
2004 : MFF_N_IDS);
d01c980f 2005
c2d936a4
BP
2006 encode_SET_L4_port(l4_port, ofp_version, OFPAT_RAW_SET_TP_DST, field, out);
2007}
7bcb1506 2008
cab50449 2009static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
2010parse_SET_L4_SRC_PORT(char *arg, struct ofpbuf *ofpacts,
2011 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2012{
2013 return str_to_u16(arg, "source port",
2014 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
2015}
d01c980f 2016
cab50449 2017static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
2018parse_SET_L4_DST_PORT(char *arg, struct ofpbuf *ofpacts,
2019 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2020{
2021 return str_to_u16(arg, "destination port",
2022 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
2023}
d01c980f 2024
c2d936a4
BP
2025static void
2026format_SET_L4_SRC_PORT(const struct ofpact_l4_port *a, struct ds *s)
2027{
b1c5bf1f 2028 ds_put_format(s, "%smod_tp_src:%s%d", colors.param, colors.end, a->port);
c2d936a4 2029}
d01c980f 2030
c2d936a4
BP
2031static void
2032format_SET_L4_DST_PORT(const struct ofpact_l4_port *a, struct ds *s)
2033{
b1c5bf1f 2034 ds_put_format(s, "%smod_tp_dst:%s%d", colors.param, colors.end, a->port);
c2d936a4
BP
2035}
2036\f
73178f20
BP
2037/* Action structure for OFPAT_COPY_FIELD. */
2038struct ofp15_action_copy_field {
2039 ovs_be16 type; /* OFPAT_COPY_FIELD. */
2040 ovs_be16 len; /* Length is padded to 64 bits. */
2041 ovs_be16 n_bits; /* Number of bits to copy. */
2042 ovs_be16 src_offset; /* Starting bit offset in source. */
2043 ovs_be16 dst_offset; /* Starting bit offset in destination. */
650763d8 2044 uint8_t pad[2];
f13cdd73
BP
2045 /* Followed by:
2046 * - OXM header for source field.
2047 * - OXM header for destination field.
2048 * - Padding with 0-bytes to a multiple of 8 bytes.
650763d8
BP
2049 * The "pad2" member is the beginning of the above. */
2050 uint8_t pad2[4];
73178f20 2051};
f13cdd73 2052OFP_ASSERT(sizeof(struct ofp15_action_copy_field) == 16);
73178f20 2053
914624f8
BP
2054/* Action structure for OpenFlow 1.3 extension copy-field action.. */
2055struct onf_action_copy_field {
2056 ovs_be16 type; /* OFPAT_EXPERIMENTER. */
2057 ovs_be16 len; /* Length is padded to 64 bits. */
2058 ovs_be32 experimenter; /* ONF_VENDOR_ID. */
2059 ovs_be16 exp_type; /* 3200. */
2060 uint8_t pad[2]; /* Not used. */
2061 ovs_be16 n_bits; /* Number of bits to copy. */
2062 ovs_be16 src_offset; /* Starting bit offset in source. */
2063 ovs_be16 dst_offset; /* Starting bit offset in destination. */
2064 uint8_t pad2[2]; /* Not used. */
2065 /* Followed by:
2066 * - OXM header for source field.
2067 * - OXM header for destination field.
2068 * - Padding with 0-bytes (either 0 or 4 of them) to a multiple of 8 bytes.
2069 * The "pad3" member is the beginning of the above. */
2070 uint8_t pad3[4]; /* Not used. */
2071};
2072OFP_ASSERT(sizeof(struct onf_action_copy_field) == 24);
2073
c2d936a4
BP
2074/* Action structure for NXAST_REG_MOVE.
2075 *
2076 * Copies src[src_ofs:src_ofs+n_bits] to dst[dst_ofs:dst_ofs+n_bits], where
2077 * a[b:c] denotes the bits within 'a' numbered 'b' through 'c' (not including
2078 * bit 'c'). Bit numbering starts at 0 for the least-significant bit, 1 for
2079 * the next most significant bit, and so on.
2080 *
2081 * 'src' and 'dst' are nxm_header values with nxm_hasmask=0. (It doesn't make
2082 * sense to use nxm_hasmask=1 because the action does not do any kind of
2083 * matching; it uses the actual value of a field.)
2084 *
2085 * The following nxm_header values are potentially acceptable as 'src':
2086 *
2087 * - NXM_OF_IN_PORT
2088 * - NXM_OF_ETH_DST
2089 * - NXM_OF_ETH_SRC
2090 * - NXM_OF_ETH_TYPE
2091 * - NXM_OF_VLAN_TCI
2092 * - NXM_OF_IP_TOS
2093 * - NXM_OF_IP_PROTO
2094 * - NXM_OF_IP_SRC
2095 * - NXM_OF_IP_DST
2096 * - NXM_OF_TCP_SRC
2097 * - NXM_OF_TCP_DST
2098 * - NXM_OF_UDP_SRC
2099 * - NXM_OF_UDP_DST
2100 * - NXM_OF_ICMP_TYPE
2101 * - NXM_OF_ICMP_CODE
2102 * - NXM_OF_ARP_OP
2103 * - NXM_OF_ARP_SPA
2104 * - NXM_OF_ARP_TPA
2105 * - NXM_NX_TUN_ID
2106 * - NXM_NX_ARP_SHA
2107 * - NXM_NX_ARP_THA
2108 * - NXM_NX_ICMPV6_TYPE
2109 * - NXM_NX_ICMPV6_CODE
2110 * - NXM_NX_ND_SLL
2111 * - NXM_NX_ND_TLL
2112 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2113 * - NXM_NX_PKT_MARK
2114 * - NXM_NX_TUN_IPV4_SRC
2115 * - NXM_NX_TUN_IPV4_DST
2116 *
2117 * The following nxm_header values are potentially acceptable as 'dst':
2118 *
2119 * - NXM_OF_ETH_DST
2120 * - NXM_OF_ETH_SRC
2121 * - NXM_OF_IP_TOS
2122 * - NXM_OF_IP_SRC
2123 * - NXM_OF_IP_DST
2124 * - NXM_OF_TCP_SRC
2125 * - NXM_OF_TCP_DST
2126 * - NXM_OF_UDP_SRC
2127 * - NXM_OF_UDP_DST
f6ecf944
JP
2128 * - NXM_OF_ICMP_TYPE
2129 * - NXM_OF_ICMP_CODE
2130 * - NXM_NX_ICMPV6_TYPE
2131 * - NXM_NX_ICMPV6_CODE
c2d936a4
BP
2132 * - NXM_NX_ARP_SHA
2133 * - NXM_NX_ARP_THA
2134 * - NXM_OF_ARP_OP
2135 * - NXM_OF_ARP_SPA
2136 * - NXM_OF_ARP_TPA
2137 * Modifying any of the above fields changes the corresponding packet
2138 * header.
2139 *
2140 * - NXM_OF_IN_PORT
2141 *
2142 * - NXM_NX_REG(idx) for idx in the switch's accepted range.
2143 *
2144 * - NXM_NX_PKT_MARK
2145 *
2146 * - NXM_OF_VLAN_TCI. Modifying this field's value has side effects on the
2147 * packet's 802.1Q header. Setting a value with CFI=0 removes the 802.1Q
2148 * header (if any), ignoring the other bits. Setting a value with CFI=1
2149 * adds or modifies the 802.1Q header appropriately, setting the TCI field
2150 * to the field's new value (with the CFI bit masked out).
2151 *
2152 * - NXM_NX_TUN_ID, NXM_NX_TUN_IPV4_SRC, NXM_NX_TUN_IPV4_DST. Modifying
2153 * any of these values modifies the corresponding tunnel header field used
2154 * for the packet's next tunnel encapsulation, if allowed by the
2155 * configuration of the output tunnel port.
2156 *
2157 * A given nxm_header value may be used as 'src' or 'dst' only on a flow whose
2158 * nx_match satisfies its prerequisites. For example, NXM_OF_IP_TOS may be
2159 * used only if the flow's nx_match includes an nxm_entry that specifies
2160 * nxm_type=NXM_OF_ETH_TYPE, nxm_hasmask=0, and nxm_value=0x0800.
2161 *
2162 * The switch will reject actions for which src_ofs+n_bits is greater than the
2163 * width of 'src' or dst_ofs+n_bits is greater than the width of 'dst' with
2164 * error type OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
2165 *
2166 * This action behaves properly when 'src' overlaps with 'dst', that is, it
2167 * behaves as if 'src' were copied out to a temporary buffer, then the
2168 * temporary buffer copied to 'dst'.
2169 */
2170struct nx_action_reg_move {
2171 ovs_be16 type; /* OFPAT_VENDOR. */
2172 ovs_be16 len; /* Length is 24. */
2173 ovs_be32 vendor; /* NX_VENDOR_ID. */
2174 ovs_be16 subtype; /* NXAST_REG_MOVE. */
2175 ovs_be16 n_bits; /* Number of bits. */
2176 ovs_be16 src_ofs; /* Starting bit offset in source. */
2177 ovs_be16 dst_ofs; /* Starting bit offset in destination. */
bad8a439
BP
2178 /* Followed by:
2179 * - OXM/NXM header for source field (4 or 8 bytes).
2180 * - OXM/NXM header for destination field (4 or 8 bytes).
2181 * - Padding with 0-bytes to a multiple of 8 bytes, if necessary. */
c2d936a4 2182};
bad8a439 2183OFP_ASSERT(sizeof(struct nx_action_reg_move) == 16);
ff14eb7a 2184
73178f20 2185static enum ofperr
914624f8
BP
2186decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
2187 const void *action, ovs_be16 action_len, size_t oxm_offset,
2188 struct ofpbuf *ofpacts)
73178f20 2189{
0a2869d5 2190 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
914624f8
BP
2191 move->ofpact.raw = ONFACT_RAW13_COPY_FIELD;
2192 move->src.ofs = ntohs(src_offset);
2193 move->src.n_bits = ntohs(n_bits);
2194 move->dst.ofs = ntohs(dst_offset);
2195 move->dst.n_bits = ntohs(n_bits);
2196
0a2869d5 2197 struct ofpbuf b = ofpbuf_const_initializer(action, ntohs(action_len));
914624f8 2198 ofpbuf_pull(&b, oxm_offset);
0a2869d5
BP
2199
2200 enum ofperr error = nx_pull_header(&b, &move->src.field, NULL);
f13cdd73
BP
2201 if (error) {
2202 return error;
2203 }
2204 error = nx_pull_header(&b, &move->dst.field, NULL);
2205 if (error) {
2206 return error;
2207 }
f13cdd73 2208
6fd6ed71 2209 if (!is_all_zeros(b.data, b.size)) {
f13cdd73
BP
2210 return OFPERR_NXBRC_MUST_BE_ZERO;
2211 }
2212
73178f20
BP
2213 return nxm_reg_move_check(move, NULL);
2214}
2215
914624f8
BP
2216static enum ofperr
2217decode_OFPAT_RAW15_COPY_FIELD(const struct ofp15_action_copy_field *oacf,
f3cd3ac7 2218 enum ofp_version ofp_version OVS_UNUSED,
914624f8
BP
2219 struct ofpbuf *ofpacts)
2220{
2221 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2222 oacf->n_bits, oacf, oacf->len,
2223 OBJECT_OFFSETOF(oacf, pad2), ofpacts);
2224}
2225
2226static enum ofperr
2227decode_ONFACT_RAW13_COPY_FIELD(const struct onf_action_copy_field *oacf,
f3cd3ac7 2228 enum ofp_version ofp_version OVS_UNUSED,
914624f8
BP
2229 struct ofpbuf *ofpacts)
2230{
2231 return decode_copy_field__(oacf->src_offset, oacf->dst_offset,
2232 oacf->n_bits, oacf, oacf->len,
2233 OBJECT_OFFSETOF(oacf, pad3), ofpacts);
2234}
2235
c2d936a4
BP
2236static enum ofperr
2237decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
f3cd3ac7 2238 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4
BP
2239 struct ofpbuf *ofpacts)
2240{
0a2869d5 2241 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
914624f8 2242 move->ofpact.raw = NXAST_RAW_REG_MOVE;
c2d936a4
BP
2243 move->src.ofs = ntohs(narm->src_ofs);
2244 move->src.n_bits = ntohs(narm->n_bits);
c2d936a4
BP
2245 move->dst.ofs = ntohs(narm->dst_ofs);
2246 move->dst.n_bits = ntohs(narm->n_bits);
d01c980f 2247
0a2869d5 2248 struct ofpbuf b = ofpbuf_const_initializer(narm, ntohs(narm->len));
bad8a439 2249 ofpbuf_pull(&b, sizeof *narm);
0a2869d5
BP
2250
2251 enum ofperr error = nx_pull_header(&b, &move->src.field, NULL);
bad8a439
BP
2252 if (error) {
2253 return error;
2254 }
2255 error = nx_pull_header(&b, &move->dst.field, NULL);
2256 if (error) {
2257 return error;
2258 }
6fd6ed71 2259 if (!is_all_zeros(b.data, b.size)) {
bad8a439
BP
2260 return OFPERR_NXBRC_MUST_BE_ZERO;
2261 }
2262
c2d936a4
BP
2263 return nxm_reg_move_check(move, NULL);
2264}
d01c980f 2265
c2d936a4
BP
2266static void
2267encode_REG_MOVE(const struct ofpact_reg_move *move,
73178f20
BP
2268 enum ofp_version ofp_version, struct ofpbuf *out)
2269{
914624f8
BP
2270 /* For OpenFlow 1.3, the choice of ONFACT_RAW13_COPY_FIELD versus
2271 * NXAST_RAW_REG_MOVE is somewhat difficult. Neither one is guaranteed to
2272 * be supported by every OpenFlow 1.3 implementation. It would be ideal to
2273 * probe for support. Until we have that ability, we currently prefer
2274 * NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
2275 * versions. */
6fd6ed71 2276 size_t start_ofs = out->size;
73178f20 2277 if (ofp_version >= OFP15_VERSION) {
bad8a439 2278 struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
73178f20
BP
2279 copy->n_bits = htons(move->dst.n_bits);
2280 copy->src_offset = htons(move->src.ofs);
2281 copy->dst_offset = htons(move->dst.ofs);
6fd6ed71 2282 out->size = out->size - sizeof copy->pad2;
f13cdd73
BP
2283 nx_put_header(out, move->src.field->id, ofp_version, false);
2284 nx_put_header(out, move->dst.field->id, ofp_version, false);
914624f8
BP
2285 } else if (ofp_version == OFP13_VERSION
2286 && move->ofpact.raw == ONFACT_RAW13_COPY_FIELD) {
2287 struct onf_action_copy_field *copy = put_ONFACT13_COPY_FIELD(out);
2288 copy->n_bits = htons(move->dst.n_bits);
2289 copy->src_offset = htons(move->src.ofs);
2290 copy->dst_offset = htons(move->dst.ofs);
6fd6ed71 2291 out->size = out->size - sizeof copy->pad3;
914624f8
BP
2292 nx_put_header(out, move->src.field->id, ofp_version, false);
2293 nx_put_header(out, move->dst.field->id, ofp_version, false);
73178f20 2294 } else {
bad8a439 2295 struct nx_action_reg_move *narm = put_NXAST_REG_MOVE(out);
73178f20
BP
2296 narm->n_bits = htons(move->dst.n_bits);
2297 narm->src_ofs = htons(move->src.ofs);
2298 narm->dst_ofs = htons(move->dst.ofs);
bad8a439
BP
2299 nx_put_header(out, move->src.field->id, 0, false);
2300 nx_put_header(out, move->dst.field->id, 0, false);
73178f20 2301 }
bad8a439 2302 pad_ofpat(out, start_ofs);
c2d936a4 2303}
e9536ecb 2304
cab50449 2305static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
2306parse_REG_MOVE(const char *arg, struct ofpbuf *ofpacts,
2307 enum ofputil_protocol *usable_protocols OVS_UNUSED)
2308{
2309 struct ofpact_reg_move *move = ofpact_put_REG_MOVE(ofpacts);
2310 const char *full_arg = arg;
2311 char *error;
097d4939 2312
c2d936a4
BP
2313 error = mf_parse_subfield__(&move->src, &arg);
2314 if (error) {
2315 return error;
2316 }
2317 if (strncmp(arg, "->", 2)) {
2318 return xasprintf("%s: missing `->' following source", full_arg);
2319 }
2320 arg += 2;
2321 error = mf_parse_subfield(&move->dst, arg);
2322 if (error) {
2323 return error;
2324 }
097d4939 2325
c2d936a4
BP
2326 if (move->src.n_bits != move->dst.n_bits) {
2327 return xasprintf("%s: source field is %d bits wide but destination is "
2328 "%d bits wide", full_arg,
2329 move->src.n_bits, move->dst.n_bits);
2330 }
2331 return NULL;
2332}
0f3f3c3d 2333
c2d936a4
BP
2334static void
2335format_REG_MOVE(const struct ofpact_reg_move *a, struct ds *s)
2336{
2337 nxm_format_reg_move(a, s);
2338}
2339\f
7eb4b1f1
BP
2340/* Action structure for OFPAT12_SET_FIELD. */
2341struct ofp12_action_set_field {
2342 ovs_be16 type; /* OFPAT12_SET_FIELD. */
2343 ovs_be16 len; /* Length is padded to 64 bits. */
2344
2345 /* Followed by:
2346 * - An OXM header, value, and (in OpenFlow 1.5+) optionally a mask.
2347 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2348 *
2349 * The "pad" member is the beginning of the above. */
2350 uint8_t pad[4];
2351};
2352OFP_ASSERT(sizeof(struct ofp12_action_set_field) == 8);
2353
c2d936a4
BP
2354/* Action structure for NXAST_REG_LOAD.
2355 *
2356 * Copies value[0:n_bits] to dst[ofs:ofs+n_bits], where a[b:c] denotes the bits
2357 * within 'a' numbered 'b' through 'c' (not including bit 'c'). Bit numbering
2358 * starts at 0 for the least-significant bit, 1 for the next most significant
2359 * bit, and so on.
2360 *
2361 * 'dst' is an nxm_header with nxm_hasmask=0. See the documentation for
2362 * NXAST_REG_MOVE, above, for the permitted fields and for the side effects of
2363 * loading them.
2364 *
2365 * The 'ofs' and 'n_bits' fields are combined into a single 'ofs_nbits' field
2366 * to avoid enlarging the structure by another 8 bytes. To allow 'n_bits' to
2367 * take a value between 1 and 64 (inclusive) while taking up only 6 bits, it is
2368 * also stored as one less than its true value:
2369 *
2370 * 15 6 5 0
2371 * +------------------------------+------------------+
2372 * | ofs | n_bits - 1 |
2373 * +------------------------------+------------------+
2374 *
2375 * The switch will reject actions for which ofs+n_bits is greater than the
2376 * width of 'dst', or in which any bits in 'value' with value 2**n_bits or
2377 * greater are set to 1, with error type OFPET_BAD_ACTION, code
2378 * OFPBAC_BAD_ARGUMENT.
2379 */
2380struct nx_action_reg_load {
2381 ovs_be16 type; /* OFPAT_VENDOR. */
2382 ovs_be16 len; /* Length is 24. */
2383 ovs_be32 vendor; /* NX_VENDOR_ID. */
2384 ovs_be16 subtype; /* NXAST_REG_LOAD. */
2385 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
2386 ovs_be32 dst; /* Destination register. */
2387 ovs_be64 value; /* Immediate value. */
2388};
2389OFP_ASSERT(sizeof(struct nx_action_reg_load) == 24);
b676167a 2390
bad8a439
BP
2391/* Action structure for NXAST_REG_LOAD2.
2392 *
2393 * Compared to OFPAT_SET_FIELD, we can use this to set whole or partial fields
2394 * in any OpenFlow version. Compared to NXAST_REG_LOAD, we can use this to set
2395 * OXM experimenter fields. */
2396struct nx_action_reg_load2 {
2397 ovs_be16 type; /* OFPAT_VENDOR. */
2398 ovs_be16 len; /* At least 16. */
2399 ovs_be32 vendor; /* NX_VENDOR_ID. */
2400 ovs_be16 subtype; /* NXAST_SET_FIELD. */
2401
2402 /* Followed by:
2403 * - An NXM/OXM header, value, and optionally a mask.
2404 * - Enough 0-bytes to pad out to a multiple of 64 bits.
2405 *
2406 * The "pad" member is the beginning of the above. */
2407 uint8_t pad[6];
2408};
2409OFP_ASSERT(sizeof(struct nx_action_reg_load2) == 16);
2410
c2d936a4 2411static enum ofperr
7eb4b1f1
BP
2412decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
2413 bool may_mask, struct ofpbuf *ofpacts)
7fdb60a7 2414{
0a2869d5 2415 struct ofpbuf b = ofpbuf_const_initializer(oasf, ntohs(oasf->len));
7eb4b1f1 2416 ofpbuf_pull(&b, OBJECT_OFFSETOF(oasf, pad));
0a2869d5
BP
2417
2418 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2419 enum ofperr error = nx_pull_entry(&b, &sf->field, &sf->value,
2420 may_mask ? &sf->mask : NULL);
178742f9 2421 if (error) {
178742f9
BP
2422 return (error == OFPERR_OFPBMC_BAD_MASK
2423 ? OFPERR_OFPBAC_BAD_SET_MASK
2424 : error);
c2d936a4 2425 }
7eb4b1f1
BP
2426 if (!may_mask) {
2427 memset(&sf->mask, 0xff, sf->field->n_bytes);
2428 }
178742f9 2429
6fd6ed71 2430 if (!is_all_zeros(b.data, b.size)) {
c2d936a4
BP
2431 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2432 }
7fdb60a7 2433
178742f9
BP
2434 /* OpenFlow says specifically that one may not set OXM_OF_IN_PORT via
2435 * Set-Field. */
2436 if (sf->field->id == MFF_IN_PORT_OXM) {
2437 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
c2d936a4 2438 }
178742f9 2439
c2d936a4 2440 /* oxm_length is now validated to be compatible with mf_value. */
178742f9
BP
2441 if (!sf->field->writable) {
2442 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2443 sf->field->name);
c2d936a4
BP
2444 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2445 }
c2d936a4 2446
d3cb080e 2447 /* The value must be valid for match. OpenFlow 1.5 also says,
7eb4b1f1
BP
2448 * "In an OXM_OF_VLAN_VID set-field action, the OFPVID_PRESENT bit must be
2449 * a 1-bit in oxm_value and in oxm_mask." */
178742f9
BP
2450 if (!mf_is_value_valid(sf->field, &sf->value)
2451 || (sf->field->id == MFF_VLAN_VID
31c9ed6c
BP
2452 && (!(sf->mask.be16 & htons(OFPVID12_PRESENT))
2453 || !(sf->value.be16 & htons(OFPVID12_PRESENT))))) {
c2d936a4 2454 struct ds ds = DS_EMPTY_INITIALIZER;
178742f9 2455 mf_format(sf->field, &sf->value, NULL, &ds);
c2d936a4 2456 VLOG_WARN_RL(&rl, "Invalid value for set field %s: %s",
178742f9 2457 sf->field->name, ds_cstr(&ds));
c2d936a4
BP
2458 ds_destroy(&ds);
2459
2460 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
7fdb60a7 2461 }
c2d936a4 2462 return 0;
7fdb60a7
SH
2463}
2464
7eb4b1f1
BP
2465static enum ofperr
2466decode_OFPAT_RAW12_SET_FIELD(const struct ofp12_action_set_field *oasf,
f3cd3ac7 2467 enum ofp_version ofp_version OVS_UNUSED,
7eb4b1f1
BP
2468 struct ofpbuf *ofpacts)
2469{
2470 return decode_ofpat_set_field(oasf, false, ofpacts);
2471}
2472
2473static enum ofperr
2474decode_OFPAT_RAW15_SET_FIELD(const struct ofp12_action_set_field *oasf,
f3cd3ac7 2475 enum ofp_version ofp_version OVS_UNUSED,
7eb4b1f1
BP
2476 struct ofpbuf *ofpacts)
2477{
2478 return decode_ofpat_set_field(oasf, true, ofpacts);
2479}
2480
2481static enum ofperr
2482decode_NXAST_RAW_REG_LOAD(const struct nx_action_reg_load *narl,
f3cd3ac7 2483 enum ofp_version ofp_version OVS_UNUSED,
7eb4b1f1
BP
2484 struct ofpbuf *out)
2485{
2486 struct ofpact_set_field *sf = ofpact_put_reg_load(out);
2487 struct mf_subfield dst;
2488 enum ofperr error;
2489
2490 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2491
2492 dst.field = mf_from_nxm_header(ntohl(narl->dst));
2493 dst.ofs = nxm_decode_ofs(narl->ofs_nbits);
2494 dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits);
2495 error = mf_check_dst(&dst, NULL);
2496 if (error) {
2497 return error;
2498 }
2499
2500 /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in
2501 * narl->value. */
2502 if (dst.n_bits < 64 && ntohll(narl->value) >> dst.n_bits) {
2503 return OFPERR_OFPBAC_BAD_ARGUMENT;
2504 }
2505
2506 sf->field = dst.field;
2507 bitwise_put(ntohll(narl->value),
2508 &sf->value, dst.field->n_bytes, dst.ofs,
2509 dst.n_bits);
2510 bitwise_put(UINT64_MAX,
2511 &sf->mask, dst.field->n_bytes, dst.ofs,
2512 dst.n_bits);
2513
2514 return 0;
2515}
2516
bad8a439
BP
2517static enum ofperr
2518decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
f3cd3ac7 2519 enum ofp_version ofp_version OVS_UNUSED,
bad8a439
BP
2520 struct ofpbuf *out)
2521{
0a2869d5 2522 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(out);
bad8a439
BP
2523 sf->ofpact.raw = NXAST_RAW_REG_LOAD2;
2524
0a2869d5 2525 struct ofpbuf b = ofpbuf_const_initializer(narl, ntohs(narl->len));
bad8a439 2526 ofpbuf_pull(&b, OBJECT_OFFSETOF(narl, pad));
0a2869d5
BP
2527
2528 enum ofperr error = nx_pull_entry(&b, &sf->field, &sf->value, &sf->mask);
bad8a439
BP
2529 if (error) {
2530 return error;
2531 }
6fd6ed71 2532 if (!is_all_zeros(b.data, b.size)) {
bad8a439
BP
2533 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2534 }
2535
2536 if (!sf->field->writable) {
2537 VLOG_WARN_RL(&rl, "destination field %s is not writable",
2538 sf->field->name);
2539 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
2540 }
2541 return 0;
2542}
2543
7fdb60a7 2544static void
c2d936a4 2545ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
178742f9 2546 enum mf_field_id field, uint64_t value_)
7fdb60a7 2547{
7eb4b1f1 2548 struct ofp12_action_set_field *oasf OVS_UNUSED;
178742f9 2549 int n_bytes = mf_from_id(field)->n_bytes;
6fd6ed71 2550 size_t start_ofs = openflow->size;
178742f9 2551 union mf_value value;
7fdb60a7 2552
178742f9 2553 value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
7fdb60a7 2554
7eb4b1f1 2555 oasf = put_OFPAT12_SET_FIELD(openflow);
6fd6ed71 2556 openflow->size = openflow->size - sizeof oasf->pad;
178742f9
BP
2557 nx_put_entry(openflow, field, ofp_version, &value, NULL);
2558 pad_ofpat(openflow, start_ofs);
7fdb60a7
SH
2559}
2560
7eb4b1f1
BP
2561static bool
2562next_load_segment(const struct ofpact_set_field *sf,
2563 struct mf_subfield *dst, uint64_t *value)
2564{
fa078489
BP
2565 int n_bits = sf->field->n_bits;
2566 int n_bytes = sf->field->n_bytes;
7eb4b1f1
BP
2567 int start = dst->ofs + dst->n_bits;
2568
fa078489 2569 if (start < n_bits) {
7eb4b1f1 2570 dst->field = sf->field;
fa078489
BP
2571 dst->ofs = bitwise_scan(&sf->mask, n_bytes, 1, start, n_bits);
2572 if (dst->ofs < n_bits) {
2573 dst->n_bits = bitwise_scan(&sf->mask, n_bytes, 0, dst->ofs + 1,
2574 MIN(dst->ofs + 64, n_bits)) - dst->ofs;
2575 *value = bitwise_get(&sf->value, n_bytes, dst->ofs, dst->n_bits);
7eb4b1f1
BP
2576 return true;
2577 }
2578 }
2579 return false;
2580}
178742f9 2581
7eb4b1f1 2582/* Convert 'sf' to a series of REG_LOADs. */
7fdb60a7 2583static void
c2d936a4 2584set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
7fdb60a7 2585{
bad8a439 2586 /* If 'sf' cannot be encoded as NXAST_REG_LOAD because it requires an
4fe04a7e
JG
2587 * experimenter OXM or is variable length (or if it came in as
2588 * NXAST_REG_LOAD2), encode as NXAST_REG_LOAD2. Otherwise use
2589 * NXAST_REG_LOAD, which is backward compatible. */
bad8a439 2590 if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
4fe04a7e 2591 || !mf_nxm_header(sf->field->id) || sf->field->variable_len) {
bad8a439 2592 struct nx_action_reg_load2 *narl OVS_UNUSED;
6fd6ed71 2593 size_t start_ofs = openflow->size;
bad8a439
BP
2594
2595 narl = put_NXAST_REG_LOAD2(openflow);
6fd6ed71 2596 openflow->size = openflow->size - sizeof narl->pad;
bad8a439
BP
2597 nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
2598 pad_ofpat(openflow, start_ofs);
2599 } else {
2600 struct mf_subfield dst;
2601 uint64_t value;
7eb4b1f1 2602
bad8a439
BP
2603 dst.ofs = dst.n_bits = 0;
2604 while (next_load_segment(sf, &dst, &value)) {
2605 struct nx_action_reg_load *narl = put_NXAST_REG_LOAD(openflow);
2606 narl->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
2607 narl->dst = htonl(mf_nxm_header(dst.field->id));
2608 narl->value = htonll(value);
2609 }
7fdb60a7
SH
2610 }
2611}
2612
7eb4b1f1
BP
2613/* Convert 'sf', which must set an entire field, to standard OpenFlow 1.0/1.1
2614 * actions, if we can, falling back to Nicira extensions if we must.
7fdb60a7 2615 *
c2d936a4
BP
2616 * We check only meta-flow types that can appear within set field actions and
2617 * that have a mapping to compatible action types. These struct mf_field
2618 * definitions have a defined OXM or NXM header value and specify the field as
2619 * writable. */
2620static void
2621set_field_to_legacy_openflow(const struct ofpact_set_field *sf,
2622 enum ofp_version ofp_version,
2623 struct ofpbuf *out)
7fdb60a7 2624{
c2d936a4
BP
2625 switch ((int) sf->field->id) {
2626 case MFF_VLAN_TCI: {
2627 ovs_be16 tci = sf->value.be16;
2628 bool cfi = (tci & htons(VLAN_CFI)) != 0;
2629 uint16_t vid = vlan_tci_to_vid(tci);
2630 uint8_t pcp = vlan_tci_to_pcp(tci);
2631
2632 if (ofp_version < OFP11_VERSION) {
2633 /* NXM_OF_VLAN_TCI to OpenFlow 1.0 mapping:
2634 *
2635 * If CFI=1, Add or modify VLAN VID & PCP.
2636 * If CFI=0, strip VLAN header, if any.
2637 */
2638 if (cfi) {
2639 put_OFPAT10_SET_VLAN_VID(out, vid);
2640 put_OFPAT10_SET_VLAN_PCP(out, pcp);
2641 } else {
2642 put_OFPAT10_STRIP_VLAN(out);
2643 }
2644 } else {
2645 /* NXM_OF_VLAN_TCI to OpenFlow 1.1 mapping:
2646 *
2647 * If CFI=1, Add or modify VLAN VID & PCP.
2648 * OpenFlow 1.1 set actions only apply if the packet
2649 * already has VLAN tags. To be sure that is the case
2650 * we have to push a VLAN header. As we do not support
2651 * multiple layers of VLANs, this is a no-op, if a VLAN
2652 * header already exists. This may backfire, however,
2653 * when we start supporting multiple layers of VLANs.
2654 * If CFI=0, strip VLAN header, if any.
2655 */
2656 if (cfi) {
2657 /* Push a VLAN tag, if one was not seen at action validation
2658 * time. */
2659 if (!sf->flow_has_vlan) {
2660 put_OFPAT11_PUSH_VLAN(out, htons(ETH_TYPE_VLAN_8021Q));
2661 }
2662 put_OFPAT11_SET_VLAN_VID(out, vid);
2663 put_OFPAT11_SET_VLAN_PCP(out, pcp);
2664 } else {
2665 /* If the flow did not match on vlan, we have no way of
2666 * knowing if the vlan tag exists, so we must POP just to be
2667 * sure. */
2668 put_OFPAT11_POP_VLAN(out);
2669 }
2670 }
2671 break;
2672 }
7fdb60a7 2673
c2d936a4
BP
2674 case MFF_VLAN_VID: {
2675 uint16_t vid = ntohs(sf->value.be16) & VLAN_VID_MASK;
2676 if (ofp_version == OFP10_VERSION) {
2677 put_OFPAT10_SET_VLAN_VID(out, vid);
2678 } else {
2679 put_OFPAT11_SET_VLAN_VID(out, vid);
2680 }
2681 break;
2682 }
2683
2684 case MFF_VLAN_PCP:
2685 if (ofp_version == OFP10_VERSION) {
2686 put_OFPAT10_SET_VLAN_PCP(out, sf->value.u8);
2687 } else {
2688 put_OFPAT11_SET_VLAN_PCP(out, sf->value.u8);
2689 }
2690 break;
2691
2692 case MFF_ETH_SRC:
74ff3298 2693 put_OFPAT_SET_DL_SRC(out, ofp_version)->dl_addr = sf->value.mac;
c2d936a4
BP
2694 break;
2695
2696 case MFF_ETH_DST:
74ff3298 2697 put_OFPAT_SET_DL_DST(out, ofp_version)->dl_addr = sf->value.mac;
c2d936a4
BP
2698 break;
2699
2700 case MFF_IPV4_SRC:
2701 put_OFPAT_SET_NW_SRC(out, ofp_version, sf->value.be32);
2702 break;
2703
2704 case MFF_IPV4_DST:
2705 put_OFPAT_SET_NW_DST(out, ofp_version, sf->value.be32);
2706 break;
2707
2708 case MFF_IP_DSCP:
2709 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8);
2710 break;
2711
2712 case MFF_IP_DSCP_SHIFTED:
2713 put_OFPAT_SET_NW_TOS(out, ofp_version, sf->value.u8 << 2);
2714 break;
2715
2716 case MFF_TCP_SRC:
2717 case MFF_UDP_SRC:
2718 put_OFPAT_SET_TP_SRC(out, sf->value.be16);
2719 break;
2720
2721 case MFF_TCP_DST:
2722 case MFF_UDP_DST:
2723 put_OFPAT_SET_TP_DST(out, sf->value.be16);
2724 break;
2725
2726 default:
2727 set_field_to_nxast(sf, out);
2728 break;
7fdb60a7
SH
2729 }
2730}
2731
7eb4b1f1
BP
2732static void
2733set_field_to_set_field(const struct ofpact_set_field *sf,
2734 enum ofp_version ofp_version, struct ofpbuf *out)
2735{
2736 struct ofp12_action_set_field *oasf OVS_UNUSED;
6fd6ed71 2737 size_t start_ofs = out->size;
7eb4b1f1
BP
2738
2739 oasf = put_OFPAT12_SET_FIELD(out);
6fd6ed71 2740 out->size = out->size - sizeof oasf->pad;
7eb4b1f1
BP
2741 nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
2742 pad_ofpat(out, start_ofs);
2743}
2744
c2d936a4
BP
2745static void
2746encode_SET_FIELD(const struct ofpact_set_field *sf,
2747 enum ofp_version ofp_version, struct ofpbuf *out)
2748{
7eb4b1f1 2749 if (ofp_version >= OFP15_VERSION) {
bad8a439
BP
2750 /* OF1.5+ only has Set-Field (reg_load is redundant so we drop it
2751 * entirely). */
7eb4b1f1 2752 set_field_to_set_field(sf, ofp_version, out);
bad8a439
BP
2753 } else if (sf->ofpact.raw == NXAST_RAW_REG_LOAD ||
2754 sf->ofpact.raw == NXAST_RAW_REG_LOAD2) {
2755 /* It came in as reg_load, send it out the same way. */
7eb4b1f1
BP
2756 set_field_to_nxast(sf, out);
2757 } else if (ofp_version < OFP12_VERSION) {
2758 /* OpenFlow 1.0 and 1.1 don't have Set-Field. */
c2d936a4 2759 set_field_to_legacy_openflow(sf, ofp_version, out);
7eb4b1f1
BP
2760 } else if (is_all_ones((const uint8_t *) &sf->mask, sf->field->n_bytes)) {
2761 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action sets an
2762 * entire field, so encode it as OFPAT_SET_FIELD. */
2763 set_field_to_set_field(sf, ofp_version, out);
c2d936a4 2764 } else {
7eb4b1f1
BP
2765 /* We're encoding to OpenFlow 1.2, 1.3, or 1.4. The action cannot be
2766 * encoded as OFPAT_SET_FIELD because it does not set an entire field,
2767 * so encode it as reg_load. */
2768 set_field_to_nxast(sf, out);
c2d936a4
BP
2769 }
2770}
2771
36b43aa6
JS
2772/* Parses the input argument 'arg' into the key, value, and delimiter
2773 * components that are common across the reg_load and set_field action format.
2774 *
2775 * With an argument like "1->metadata", sets the following pointers to
2776 * point within 'arg':
2777 * key: "metadata"
2778 * value: "1"
2779 * delim: "->"
2780 *
2781 * Returns NULL if successful, otherwise a malloc()'d string describing the
2782 * error. The caller is responsible for freeing the returned string. */
2783static char * OVS_WARN_UNUSED_RESULT
2784set_field_split_str(char *arg, char **key, char **value, char **delim)
2785{
2786 char *value_end;
2787
2788 *value = arg;
2789 value_end = strstr(arg, "->");
2790 *key = value_end + strlen("->");
2791 if (delim) {
2792 *delim = value_end;
2793 }
2794
2795 if (!value_end) {
2796 return xasprintf("%s: missing `->'", arg);
2797 }
2798 if (strlen(value_end) <= strlen("->")) {
2799 return xasprintf("%s: missing field name following `->'", arg);
2800 }
2801
2802 return NULL;
2803}
2804
c2d936a4
BP
2805/* Parses a "set_field" action with argument 'arg', appending the parsed
2806 * action to 'ofpacts'.
2807 *
2808 * Returns NULL if successful, otherwise a malloc()'d string describing the
2809 * error. The caller is responsible for freeing the returned string. */
cab50449 2810static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
2811set_field_parse__(char *arg, struct ofpbuf *ofpacts,
2812 enum ofputil_protocol *usable_protocols)
7fdb60a7 2813{
c2d936a4
BP
2814 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2815 char *value;
2816 char *delim;
2817 char *key;
2818 const struct mf_field *mf;
2819 char *error;
7fdb60a7 2820
36b43aa6
JS
2821 error = set_field_split_str(arg, &key, &value, &delim);
2822 if (error) {
2823 return error;
c2d936a4 2824 }
e3f8f887 2825
c2d936a4
BP
2826 mf = mf_from_name(key);
2827 if (!mf) {
2828 return xasprintf("%s is not a valid OXM field name", key);
2829 }
2830 if (!mf->writable) {
2831 return xasprintf("%s is read-only", key);
2832 }
2833 sf->field = mf;
2834 delim[0] = '\0';
7eb4b1f1 2835 error = mf_parse(mf, value, &sf->value, &sf->mask);
7fdb60a7
SH
2836 if (error) {
2837 return error;
2838 }
2839
c2d936a4
BP
2840 if (!mf_is_value_valid(mf, &sf->value)) {
2841 return xasprintf("%s is not a valid value for field %s", value, key);
7fdb60a7
SH
2842 }
2843
a4ce8b25 2844 *usable_protocols &= mf->usable_protocols_exact;
c2d936a4 2845 return NULL;
7fdb60a7
SH
2846}
2847
c2d936a4
BP
2848/* Parses 'arg' as the argument to a "set_field" action, and appends such an
2849 * action to 'ofpacts'.
2850 *
2851 * Returns NULL if successful, otherwise a malloc()'d string describing the
2852 * error. The caller is responsible for freeing the returned string. */
cab50449 2853static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
2854parse_SET_FIELD(const char *arg, struct ofpbuf *ofpacts,
2855 enum ofputil_protocol *usable_protocols)
2856{
2857 char *copy = xstrdup(arg);
2858 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
2859 free(copy);
2860 return error;
2861}
a359d5ad 2862
cab50449 2863static char * OVS_WARN_UNUSED_RESULT
7eb4b1f1
BP
2864parse_reg_load(char *arg, struct ofpbuf *ofpacts)
2865{
2866 struct ofpact_set_field *sf = ofpact_put_reg_load(ofpacts);
7eb4b1f1 2867 struct mf_subfield dst;
36b43aa6 2868 char *key, *value_str;
81270993 2869 union mf_value value;
7eb4b1f1
BP
2870 char *error;
2871
36b43aa6
JS
2872 error = set_field_split_str(arg, &key, &value_str, NULL);
2873 if (error) {
2874 return error;
7eb4b1f1 2875 }
36b43aa6
JS
2876
2877 error = mf_parse_subfield(&dst, key);
7eb4b1f1
BP
2878 if (error) {
2879 return error;
2880 }
2881
81270993
JS
2882 if (parse_int_string(value_str, (uint8_t *)&value, dst.field->n_bytes,
2883 &key)) {
2884 return xasprintf("%s: cannot parse integer value", arg);
2885 }
2886
2887 if (!bitwise_is_all_zeros(&value, dst.field->n_bytes, dst.n_bits,
2888 dst.field->n_bytes * 8 - dst.n_bits)) {
2889 struct ds ds;
2890
2891 ds_init(&ds);
2892 mf_format(dst.field, &value, NULL, &ds);
2893 error = xasprintf("%s: value %s does not fit into %d bits",
2894 arg, ds_cstr(&ds), dst.n_bits);
2895 ds_destroy(&ds);
2896 return error;
7eb4b1f1
BP
2897 }
2898
2899 sf->field = dst.field;
2900 memset(&sf->value, 0, sizeof sf->value);
81270993
JS
2901 bitwise_copy(&value, dst.field->n_bytes, 0, &sf->value,
2902 dst.field->n_bytes, dst.ofs, dst.n_bits);
2903 bitwise_one(&sf->mask, dst.field->n_bytes, dst.ofs, dst.n_bits);
2904
7eb4b1f1
BP
2905 return NULL;
2906}
2907
c2d936a4
BP
2908static void
2909format_SET_FIELD(const struct ofpact_set_field *a, struct ds *s)
2910{
7eb4b1f1
BP
2911 if (a->ofpact.raw == NXAST_RAW_REG_LOAD) {
2912 struct mf_subfield dst;
2913 uint64_t value;
2914
2915 dst.ofs = dst.n_bits = 0;
2916 while (next_load_segment(a, &dst, &value)) {
b1c5bf1f
QM
2917 ds_put_format(s, "%sload:%s%#"PRIx64"%s->%s",
2918 colors.special, colors.end, value,
2919 colors.special, colors.end);
7eb4b1f1
BP
2920 mf_format_subfield(&dst, s);
2921 ds_put_char(s, ',');
2922 }
2923 ds_chomp(s, ',');
2924 } else {
b1c5bf1f 2925 ds_put_format(s, "%sset_field:%s", colors.special, colors.end);
7eb4b1f1 2926 mf_format(a->field, &a->value, &a->mask, s);
b1c5bf1f
QM
2927 ds_put_format(s, "%s->%s%s",
2928 colors.special, colors.end, a->field->name);
7eb4b1f1
BP
2929 }
2930}
2931
2932/* Appends an OFPACT_SET_FIELD ofpact to 'ofpacts' and returns it. The ofpact
2933 * is marked such that, if possible, it will be translated to OpenFlow as
2934 * NXAST_REG_LOAD extension actions rather than OFPAT_SET_FIELD, either because
2935 * that was the way that the action was expressed when it came into OVS or for
2936 * backward compatibility. */
2937struct ofpact_set_field *
2938ofpact_put_reg_load(struct ofpbuf *ofpacts)
2939{
2940 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
2941 sf->ofpact.raw = NXAST_RAW_REG_LOAD;
2942 return sf;
c2d936a4
BP
2943}
2944\f
2945/* Action structure for NXAST_STACK_PUSH and NXAST_STACK_POP.
2946 *
2947 * Pushes (or pops) field[offset: offset + n_bits] to (or from)
2948 * top of the stack.
2949 */
2950struct nx_action_stack {
2951 ovs_be16 type; /* OFPAT_VENDOR. */
2952 ovs_be16 len; /* Length is 16. */
2953 ovs_be32 vendor; /* NX_VENDOR_ID. */
2954 ovs_be16 subtype; /* NXAST_STACK_PUSH or NXAST_STACK_POP. */
2955 ovs_be16 offset; /* Bit offset into the field. */
bad8a439
BP
2956 /* Followed by:
2957 * - OXM/NXM header for field to push or pop (4 or 8 bytes).
2958 * - ovs_be16 'n_bits', the number of bits to extract from the field.
2959 * - Enough 0-bytes to pad out the action to 24 bytes. */
2960 uint8_t pad[12]; /* See above. */
a359d5ad 2961};
c2d936a4 2962OFP_ASSERT(sizeof(struct nx_action_stack) == 24);
a359d5ad 2963
bad8a439 2964static enum ofperr
c2d936a4
BP
2965decode_stack_action(const struct nx_action_stack *nasp,
2966 struct ofpact_stack *stack_action)
a359d5ad 2967{
c2d936a4 2968 stack_action->subfield.ofs = ntohs(nasp->offset);
bad8a439 2969
0a2869d5 2970 struct ofpbuf b = ofpbuf_const_initializer(nasp, sizeof *nasp);
bad8a439 2971 ofpbuf_pull(&b, OBJECT_OFFSETOF(nasp, pad));
0a2869d5
BP
2972 enum ofperr error = nx_pull_header(&b, &stack_action->subfield.field,
2973 NULL);
bad8a439
BP
2974 if (error) {
2975 return error;
2976 }
6fd6ed71 2977 stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
bad8a439 2978 ofpbuf_pull(&b, 2);
6fd6ed71 2979 if (!is_all_zeros(b.data, b.size)) {
bad8a439
BP
2980 return OFPERR_NXBRC_MUST_BE_ZERO;
2981 }
2982
2983 return 0;
a359d5ad
IY
2984}
2985
c2d936a4
BP
2986static enum ofperr
2987decode_NXAST_RAW_STACK_PUSH(const struct nx_action_stack *nasp,
f3cd3ac7
JS
2988 enum ofp_version ofp_version OVS_UNUSED,
2989 struct ofpbuf *ofpacts)
a359d5ad 2990{
c2d936a4 2991 struct ofpact_stack *push = ofpact_put_STACK_PUSH(ofpacts);
bad8a439
BP
2992 enum ofperr error = decode_stack_action(nasp, push);
2993 return error ? error : nxm_stack_push_check(push, NULL);
a359d5ad
IY
2994}
2995
c2d936a4
BP
2996static enum ofperr
2997decode_NXAST_RAW_STACK_POP(const struct nx_action_stack *nasp,
f3cd3ac7 2998 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4 2999 struct ofpbuf *ofpacts)
084c53de 3000{
c2d936a4 3001 struct ofpact_stack *pop = ofpact_put_STACK_POP(ofpacts);
bad8a439
BP
3002 enum ofperr error = decode_stack_action(nasp, pop);
3003 return error ? error : nxm_stack_pop_check(pop, NULL);
084c53de
BP
3004}
3005
c2d936a4
BP
3006static void
3007encode_STACK_op(const struct ofpact_stack *stack_action,
3008 struct nx_action_stack *nasp)
c0621c39 3009{
bad8a439
BP
3010 struct ofpbuf b;
3011 ovs_be16 n_bits;
3012
c2d936a4 3013 nasp->offset = htons(stack_action->subfield.ofs);
bad8a439
BP
3014
3015 ofpbuf_use_stack(&b, nasp, ntohs(nasp->len));
3016 ofpbuf_put_uninit(&b, OBJECT_OFFSETOF(nasp, pad));
3017 nx_put_header(&b, stack_action->subfield.field->id, 0, false);
3018 n_bits = htons(stack_action->subfield.n_bits);
3019 ofpbuf_put(&b, &n_bits, sizeof n_bits);
c0621c39
AW
3020}
3021
c2d936a4
BP
3022static void
3023encode_STACK_PUSH(const struct ofpact_stack *stack,
3024 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3025{
3026 encode_STACK_op(stack, put_NXAST_STACK_PUSH(out));
3027}
8e97815e 3028
c2d936a4
BP
3029static void
3030encode_STACK_POP(const struct ofpact_stack *stack,
3031 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
8e97815e 3032{
c2d936a4
BP
3033 encode_STACK_op(stack, put_NXAST_STACK_POP(out));
3034}
8e97815e 3035
cab50449 3036static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3037parse_STACK_PUSH(char *arg, struct ofpbuf *ofpacts,
3038 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3039{
3040 return nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
3041}
8e97815e 3042
cab50449 3043static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3044parse_STACK_POP(char *arg, struct ofpbuf *ofpacts,
3045 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3046{
3047 return nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
8e97815e
BP
3048}
3049
c2d936a4
BP
3050static void
3051format_STACK_PUSH(const struct ofpact_stack *a, struct ds *s)
8e97815e 3052{
c2d936a4
BP
3053 nxm_format_stack_push(a, s);
3054}
8e97815e 3055
c2d936a4
BP
3056static void
3057format_STACK_POP(const struct ofpact_stack *a, struct ds *s)
3058{
3059 nxm_format_stack_pop(a, s);
3060}
3061\f
3062/* Action structure for NXAST_DEC_TTL_CNT_IDS.
3063 *
3064 * If the packet is not IPv4 or IPv6, does nothing. For IPv4 or IPv6, if the
3065 * TTL or hop limit is at least 2, decrements it by 1. Otherwise, if TTL or
3066 * hop limit is 0 or 1, sends a packet-in to the controllers with each of the
3067 * 'n_controllers' controller IDs specified in 'cnt_ids'.
3068 *
3069 * (This differs from NXAST_DEC_TTL in that for NXAST_DEC_TTL the packet-in is
3070 * sent only to controllers with id 0.)
3071 */
3072struct nx_action_cnt_ids {
3073 ovs_be16 type; /* OFPAT_VENDOR. */
3074 ovs_be16 len; /* Length including slaves. */
3075 ovs_be32 vendor; /* NX_VENDOR_ID. */
3076 ovs_be16 subtype; /* NXAST_DEC_TTL_CNT_IDS. */
3077
3078 ovs_be16 n_controllers; /* Number of controllers. */
3079 uint8_t zeros[4]; /* Must be zero. */
3080
3081 /* Followed by 1 or more controller ids.
3082 *
3083 * uint16_t cnt_ids[]; // Controller ids.
3084 * uint8_t pad[]; // Must be 0 to 8-byte align cnt_ids[].
3085 */
3086};
3087OFP_ASSERT(sizeof(struct nx_action_cnt_ids) == 16);
3088
3089static enum ofperr
3090decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
3091{
3092 uint16_t id = 0;
3093 struct ofpact_cnt_ids *ids;
3094 enum ofperr error = 0;
3095
3096 ids = ofpact_put_DEC_TTL(out);
3097 ids->n_controllers = 1;
3098 ofpbuf_put(out, &id, sizeof id);
6fd6ed71 3099 ids = out->header;
34abaa3d 3100 ofpact_finish(out, &ids->ofpact);
c2d936a4
BP
3101 return error;
3102}
3103
3104static enum ofperr
3105decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
f3cd3ac7 3106 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4
BP
3107 struct ofpbuf *out)
3108{
3109 struct ofpact_cnt_ids *ids;
3110 size_t ids_size;
3111 int i;
3112
3113 ids = ofpact_put_DEC_TTL(out);
3114 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3115 ids->n_controllers = ntohs(nac_ids->n_controllers);
3116 ids_size = ntohs(nac_ids->len) - sizeof *nac_ids;
3117
3118 if (!is_all_zeros(nac_ids->zeros, sizeof nac_ids->zeros)) {
3119 return OFPERR_NXBRC_MUST_BE_ZERO;
8e97815e 3120 }
c2d936a4
BP
3121
3122 if (ids_size < ids->n_controllers * sizeof(ovs_be16)) {
3123 VLOG_WARN_RL(&rl, "Nicira action dec_ttl_cnt_ids only has %"PRIuSIZE" "
3124 "bytes allocated for controller ids. %"PRIuSIZE" bytes "
3125 "are required for %"PRIu16" controllers.",
3126 ids_size, ids->n_controllers * sizeof(ovs_be16),
3127 ids->n_controllers);
3128 return OFPERR_OFPBAC_BAD_LEN;
3129 }
3130
3131 for (i = 0; i < ids->n_controllers; i++) {
3132 uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
3133 ofpbuf_put(out, &id, sizeof id);
6fd6ed71 3134 ids = out->header;
c2d936a4
BP
3135 }
3136
34abaa3d 3137 ofpact_finish(out, &ids->ofpact);
c2d936a4
BP
3138
3139 return 0;
8e97815e
BP
3140}
3141
c2d936a4
BP
3142static void
3143encode_DEC_TTL(const struct ofpact_cnt_ids *dec_ttl,
3144 enum ofp_version ofp_version, struct ofpbuf *out)
8e97815e 3145{
c2d936a4
BP
3146 if (dec_ttl->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS
3147 || dec_ttl->n_controllers != 1
3148 || dec_ttl->cnt_ids[0] != 0) {
3149 struct nx_action_cnt_ids *nac_ids = put_NXAST_DEC_TTL_CNT_IDS(out);
3150 int ids_len = ROUND_UP(2 * dec_ttl->n_controllers, OFP_ACTION_ALIGN);
3151 ovs_be16 *ids;
3152 size_t i;
8e97815e 3153
c2d936a4
BP
3154 nac_ids->len = htons(ntohs(nac_ids->len) + ids_len);
3155 nac_ids->n_controllers = htons(dec_ttl->n_controllers);
3156
3157 ids = ofpbuf_put_zeros(out, ids_len);
3158 for (i = 0; i < dec_ttl->n_controllers; i++) {
3159 ids[i] = htons(dec_ttl->cnt_ids[i]);
8e97815e 3160 }
c2d936a4
BP
3161 } else {
3162 put_OFPAT_DEC_NW_TTL(out, ofp_version);
8e97815e 3163 }
8e97815e
BP
3164}
3165
c2d936a4
BP
3166static void
3167parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
d01c980f 3168{
c2d936a4
BP
3169 struct ofpact_cnt_ids *ids;
3170 uint16_t id = 0;
3171
3172 ofpact_put_DEC_TTL(ofpacts);
3173 ofpbuf_put(ofpacts, &id, sizeof id);
6fd6ed71 3174 ids = ofpacts->header;
c2d936a4 3175 ids->n_controllers++;
34abaa3d 3176 ofpact_finish(ofpacts, &ids->ofpact);
d01c980f
BP
3177}
3178
cab50449 3179static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3180parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
3181 enum ofputil_protocol *usable_protocols OVS_UNUSED)
d01c980f 3182{
c2d936a4
BP
3183 if (*arg == '\0') {
3184 parse_noargs_dec_ttl(ofpacts);
3185 } else {
3186 struct ofpact_cnt_ids *ids;
3187 char *cntr;
3188
3189 ids = ofpact_put_DEC_TTL(ofpacts);
3190 ids->ofpact.raw = NXAST_RAW_DEC_TTL_CNT_IDS;
3191 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
3192 cntr = strtok_r(NULL, ", ", &arg)) {
3193 uint16_t id = atoi(cntr);
3194
3195 ofpbuf_put(ofpacts, &id, sizeof id);
6fd6ed71 3196 ids = ofpacts->header;
c2d936a4
BP
3197 ids->n_controllers++;
3198 }
3199 if (!ids->n_controllers) {
3200 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
3201 "id.");
3202 }
34abaa3d 3203 ofpact_finish(ofpacts, &ids->ofpact);
c2d936a4
BP
3204 }
3205 return NULL;
d01c980f
BP
3206}
3207
c2d936a4
BP
3208static void
3209format_DEC_TTL(const struct ofpact_cnt_ids *a, struct ds *s)
d01c980f 3210{
c2d936a4 3211 size_t i;
d01c980f 3212
b1c5bf1f 3213 ds_put_format(s, "%sdec_ttl%s", colors.paren, colors.end);
c2d936a4 3214 if (a->ofpact.raw == NXAST_RAW_DEC_TTL_CNT_IDS) {
b1c5bf1f 3215 ds_put_format(s, "%s(%s", colors.paren, colors.end);
c2d936a4
BP
3216 for (i = 0; i < a->n_controllers; i++) {
3217 if (i) {
3218 ds_put_cstr(s, ",");
d01c980f 3219 }
c2d936a4
BP
3220 ds_put_format(s, "%"PRIu16, a->cnt_ids[i]);
3221 }
b1c5bf1f 3222 ds_put_format(s, "%s)%s", colors.paren, colors.end);
d01c980f
BP
3223 }
3224}
c2d936a4
BP
3225\f
3226/* Set MPLS label actions. */
d01c980f
BP
3227
3228static enum ofperr
f3cd3ac7
JS
3229decode_OFPAT_RAW_SET_MPLS_LABEL(ovs_be32 label,
3230 enum ofp_version ofp_version OVS_UNUSED,
3231 struct ofpbuf *out)
d01c980f 3232{
c2d936a4
BP
3233 ofpact_put_SET_MPLS_LABEL(out)->label = label;
3234 return 0;
3235}
d01c980f 3236
c2d936a4
BP
3237static void
3238encode_SET_MPLS_LABEL(const struct ofpact_mpls_label *label,
3239 enum ofp_version ofp_version,
3240 struct ofpbuf *out)
3241{
3242 if (ofp_version < OFP12_VERSION) {
3243 put_OFPAT_SET_MPLS_LABEL(out, ofp_version, label->label);
3244 } else {
3245 ofpact_put_set_field(out, ofp_version, MFF_MPLS_LABEL,
3246 ntohl(label->label));
d01c980f 3247 }
c2d936a4 3248}
d01c980f 3249
cab50449 3250static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3251parse_SET_MPLS_LABEL(char *arg, struct ofpbuf *ofpacts,
3252 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3253{
3254 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(ofpacts);
3255 if (*arg == '\0') {
3256 return xstrdup("set_mpls_label: expected label.");
d01c980f 3257 }
c2d936a4
BP
3258
3259 mpls_label->label = htonl(atoi(arg));
3260 return NULL;
d01c980f
BP
3261}
3262
3263static void
c2d936a4 3264format_SET_MPLS_LABEL(const struct ofpact_mpls_label *a, struct ds *s)
d01c980f 3265{
b1c5bf1f
QM
3266 ds_put_format(s, "%sset_mpls_label(%s%"PRIu32"%s)%s",
3267 colors.paren, colors.end, ntohl(a->label),
3268 colors.paren, colors.end);
d01c980f 3269}
c2d936a4
BP
3270\f
3271/* Set MPLS TC actions. */
d01c980f 3272
c2d936a4 3273static enum ofperr
f3cd3ac7
JS
3274decode_OFPAT_RAW_SET_MPLS_TC(uint8_t tc,
3275 enum ofp_version ofp_version OVS_UNUSED,
3276 struct ofpbuf *out)
d01c980f 3277{
c2d936a4
BP
3278 ofpact_put_SET_MPLS_TC(out)->tc = tc;
3279 return 0;
3280}
d01c980f 3281
c2d936a4
BP
3282static void
3283encode_SET_MPLS_TC(const struct ofpact_mpls_tc *tc,
3284 enum ofp_version ofp_version, struct ofpbuf *out)
3285{
3286 if (ofp_version < OFP12_VERSION) {
3287 put_OFPAT_SET_MPLS_TC(out, ofp_version, tc->tc);
3288 } else {
3289 ofpact_put_set_field(out, ofp_version, MFF_MPLS_TC, tc->tc);
8f2cded4 3290 }
c2d936a4 3291}
8f2cded4 3292
cab50449 3293static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3294parse_SET_MPLS_TC(char *arg, struct ofpbuf *ofpacts,
3295 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3296{
3297 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(ofpacts);
d01c980f 3298
c2d936a4
BP
3299 if (*arg == '\0') {
3300 return xstrdup("set_mpls_tc: expected tc.");
d01c980f
BP
3301 }
3302
c2d936a4
BP
3303 mpls_tc->tc = atoi(arg);
3304 return NULL;
3305}
d01c980f 3306
c2d936a4
BP
3307static void
3308format_SET_MPLS_TC(const struct ofpact_mpls_tc *a, struct ds *s)
3309{
b1c5bf1f
QM
3310 ds_put_format(s, "%sset_mpls_ttl(%s%"PRIu8"%s)%s",
3311 colors.paren, colors.end, a->tc,
3312 colors.paren, colors.end);
c2d936a4
BP
3313}
3314\f
3315/* Set MPLS TTL actions. */
d01c980f 3316
c2d936a4 3317static enum ofperr
f3cd3ac7
JS
3318decode_OFPAT_RAW_SET_MPLS_TTL(uint8_t ttl,
3319 enum ofp_version ofp_version OVS_UNUSED,
3320 struct ofpbuf *out)
c2d936a4
BP
3321{
3322 ofpact_put_SET_MPLS_TTL(out)->ttl = ttl;
3323 return 0;
3324}
638a19b0 3325
c2d936a4
BP
3326static void
3327encode_SET_MPLS_TTL(const struct ofpact_mpls_ttl *ttl,
3328 enum ofp_version ofp_version, struct ofpbuf *out)
3329{
3330 put_OFPAT_SET_MPLS_TTL(out, ofp_version, ttl->ttl);
3331}
638a19b0 3332
c2d936a4
BP
3333/* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
3334 * action to 'ofpacts'.
3335 *
3336 * Returns NULL if successful, otherwise a malloc()'d string describing the
3337 * error. The caller is responsible for freeing the returned string. */
cab50449 3338static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3339parse_SET_MPLS_TTL(char *arg, struct ofpbuf *ofpacts,
3340 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3341{
3342 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(ofpacts);
d01c980f 3343
c2d936a4
BP
3344 if (*arg == '\0') {
3345 return xstrdup("set_mpls_ttl: expected ttl.");
b19e8793 3346 }
7fdb60a7 3347
c2d936a4
BP
3348 mpls_ttl->ttl = atoi(arg);
3349 return NULL;
3350}
4cceacb9 3351
c2d936a4
BP
3352static void
3353format_SET_MPLS_TTL(const struct ofpact_mpls_ttl *a, struct ds *s)
3354{
b1c5bf1f
QM
3355 ds_put_format(s, "%sset_mpls_ttl(%s%"PRIu8"%s)%s",
3356 colors.paren, colors.end, a->ttl,
3357 colors.paren, colors.end);
c2d936a4
BP
3358}
3359\f
3360/* Decrement MPLS TTL actions. */
4cceacb9 3361
c2d936a4
BP
3362static enum ofperr
3363decode_OFPAT_RAW_DEC_MPLS_TTL(struct ofpbuf *out)
3364{
3365 ofpact_put_DEC_MPLS_TTL(out);
3366 return 0;
3367}
8dd54666 3368
c2d936a4
BP
3369static void
3370encode_DEC_MPLS_TTL(const struct ofpact_null *null OVS_UNUSED,
3371 enum ofp_version ofp_version, struct ofpbuf *out)
3372{
3373 put_OFPAT_DEC_MPLS_TTL(out, ofp_version);
3374}
3375
cab50449 3376static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3377parse_DEC_MPLS_TTL(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3378 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3379{
3380 ofpact_put_DEC_MPLS_TTL(ofpacts);
3381 return NULL;
3382}
3383
3384static void
3385format_DEC_MPLS_TTL(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3386{
b1c5bf1f 3387 ds_put_format(s, "%sdec_mpls_ttl%s", colors.value, colors.end);
c2d936a4
BP
3388}
3389\f
3390/* Push MPLS label action. */
3391
3392static enum ofperr
f3cd3ac7
JS
3393decode_OFPAT_RAW_PUSH_MPLS(ovs_be16 ethertype,
3394 enum ofp_version ofp_version OVS_UNUSED,
3395 struct ofpbuf *out)
c2d936a4
BP
3396{
3397 struct ofpact_push_mpls *oam;
3398
3399 if (!eth_type_mpls(ethertype)) {
3400 return OFPERR_OFPBAC_BAD_ARGUMENT;
8dd54666 3401 }
c2d936a4
BP
3402 oam = ofpact_put_PUSH_MPLS(out);
3403 oam->ethertype = ethertype;
d01c980f 3404
c2d936a4
BP
3405 return 0;
3406}
3407
3408static void
3409encode_PUSH_MPLS(const struct ofpact_push_mpls *push_mpls,
3410 enum ofp_version ofp_version, struct ofpbuf *out)
3411{
3412 put_OFPAT_PUSH_MPLS(out, ofp_version, push_mpls->ethertype);
3413}
3414
cab50449 3415static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3416parse_PUSH_MPLS(char *arg, struct ofpbuf *ofpacts,
3417 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3418{
3419 uint16_t ethertype;
3420 char *error;
3421
3422 error = str_to_u16(arg, "push_mpls", &ethertype);
3423 if (!error) {
3424 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
d01c980f
BP
3425 }
3426 return error;
3427}
c2d936a4
BP
3428
3429static void
3430format_PUSH_MPLS(const struct ofpact_push_mpls *a, struct ds *s)
3431{
b1c5bf1f
QM
3432 ds_put_format(s, "%spush_mpls:%s0x%04"PRIx16,
3433 colors.param, colors.end, ntohs(a->ethertype));
c2d936a4 3434}
d01c980f 3435\f
c2d936a4
BP
3436/* Pop MPLS label action. */
3437
3438static enum ofperr
f3cd3ac7
JS
3439decode_OFPAT_RAW_POP_MPLS(ovs_be16 ethertype,
3440 enum ofp_version ofp_version OVS_UNUSED,
3441 struct ofpbuf *out)
57ad4e9e 3442{
c2d936a4
BP
3443 ofpact_put_POP_MPLS(out)->ethertype = ethertype;
3444 return 0;
3445}
57ad4e9e 3446
c2d936a4
BP
3447static void
3448encode_POP_MPLS(const struct ofpact_pop_mpls *pop_mpls,
3449 enum ofp_version ofp_version, struct ofpbuf *out)
3450{
3451 put_OFPAT_POP_MPLS(out, ofp_version, pop_mpls->ethertype);
3452}
3453
cab50449 3454static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3455parse_POP_MPLS(char *arg, struct ofpbuf *ofpacts,
3456 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3457{
3458 uint16_t ethertype;
3459 char *error;
3460
3461 error = str_to_u16(arg, "pop_mpls", &ethertype);
3462 if (!error) {
3463 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
57ad4e9e 3464 }
c2d936a4 3465 return error;
57ad4e9e
BP
3466}
3467
ba2fe8e9 3468static void
c2d936a4 3469format_POP_MPLS(const struct ofpact_pop_mpls *a, struct ds *s)
ba2fe8e9 3470{
b1c5bf1f
QM
3471 ds_put_format(s, "%spop_mpls:%s0x%04"PRIx16,
3472 colors.param, colors.end, ntohs(a->ethertype));
ba2fe8e9 3473}
c2d936a4
BP
3474\f
3475/* Set tunnel ID actions. */
ba2fe8e9 3476
f25d0cf3 3477static enum ofperr
f3cd3ac7
JS
3478decode_NXAST_RAW_SET_TUNNEL(uint32_t tun_id,
3479 enum ofp_version ofp_version OVS_UNUSED,
3480 struct ofpbuf *out)
f25d0cf3 3481{
c2d936a4
BP
3482 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3483 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL;
3484 tunnel->tun_id = tun_id;
3485 return 0;
3486}
f25d0cf3 3487
c2d936a4 3488static enum ofperr
f3cd3ac7
JS
3489decode_NXAST_RAW_SET_TUNNEL64(uint64_t tun_id,
3490 enum ofp_version ofp_version OVS_UNUSED,
3491 struct ofpbuf *out)
c2d936a4
BP
3492{
3493 struct ofpact_tunnel *tunnel = ofpact_put_SET_TUNNEL(out);
3494 tunnel->ofpact.raw = NXAST_RAW_SET_TUNNEL64;
3495 tunnel->tun_id = tun_id;
3496 return 0;
3497}
f25d0cf3 3498
c2d936a4
BP
3499static void
3500encode_SET_TUNNEL(const struct ofpact_tunnel *tunnel,
3501 enum ofp_version ofp_version, struct ofpbuf *out)
3502{
3503 uint64_t tun_id = tunnel->tun_id;
f25d0cf3 3504
c2d936a4
BP
3505 if (ofp_version < OFP12_VERSION) {
3506 if (tun_id <= UINT32_MAX
3507 && tunnel->ofpact.raw != NXAST_RAW_SET_TUNNEL64) {
3508 put_NXAST_SET_TUNNEL(out, tun_id);
3509 } else {
3510 put_NXAST_SET_TUNNEL64(out, tun_id);
f25d0cf3 3511 }
c2d936a4
BP
3512 } else {
3513 ofpact_put_set_field(out, ofp_version, MFF_TUN_ID, tun_id);
3514 }
3515}
f25d0cf3 3516
cab50449 3517static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3518parse_set_tunnel(char *arg, struct ofpbuf *ofpacts,
3519 enum ofp_raw_action_type raw)
3520{
3521 struct ofpact_tunnel *tunnel;
ca287d20 3522
c2d936a4
BP
3523 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
3524 tunnel->ofpact.raw = raw;
3525 return str_to_u64(arg, &tunnel->tun_id);
3526}
8621547c 3527
cab50449 3528static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3529parse_SET_TUNNEL(char *arg, struct ofpbuf *ofpacts,
3530 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3531{
3532 return parse_set_tunnel(arg, ofpacts, NXAST_RAW_SET_TUNNEL);
3533}
8621547c 3534
c2d936a4
BP
3535static void
3536format_SET_TUNNEL(const struct ofpact_tunnel *a, struct ds *s)
3537{
b1c5bf1f 3538 ds_put_format(s, "%sset_tunnel%s:%s%#"PRIx64, colors.param,
c2d936a4
BP
3539 (a->tun_id > UINT32_MAX
3540 || a->ofpact.raw == NXAST_RAW_SET_TUNNEL64 ? "64" : ""),
b1c5bf1f 3541 colors.end, a->tun_id);
c2d936a4
BP
3542}
3543\f
3544/* Set queue action. */
8621547c 3545
c2d936a4 3546static enum ofperr
f3cd3ac7
JS
3547decode_OFPAT_RAW_SET_QUEUE(uint32_t queue_id,
3548 enum ofp_version ofp_version OVS_UNUSED,
3549 struct ofpbuf *out)
c2d936a4
BP
3550{
3551 ofpact_put_SET_QUEUE(out)->queue_id = queue_id;
3552 return 0;
3553}
8621547c 3554
c2d936a4
BP
3555static void
3556encode_SET_QUEUE(const struct ofpact_queue *queue,
3557 enum ofp_version ofp_version, struct ofpbuf *out)
3558{
3559 put_OFPAT_SET_QUEUE(out, ofp_version, queue->queue_id);
3560}
8621547c 3561
cab50449 3562static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3563parse_SET_QUEUE(char *arg, struct ofpbuf *ofpacts,
3564 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3565{
3566 return str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
3567}
8621547c 3568
c2d936a4
BP
3569static void
3570format_SET_QUEUE(const struct ofpact_queue *a, struct ds *s)
3571{
b1c5bf1f
QM
3572 ds_put_format(s, "%sset_queue:%s%"PRIu32,
3573 colors.param, colors.end, a->queue_id);
c2d936a4
BP
3574}
3575\f
3576/* Pop queue action. */
1e7db674 3577
c2d936a4
BP
3578static enum ofperr
3579decode_NXAST_RAW_POP_QUEUE(struct ofpbuf *out)
3580{
3581 ofpact_put_POP_QUEUE(out);
3582 return 0;
3583}
f25d0cf3 3584
c2d936a4
BP
3585static void
3586encode_POP_QUEUE(const struct ofpact_null *null OVS_UNUSED,
3587 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
3588{
3589 put_NXAST_POP_QUEUE(out);
3590}
f25d0cf3 3591
cab50449 3592static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3593parse_POP_QUEUE(const char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
3594 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3595{
3596 ofpact_put_POP_QUEUE(ofpacts);
3597 return NULL;
3598}
f25d0cf3 3599
c2d936a4
BP
3600static void
3601format_POP_QUEUE(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
3602{
b1c5bf1f 3603 ds_put_format(s, "%spop_queue%s", colors.value, colors.end);
c2d936a4
BP
3604}
3605\f
3606/* Action structure for NXAST_FIN_TIMEOUT.
3607 *
3608 * This action changes the idle timeout or hard timeout, or both, of this
3609 * OpenFlow rule when the rule matches a TCP packet with the FIN or RST flag.
3610 * When such a packet is observed, the action reduces the rule's idle timeout
3611 * to 'fin_idle_timeout' and its hard timeout to 'fin_hard_timeout'. This
3612 * action has no effect on an existing timeout that is already shorter than the
3613 * one that the action specifies. A 'fin_idle_timeout' or 'fin_hard_timeout'
3614 * of zero has no effect on the respective timeout.
3615 *
3616 * 'fin_idle_timeout' and 'fin_hard_timeout' are measured in seconds.
3617 * 'fin_hard_timeout' specifies time since the flow's creation, not since the
3618 * receipt of the FIN or RST.
3619 *
3620 * This is useful for quickly discarding learned TCP flows that otherwise will
3621 * take a long time to expire.
3622 *
3623 * This action is intended for use with an OpenFlow rule that matches only a
3624 * single TCP flow. If the rule matches multiple TCP flows (e.g. it wildcards
3625 * all TCP traffic, or all TCP traffic to a particular port), then any FIN or
3626 * RST in any of those flows will cause the entire OpenFlow rule to expire
3627 * early, which is not normally desirable.
3628 */
3629struct nx_action_fin_timeout {
3630 ovs_be16 type; /* OFPAT_VENDOR. */
3631 ovs_be16 len; /* 16. */
3632 ovs_be32 vendor; /* NX_VENDOR_ID. */
3633 ovs_be16 subtype; /* NXAST_FIN_TIMEOUT. */
3634 ovs_be16 fin_idle_timeout; /* New idle timeout, if nonzero. */
3635 ovs_be16 fin_hard_timeout; /* New hard timeout, if nonzero. */
3636 ovs_be16 pad; /* Must be zero. */
3637};
3638OFP_ASSERT(sizeof(struct nx_action_fin_timeout) == 16);
f25d0cf3 3639
c2d936a4
BP
3640static enum ofperr
3641decode_NXAST_RAW_FIN_TIMEOUT(const struct nx_action_fin_timeout *naft,
f3cd3ac7 3642 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4
BP
3643 struct ofpbuf *out)
3644{
3645 struct ofpact_fin_timeout *oft;
8621547c 3646
c2d936a4
BP
3647 oft = ofpact_put_FIN_TIMEOUT(out);
3648 oft->fin_idle_timeout = ntohs(naft->fin_idle_timeout);
3649 oft->fin_hard_timeout = ntohs(naft->fin_hard_timeout);
3650 return 0;
3651}
f25d0cf3 3652
c2d936a4
BP
3653static void
3654encode_FIN_TIMEOUT(const struct ofpact_fin_timeout *fin_timeout,
3655 enum ofp_version ofp_version OVS_UNUSED,
3656 struct ofpbuf *out)
3657{
3658 struct nx_action_fin_timeout *naft = put_NXAST_FIN_TIMEOUT(out);
3659 naft->fin_idle_timeout = htons(fin_timeout->fin_idle_timeout);
3660 naft->fin_hard_timeout = htons(fin_timeout->fin_hard_timeout);
3661}
f25d0cf3 3662
cab50449 3663static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3664parse_FIN_TIMEOUT(char *arg, struct ofpbuf *ofpacts,
3665 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3666{
3667 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(ofpacts);
3668 char *key, *value;
f25d0cf3 3669
c2d936a4
BP
3670 while (ofputil_parse_key_value(&arg, &key, &value)) {
3671 char *error;
b02475c5 3672
c2d936a4
BP
3673 if (!strcmp(key, "idle_timeout")) {
3674 error = str_to_u16(value, key, &oft->fin_idle_timeout);
3675 } else if (!strcmp(key, "hard_timeout")) {
3676 error = str_to_u16(value, key, &oft->fin_hard_timeout);
3677 } else {
3678 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
3679 key);
8621547c 3680 }
8dd54666 3681
c2d936a4
BP
3682 if (error) {
3683 return error;
9cae45dc 3684 }
9cae45dc 3685 }
c2d936a4
BP
3686 return NULL;
3687}
e3b56933 3688
c2d936a4
BP
3689static void
3690format_FIN_TIMEOUT(const struct ofpact_fin_timeout *a, struct ds *s)
3691{
b1c5bf1f 3692 ds_put_format(s, "%sfin_timeout(%s", colors.paren, colors.end);
c2d936a4 3693 if (a->fin_idle_timeout) {
b1c5bf1f
QM
3694 ds_put_format(s, "%sidle_timeout=%s%"PRIu16",",
3695 colors.param, colors.end, a->fin_idle_timeout);
7e9f8266 3696 }
c2d936a4 3697 if (a->fin_hard_timeout) {
b1c5bf1f
QM
3698 ds_put_format(s, "%shard_timeout=%s%"PRIu16",",
3699 colors.param, colors.end, a->fin_hard_timeout);
f25d0cf3 3700 }
c2d936a4 3701 ds_chomp(s, ',');
b1c5bf1f 3702 ds_put_format(s, "%s)%s", colors.paren, colors.end);
f25d0cf3 3703}
c2d936a4
BP
3704\f
3705/* Action structures for NXAST_RESUBMIT and NXAST_RESUBMIT_TABLE.
eca39bce 3706 *
c2d936a4 3707 * These actions search one of the switch's flow tables:
ba2fe8e9 3708 *
c2d936a4
BP
3709 * - For NXAST_RESUBMIT_TABLE only, if the 'table' member is not 255, then
3710 * it specifies the table to search.
ca287d20 3711 *
c2d936a4
BP
3712 * - Otherwise (for NXAST_RESUBMIT_TABLE with a 'table' of 255, or for
3713 * NXAST_RESUBMIT regardless of 'table'), it searches the current flow
3714 * table, that is, the OpenFlow flow table that contains the flow from
3715 * which this action was obtained. If this action did not come from a
3716 * flow table (e.g. it came from an OFPT_PACKET_OUT message), then table 0
3717 * is the current table.
3718 *
3719 * The flow table lookup uses a flow that may be slightly modified from the
3720 * original lookup:
3721 *
3722 * - For NXAST_RESUBMIT, the 'in_port' member of struct nx_action_resubmit
3723 * is used as the flow's in_port.
3724 *
3725 * - For NXAST_RESUBMIT_TABLE, if the 'in_port' member is not OFPP_IN_PORT,
3726 * then its value is used as the flow's in_port. Otherwise, the original
3727 * in_port is used.
3728 *
3729 * - If actions that modify the flow (e.g. OFPAT_SET_VLAN_VID) precede the
3730 * resubmit action, then the flow is updated with the new values.
3731 *
3732 * Following the lookup, the original in_port is restored.
3733 *
3734 * If the modified flow matched in the flow table, then the corresponding
3735 * actions are executed. Afterward, actions following the resubmit in the
3736 * original set of actions, if any, are executed; any changes made to the
3737 * packet (e.g. changes to VLAN) by secondary actions persist when those
3738 * actions are executed, although the original in_port is restored.
3739 *
3740 * Resubmit actions may be used any number of times within a set of actions.
3741 *
3742 * Resubmit actions may nest to an implementation-defined depth. Beyond this
3743 * implementation-defined depth, further resubmit actions are simply ignored.
3744 *
3745 * NXAST_RESUBMIT ignores 'table' and 'pad'. NXAST_RESUBMIT_TABLE requires
3746 * 'pad' to be all-bits-zero.
3747 *
3748 * Open vSwitch 1.0.1 and earlier did not support recursion. Open vSwitch
3749 * before 1.2.90 did not support NXAST_RESUBMIT_TABLE.
3750 */
3751struct nx_action_resubmit {
3752 ovs_be16 type; /* OFPAT_VENDOR. */
3753 ovs_be16 len; /* Length is 16. */
3754 ovs_be32 vendor; /* NX_VENDOR_ID. */
3755 ovs_be16 subtype; /* NXAST_RESUBMIT. */
3756 ovs_be16 in_port; /* New in_port for checking flow table. */
3757 uint8_t table; /* NXAST_RESUBMIT_TABLE: table to use. */
3758 uint8_t pad[3];
3759};
3760OFP_ASSERT(sizeof(struct nx_action_resubmit) == 16);
3761
3762static enum ofperr
f3cd3ac7
JS
3763decode_NXAST_RAW_RESUBMIT(uint16_t port,
3764 enum ofp_version ofp_version OVS_UNUSED,
3765 struct ofpbuf *out)
f25d0cf3 3766{
c2d936a4 3767 struct ofpact_resubmit *resubmit;
f25d0cf3 3768
c2d936a4
BP
3769 resubmit = ofpact_put_RESUBMIT(out);
3770 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT;
3771 resubmit->in_port = u16_to_ofp(port);
3772 resubmit->table_id = 0xff;
3773 return 0;
f25d0cf3 3774}
4cceacb9 3775
c2d936a4
BP
3776static enum ofperr
3777decode_NXAST_RAW_RESUBMIT_TABLE(const struct nx_action_resubmit *nar,
f3cd3ac7 3778 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4 3779 struct ofpbuf *out)
ba2fe8e9 3780{
c2d936a4 3781 struct ofpact_resubmit *resubmit;
ba2fe8e9 3782
c2d936a4
BP
3783 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3784 return OFPERR_OFPBAC_BAD_ARGUMENT;
3785 }
3786
3787 resubmit = ofpact_put_RESUBMIT(out);
3788 resubmit->ofpact.raw = NXAST_RAW_RESUBMIT_TABLE;
3789 resubmit->in_port = u16_to_ofp(ntohs(nar->in_port));
3790 resubmit->table_id = nar->table;
3791 return 0;
ba2fe8e9
BP
3792}
3793
c2d936a4
BP
3794static void
3795encode_RESUBMIT(const struct ofpact_resubmit *resubmit,
3796 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4cceacb9 3797{
c2d936a4 3798 uint16_t in_port = ofp_to_u16(resubmit->in_port);
4cceacb9 3799
c2d936a4
BP
3800 if (resubmit->table_id == 0xff
3801 && resubmit->ofpact.raw != NXAST_RAW_RESUBMIT_TABLE) {
3802 put_NXAST_RESUBMIT(out, in_port);
3803 } else {
3804 struct nx_action_resubmit *nar = put_NXAST_RESUBMIT_TABLE(out);
3805 nar->table = resubmit->table_id;
3806 nar->in_port = htons(in_port);
3807 }
3808}
6813ee7c 3809
cab50449 3810static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
3811parse_RESUBMIT(char *arg, struct ofpbuf *ofpacts,
3812 enum ofputil_protocol *usable_protocols OVS_UNUSED)
3813{
3814 struct ofpact_resubmit *resubmit;
3815 char *in_port_s, *table_s;
6813ee7c 3816
c2d936a4 3817 resubmit = ofpact_put_RESUBMIT(ofpacts);
8f2cded4 3818
c2d936a4
BP
3819 in_port_s = strsep(&arg, ",");
3820 if (in_port_s && in_port_s[0]) {
3821 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
3822 return xasprintf("%s: resubmit to unknown port", in_port_s);
8f2cded4 3823 }
c2d936a4
BP
3824 } else {
3825 resubmit->in_port = OFPP_IN_PORT;
3826 }
4cceacb9 3827
c2d936a4
BP
3828 table_s = strsep(&arg, ",");
3829 if (table_s && table_s[0]) {
3830 uint32_t table_id = 0;
3831 char *error;
3832
3833 error = str_to_u32(table_s, &table_id);
3834 if (error) {
3835 return error;
3836 }
3837 resubmit->table_id = table_id;
3838 } else {
3839 resubmit->table_id = 255;
4cceacb9
JS
3840 }
3841
c2d936a4
BP
3842 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
3843 return xstrdup("at least one \"in_port\" or \"table\" must be "
3844 "specified on resubmit");
3845 }
3846 return NULL;
3847}
3848
3849static void
3850format_RESUBMIT(const struct ofpact_resubmit *a, struct ds *s)
3851{
3852 if (a->in_port != OFPP_IN_PORT && a->table_id == 255) {
b1c5bf1f 3853 ds_put_format(s, "%sresubmit:%s", colors.special, colors.end);
c2d936a4
BP
3854 ofputil_format_port(a->in_port, s);
3855 } else {
b1c5bf1f 3856 ds_put_format(s, "%sresubmit(%s", colors.paren, colors.end);
c2d936a4
BP
3857 if (a->in_port != OFPP_IN_PORT) {
3858 ofputil_format_port(a->in_port, s);
3859 }
3860 ds_put_char(s, ',');
3861 if (a->table_id != 255) {
3862 ds_put_format(s, "%"PRIu8, a->table_id);
3863 }
b1c5bf1f 3864 ds_put_format(s, "%s)%s", colors.paren, colors.end);
c2d936a4
BP
3865 }
3866}
3867\f
3868/* Action structure for NXAST_LEARN.
3869 *
3870 * This action adds or modifies a flow in an OpenFlow table, similar to
3871 * OFPT_FLOW_MOD with OFPFC_MODIFY_STRICT as 'command'. The new flow has the
3872 * specified idle timeout, hard timeout, priority, cookie, and flags. The new
3873 * flow's match criteria and actions are built by applying each of the series
3874 * of flow_mod_spec elements included as part of the action.
3875 *
3876 * A flow_mod_spec starts with a 16-bit header. A header that is all-bits-0 is
3877 * a no-op used for padding the action as a whole to a multiple of 8 bytes in
3878 * length. Otherwise, the flow_mod_spec can be thought of as copying 'n_bits'
3879 * bits from a source to a destination. In this case, the header contains
3880 * multiple fields:
3881 *
3882 * 15 14 13 12 11 10 0
3883 * +------+---+------+---------------------------------+
3884 * | 0 |src| dst | n_bits |
3885 * +------+---+------+---------------------------------+
3886 *
3887 * The meaning and format of a flow_mod_spec depends on 'src' and 'dst'. The
3888 * following table summarizes the meaning of each possible combination.
3889 * Details follow the table:
3890 *
3891 * src dst meaning
3892 * --- --- ----------------------------------------------------------
3893 * 0 0 Add match criteria based on value in a field.
3894 * 1 0 Add match criteria based on an immediate value.
3895 * 0 1 Add NXAST_REG_LOAD action to copy field into a different field.
3896 * 1 1 Add NXAST_REG_LOAD action to load immediate value into a field.
3897 * 0 2 Add OFPAT_OUTPUT action to output to port from specified field.
3898 * All other combinations are undefined and not allowed.
3899 *
3900 * The flow_mod_spec header is followed by a source specification and a
3901 * destination specification. The format and meaning of the source
3902 * specification depends on 'src':
3903 *
3904 * - If 'src' is 0, the source bits are taken from a field in the flow to
3905 * which this action is attached. (This should be a wildcarded field. If
3906 * its value is fully specified then the source bits being copied have
3907 * constant values.)
3908 *
3909 * The source specification is an ovs_be32 'field' and an ovs_be16 'ofs'.
3910 * 'field' is an nxm_header with nxm_hasmask=0, and 'ofs' the starting bit
3911 * offset within that field. The source bits are field[ofs:ofs+n_bits-1].
3912 * 'field' and 'ofs' are subject to the same restrictions as the source
3913 * field in NXAST_REG_MOVE.
3914 *
3915 * - If 'src' is 1, the source bits are a constant value. The source
3916 * specification is (n_bits+15)/16*2 bytes long. Taking those bytes as a
3917 * number in network order, the source bits are the 'n_bits'
3918 * least-significant bits. The switch will report an error if other bits
3919 * in the constant are nonzero.
3920 *
3921 * The flow_mod_spec destination specification, for 'dst' of 0 or 1, is an
3922 * ovs_be32 'field' and an ovs_be16 'ofs'. 'field' is an nxm_header with
3923 * nxm_hasmask=0 and 'ofs' is a starting bit offset within that field. The
3924 * meaning of the flow_mod_spec depends on 'dst':
3925 *
3926 * - If 'dst' is 0, the flow_mod_spec specifies match criteria for the new
3927 * flow. The new flow matches only if bits field[ofs:ofs+n_bits-1] in a
3928 * packet equal the source bits. 'field' may be any nxm_header with
3929 * nxm_hasmask=0 that is allowed in NXT_FLOW_MOD.
3930 *
3931 * Order is significant. Earlier flow_mod_specs must satisfy any
3932 * prerequisites for matching fields specified later, by copying constant
3933 * values into prerequisite fields.
3934 *
3935 * The switch will reject flow_mod_specs that do not satisfy NXM masking
3936 * restrictions.
3937 *
3938 * - If 'dst' is 1, the flow_mod_spec specifies an NXAST_REG_LOAD action for
3939 * the new flow. The new flow copies the source bits into
3940 * field[ofs:ofs+n_bits-1]. Actions are executed in the same order as the
3941 * flow_mod_specs.
3942 *
3943 * A single NXAST_REG_LOAD action writes no more than 64 bits, so n_bits
3944 * greater than 64 yields multiple NXAST_REG_LOAD actions.
3945 *
3946 * The flow_mod_spec destination spec for 'dst' of 2 (when 'src' is 0) is
3947 * empty. It has the following meaning:
3948 *
3949 * - The flow_mod_spec specifies an OFPAT_OUTPUT action for the new flow.
3950 * The new flow outputs to the OpenFlow port specified by the source field.
3951 * Of the special output ports with value OFPP_MAX or larger, OFPP_IN_PORT,
3952 * OFPP_FLOOD, OFPP_LOCAL, and OFPP_ALL are supported. Other special ports
3953 * may not be used.
3954 *
3955 * Resource Management
3956 * -------------------
3957 *
3958 * A switch has a finite amount of flow table space available for learning.
3959 * When this space is exhausted, no new learning table entries will be learned
3960 * until some existing flow table entries expire. The controller should be
3961 * prepared to handle this by flooding (which can be implemented as a
3962 * low-priority flow).
3963 *
3964 * If a learned flow matches a single TCP stream with a relatively long
3965 * timeout, one may make the best of resource constraints by setting
3966 * 'fin_idle_timeout' or 'fin_hard_timeout' (both measured in seconds), or
3967 * both, to shorter timeouts. When either of these is specified as a nonzero
3968 * value, OVS adds a NXAST_FIN_TIMEOUT action, with the specified timeouts, to
3969 * the learned flow.
3970 *
3971 * Examples
3972 * --------
3973 *
3974 * The following examples give a prose description of the flow_mod_specs along
3975 * with informal notation for how those would be represented and a hex dump of
3976 * the bytes that would be required.
3977 *
3978 * These examples could work with various nx_action_learn parameters. Typical
3979 * values would be idle_timeout=OFP_FLOW_PERMANENT, hard_timeout=60,
3980 * priority=OFP_DEFAULT_PRIORITY, flags=0, table_id=10.
3981 *
3982 * 1. Learn input port based on the source MAC, with lookup into
3983 * NXM_NX_REG1[16:31] by resubmit to in_port=99:
3984 *
3985 * Match on in_port=99:
3986 * ovs_be16(src=1, dst=0, n_bits=16), 20 10
3987 * ovs_be16(99), 00 63
3988 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3989 *
3990 * Match Ethernet destination on Ethernet source from packet:
3991 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
3992 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
3993 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
3994 *
3995 * Set NXM_NX_REG1[16:31] to the packet's input port:
3996 * ovs_be16(src=0, dst=1, n_bits=16), 08 10
3997 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
3998 * ovs_be32(NXM_NX_REG1), ovs_be16(16) 00 01 02 04 00 10
3999 *
4000 * Given a packet that arrived on port A with Ethernet source address B,
4001 * this would set up the flow "in_port=99, dl_dst=B,
4002 * actions=load:A->NXM_NX_REG1[16..31]".
4003 *
4004 * In syntax accepted by ovs-ofctl, this action is: learn(in_port=99,
4005 * NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4006 * load:NXM_OF_IN_PORT[]->NXM_NX_REG1[16..31])
4007 *
4008 * 2. Output to input port based on the source MAC and VLAN VID, with lookup
4009 * into NXM_NX_REG1[16:31]:
4010 *
4011 * Match on same VLAN ID as packet:
4012 * ovs_be16(src=0, dst=0, n_bits=12), 00 0c
4013 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4014 * ovs_be32(NXM_OF_VLAN_TCI), ovs_be16(0) 00 00 08 02 00 00
4015 *
4016 * Match Ethernet destination on Ethernet source from packet:
4017 * ovs_be16(src=0, dst=0, n_bits=48), 00 30
4018 * ovs_be32(NXM_OF_ETH_SRC), ovs_be16(0) 00 00 04 06 00 00
4019 * ovs_be32(NXM_OF_ETH_DST), ovs_be16(0) 00 00 02 06 00 00
4020 *
4021 * Output to the packet's input port:
4022 * ovs_be16(src=0, dst=2, n_bits=16), 10 10
4023 * ovs_be32(NXM_OF_IN_PORT), ovs_be16(0) 00 00 00 02 00 00
4024 *
4025 * Given a packet that arrived on port A with Ethernet source address B in
4026 * VLAN C, this would set up the flow "dl_dst=B, vlan_vid=C,
4027 * actions=output:A".
4028 *
4029 * In syntax accepted by ovs-ofctl, this action is:
4030 * learn(NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],
4031 * output:NXM_OF_IN_PORT[])
4032 *
4033 * 3. Here's a recipe for a very simple-minded MAC learning switch. It uses a
4034 * 10-second MAC expiration time to make it easier to see what's going on
4035 *
4036 * ovs-vsctl del-controller br0
4037 * ovs-ofctl del-flows br0
4038 * ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, \
4039 hard_timeout=10, NXM_OF_VLAN_TCI[0..11], \
4040 NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \
4041 output:NXM_OF_IN_PORT[]), resubmit(,1)"
4042 * ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood"
4043 *
4044 * You can then dump the MAC learning table with:
4045 *
4046 * ovs-ofctl dump-flows br0 table=1
4047 *
4048 * Usage Advice
4049 * ------------
4050 *
4051 * For best performance, segregate learned flows into a table that is not used
4052 * for any other flows except possibly for a lowest-priority "catch-all" flow
4053 * (a flow with no match criteria). If different learning actions specify
4054 * different match criteria, use different tables for the learned flows.
4055 *
4056 * The meaning of 'hard_timeout' and 'idle_timeout' can be counterintuitive.
4057 * These timeouts apply to the flow that is added, which means that a flow with
4058 * an idle timeout will expire when no traffic has been sent *to* the learned
4059 * address. This is not usually the intent in MAC learning; instead, we want
4060 * the MAC learn entry to expire when no traffic has been sent *from* the
4061 * learned address. Use a hard timeout for that.
39c94593
JR
4062 *
4063 *
4064 * Visibility of Changes
4065 * ---------------------
4066 *
4067 * Prior to Open vSwitch 2.4, any changes made by a "learn" action in a given
4068 * flow translation are visible to flow table lookups made later in the flow
4069 * translation. This means that, in the example above, a MAC learned by the
4070 * learn action in table 0 would be found in table 1 (if the packet being
4071 * processed had the same source and destination MAC address).
4072 *
4073 * In Open vSwitch 2.4 and later, changes to a flow table (whether to add or
4074 * modify a flow) by a "learn" action are visible only for later flow
4075 * translations, not for later lookups within the same flow translation. In
4076 * the MAC learning example, a MAC learned by the learn action in table 0 would
4077 * not be found in table 1 if the flow translation would resubmit to table 1
4078 * after the processing of the learn action, meaning that if this MAC had not
4079 * been learned before then the packet would be flooded. */
c2d936a4
BP
4080struct nx_action_learn {
4081 ovs_be16 type; /* OFPAT_VENDOR. */
4082 ovs_be16 len; /* At least 24. */
4083 ovs_be32 vendor; /* NX_VENDOR_ID. */
4084 ovs_be16 subtype; /* NXAST_LEARN. */
4085 ovs_be16 idle_timeout; /* Idle time before discarding (seconds). */
4086 ovs_be16 hard_timeout; /* Max time before discarding (seconds). */
4087 ovs_be16 priority; /* Priority level of flow entry. */
4088 ovs_be64 cookie; /* Cookie for new flow. */
4089 ovs_be16 flags; /* NX_LEARN_F_*. */
4090 uint8_t table_id; /* Table to insert flow entry. */
4091 uint8_t pad; /* Must be zero. */
4092 ovs_be16 fin_idle_timeout; /* Idle timeout after FIN, if nonzero. */
4093 ovs_be16 fin_hard_timeout; /* Hard timeout after FIN, if nonzero. */
4094 /* Followed by a sequence of flow_mod_spec elements, as described above,
4095 * until the end of the action is reached. */
4096};
4097OFP_ASSERT(sizeof(struct nx_action_learn) == 32);
4098
4099static ovs_be16
4100get_be16(const void **pp)
4101{
4102 const ovs_be16 *p = *pp;
4103 ovs_be16 value = *p;
4104 *pp = p + 1;
4105 return value;
4106}
4107
4108static ovs_be32
4109get_be32(const void **pp)
4110{
4111 const ovs_be32 *p = *pp;
4112 ovs_be32 value = get_unaligned_be32(p);
4113 *pp = p + 1;
4114 return value;
4115}
4116
4117static void
4118get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
4119{
4120 sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
4121 sf->ofs = ntohs(get_be16(p));
4122 sf->n_bits = n_bits;
4123}
4124
4125static unsigned int
4126learn_min_len(uint16_t header)
4127{
4128 int n_bits = header & NX_LEARN_N_BITS_MASK;
4129 int src_type = header & NX_LEARN_SRC_MASK;
4130 int dst_type = header & NX_LEARN_DST_MASK;
4131 unsigned int min_len;
4132
4133 min_len = 0;
4134 if (src_type == NX_LEARN_SRC_FIELD) {
4135 min_len += sizeof(ovs_be32); /* src_field */
4136 min_len += sizeof(ovs_be16); /* src_ofs */
4137 } else {
4138 min_len += DIV_ROUND_UP(n_bits, 16);
4139 }
4140 if (dst_type == NX_LEARN_DST_MATCH ||
4141 dst_type == NX_LEARN_DST_LOAD) {
4142 min_len += sizeof(ovs_be32); /* dst_field */
4143 min_len += sizeof(ovs_be16); /* dst_ofs */
4144 }
4145 return min_len;
4146}
4147
4148/* Converts 'nal' into a "struct ofpact_learn" and appends that struct to
4149 * 'ofpacts'. Returns 0 if successful, otherwise an OFPERR_*. */
4150static enum ofperr
4151decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
f3cd3ac7 4152 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4
BP
4153 struct ofpbuf *ofpacts)
4154{
4155 struct ofpact_learn *learn;
4156 const void *p, *end;
4157
4158 if (nal->pad) {
4159 return OFPERR_OFPBAC_BAD_ARGUMENT;
4160 }
4161
4162 learn = ofpact_put_LEARN(ofpacts);
4163
4164 learn->idle_timeout = ntohs(nal->idle_timeout);
4165 learn->hard_timeout = ntohs(nal->hard_timeout);
4166 learn->priority = ntohs(nal->priority);
4167 learn->cookie = nal->cookie;
4168 learn->table_id = nal->table_id;
4169 learn->fin_idle_timeout = ntohs(nal->fin_idle_timeout);
4170 learn->fin_hard_timeout = ntohs(nal->fin_hard_timeout);
4171
4172 learn->flags = ntohs(nal->flags);
4173 if (learn->flags & ~(NX_LEARN_F_SEND_FLOW_REM |
4174 NX_LEARN_F_DELETE_LEARNED)) {
4175 return OFPERR_OFPBAC_BAD_ARGUMENT;
4176 }
4177
4178 if (learn->table_id == 0xff) {
4179 return OFPERR_OFPBAC_BAD_ARGUMENT;
4180 }
4181
4182 end = (char *) nal + ntohs(nal->len);
4183 for (p = nal + 1; p != end; ) {
4184 struct ofpact_learn_spec *spec;
4185 uint16_t header = ntohs(get_be16(&p));
4186
4187 if (!header) {
4188 break;
4189 }
4190
4191 spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
6fd6ed71 4192 learn = ofpacts->header;
c2d936a4
BP
4193 learn->n_specs++;
4194
4195 spec->src_type = header & NX_LEARN_SRC_MASK;
4196 spec->dst_type = header & NX_LEARN_DST_MASK;
4197 spec->n_bits = header & NX_LEARN_N_BITS_MASK;
4198
4199 /* Check for valid src and dst type combination. */
4200 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4201 spec->dst_type == NX_LEARN_DST_LOAD ||
4202 (spec->dst_type == NX_LEARN_DST_OUTPUT &&
4203 spec->src_type == NX_LEARN_SRC_FIELD)) {
4204 /* OK. */
4205 } else {
4206 return OFPERR_OFPBAC_BAD_ARGUMENT;
4207 }
4208
4209 /* Check that the arguments don't overrun the end of the action. */
4210 if ((char *) end - (char *) p < learn_min_len(header)) {
4211 return OFPERR_OFPBAC_BAD_LEN;
4212 }
4213
4214 /* Get the source. */
4215 if (spec->src_type == NX_LEARN_SRC_FIELD) {
4216 get_subfield(spec->n_bits, &p, &spec->src);
4217 } else {
4218 int p_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4219
4220 bitwise_copy(p, p_bytes, 0,
4221 &spec->src_imm, sizeof spec->src_imm, 0,
4222 spec->n_bits);
4223 p = (const uint8_t *) p + p_bytes;
4224 }
4225
4226 /* Get the destination. */
4227 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4228 spec->dst_type == NX_LEARN_DST_LOAD) {
4229 get_subfield(spec->n_bits, &p, &spec->dst);
4230 }
4231 }
34abaa3d 4232 ofpact_finish(ofpacts, &learn->ofpact);
c2d936a4
BP
4233
4234 if (!is_all_zeros(p, (char *) end - (char *) p)) {
4235 return OFPERR_OFPBAC_BAD_ARGUMENT;
4236 }
4237
4238 return 0;
4239}
4240
4241static void
4242put_be16(struct ofpbuf *b, ovs_be16 x)
4243{
4244 ofpbuf_put(b, &x, sizeof x);
4245}
4246
4247static void
4248put_be32(struct ofpbuf *b, ovs_be32 x)
4249{
4250 ofpbuf_put(b, &x, sizeof x);
4251}
4252
4253static void
4254put_u16(struct ofpbuf *b, uint16_t x)
4255{
4256 put_be16(b, htons(x));
4257}
4258
4259static void
4260put_u32(struct ofpbuf *b, uint32_t x)
4261{
4262 put_be32(b, htonl(x));
4263}
4264
4265static void
4266encode_LEARN(const struct ofpact_learn *learn,
4267 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4268{
4269 const struct ofpact_learn_spec *spec;
4270 struct nx_action_learn *nal;
4271 size_t start_ofs;
4272
6fd6ed71 4273 start_ofs = out->size;
c2d936a4
BP
4274 nal = put_NXAST_LEARN(out);
4275 nal->idle_timeout = htons(learn->idle_timeout);
4276 nal->hard_timeout = htons(learn->hard_timeout);
4277 nal->fin_idle_timeout = htons(learn->fin_idle_timeout);
4278 nal->fin_hard_timeout = htons(learn->fin_hard_timeout);
4279 nal->priority = htons(learn->priority);
4280 nal->cookie = learn->cookie;
4281 nal->flags = htons(learn->flags);
4282 nal->table_id = learn->table_id;
4283
4284 for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
4285 put_u16(out, spec->n_bits | spec->dst_type | spec->src_type);
4286
4287 if (spec->src_type == NX_LEARN_SRC_FIELD) {
508a9338 4288 put_u32(out, mf_nxm_header(spec->src.field->id));
c2d936a4
BP
4289 put_u16(out, spec->src.ofs);
4290 } else {
4291 size_t n_dst_bytes = 2 * DIV_ROUND_UP(spec->n_bits, 16);
4292 uint8_t *bits = ofpbuf_put_zeros(out, n_dst_bytes);
4293 bitwise_copy(&spec->src_imm, sizeof spec->src_imm, 0,
4294 bits, n_dst_bytes, 0,
4295 spec->n_bits);
4296 }
4297
4298 if (spec->dst_type == NX_LEARN_DST_MATCH ||
4299 spec->dst_type == NX_LEARN_DST_LOAD) {
508a9338 4300 put_u32(out, mf_nxm_header(spec->dst.field->id));
c2d936a4
BP
4301 put_u16(out, spec->dst.ofs);
4302 }
4303 }
4304
178742f9 4305 pad_ofpat(out, start_ofs);
c2d936a4
BP
4306}
4307
cab50449 4308static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
4309parse_LEARN(char *arg, struct ofpbuf *ofpacts,
4310 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4311{
4312 return learn_parse(arg, ofpacts);
4313}
4314
4315static void
4316format_LEARN(const struct ofpact_learn *a, struct ds *s)
4317{
4318 learn_format(a, s);
4319}
4320\f
18080541
BP
4321/* Action structure for NXAST_CONJUNCTION. */
4322struct nx_action_conjunction {
4323 ovs_be16 type; /* OFPAT_VENDOR. */
4324 ovs_be16 len; /* At least 16. */
4325 ovs_be32 vendor; /* NX_VENDOR_ID. */
4326 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
4327 uint8_t clause;
4328 uint8_t n_clauses;
4329 ovs_be32 id;
4330};
4331OFP_ASSERT(sizeof(struct nx_action_conjunction) == 16);
4332
4333static void
4334add_conjunction(struct ofpbuf *out,
4335 uint32_t id, uint8_t clause, uint8_t n_clauses)
4336{
4337 struct ofpact_conjunction *oc;
4338
4339 oc = ofpact_put_CONJUNCTION(out);
4340 oc->id = id;
4341 oc->clause = clause;
4342 oc->n_clauses = n_clauses;
4343}
4344
4345static enum ofperr
4346decode_NXAST_RAW_CONJUNCTION(const struct nx_action_conjunction *nac,
f3cd3ac7 4347 enum ofp_version ofp_version OVS_UNUSED,
18080541
BP
4348 struct ofpbuf *out)
4349{
4350 if (nac->n_clauses < 2 || nac->n_clauses > 64
4351 || nac->clause >= nac->n_clauses) {
4352 return OFPERR_NXBAC_BAD_CONJUNCTION;
4353 } else {
4354 add_conjunction(out, ntohl(nac->id), nac->clause, nac->n_clauses);
4355 return 0;
4356 }
4357}
4358
4359static void
4360encode_CONJUNCTION(const struct ofpact_conjunction *oc,
4361 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4362{
4363 struct nx_action_conjunction *nac = put_NXAST_CONJUNCTION(out);
4364 nac->clause = oc->clause;
4365 nac->n_clauses = oc->n_clauses;
4366 nac->id = htonl(oc->id);
4367}
4368
4369static void
4370format_CONJUNCTION(const struct ofpact_conjunction *oc, struct ds *s)
4371{
b1c5bf1f
QM
4372 ds_put_format(s, "%sconjunction(%s%"PRIu32",%"PRIu8"/%"PRIu8"%s)%s",
4373 colors.paren, colors.end,
4374 oc->id, oc->clause + 1, oc->n_clauses,
4375 colors.paren, colors.end);
18080541
BP
4376}
4377
4378static char * OVS_WARN_UNUSED_RESULT
4379parse_CONJUNCTION(const char *arg, struct ofpbuf *ofpacts,
4380 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4381{
4382 uint8_t n_clauses;
4383 uint8_t clause;
4384 uint32_t id;
4385 int n;
4386
4387 if (!ovs_scan(arg, "%"SCNi32" , %"SCNu8" / %"SCNu8" %n",
4388 &id, &clause, &n_clauses, &n) || n != strlen(arg)) {
4389 return xstrdup("\"conjunction\" syntax is \"conjunction(id,i/n)\"");
4390 }
4391
4392 if (n_clauses < 2) {
4393 return xstrdup("conjunction must have at least 2 clauses");
4394 } else if (n_clauses > 64) {
4395 return xstrdup("conjunction must have at most 64 clauses");
4396 } else if (clause < 1) {
4397 return xstrdup("clause index must be positive");
4398 } else if (clause > n_clauses) {
4399 return xstrdup("clause index must be less than or equal to "
4400 "number of clauses");
4401 }
4402
4403 add_conjunction(ofpacts, id, clause - 1, n_clauses);
4404 return NULL;
4405}
4406\f
c2d936a4
BP
4407/* Action structure for NXAST_MULTIPATH.
4408 *
4409 * This action performs the following steps in sequence:
4410 *
4411 * 1. Hashes the fields designated by 'fields', one of NX_HASH_FIELDS_*.
4412 * Refer to the definition of "enum nx_mp_fields" for details.
4413 *
4414 * The 'basis' value is used as a universal hash parameter, that is,
4415 * different values of 'basis' yield different hash functions. The
4416 * particular universal hash function used is implementation-defined.
4417 *
4418 * The hashed fields' values are drawn from the current state of the
4419 * flow, including all modifications that have been made by actions up to
4420 * this point.
4421 *
4422 * 2. Applies the multipath link choice algorithm specified by 'algorithm',
4423 * one of NX_MP_ALG_*. Refer to the definition of "enum nx_mp_algorithm"
4424 * for details.
4425 *
4426 * The output of the algorithm is 'link', an unsigned integer less than
4427 * or equal to 'max_link'.
4428 *
4429 * Some algorithms use 'arg' as an additional argument.
4430 *
4431 * 3. Stores 'link' in dst[ofs:ofs+n_bits]. The format and semantics of
4432 * 'dst' and 'ofs_nbits' are similar to those for the NXAST_REG_LOAD
4433 * action.
4434 *
4435 * The switch will reject actions that have an unknown 'fields', or an unknown
4436 * 'algorithm', or in which ofs+n_bits is greater than the width of 'dst', or
4437 * in which 'max_link' is greater than or equal to 2**n_bits, with error type
4438 * OFPET_BAD_ACTION, code OFPBAC_BAD_ARGUMENT.
4439 */
4440struct nx_action_multipath {
4441 ovs_be16 type; /* OFPAT_VENDOR. */
4442 ovs_be16 len; /* Length is 32. */
4443 ovs_be32 vendor; /* NX_VENDOR_ID. */
4444 ovs_be16 subtype; /* NXAST_MULTIPATH. */
4445
4446 /* What fields to hash and how. */
4447 ovs_be16 fields; /* One of NX_HASH_FIELDS_*. */
4448 ovs_be16 basis; /* Universal hash parameter. */
4449 ovs_be16 pad0;
4450
4451 /* Multipath link choice algorithm to apply to hash value. */
4452 ovs_be16 algorithm; /* One of NX_MP_ALG_*. */
4453 ovs_be16 max_link; /* Number of output links, minus 1. */
4454 ovs_be32 arg; /* Algorithm-specific argument. */
4455 ovs_be16 pad1;
4456
4457 /* Where to store the result. */
4458 ovs_be16 ofs_nbits; /* (ofs << 6) | (n_bits - 1). */
4459 ovs_be32 dst; /* Destination. */
4460};
4461OFP_ASSERT(sizeof(struct nx_action_multipath) == 32);
4462
4463static enum ofperr
4464decode_NXAST_RAW_MULTIPATH(const struct nx_action_multipath *nam,
f3cd3ac7 4465 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4
BP
4466 struct ofpbuf *out)
4467{
4468 uint32_t n_links = ntohs(nam->max_link) + 1;
4469 size_t min_n_bits = log_2_ceil(n_links);
4470 struct ofpact_multipath *mp;
4471
4472 mp = ofpact_put_MULTIPATH(out);
4473 mp->fields = ntohs(nam->fields);
4474 mp->basis = ntohs(nam->basis);
4475 mp->algorithm = ntohs(nam->algorithm);
4476 mp->max_link = ntohs(nam->max_link);
4477 mp->arg = ntohl(nam->arg);
4478 mp->dst.field = mf_from_nxm_header(ntohl(nam->dst));
4479 mp->dst.ofs = nxm_decode_ofs(nam->ofs_nbits);
4480 mp->dst.n_bits = nxm_decode_n_bits(nam->ofs_nbits);
4481
4482 if (!flow_hash_fields_valid(mp->fields)) {
4483 VLOG_WARN_RL(&rl, "unsupported fields %d", (int) mp->fields);
4484 return OFPERR_OFPBAC_BAD_ARGUMENT;
4485 } else if (mp->algorithm != NX_MP_ALG_MODULO_N
4486 && mp->algorithm != NX_MP_ALG_HASH_THRESHOLD
4487 && mp->algorithm != NX_MP_ALG_HRW
4488 && mp->algorithm != NX_MP_ALG_ITER_HASH) {
4489 VLOG_WARN_RL(&rl, "unsupported algorithm %d", (int) mp->algorithm);
4490 return OFPERR_OFPBAC_BAD_ARGUMENT;
4491 } else if (mp->dst.n_bits < min_n_bits) {
4492 VLOG_WARN_RL(&rl, "multipath action requires at least %"PRIuSIZE" bits for "
4493 "%"PRIu32" links", min_n_bits, n_links);
4494 return OFPERR_OFPBAC_BAD_ARGUMENT;
4495 }
4496
4497 return multipath_check(mp, NULL);
4498}
4499
4500static void
4501encode_MULTIPATH(const struct ofpact_multipath *mp,
4502 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4503{
4504 struct nx_action_multipath *nam = put_NXAST_MULTIPATH(out);
4505
4506 nam->fields = htons(mp->fields);
4507 nam->basis = htons(mp->basis);
4508 nam->algorithm = htons(mp->algorithm);
4509 nam->max_link = htons(mp->max_link);
4510 nam->arg = htonl(mp->arg);
4511 nam->ofs_nbits = nxm_encode_ofs_nbits(mp->dst.ofs, mp->dst.n_bits);
508a9338 4512 nam->dst = htonl(mf_nxm_header(mp->dst.field->id));
c2d936a4
BP
4513}
4514
cab50449 4515static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
4516parse_MULTIPATH(const char *arg, struct ofpbuf *ofpacts,
4517 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4518{
4519 return multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
4520}
4521
4522static void
4523format_MULTIPATH(const struct ofpact_multipath *a, struct ds *s)
4524{
4525 multipath_format(a, s);
4526}
4527\f
4528/* Action structure for NXAST_NOTE.
4529 *
4530 * This action has no effect. It is variable length. The switch does not
4531 * attempt to interpret the user-defined 'note' data in any way. A controller
4532 * can use this action to attach arbitrary metadata to a flow.
4533 *
4534 * This action might go away in the future.
4535 */
4536struct nx_action_note {
4537 ovs_be16 type; /* OFPAT_VENDOR. */
4538 ovs_be16 len; /* A multiple of 8, but at least 16. */
4539 ovs_be32 vendor; /* NX_VENDOR_ID. */
4540 ovs_be16 subtype; /* NXAST_NOTE. */
4541 uint8_t note[6]; /* Start of user-defined data. */
4542 /* Possibly followed by additional user-defined data. */
4543};
4544OFP_ASSERT(sizeof(struct nx_action_note) == 16);
4545
4546static enum ofperr
f3cd3ac7
JS
4547decode_NXAST_RAW_NOTE(const struct nx_action_note *nan,
4548 enum ofp_version ofp_version OVS_UNUSED,
4549 struct ofpbuf *out)
c2d936a4
BP
4550{
4551 struct ofpact_note *note;
4552 unsigned int length;
4553
4554 length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
2bd318de 4555 note = ofpact_put_NOTE(out);
c2d936a4 4556 note->length = length;
2bd318de 4557 ofpbuf_put(out, nan->note, length);
34abaa3d 4558 ofpact_finish(out, out->header);
c2d936a4
BP
4559
4560 return 0;
4561}
4562
4563static void
4564encode_NOTE(const struct ofpact_note *note,
4565 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4566{
6fd6ed71 4567 size_t start_ofs = out->size;
c2d936a4 4568 struct nx_action_note *nan;
c2d936a4
BP
4569
4570 put_NXAST_NOTE(out);
6fd6ed71 4571 out->size = out->size - sizeof nan->note;
c2d936a4
BP
4572
4573 ofpbuf_put(out, note->data, note->length);
9ac0aada 4574 pad_ofpat(out, start_ofs);
c2d936a4
BP
4575}
4576
cab50449 4577static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
4578parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
4579 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4580{
27aa8793
BP
4581 size_t start_ofs = ofpacts->size;
4582 ofpact_put_NOTE(ofpacts);
4583 arg = ofpbuf_put_hex(ofpacts, arg, NULL);
4584 if (arg[0]) {
4585 return xstrdup("bad hex digit in `note' argument");
4586 }
4587 struct ofpact_note *note = ofpbuf_at_assert(ofpacts, start_ofs,
4588 sizeof *note);
4589 note->length = ofpacts->size - (start_ofs + sizeof *note);
34abaa3d 4590 ofpact_finish(ofpacts, &note->ofpact);
c2d936a4
BP
4591 return NULL;
4592}
4593
4594static void
4595format_NOTE(const struct ofpact_note *a, struct ds *s)
4596{
b1c5bf1f 4597 ds_put_format(s, "%snote:%s", colors.param, colors.end);
bdcad671 4598 format_hex_arg(s, a->data, a->length);
c2d936a4
BP
4599}
4600\f
4601/* Exit action. */
4602
4603static enum ofperr
4604decode_NXAST_RAW_EXIT(struct ofpbuf *out)
4605{
4606 ofpact_put_EXIT(out);
4607 return 0;
4608}
4609
4610static void
4611encode_EXIT(const struct ofpact_null *null OVS_UNUSED,
4612 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4613{
4614 put_NXAST_EXIT(out);
4615}
4616
cab50449 4617static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
4618parse_EXIT(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4619 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4620{
4621 ofpact_put_EXIT(ofpacts);
4622 return NULL;
4623}
4624
4625static void
4626format_EXIT(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4627{
b1c5bf1f 4628 ds_put_format(s, "%sexit%s", colors.special, colors.end);
c2d936a4
BP
4629}
4630\f
e672ff9b
JR
4631/* Unroll xlate action. */
4632
4633static void
4634encode_UNROLL_XLATE(const struct ofpact_unroll_xlate *unroll OVS_UNUSED,
4635 enum ofp_version ofp_version OVS_UNUSED,
4636 struct ofpbuf *out OVS_UNUSED)
4637{
4638 OVS_NOT_REACHED();
4639}
4640
4641static char * OVS_WARN_UNUSED_RESULT
4642parse_UNROLL_XLATE(char *arg OVS_UNUSED, struct ofpbuf *ofpacts OVS_UNUSED,
4643 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4644{
4645 OVS_NOT_REACHED();
4646 return NULL;
4647}
4648
4649static void
28632a8a 4650format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a, struct ds *s)
e672ff9b 4651{
b1c5bf1f
QM
4652 ds_put_format(s, "%sunroll_xlate(%s%stable=%s%"PRIu8
4653 ", %scookie=%s%"PRIu64"%s)%s",
4654 colors.paren, colors.end,
4655 colors.special, colors.end, a->rule_table_id,
4656 colors.param, colors.end, ntohll(a->rule_cookie),
4657 colors.paren, colors.end);
e672ff9b
JR
4658}
4659\f
c2d936a4
BP
4660/* Action structure for NXAST_SAMPLE.
4661 *
4662 * Samples matching packets with the given probability and sends them
4663 * each to the set of collectors identified with the given ID. The
4664 * probability is expressed as a number of packets to be sampled out
4665 * of USHRT_MAX packets, and must be >0.
4666 *
4667 * When sending packet samples to IPFIX collectors, the IPFIX flow
4668 * record sent for each sampled packet is associated with the given
4669 * observation domain ID and observation point ID. Each IPFIX flow
4670 * record contain the sampled packet's headers when executing this
4671 * rule. If a sampled packet's headers are modified by previous
4672 * actions in the flow, those modified headers are sent. */
4673struct nx_action_sample {
4674 ovs_be16 type; /* OFPAT_VENDOR. */
4675 ovs_be16 len; /* Length is 24. */
4676 ovs_be32 vendor; /* NX_VENDOR_ID. */
4677 ovs_be16 subtype; /* NXAST_SAMPLE. */
4678 ovs_be16 probability; /* Fraction of packets to sample. */
4679 ovs_be32 collector_set_id; /* ID of collector set in OVSDB. */
4680 ovs_be32 obs_domain_id; /* ID of sampling observation domain. */
4681 ovs_be32 obs_point_id; /* ID of sampling observation point. */
4682};
4683OFP_ASSERT(sizeof(struct nx_action_sample) == 24);
4684
4685static enum ofperr
f3cd3ac7
JS
4686decode_NXAST_RAW_SAMPLE(const struct nx_action_sample *nas,
4687 enum ofp_version ofp_version OVS_UNUSED,
4688 struct ofpbuf *out)
c2d936a4
BP
4689{
4690 struct ofpact_sample *sample;
4691
4692 sample = ofpact_put_SAMPLE(out);
4693 sample->probability = ntohs(nas->probability);
4694 sample->collector_set_id = ntohl(nas->collector_set_id);
4695 sample->obs_domain_id = ntohl(nas->obs_domain_id);
4696 sample->obs_point_id = ntohl(nas->obs_point_id);
4697
4698 if (sample->probability == 0) {
4699 return OFPERR_OFPBAC_BAD_ARGUMENT;
4700 }
4701
4702 return 0;
4703}
4704
4705static void
4706encode_SAMPLE(const struct ofpact_sample *sample,
4707 enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
4708{
4709 struct nx_action_sample *nas;
4710
4711 nas = put_NXAST_SAMPLE(out);
4712 nas->probability = htons(sample->probability);
4713 nas->collector_set_id = htonl(sample->collector_set_id);
4714 nas->obs_domain_id = htonl(sample->obs_domain_id);
4715 nas->obs_point_id = htonl(sample->obs_point_id);
4716}
4717
4718/* Parses 'arg' as the argument to a "sample" action, and appends such an
4719 * action to 'ofpacts'.
4720 *
4721 * Returns NULL if successful, otherwise a malloc()'d string describing the
4722 * error. The caller is responsible for freeing the returned string. */
cab50449 4723static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
4724parse_SAMPLE(char *arg, struct ofpbuf *ofpacts,
4725 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4726{
4727 struct ofpact_sample *os = ofpact_put_SAMPLE(ofpacts);
4728 char *key, *value;
4729
4730 while (ofputil_parse_key_value(&arg, &key, &value)) {
4731 char *error = NULL;
4732
4733 if (!strcmp(key, "probability")) {
4734 error = str_to_u16(value, "probability", &os->probability);
4735 if (!error && os->probability == 0) {
4736 error = xasprintf("invalid probability value \"%s\"", value);
4737 }
4738 } else if (!strcmp(key, "collector_set_id")) {
4739 error = str_to_u32(value, &os->collector_set_id);
4740 } else if (!strcmp(key, "obs_domain_id")) {
4741 error = str_to_u32(value, &os->obs_domain_id);
4742 } else if (!strcmp(key, "obs_point_id")) {
4743 error = str_to_u32(value, &os->obs_point_id);
4744 } else {
4745 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
4746 key);
4747 }
4748 if (error) {
4749 return error;
4750 }
4751 }
4752 if (os->probability == 0) {
4753 return xstrdup("non-zero \"probability\" must be specified on sample");
4754 }
4755 return NULL;
4756}
4757
4758static void
4759format_SAMPLE(const struct ofpact_sample *a, struct ds *s)
4760{
b1c5bf1f
QM
4761 ds_put_format(s, "%ssample(%s%sprobability=%s%"PRIu16
4762 ",%scollector_set_id=%s%"PRIu32
4763 ",%sobs_domain_id=%s%"PRIu32
4764 ",%sobs_point_id=%s%"PRIu32"%s)%s",
4765 colors.paren, colors.end,
4766 colors.param, colors.end, a->probability,
4767 colors.param, colors.end, a->collector_set_id,
4768 colors.param, colors.end, a->obs_domain_id,
4769 colors.param, colors.end, a->obs_point_id,
4770 colors.paren, colors.end);
c2d936a4
BP
4771}
4772\f
d4abaff5
BP
4773/* debug_recirc instruction. */
4774
4775static bool enable_debug;
4776
4777void
4778ofpact_dummy_enable(void)
4779{
4780 enable_debug = true;
4781}
4782
4783static enum ofperr
4784decode_NXAST_RAW_DEBUG_RECIRC(struct ofpbuf *out)
4785{
4786 if (!enable_debug) {
4787 return OFPERR_OFPBAC_BAD_VENDOR_TYPE;
4788 }
4789
4790 ofpact_put_DEBUG_RECIRC(out);
4791 return 0;
4792}
4793
4794static void
4795encode_DEBUG_RECIRC(const struct ofpact_null *n OVS_UNUSED,
4796 enum ofp_version ofp_version OVS_UNUSED,
4797 struct ofpbuf *out)
4798{
4799 put_NXAST_DEBUG_RECIRC(out);
4800}
4801
4802static char * OVS_WARN_UNUSED_RESULT
4803parse_DEBUG_RECIRC(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
4804 enum ofputil_protocol *usable_protocols OVS_UNUSED)
4805{
4806 ofpact_put_DEBUG_RECIRC(ofpacts);
4807 return NULL;
4808}
4809
4810static void
4811format_DEBUG_RECIRC(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
4812{
b1c5bf1f 4813 ds_put_format(s, "%sdebug_recirc%s", colors.value, colors.end);
d4abaff5 4814}
07659514
JS
4815
4816/* Action structure for NXAST_CT.
4817 *
4818 * Pass traffic to the connection tracker.
4819 *
4820 * There are two important concepts to understanding the connection tracking
4821 * interface: Packet state and Connection state. Packets may be "Untracked" or
4822 * "Tracked". Connections may be "Uncommitted" or "Committed".
4823 *
4824 * - Packet State:
4825 *
4826 * Untracked packets have not yet passed through the connection tracker,
4827 * and the connection state for such packets is unknown. In most cases,
4828 * packets entering the OpenFlow pipeline will initially be in the
4829 * untracked state. Untracked packets may become tracked by executing
4830 * NXAST_CT with a "recirc_table" specified. This makes various aspects
4831 * about the connection available, in particular the connection state.
4832 *
4833 * Tracked packets have previously passed through the connection tracker.
4834 * These packets will remain tracked through until the end of the OpenFlow
4835 * pipeline. Tracked packets which have NXAST_CT executed with a
4836 * "recirc_table" specified will return to the tracked state.
4837 *
4838 * The packet state is only significant for the duration of packet
4839 * processing within the OpenFlow pipeline.
4840 *
4841 * - Connection State:
4842 *
4843 * Multiple packets may be associated with a single connection. Initially,
4844 * all connections are uncommitted. The connection state corresponding to
4845 * a packet is available in the NXM_NX_CT_STATE field for tracked packets.
4846 *
4847 * Uncommitted connections have no state stored about them. Uncommitted
4848 * connections may transition into the committed state by executing
4849 * NXAST_CT with the NX_CT_F_COMMIT flag.
4850 *
4851 * Once a connection becomes committed, information may be gathered about
4852 * the connection by passing subsequent packets through the connection
4853 * tracker, and the state of the connection will be stored beyond the
4854 * lifetime of packet processing.
4855 *
4856 * Connections may transition back into the uncommitted state due to
4857 * external timers, or due to the contents of packets that are sent to the
4858 * connection tracker. This behaviour is outside of the scope of the
4859 * OpenFlow interface.
4860 *
4861 * The "zone" specifies a context within which the tracking is done:
4862 *
4863 * The connection tracking zone is a 16-bit number. Each zone is an
4864 * independent connection tracking context. The connection state for each
4865 * connection is completely separate for each zone, so if a connection
4866 * is committed to zone A, then it will remain uncommitted in zone B.
4867 * If NXAST_CT is executed with the same zone multiple times, later
4868 * executions have no effect.
4869 *
4870 * If 'zone_src' is nonzero, this specifies that the zone should be
4871 * sourced from a field zone_src[ofs:ofs+nbits]. The format and semantics
4872 * of 'zone_src' and 'zone_ofs_nbits' are similar to those for the
4873 * NXAST_REG_LOAD action. The acceptable nxm_header values for 'zone_src'
4874 * are the same as the acceptable nxm_header values for the 'src' field of
4875 * NXAST_REG_MOVE.
4876 *
4877 * If 'zone_src' is zero, then the value of 'zone_imm' will be used as the
4878 * connection tracking zone.
4879 *
4880 * The "recirc_table" allows NXM_NX_CT_* fields to become available:
4881 *
4882 * If "recirc_table" has a value other than NX_CT_RECIRC_NONE, then the
4883 * packet will be logically cloned prior to executing this action. One
4884 * copy will be sent to the connection tracker, then will be re-injected
4885 * into the OpenFlow pipeline beginning at the OpenFlow table specified in
4886 * this field. When the packet re-enters the pipeline, the NXM_NX_CT_*
4887 * fields will be populated. The original instance of the packet will
4888 * continue the current actions list. This can be thought of as similar to
4889 * the effect of the "output" action: One copy is sent out (in this case,
4890 * to the connection tracker), but the current copy continues processing.
4891 *
4892 * It is strongly recommended that this table is later than the current
4893 * table, to prevent loops.
8e53fe8c 4894 *
d787ad39
JS
4895 * The "alg" attaches protocol-specific behaviour to this action:
4896 *
4897 * The ALG is a 16-bit number which specifies that additional
4898 * processing should be applied to this traffic.
4899 *
4900 * Protocol | Value | Meaning
4901 * --------------------------------------------------------------------
4902 * None | 0 | No protocol-specific behaviour.
4903 * FTP | 21 | Parse FTP control connections and observe the
4904 * | | negotiation of related data connections.
4905 * Other | Other | Unsupported protocols.
4906 *
4907 * By way of example, if FTP control connections have this action applied
4908 * with the ALG set to FTP (21), then the connection tracker will observe
4909 * the negotiation of data connections. This allows the connection
4910 * tracker to identify subsequent data connections as "related" to this
4911 * existing connection. The "related" flag will be populated in the
4912 * NXM_NX_CT_STATE field for such connections if the 'recirc_table' is
4913 * specified.
4914 *
8e53fe8c
JS
4915 * Zero or more actions may immediately follow this action. These actions will
4916 * be executed within the context of the connection tracker, and they require
4917 * the NX_CT_F_COMMIT flag to be set.
07659514
JS
4918 */
4919struct nx_action_conntrack {
4920 ovs_be16 type; /* OFPAT_VENDOR. */
4921 ovs_be16 len; /* At least 24. */
4922 ovs_be32 vendor; /* NX_VENDOR_ID. */
4923 ovs_be16 subtype; /* NXAST_CT. */
4924 ovs_be16 flags; /* Zero or more NX_CT_F_* flags.
4925 * Unspecified flag bits must be zero. */
4926 ovs_be32 zone_src; /* Connection tracking context. */
4927 union {
4928 ovs_be16 zone_ofs_nbits;/* Range to use from source field. */
4929 ovs_be16 zone_imm; /* Immediate value for zone. */
4930 };
4931 uint8_t recirc_table; /* Recirculate to a specific table, or
4932 NX_CT_RECIRC_NONE for no recirculation. */
d787ad39
JS
4933 uint8_t pad[3]; /* Zeroes */
4934 ovs_be16 alg; /* Well-known port number for the protocol.
4935 * 0 indicates no ALG is required. */
07659514
JS
4936 /* Followed by a sequence of zero or more OpenFlow actions. The length of
4937 * these is included in 'len'. */
4938};
4939OFP_ASSERT(sizeof(struct nx_action_conntrack) == 24);
4940
4941static enum ofperr
4942decode_ct_zone(const struct nx_action_conntrack *nac,
4943 struct ofpact_conntrack *out)
4944{
4945 if (nac->zone_src) {
4946 enum ofperr error;
4947
4948 out->zone_src.field = mf_from_nxm_header(ntohl(nac->zone_src));
4949 out->zone_src.ofs = nxm_decode_ofs(nac->zone_ofs_nbits);
4950 out->zone_src.n_bits = nxm_decode_n_bits(nac->zone_ofs_nbits);
4951 error = mf_check_src(&out->zone_src, NULL);
4952 if (error) {
4953 return error;
4954 }
4955
4956 if (out->zone_src.n_bits != 16) {
4957 VLOG_WARN_RL(&rl, "zone n_bits %d not within valid range [16..16]",
4958 out->zone_src.n_bits);
4959 return OFPERR_OFPBAC_BAD_SET_LEN;
4960 }
4961 } else {
4962 out->zone_src.field = NULL;
4963 out->zone_imm = ntohs(nac->zone_imm);
4964 }
4965
4966 return 0;
4967}
4968
4969static enum ofperr
4970decode_NXAST_RAW_CT(const struct nx_action_conntrack *nac,
8e53fe8c 4971 enum ofp_version ofp_version, struct ofpbuf *out)
07659514 4972{
8e53fe8c 4973 const size_t ct_offset = ofpacts_pull(out);
0a2869d5 4974 struct ofpact_conntrack *conntrack = ofpact_put_CT(out);
07659514 4975 conntrack->flags = ntohs(nac->flags);
0a2869d5
BP
4976
4977 int error = decode_ct_zone(nac, conntrack);
07659514
JS
4978 if (error) {
4979 goto out;
4980 }
4981 conntrack->recirc_table = nac->recirc_table;
d787ad39 4982 conntrack->alg = ntohs(nac->alg);
07659514 4983
8e53fe8c
JS
4984 ofpbuf_pull(out, sizeof(*conntrack));
4985
0a2869d5
BP
4986 struct ofpbuf openflow = ofpbuf_const_initializer(
4987 nac + 1, ntohs(nac->len) - sizeof(*nac));
8e53fe8c
JS
4988 error = ofpacts_pull_openflow_actions__(&openflow, openflow.size,
4989 ofp_version,
4990 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
4991 out, OFPACT_CT);
4992 if (error) {
4993 goto out;
4994 }
4995
4996 conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
4997 out->header = &conntrack->ofpact;
34abaa3d 4998 ofpact_finish(out, &conntrack->ofpact);
8e53fe8c
JS
4999
5000 if (conntrack->ofpact.len > sizeof(*conntrack)
5001 && !(conntrack->flags & NX_CT_F_COMMIT)) {
9ac0aada
JR
5002 const struct ofpact *a;
5003 size_t ofpacts_len = conntrack->ofpact.len - sizeof(*conntrack);
5004
5005 OFPACT_FOR_EACH (a, conntrack->actions, ofpacts_len) {
5006 if (a->type != OFPACT_NAT || ofpact_get_NAT(a)->flags
5007 || ofpact_get_NAT(a)->range_af != AF_UNSPEC) {
5008 VLOG_WARN_RL(&rl, "CT action requires commit flag if actions "
5009 "other than NAT without arguments are specified.");
5010 error = OFPERR_OFPBAC_BAD_ARGUMENT;
5011 goto out;
5012 }
5013 }
8e53fe8c
JS
5014 }
5015
07659514 5016out:
8e53fe8c 5017 ofpbuf_push_uninit(out, ct_offset);
07659514
JS
5018 return error;
5019}
5020
5021static void
5022encode_CT(const struct ofpact_conntrack *conntrack,
8e53fe8c 5023 enum ofp_version ofp_version, struct ofpbuf *out)
07659514
JS
5024{
5025 struct nx_action_conntrack *nac;
8e53fe8c
JS
5026 const size_t ofs = out->size;
5027 size_t len;
07659514
JS
5028
5029 nac = put_NXAST_CT(out);
5030 nac->flags = htons(conntrack->flags);
5031 if (conntrack->zone_src.field) {
5032 nac->zone_src = htonl(mf_nxm_header(conntrack->zone_src.field->id));
5033 nac->zone_ofs_nbits = nxm_encode_ofs_nbits(conntrack->zone_src.ofs,
5034 conntrack->zone_src.n_bits);
5035 } else {
5036 nac->zone_src = htonl(0);
5037 nac->zone_imm = htons(conntrack->zone_imm);
5038 }
5039 nac->recirc_table = conntrack->recirc_table;
d787ad39 5040 nac->alg = htons(conntrack->alg);
8e53fe8c
JS
5041
5042 len = ofpacts_put_openflow_actions(conntrack->actions,
5043 ofpact_ct_get_action_len(conntrack),
5044 out, ofp_version);
5045 len += sizeof(*nac);
5046 nac = ofpbuf_at(out, ofs, sizeof(*nac));
5047 nac->len = htons(len);
07659514
JS
5048}
5049
9ac0aada
JR
5050static char * OVS_WARN_UNUSED_RESULT parse_NAT(char *arg, struct ofpbuf *,
5051 enum ofputil_protocol * OVS_UNUSED);
5052
07659514
JS
5053/* Parses 'arg' as the argument to a "ct" action, and appends such an
5054 * action to 'ofpacts'.
5055 *
5056 * Returns NULL if successful, otherwise a malloc()'d string describing the
5057 * error. The caller is responsible for freeing the returned string. */
5058static char * OVS_WARN_UNUSED_RESULT
5059parse_CT(char *arg, struct ofpbuf *ofpacts,
8e53fe8c 5060 enum ofputil_protocol *usable_protocols)
07659514 5061{
8e53fe8c 5062 const size_t ct_offset = ofpacts_pull(ofpacts);
07659514
JS
5063 struct ofpact_conntrack *oc;
5064 char *error = NULL;
5065 char *key, *value;
5066
5067 oc = ofpact_put_CT(ofpacts);
5068 oc->flags = 0;
5069 oc->recirc_table = NX_CT_RECIRC_NONE;
5070 while (ofputil_parse_key_value(&arg, &key, &value)) {
5071 if (!strcmp(key, "commit")) {
5072 oc->flags |= NX_CT_F_COMMIT;
5073 } else if (!strcmp(key, "table")) {
5074 error = str_to_u8(value, "recirc_table", &oc->recirc_table);
5075 if (!error && oc->recirc_table == NX_CT_RECIRC_NONE) {
5076 error = xasprintf("invalid table %#"PRIx16, oc->recirc_table);
5077 }
5078 } else if (!strcmp(key, "zone")) {
5079 error = str_to_u16(value, "zone", &oc->zone_imm);
5080
5081 if (error) {
5082 free(error);
5083 error = mf_parse_subfield(&oc->zone_src, value);
5084 if (error) {
5085 return error;
5086 }
5087 }
d787ad39
JS
5088 } else if (!strcmp(key, "alg")) {
5089 error = str_to_connhelper(value, &oc->alg);
9ac0aada
JR
5090 } else if (!strcmp(key, "nat")) {
5091 const size_t nat_offset = ofpacts_pull(ofpacts);
5092
5093 error = parse_NAT(value, ofpacts, usable_protocols);
9ac0aada
JR
5094 /* Update CT action pointer and length. */
5095 ofpacts->header = ofpbuf_push_uninit(ofpacts, nat_offset);
5096 oc = ofpacts->header;
8e53fe8c
JS
5097 } else if (!strcmp(key, "exec")) {
5098 /* Hide existing actions from ofpacts_parse_copy(), so the
5099 * nesting can be handled transparently. */
76e3e669 5100 enum ofputil_protocol usable_protocols2;
9ac0aada 5101 const size_t exec_offset = ofpacts_pull(ofpacts);
76e3e669 5102
76e3e669
JR
5103 /* Initializes 'usable_protocol2', fold it back to
5104 * '*usable_protocols' afterwards, so that we do not lose
5105 * restrictions already in there. */
5106 error = ofpacts_parse_copy(value, ofpacts, &usable_protocols2,
5107 false, OFPACT_CT);
5108 *usable_protocols &= usable_protocols2;
9ac0aada 5109 ofpacts->header = ofpbuf_push_uninit(ofpacts, exec_offset);
8e53fe8c 5110 oc = ofpacts->header;
07659514
JS
5111 } else {
5112 error = xasprintf("invalid argument to \"ct\" action: `%s'", key);
5113 }
5114 if (error) {
5115 break;
5116 }
5117 }
5118
34abaa3d 5119 ofpact_finish(ofpacts, &oc->ofpact);
8e53fe8c 5120 ofpbuf_push_uninit(ofpacts, ct_offset);
07659514
JS
5121 return error;
5122}
5123
d787ad39
JS
5124static void
5125format_alg(int port, struct ds *s)
5126{
5127 if (port == IPPORT_FTP) {
b1c5bf1f 5128 ds_put_format(s, "%salg=%sftp,", colors.param, colors.end);
d787ad39 5129 } else if (port) {
b1c5bf1f 5130 ds_put_format(s, "%salg=%s%d,", colors.param, colors.end, port);
d787ad39
JS
5131 }
5132}
5133
9ac0aada
JR
5134static void format_NAT(const struct ofpact_nat *a, struct ds *ds);
5135
07659514
JS
5136static void
5137format_CT(const struct ofpact_conntrack *a, struct ds *s)
5138{
b1c5bf1f 5139 ds_put_format(s, "%sct(%s", colors.paren, colors.end);
07659514 5140 if (a->flags & NX_CT_F_COMMIT) {
b1c5bf1f 5141 ds_put_format(s, "%scommit%s,", colors.value, colors.end);
07659514
JS
5142 }
5143 if (a->recirc_table != NX_CT_RECIRC_NONE) {
b1c5bf1f
QM
5144 ds_put_format(s, "%stable=%s%"PRIu8",",
5145 colors.special, colors.end, a->recirc_table);
07659514
JS
5146 }
5147 if (a->zone_src.field) {
b1c5bf1f 5148 ds_put_format(s, "%szone=%s", colors.param, colors.end);
07659514
JS
5149 mf_format_subfield(&a->zone_src, s);
5150 ds_put_char(s, ',');
5151 } else if (a->zone_imm) {
b1c5bf1f
QM
5152 ds_put_format(s, "%szone=%s%"PRIu16",",
5153 colors.param, colors.end, a->zone_imm);
07659514 5154 }
9ac0aada
JR
5155 /* If the first action is a NAT action, format it outside of the 'exec'
5156 * envelope. */
5157 const struct ofpact *action = a->actions;
5158 size_t actions_len = ofpact_ct_get_action_len(a);
5159 if (actions_len && action->type == OFPACT_NAT) {
5160 format_NAT(ofpact_get_NAT(action), s);
5161 ds_put_char(s, ',');
5162 actions_len -= OFPACT_ALIGN(action->len);
5163 action = ofpact_next(action);
5164 }
5165 if (actions_len) {
b1c5bf1f 5166 ds_put_format(s, "%sexec(%s", colors.paren, colors.end);
9ac0aada 5167 ofpacts_format(action, actions_len, s);
b1c5bf1f 5168 ds_put_format(s, "%s),%s", colors.paren, colors.end);
8e53fe8c 5169 }
d787ad39 5170 format_alg(a->alg, s);
07659514 5171 ds_chomp(s, ',');
b1c5bf1f 5172 ds_put_format(s, "%s)%s", colors.paren, colors.end);
07659514 5173}
9ac0aada
JR
5174\f
5175/* NAT action. */
5176
5177/* Which optional fields are present? */
5178enum nx_nat_range {
5179 NX_NAT_RANGE_IPV4_MIN = 1 << 0, /* ovs_be32 */
5180 NX_NAT_RANGE_IPV4_MAX = 1 << 1, /* ovs_be32 */
5181 NX_NAT_RANGE_IPV6_MIN = 1 << 2, /* struct in6_addr */
5182 NX_NAT_RANGE_IPV6_MAX = 1 << 3, /* struct in6_addr */
5183 NX_NAT_RANGE_PROTO_MIN = 1 << 4, /* ovs_be16 */
5184 NX_NAT_RANGE_PROTO_MAX = 1 << 5, /* ovs_be16 */
5185};
5186
5187/* Action structure for NXAST_NAT. */
5188struct nx_action_nat {
5189 ovs_be16 type; /* OFPAT_VENDOR. */
5190 ovs_be16 len; /* At least 16. */
5191 ovs_be32 vendor; /* NX_VENDOR_ID. */
5192 ovs_be16 subtype; /* NXAST_NAT. */
5193 uint8_t pad[2]; /* Must be zero. */
5194 ovs_be16 flags; /* Zero or more NX_NAT_F_* flags.
5195 * Unspecified flag bits must be zero. */
5196 ovs_be16 range_present; /* NX_NAT_RANGE_* */
5197 /* Followed by optional parameters as specified by 'range_present' */
5198};
5199OFP_ASSERT(sizeof(struct nx_action_nat) == 16);
5200
5201static void
5202encode_NAT(const struct ofpact_nat *nat,
5203 enum ofp_version ofp_version OVS_UNUSED,
5204 struct ofpbuf *out)
5205{
5206 struct nx_action_nat *nan;
5207 const size_t ofs = out->size;
5208 uint16_t range_present = 0;
5209
5210 nan = put_NXAST_NAT(out);
5211 nan->flags = htons(nat->flags);
5212 if (nat->range_af == AF_INET) {
5213 if (nat->range.addr.ipv4.min) {
5214 ovs_be32 *min = ofpbuf_put_uninit(out, sizeof *min);
5215 *min = nat->range.addr.ipv4.min;
5216 range_present |= NX_NAT_RANGE_IPV4_MIN;
5217 }
5218 if (nat->range.addr.ipv4.max) {
5219 ovs_be32 *max = ofpbuf_put_uninit(out, sizeof *max);
5220 *max = nat->range.addr.ipv4.max;
5221 range_present |= NX_NAT_RANGE_IPV4_MAX;
5222 }
5223 } else if (nat->range_af == AF_INET6) {
5224 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5225 struct in6_addr *min = ofpbuf_put_uninit(out, sizeof *min);
5226 *min = nat->range.addr.ipv6.min;
5227 range_present |= NX_NAT_RANGE_IPV6_MIN;
5228 }
5229 if (!ipv6_mask_is_any(&nat->range.addr.ipv6.max)) {
5230 struct in6_addr *max = ofpbuf_put_uninit(out, sizeof *max);
5231 *max = nat->range.addr.ipv6.max;
5232 range_present |= NX_NAT_RANGE_IPV6_MAX;
5233 }
5234 }
5235 if (nat->range_af != AF_UNSPEC) {
5236 if (nat->range.proto.min) {
5237 ovs_be16 *min = ofpbuf_put_uninit(out, sizeof *min);
5238 *min = htons(nat->range.proto.min);
5239 range_present |= NX_NAT_RANGE_PROTO_MIN;
5240 }
5241 if (nat->range.proto.max) {
5242 ovs_be16 *max = ofpbuf_put_uninit(out, sizeof *max);
5243 *max = htons(nat->range.proto.max);
5244 range_present |= NX_NAT_RANGE_PROTO_MAX;
5245 }
5246 }
5247 pad_ofpat(out, ofs);
5248 nan = ofpbuf_at(out, ofs, sizeof *nan);
5249 nan->range_present = htons(range_present);
5250}
5251
5252static enum ofperr
5253decode_NXAST_RAW_NAT(const struct nx_action_nat *nan,
5254 enum ofp_version ofp_version OVS_UNUSED,
5255 struct ofpbuf *out)
5256{
5257 struct ofpact_nat *nat;
5258 uint16_t range_present = ntohs(nan->range_present);
5259 const char *opts = (char *)(nan + 1);
5260 uint16_t len = ntohs(nan->len) - sizeof *nan;
5261
5262 nat = ofpact_put_NAT(out);
5263 nat->flags = ntohs(nan->flags);
5264
5265#define NX_NAT_GET_OPT(DST, SRC, LEN, TYPE) \
5266 (LEN >= sizeof(TYPE) \
5267 ? (memcpy(DST, SRC, sizeof(TYPE)), LEN -= sizeof(TYPE), \
5268 SRC += sizeof(TYPE)) \
5269 : NULL)
5270
5271 nat->range_af = AF_UNSPEC;
5272 if (range_present & NX_NAT_RANGE_IPV4_MIN) {
5273 if (range_present & (NX_NAT_RANGE_IPV6_MIN | NX_NAT_RANGE_IPV6_MAX)) {
5274 return OFPERR_OFPBAC_BAD_ARGUMENT;
5275 }
5276
5277 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.min, opts, len, ovs_be32)
5278 || !nat->range.addr.ipv4.min) {
5279 return OFPERR_OFPBAC_BAD_ARGUMENT;
5280 }
5281
5282 nat->range_af = AF_INET;
5283
5284 if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5285 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv4.max, opts, len,
5286 ovs_be32)) {
5287 return OFPERR_OFPBAC_BAD_ARGUMENT;
5288 }
5289 if (ntohl(nat->range.addr.ipv4.max)
5290 < ntohl(nat->range.addr.ipv4.min)) {
5291 return OFPERR_OFPBAC_BAD_ARGUMENT;
5292 }
5293 }
5294 } else if (range_present & NX_NAT_RANGE_IPV4_MAX) {
5295 return OFPERR_OFPBAC_BAD_ARGUMENT;
5296 } else if (range_present & NX_NAT_RANGE_IPV6_MIN) {
5297 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.min, opts, len,
5298 struct in6_addr)
5299 || ipv6_mask_is_any(&nat->range.addr.ipv6.min)) {
5300 return OFPERR_OFPBAC_BAD_ARGUMENT;
5301 }
5302
5303 nat->range_af = AF_INET6;
5304
5305 if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5306 if (!NX_NAT_GET_OPT(&nat->range.addr.ipv6.max, opts, len,
5307 struct in6_addr)) {
5308 return OFPERR_OFPBAC_BAD_ARGUMENT;
5309 }
5310 if (memcmp(&nat->range.addr.ipv6.max, &nat->range.addr.ipv6.min,
5311 sizeof(struct in6_addr)) < 0) {
5312 return OFPERR_OFPBAC_BAD_ARGUMENT;
5313 }
5314 }
5315 } else if (range_present & NX_NAT_RANGE_IPV6_MAX) {
5316 return OFPERR_OFPBAC_BAD_ARGUMENT;
5317 }
5318
5319 if (range_present & NX_NAT_RANGE_PROTO_MIN) {
5320 ovs_be16 proto;
5321
5322 if (nat->range_af == AF_UNSPEC) {
5323 return OFPERR_OFPBAC_BAD_ARGUMENT;
5324 }
5325 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16) || proto == 0) {
5326 return OFPERR_OFPBAC_BAD_ARGUMENT;
5327 }
5328 nat->range.proto.min = ntohs(proto);
5329 if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5330 if (!NX_NAT_GET_OPT(&proto, opts, len, ovs_be16)) {
5331 return OFPERR_OFPBAC_BAD_ARGUMENT;
5332 }
5333 nat->range.proto.max = ntohs(proto);
5334 if (nat->range.proto.max < nat->range.proto.min) {
5335 return OFPERR_OFPBAC_BAD_ARGUMENT;
5336 }
5337 }
5338 } else if (range_present & NX_NAT_RANGE_PROTO_MAX) {
5339 return OFPERR_OFPBAC_BAD_ARGUMENT;
5340 }
5341
5342 return 0;
5343}
5344
5345static void
5346format_NAT(const struct ofpact_nat *a, struct ds *ds)
5347{
b1c5bf1f 5348 ds_put_format(ds, "%snat%s", colors.paren, colors.end);
9ac0aada
JR
5349
5350 if (a->flags & (NX_NAT_F_SRC | NX_NAT_F_DST)) {
b1c5bf1f
QM
5351 ds_put_format(ds, "%s(%s", colors.paren, colors.end);
5352 ds_put_format(ds, a->flags & NX_NAT_F_SRC ? "%ssrc%s" : "%sdst%s",
5353 colors.param, colors.end);
9ac0aada
JR
5354
5355 if (a->range_af != AF_UNSPEC) {
b1c5bf1f 5356 ds_put_format(ds, "%s=%s", colors.param, colors.end);
9ac0aada
JR
5357
5358 if (a->range_af == AF_INET) {
5359 ds_put_format(ds, IP_FMT, IP_ARGS(a->range.addr.ipv4.min));
5360
5361 if (a->range.addr.ipv4.max
5362 && a->range.addr.ipv4.max != a->range.addr.ipv4.min) {
5363 ds_put_format(ds, "-"IP_FMT,
5364 IP_ARGS(a->range.addr.ipv4.max));
5365 }
5366 } else if (a->range_af == AF_INET6) {
5367 ipv6_format_addr_bracket(&a->range.addr.ipv6.min, ds,
5368 a->range.proto.min);
5369
5370 if (!ipv6_mask_is_any(&a->range.addr.ipv6.max)
5371 && memcmp(&a->range.addr.ipv6.max, &a->range.addr.ipv6.min,
5372 sizeof(struct in6_addr)) != 0) {
5373 ds_put_char(ds, '-');
5374 ipv6_format_addr_bracket(&a->range.addr.ipv6.max, ds,
5375 a->range.proto.min);
5376 }
5377 }
5378 if (a->range.proto.min) {
5379 ds_put_char(ds, ':');
5380 ds_put_format(ds, "%"PRIu16, a->range.proto.min);
5381
5382 if (a->range.proto.max
5383 && a->range.proto.max != a->range.proto.min) {
5384 ds_put_format(ds, "-%"PRIu16, a->range.proto.max);
5385 }
5386 }
5387 ds_put_char(ds, ',');
5388
5389 if (a->flags & NX_NAT_F_PERSISTENT) {
b1c5bf1f
QM
5390 ds_put_format(ds, "%spersistent%s,",
5391 colors.value, colors.end);
9ac0aada
JR
5392 }
5393 if (a->flags & NX_NAT_F_PROTO_HASH) {
b1c5bf1f 5394 ds_put_format(ds, "%shash%s,", colors.value, colors.end);
9ac0aada
JR
5395 }
5396 if (a->flags & NX_NAT_F_PROTO_RANDOM) {
b1c5bf1f 5397 ds_put_format(ds, "%srandom%s,", colors.value, colors.end);
9ac0aada
JR
5398 }
5399 }
5400 ds_chomp(ds, ',');
b1c5bf1f 5401 ds_put_format(ds, "%s)%s", colors.paren, colors.end);
9ac0aada
JR
5402 }
5403}
5404
5405static char * OVS_WARN_UNUSED_RESULT
5406str_to_nat_range(const char *s, struct ofpact_nat *on)
5407{
5408 char ipv6_s[IPV6_SCAN_LEN + 1];
5409 int n = 0;
5410
5411 on->range_af = AF_UNSPEC;
5412 if (ovs_scan_len(s, &n, IP_SCAN_FMT,
5413 IP_SCAN_ARGS(&on->range.addr.ipv4.min))) {
5414 on->range_af = AF_INET;
5415
5416 if (s[n] == '-') {
5417 n++;
5418 if (!ovs_scan_len(s, &n, IP_SCAN_FMT,
5419 IP_SCAN_ARGS(&on->range.addr.ipv4.max))
5420 || (ntohl(on->range.addr.ipv4.max)
5421 < ntohl(on->range.addr.ipv4.min))) {
5422 goto error;
5423 }
5424 }
5425 } else if ((ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5426 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5427 && inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.min) == 1) {
5428 on->range_af = AF_INET6;
5429
5430 if (s[n] == '-') {
5431 n++;
5432 if (!(ovs_scan_len(s, &n, IPV6_SCAN_FMT, ipv6_s)
5433 || ovs_scan_len(s, &n, "["IPV6_SCAN_FMT"]", ipv6_s))
5434 || inet_pton(AF_INET6, ipv6_s, &on->range.addr.ipv6.max) != 1
5435 || memcmp(&on->range.addr.ipv6.max, &on->range.addr.ipv6.min,
5436 sizeof on->range.addr.ipv6.max) < 0) {
5437 goto error;
5438 }
5439 }
5440 }
5441 if (on->range_af != AF_UNSPEC && s[n] == ':') {
5442 n++;
5443 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.min)) {
5444 goto error;
5445 }
5446 if (s[n] == '-') {
5447 n++;
5448 if (!ovs_scan_len(s, &n, "%"SCNu16, &on->range.proto.max)
5449 || on->range.proto.max < on->range.proto.min) {
5450 goto error;
5451 }
5452 }
5453 }
5454 if (strlen(s) != n) {
5455 return xasprintf("garbage (%s) after nat range \"%s\" (pos: %d)",
5456 &s[n], s, n);
5457 }
5458 return NULL;
5459error:
5460 return xasprintf("invalid nat range \"%s\"", s);
5461}
5462
5463
5464/* Parses 'arg' as the argument to a "nat" action, and appends such an
5465 * action to 'ofpacts'.
5466 *
5467 * Returns NULL if successful, otherwise a malloc()'d string describing the
5468 * error. The caller is responsible for freeing the returned string. */
5469static char * OVS_WARN_UNUSED_RESULT
5470parse_NAT(char *arg, struct ofpbuf *ofpacts,
5471 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5472{
5473 struct ofpact_nat *on = ofpact_put_NAT(ofpacts);
5474 char *key, *value;
5475
5476 on->flags = 0;
5477 on->range_af = AF_UNSPEC;
5478
5479 while (ofputil_parse_key_value(&arg, &key, &value)) {
5480 char *error = NULL;
5481
5482 if (!strcmp(key, "src")) {
5483 on->flags |= NX_NAT_F_SRC;
5484 error = str_to_nat_range(value, on);
5485 } else if (!strcmp(key, "dst")) {
5486 on->flags |= NX_NAT_F_DST;
5487 error = str_to_nat_range(value, on);
5488 } else if (!strcmp(key, "persistent")) {
5489 on->flags |= NX_NAT_F_PERSISTENT;
5490 } else if (!strcmp(key, "hash")) {
5491 on->flags |= NX_NAT_F_PROTO_HASH;
5492 } else if (!strcmp(key, "random")) {
5493 on->flags |= NX_NAT_F_PROTO_RANDOM;
5494 } else {
5495 error = xasprintf("invalid key \"%s\" in \"nat\" argument",
5496 key);
5497 }
5498 if (error) {
5499 return error;
5500 }
5501 }
5502 if (on->flags & NX_NAT_F_SRC && on->flags & NX_NAT_F_DST) {
5503 return xasprintf("May only specify one of \"snat\" or \"dnat\".");
5504 }
5505 if (!(on->flags & NX_NAT_F_SRC || on->flags & NX_NAT_F_DST)) {
5506 if (on->flags) {
5507 return xasprintf("Flags allowed only with \"snat\" or \"dnat\".");
5508 }
5509 if (on->range_af != AF_UNSPEC) {
5510 return xasprintf("Range allowed only with \"snat\" or \"dnat\".");
5511 }
5512 }
5513 return NULL;
5514}
5515
d4abaff5 5516\f
c2d936a4
BP
5517/* Meter instruction. */
5518
5519static void
5520encode_METER(const struct ofpact_meter *meter,
5521 enum ofp_version ofp_version, struct ofpbuf *out)
5522{
5523 if (ofp_version >= OFP13_VERSION) {
5524 instruction_put_OFPIT13_METER(out)->meter_id = htonl(meter->meter_id);
5525 }
5526}
5527
cab50449 5528static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
5529parse_METER(char *arg, struct ofpbuf *ofpacts,
5530 enum ofputil_protocol *usable_protocols)
5531{
5532 *usable_protocols &= OFPUTIL_P_OF13_UP;
5533 return str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
5534}
5535
5536static void
5537format_METER(const struct ofpact_meter *a, struct ds *s)
5538{
b1c5bf1f
QM
5539 ds_put_format(s, "%smeter:%s%"PRIu32,
5540 colors.param, colors.end, a->meter_id);
c2d936a4
BP
5541}
5542\f
5543/* Clear-Actions instruction. */
5544
5545static void
5546encode_CLEAR_ACTIONS(const struct ofpact_null *null OVS_UNUSED,
5547 enum ofp_version ofp_version OVS_UNUSED,
5548 struct ofpbuf *out OVS_UNUSED)
5549{
5550 if (ofp_version > OFP10_VERSION) {
5551 instruction_put_OFPIT11_CLEAR_ACTIONS(out);
5552 }
5553}
5554
cab50449 5555static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
5556parse_CLEAR_ACTIONS(char *arg OVS_UNUSED, struct ofpbuf *ofpacts,
5557 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5558{
5559 ofpact_put_CLEAR_ACTIONS(ofpacts);
5560 return NULL;
5561}
5562
5563static void
5564format_CLEAR_ACTIONS(const struct ofpact_null *a OVS_UNUSED, struct ds *s)
5565{
b1c5bf1f 5566 ds_put_format(s, "%sclear_actions%s", colors.value, colors.end);
c2d936a4
BP
5567}
5568\f
5569/* Write-Actions instruction. */
5570
5571static void
5572encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
5573 enum ofp_version ofp_version, struct ofpbuf *out)
5574{
5575 if (ofp_version > OFP10_VERSION) {
6fd6ed71 5576 const size_t ofs = out->size;
c2d936a4
BP
5577
5578 instruction_put_OFPIT11_WRITE_ACTIONS(out);
5579 ofpacts_put_openflow_actions(actions->actions,
5580 ofpact_nest_get_action_len(actions),
5581 out, ofp_version);
5582 ofpacts_update_instruction_actions(out, ofs);
5583 }
5584}
5585
cab50449 5586static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
5587parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
5588 enum ofputil_protocol *usable_protocols)
5589{
d824b5b7 5590 size_t ofs = ofpacts_pull(ofpacts);
c2d936a4
BP
5591 struct ofpact_nest *on;
5592 char *error;
c2d936a4
BP
5593
5594 /* Add a Write-Actions instruction and then pull it off. */
5595 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, sizeof *on);
5596 ofpbuf_pull(ofpacts, sizeof *on);
5597
5598 /* Parse nested actions.
5599 *
5600 * We pulled off "write-actions" and the previous actions because the
5601 * OFPACT_WRITE_ACTIONS is only partially constructed: its length is such
5602 * that it doesn't actually include the nested actions. That means that
5603 * ofpacts_parse() would reject them as being part of an Apply-Actions that
5604 * follows a Write-Actions, which is an invalid order. */
d824b5b7
JS
5605 error = ofpacts_parse(arg, ofpacts, usable_protocols, false,
5606 OFPACT_WRITE_ACTIONS);
c2d936a4
BP
5607
5608 /* Put the Write-Actions back on and update its length. */
5609 on = ofpbuf_push_uninit(ofpacts, sizeof *on);
6fd6ed71 5610 on->ofpact.len = ofpacts->size;
c2d936a4
BP
5611
5612 /* Put any previous actions or instructions back on. */
5613 ofpbuf_push_uninit(ofpacts, ofs);
5614
5615 return error;
5616}
5617
5618static void
5619format_WRITE_ACTIONS(const struct ofpact_nest *a, struct ds *s)
5620{
b1c5bf1f 5621 ds_put_format(s, "%swrite_actions(%s", colors.paren, colors.end);
c2d936a4 5622 ofpacts_format(a->actions, ofpact_nest_get_action_len(a), s);
b1c5bf1f 5623 ds_put_format(s, "%s)%s", colors.paren, colors.end);
c2d936a4
BP
5624}
5625\f
5626/* Action structure for NXAST_WRITE_METADATA.
5627 *
5628 * Modifies the 'mask' bits of the metadata value. */
5629struct nx_action_write_metadata {
5630 ovs_be16 type; /* OFPAT_VENDOR. */
5631 ovs_be16 len; /* Length is 32. */
5632 ovs_be32 vendor; /* NX_VENDOR_ID. */
5633 ovs_be16 subtype; /* NXAST_WRITE_METADATA. */
5634 uint8_t zeros[6]; /* Must be zero. */
5635 ovs_be64 metadata; /* Metadata register. */
5636 ovs_be64 mask; /* Metadata mask. */
5637};
5638OFP_ASSERT(sizeof(struct nx_action_write_metadata) == 32);
5639
5640static enum ofperr
5641decode_NXAST_RAW_WRITE_METADATA(const struct nx_action_write_metadata *nawm,
f3cd3ac7 5642 enum ofp_version ofp_version OVS_UNUSED,
c2d936a4
BP
5643 struct ofpbuf *out)
5644{
5645 struct ofpact_metadata *om;
5646
5647 if (!is_all_zeros(nawm->zeros, sizeof nawm->zeros)) {
5648 return OFPERR_NXBRC_MUST_BE_ZERO;
5649 }
5650
5651 om = ofpact_put_WRITE_METADATA(out);
5652 om->metadata = nawm->metadata;
5653 om->mask = nawm->mask;
5654
5655 return 0;
5656}
5657
5658static void
5659encode_WRITE_METADATA(const struct ofpact_metadata *metadata,
5660 enum ofp_version ofp_version, struct ofpbuf *out)
5661{
5662 if (ofp_version == OFP10_VERSION) {
5663 struct nx_action_write_metadata *nawm;
5664
5665 nawm = put_NXAST_WRITE_METADATA(out);
5666 nawm->metadata = metadata->metadata;
5667 nawm->mask = metadata->mask;
5668 } else {
5669 struct ofp11_instruction_write_metadata *oiwm;
5670
5671 oiwm = instruction_put_OFPIT11_WRITE_METADATA(out);
5672 oiwm->metadata = metadata->metadata;
5673 oiwm->metadata_mask = metadata->mask;
5674 }
5675}
5676
cab50449 5677static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
5678parse_WRITE_METADATA(char *arg, struct ofpbuf *ofpacts,
5679 enum ofputil_protocol *usable_protocols)
5680{
5681 struct ofpact_metadata *om;
5682 char *mask = strchr(arg, '/');
5683
5684 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
5685
5686 om = ofpact_put_WRITE_METADATA(ofpacts);
5687 if (mask) {
5688 char *error;
5689
5690 *mask = '\0';
5691 error = str_to_be64(mask + 1, &om->mask);
5692 if (error) {
5693 return error;
5694 }
5695 } else {
5696 om->mask = OVS_BE64_MAX;
5697 }
5698
5699 return str_to_be64(arg, &om->metadata);
5700}
5701
5702static void
5703format_WRITE_METADATA(const struct ofpact_metadata *a, struct ds *s)
5704{
b1c5bf1f
QM
5705 ds_put_format(s, "%swrite_metadata:%s%#"PRIx64,
5706 colors.param, colors.end, ntohll(a->metadata));
c2d936a4
BP
5707 if (a->mask != OVS_BE64_MAX) {
5708 ds_put_format(s, "/%#"PRIx64, ntohll(a->mask));
5709 }
4cceacb9 5710}
f25d0cf3 5711\f
c2d936a4 5712/* Goto-Table instruction. */
f25d0cf3
BP
5713
5714static void
c2d936a4
BP
5715encode_GOTO_TABLE(const struct ofpact_goto_table *goto_table,
5716 enum ofp_version ofp_version, struct ofpbuf *out)
f25d0cf3 5717{
c2d936a4
BP
5718 if (ofp_version == OFP10_VERSION) {
5719 struct nx_action_resubmit *nar;
f25d0cf3 5720
c2d936a4
BP
5721 nar = put_NXAST_RESUBMIT_TABLE(out);
5722 nar->table = goto_table->table_id;
5723 nar->in_port = htons(ofp_to_u16(OFPP_IN_PORT));
5724 } else {
5725 struct ofp11_instruction_goto_table *oigt;
5726
5727 oigt = instruction_put_OFPIT11_GOTO_TABLE(out);
5728 oigt->table_id = goto_table->table_id;
5729 memset(oigt->pad, 0, sizeof oigt->pad);
5730 }
5731}
5732
cab50449 5733static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
5734parse_GOTO_TABLE(char *arg, struct ofpbuf *ofpacts,
5735 enum ofputil_protocol *usable_protocols OVS_UNUSED)
5736{
5737 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
5738 char *table_s = strsep(&arg, ",");
5739 if (!table_s || !table_s[0]) {
5740 return xstrdup("instruction goto-table needs table id");
5741 }
5742 return str_to_u8(table_s, "table", &ogt->table_id);
5743}
5744
5745static void
5746format_GOTO_TABLE(const struct ofpact_goto_table *a, struct ds *s)
5747{
b1c5bf1f
QM
5748 ds_put_format(s, "%sgoto_table:%s%"PRIu8,
5749 colors.param, colors.end, a->table_id);
c2d936a4
BP
5750}
5751\f
5752static void
5ad4b3f8 5753log_bad_action(const struct ofp_action_header *actions, size_t actions_len,
c2d936a4
BP
5754 const struct ofp_action_header *bad_action, enum ofperr error)
5755{
5756 if (!VLOG_DROP_WARN(&rl)) {
5757 struct ds s;
5758
5759 ds_init(&s);
5ad4b3f8 5760 ds_put_hex_dump(&s, actions, actions_len, 0, false);
c2d936a4
BP
5761 VLOG_WARN("bad action at offset %#"PRIxPTR" (%s):\n%s",
5762 (char *)bad_action - (char *)actions,
5763 ofperr_get_name(error), ds_cstr(&s));
5764 ds_destroy(&s);
5765 }
5766}
5767
5768static enum ofperr
5769ofpacts_decode(const void *actions, size_t actions_len,
5770 enum ofp_version ofp_version, struct ofpbuf *ofpacts)
5771{
0a2869d5 5772 struct ofpbuf openflow = ofpbuf_const_initializer(actions, actions_len);
6fd6ed71
PS
5773 while (openflow.size) {
5774 const struct ofp_action_header *action = openflow.data;
c2d936a4
BP
5775 enum ofp_raw_action_type raw;
5776 enum ofperr error;
5777 uint64_t arg;
5778
5779 error = ofpact_pull_raw(&openflow, ofp_version, &raw, &arg);
5780 if (!error) {
f3cd3ac7 5781 error = ofpact_decode(action, raw, ofp_version, arg, ofpacts);
c2d936a4
BP
5782 }
5783
5784 if (error) {
5ad4b3f8 5785 log_bad_action(actions, actions_len, action, error);
c2d936a4
BP
5786 return error;
5787 }
5788 }
c2d936a4
BP
5789 return 0;
5790}
5791
5792static enum ofperr
5793ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
5794 unsigned int actions_len,
5795 enum ofp_version version,
5796 uint32_t allowed_ovsinsts,
d824b5b7
JS
5797 struct ofpbuf *ofpacts,
5798 enum ofpact_type outer_action)
c2d936a4
BP
5799{
5800 const struct ofp_action_header *actions;
9abca1e5 5801 size_t orig_size = ofpacts->size;
c2d936a4
BP
5802 enum ofperr error;
5803
c2d936a4
BP
5804 if (actions_len % OFP_ACTION_ALIGN != 0) {
5805 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
5806 "multiple of %d", actions_len, OFP_ACTION_ALIGN);
5807 return OFPERR_OFPBRC_BAD_LEN;
5808 }
5809
5810 actions = ofpbuf_try_pull(openflow, actions_len);
5811 if (actions == NULL) {
5812 VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
5813 "remaining message length (%"PRIu32")",
6fd6ed71 5814 actions_len, openflow->size);
c2d936a4
BP
5815 return OFPERR_OFPBRC_BAD_LEN;
5816 }
5817
5818 error = ofpacts_decode(actions, actions_len, version, ofpacts);
5819 if (error) {
9abca1e5 5820 ofpacts->size = orig_size;
c2d936a4
BP
5821 return error;
5822 }
5823
d824b5b7
JS
5824 error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
5825 outer_action);
c2d936a4 5826 if (error) {
9abca1e5 5827 ofpacts->size = orig_size;
c2d936a4
BP
5828 }
5829 return error;
5830}
5831
9abca1e5
BP
5832/* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
5833 * of 'openflow' into ofpacts. On success, appends the converted actions to
5834 * 'ofpacts'; on failure, 'ofpacts' is unchanged (but might be reallocated) .
c2d936a4
BP
5835 * Returns 0 if successful, otherwise an OpenFlow error.
5836 *
5837 * Actions are processed according to their OpenFlow version which
5838 * is provided in the 'version' parameter.
5839 *
5840 * In most places in OpenFlow, actions appear encapsulated in instructions, so
5841 * you should call ofpacts_pull_openflow_instructions() instead of this
5842 * function.
5843 *
5844 * The parsed actions are valid generically, but they may not be valid in a
5845 * specific context. For example, port numbers up to OFPP_MAX are valid
5846 * generically, but specific datapaths may only support port numbers in a
5847 * smaller range. Use ofpacts_check() to additional check whether actions are
5848 * valid in a specific context. */
5849enum ofperr
5850ofpacts_pull_openflow_actions(struct ofpbuf *openflow,
5851 unsigned int actions_len,
5852 enum ofp_version version,
5853 struct ofpbuf *ofpacts)
5854{
5855 return ofpacts_pull_openflow_actions__(openflow, actions_len, version,
5856 1u << OVSINST_OFPIT11_APPLY_ACTIONS,
d824b5b7 5857 ofpacts, 0);
c2d936a4
BP
5858}
5859\f
5860/* OpenFlow 1.1 actions. */
5861
5862
5863/* True if an action sets the value of a field
5864 * in a way that is compatibile with the action set.
1b0ee636 5865 * The field can be set via either a set or a move action.
c2d936a4
BP
5866 * False otherwise. */
5867static bool
1b0ee636 5868ofpact_is_set_or_move_action(const struct ofpact *a)
c2d936a4
BP
5869{
5870 switch (a->type) {
5871 case OFPACT_SET_FIELD:
1b0ee636 5872 case OFPACT_REG_MOVE:
c2d936a4
BP
5873 case OFPACT_SET_ETH_DST:
5874 case OFPACT_SET_ETH_SRC:
5875 case OFPACT_SET_IP_DSCP:
5876 case OFPACT_SET_IP_ECN:
5877 case OFPACT_SET_IP_TTL:
5878 case OFPACT_SET_IPV4_DST:
5879 case OFPACT_SET_IPV4_SRC:
5880 case OFPACT_SET_L4_DST_PORT:
5881 case OFPACT_SET_L4_SRC_PORT:
5882 case OFPACT_SET_MPLS_LABEL:
5883 case OFPACT_SET_MPLS_TC:
5884 case OFPACT_SET_MPLS_TTL:
5885 case OFPACT_SET_QUEUE:
5886 case OFPACT_SET_TUNNEL:
5887 case OFPACT_SET_VLAN_PCP:
5888 case OFPACT_SET_VLAN_VID:
5889 return true;
5890 case OFPACT_BUNDLE:
5891 case OFPACT_CLEAR_ACTIONS:
07659514 5892 case OFPACT_CT:
9ac0aada 5893 case OFPACT_NAT:
c2d936a4
BP
5894 case OFPACT_CONTROLLER:
5895 case OFPACT_DEC_MPLS_TTL:
5896 case OFPACT_DEC_TTL:
5897 case OFPACT_ENQUEUE:
5898 case OFPACT_EXIT:
e672ff9b 5899 case OFPACT_UNROLL_XLATE:
c2d936a4
BP
5900 case OFPACT_FIN_TIMEOUT:
5901 case OFPACT_GOTO_TABLE:
5902 case OFPACT_GROUP:
5903 case OFPACT_LEARN:
18080541 5904 case OFPACT_CONJUNCTION:
c2d936a4
BP
5905 case OFPACT_METER:
5906 case OFPACT_MULTIPATH:
5907 case OFPACT_NOTE:
5908 case OFPACT_OUTPUT:
5909 case OFPACT_OUTPUT_REG:
5910 case OFPACT_POP_MPLS:
5911 case OFPACT_POP_QUEUE:
5912 case OFPACT_PUSH_MPLS:
5913 case OFPACT_PUSH_VLAN:
c2d936a4
BP
5914 case OFPACT_RESUBMIT:
5915 case OFPACT_SAMPLE:
5916 case OFPACT_STACK_POP:
5917 case OFPACT_STACK_PUSH:
5918 case OFPACT_STRIP_VLAN:
5919 case OFPACT_WRITE_ACTIONS:
5920 case OFPACT_WRITE_METADATA:
d4abaff5 5921 case OFPACT_DEBUG_RECIRC:
c2d936a4
BP
5922 return false;
5923 default:
5924 OVS_NOT_REACHED();
5925 }
5926}
5927
5928/* True if an action is allowed in the action set.
5929 * False otherwise. */
5930static bool
5931ofpact_is_allowed_in_actions_set(const struct ofpact *a)
5932{
5933 switch (a->type) {
5934 case OFPACT_DEC_MPLS_TTL:
5935 case OFPACT_DEC_TTL:
5936 case OFPACT_GROUP:
5937 case OFPACT_OUTPUT:
5938 case OFPACT_POP_MPLS:
5939 case OFPACT_PUSH_MPLS:
5940 case OFPACT_PUSH_VLAN:
1b0ee636 5941 case OFPACT_REG_MOVE:
c2d936a4
BP
5942 case OFPACT_SET_FIELD:
5943 case OFPACT_SET_ETH_DST:
5944 case OFPACT_SET_ETH_SRC:
5945 case OFPACT_SET_IP_DSCP:
5946 case OFPACT_SET_IP_ECN:
5947 case OFPACT_SET_IP_TTL:
5948 case OFPACT_SET_IPV4_DST:
5949 case OFPACT_SET_IPV4_SRC:
5950 case OFPACT_SET_L4_DST_PORT:
5951 case OFPACT_SET_L4_SRC_PORT:
5952 case OFPACT_SET_MPLS_LABEL:
5953 case OFPACT_SET_MPLS_TC:
5954 case OFPACT_SET_MPLS_TTL:
5955 case OFPACT_SET_QUEUE:
5956 case OFPACT_SET_TUNNEL:
5957 case OFPACT_SET_VLAN_PCP:
5958 case OFPACT_SET_VLAN_VID:
5959 case OFPACT_STRIP_VLAN:
5960 return true;
5961
5962 /* In general these actions are excluded because they are not part of
5963 * the OpenFlow specification nor map to actions that are defined in
5964 * the specification. Thus the order in which they should be applied
5965 * in the action set is undefined. */
5966 case OFPACT_BUNDLE:
5967 case OFPACT_CONTROLLER:
07659514 5968 case OFPACT_CT:
9ac0aada 5969 case OFPACT_NAT:
c2d936a4
BP
5970 case OFPACT_ENQUEUE:
5971 case OFPACT_EXIT:
e672ff9b 5972 case OFPACT_UNROLL_XLATE:
c2d936a4
BP
5973 case OFPACT_FIN_TIMEOUT:
5974 case OFPACT_LEARN:
18080541 5975 case OFPACT_CONJUNCTION:
c2d936a4
BP
5976 case OFPACT_MULTIPATH:
5977 case OFPACT_NOTE:
5978 case OFPACT_OUTPUT_REG:
5979 case OFPACT_POP_QUEUE:
c2d936a4
BP
5980 case OFPACT_RESUBMIT:
5981 case OFPACT_SAMPLE:
5982 case OFPACT_STACK_POP:
5983 case OFPACT_STACK_PUSH:
d4abaff5 5984 case OFPACT_DEBUG_RECIRC:
c2d936a4
BP
5985
5986 /* The action set may only include actions and thus
5987 * may not include any instructions */
5988 case OFPACT_CLEAR_ACTIONS:
5989 case OFPACT_GOTO_TABLE:
5990 case OFPACT_METER:
5991 case OFPACT_WRITE_ACTIONS:
5992 case OFPACT_WRITE_METADATA:
5993 return false;
5994 default:
5995 OVS_NOT_REACHED();
5996 }
f25d0cf3
BP
5997}
5998
c2d936a4 5999/* Append ofpact 'a' onto the tail of 'out' */
f25d0cf3 6000static void
c2d936a4 6001ofpact_copy(struct ofpbuf *out, const struct ofpact *a)
f25d0cf3 6002{
c2d936a4 6003 ofpbuf_put(out, a, OFPACT_ALIGN(a->len));
f25d0cf3
BP
6004}
6005
c2d936a4
BP
6006/* Copies the last ofpact whose type is 'filter' from 'in' to 'out'. */
6007static bool
6008ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
6009 enum ofpact_type filter)
f25d0cf3 6010{
c2d936a4
BP
6011 const struct ofpact *target;
6012 const struct ofpact *a;
f25d0cf3 6013
c2d936a4 6014 target = NULL;
6fd6ed71 6015 OFPACT_FOR_EACH (a, in->data, in->size) {
c2d936a4
BP
6016 if (a->type == filter) {
6017 target = a;
6018 }
6019 }
6020 if (target) {
6021 ofpact_copy(out, target);
f25d0cf3 6022 }
c2d936a4 6023 return target != NULL;
f25d0cf3
BP
6024}
6025
c2d936a4
BP
6026/* Append all ofpacts, for which 'filter' returns true, from 'in' to 'out'.
6027 * The order of appended ofpacts is preserved between 'in' and 'out' */
4cceacb9 6028static void
c2d936a4
BP
6029ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
6030 bool (*filter)(const struct ofpact *))
4cceacb9 6031{
c2d936a4 6032 const struct ofpact *a;
4cceacb9 6033
6fd6ed71 6034 OFPACT_FOR_EACH (a, in->data, in->size) {
c2d936a4
BP
6035 if (filter(a)) {
6036 ofpact_copy(out, a);
6037 }
6038 }
4cceacb9
JS
6039}
6040
c2d936a4
BP
6041/* Reads 'action_set', which contains ofpacts accumulated by
6042 * OFPACT_WRITE_ACTIONS instructions, and writes equivalent actions to be
6043 * executed directly into 'action_list'. (These names correspond to the
6044 * "Action Set" and "Action List" terms used in OpenFlow 1.1+.)
6045 *
6046 * In general this involves appending the last instance of each action that is
e672ff9b 6047 * admissible in the action set in the order described in the OpenFlow
c2d936a4
BP
6048 * specification.
6049 *
6050 * Exceptions:
6051 * + output action is only appended if no group action was present in 'in'.
6052 * + As a simplification all set actions are copied in the order the are
6053 * provided in 'in' as many set actions applied to a field has the same
6054 * affect as only applying the last action that sets a field and
6055 * duplicates are removed by do_xlate_actions().
6056 * This has an unwanted side-effect of compsoting multiple
6057 * LOAD_REG actions that touch different regions of the same field. */
6058void
6059ofpacts_execute_action_set(struct ofpbuf *action_list,
6060 const struct ofpbuf *action_set)
f25d0cf3 6061{
c2d936a4
BP
6062 /* The OpenFlow spec "Action Set" section specifies this order. */
6063 ofpacts_copy_last(action_list, action_set, OFPACT_STRIP_VLAN);
6064 ofpacts_copy_last(action_list, action_set, OFPACT_POP_MPLS);
6065 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_MPLS);
6066 ofpacts_copy_last(action_list, action_set, OFPACT_PUSH_VLAN);
6067 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_TTL);
6068 ofpacts_copy_last(action_list, action_set, OFPACT_DEC_MPLS_TTL);
1b0ee636 6069 ofpacts_copy_all(action_list, action_set, ofpact_is_set_or_move_action);
c2d936a4 6070 ofpacts_copy_last(action_list, action_set, OFPACT_SET_QUEUE);
f25d0cf3 6071
c2d936a4
BP
6072 /* If both OFPACT_GROUP and OFPACT_OUTPUT are present, OpenFlow says that
6073 * we should execute only OFPACT_GROUP.
6074 *
6075 * If neither OFPACT_GROUP nor OFPACT_OUTPUT is present, then we can drop
6076 * all the actions because there's no point in modifying a packet that will
6077 * not be sent anywhere. */
6078 if (!ofpacts_copy_last(action_list, action_set, OFPACT_GROUP) &&
2e34a6a3
SS
6079 !ofpacts_copy_last(action_list, action_set, OFPACT_OUTPUT) &&
6080 !ofpacts_copy_last(action_list, action_set, OFPACT_RESUBMIT)) {
c2d936a4 6081 ofpbuf_clear(action_list);
f25d0cf3 6082 }
f25d0cf3
BP
6083}
6084
f25d0cf3 6085
c2d936a4
BP
6086static enum ofperr
6087ofpacts_decode_for_action_set(const struct ofp_action_header *in,
6088 size_t n_in, enum ofp_version version,
6089 struct ofpbuf *out)
c2d967a5 6090{
c2d936a4
BP
6091 enum ofperr error;
6092 struct ofpact *a;
6fd6ed71 6093 size_t start = out->size;
c2d967a5 6094
c2d936a4 6095 error = ofpacts_decode(in, n_in, version, out);
c2d967a5 6096
c2d936a4
BP
6097 if (error) {
6098 return error;
6099 }
6100
6fd6ed71 6101 OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
c2d936a4
BP
6102 if (!ofpact_is_allowed_in_actions_set(a)) {
6103 VLOG_WARN_RL(&rl, "disallowed action in action set");
6104 return OFPERR_OFPBAC_BAD_TYPE;
c2d967a5
MM
6105 }
6106 }
c2d936a4
BP
6107
6108 return 0;
c2d967a5 6109}
c2d936a4
BP
6110\f
6111/* OpenFlow 1.1 instructions. */
c2d967a5 6112
c2d936a4
BP
6113struct instruction_type_info {
6114 enum ovs_instruction_type type;
6115 const char *name;
6116};
6117
6118static const struct instruction_type_info inst_info[] = {
6119#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) {OVSINST_##ENUM, NAME},
6120OVS_INSTRUCTIONS
6121#undef DEFINE_INST
6122};
6123
6124const char *
6125ovs_instruction_name_from_type(enum ovs_instruction_type type)
f25d0cf3 6126{
c2d936a4 6127 return inst_info[type].name;
f25d0cf3
BP
6128}
6129
c2d936a4
BP
6130int
6131ovs_instruction_type_from_name(const char *name)
29089a54 6132{
c2d936a4
BP
6133 const struct instruction_type_info *p;
6134 for (p = inst_info; p < &inst_info[ARRAY_SIZE(inst_info)]; p++) {
6135 if (!strcasecmp(name, p->name)) {
6136 return p->type;
6137 }
6138 }
6139 return -1;
29089a54
RL
6140}
6141
c2d936a4
BP
6142enum ovs_instruction_type
6143ovs_instruction_type_from_ofpact_type(enum ofpact_type type)
f25d0cf3 6144{
c2d936a4
BP
6145 switch (type) {
6146 case OFPACT_METER:
6147 return OVSINST_OFPIT13_METER;
6148 case OFPACT_CLEAR_ACTIONS:
6149 return OVSINST_OFPIT11_CLEAR_ACTIONS;
6150 case OFPACT_WRITE_ACTIONS:
6151 return OVSINST_OFPIT11_WRITE_ACTIONS;
6152 case OFPACT_WRITE_METADATA:
6153 return OVSINST_OFPIT11_WRITE_METADATA;
6154 case OFPACT_GOTO_TABLE:
6155 return OVSINST_OFPIT11_GOTO_TABLE;
6156 case OFPACT_OUTPUT:
6157 case OFPACT_GROUP:
f25d0cf3 6158 case OFPACT_CONTROLLER:
c2d936a4 6159 case OFPACT_ENQUEUE:
f25d0cf3 6160 case OFPACT_OUTPUT_REG:
f25d0cf3 6161 case OFPACT_BUNDLE:
c2d936a4
BP
6162 case OFPACT_SET_VLAN_VID:
6163 case OFPACT_SET_VLAN_PCP:
6164 case OFPACT_STRIP_VLAN:
6165 case OFPACT_PUSH_VLAN:
6166 case OFPACT_SET_ETH_SRC:
6167 case OFPACT_SET_ETH_DST:
6168 case OFPACT_SET_IPV4_SRC:
6169 case OFPACT_SET_IPV4_DST:
6170 case OFPACT_SET_IP_DSCP:
6171 case OFPACT_SET_IP_ECN:
6172 case OFPACT_SET_IP_TTL:
6173 case OFPACT_SET_L4_SRC_PORT:
6174 case OFPACT_SET_L4_DST_PORT:
f25d0cf3 6175 case OFPACT_REG_MOVE:
c2d936a4 6176 case OFPACT_SET_FIELD:
bd85dac1 6177 case OFPACT_STACK_PUSH:
bd85dac1 6178 case OFPACT_STACK_POP:
f25d0cf3 6179 case OFPACT_DEC_TTL:
097d4939 6180 case OFPACT_SET_MPLS_LABEL:
097d4939 6181 case OFPACT_SET_MPLS_TC:
0f3f3c3d 6182 case OFPACT_SET_MPLS_TTL:
b676167a 6183 case OFPACT_DEC_MPLS_TTL:
c2d936a4
BP
6184 case OFPACT_PUSH_MPLS:
6185 case OFPACT_POP_MPLS:
f25d0cf3 6186 case OFPACT_SET_TUNNEL:
c2d936a4
BP
6187 case OFPACT_SET_QUEUE:
6188 case OFPACT_POP_QUEUE:
6189 case OFPACT_FIN_TIMEOUT:
6190 case OFPACT_RESUBMIT:
6191 case OFPACT_LEARN:
18080541 6192 case OFPACT_CONJUNCTION:
c2d936a4
BP
6193 case OFPACT_MULTIPATH:
6194 case OFPACT_NOTE:
6195 case OFPACT_EXIT:
e672ff9b 6196 case OFPACT_UNROLL_XLATE:
c2d936a4 6197 case OFPACT_SAMPLE:
d4abaff5 6198 case OFPACT_DEBUG_RECIRC:
07659514 6199 case OFPACT_CT:
9ac0aada 6200 case OFPACT_NAT:
c2d936a4
BP
6201 default:
6202 return OVSINST_OFPIT11_APPLY_ACTIONS;
6203 }
6204}
6205
6206enum ofperr
6207ovs_instruction_type_from_inst_type(enum ovs_instruction_type *instruction_type,
6208 const uint16_t inst_type)
6209{
6210 switch (inst_type) {
6211
6212#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6213 case ENUM: \
6214 *instruction_type = OVSINST_##ENUM; \
6215 return 0;
6216OVS_INSTRUCTIONS
6217#undef DEFINE_INST
6218
6219 default:
6220 return OFPERR_OFPBIC_UNKNOWN_INST;
6221 }
6222}
6223
6224/* Two-way translation between OVS's internal "OVSINST_*" representation of
6225 * instructions and the "OFPIT_*" representation used in OpenFlow. */
6226struct ovsinst_map {
6227 enum ovs_instruction_type ovsinst; /* Internal name for instruction. */
6228 int ofpit; /* OFPIT_* number from OpenFlow spec. */
6229};
6230
6231static const struct ovsinst_map *
6232get_ovsinst_map(enum ofp_version version)
6233{
6234 /* OpenFlow 1.1 and 1.2 instructions. */
6235 static const struct ovsinst_map of11[] = {
6236 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6237 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6238 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6239 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6240 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6241 { 0, -1 },
6242 };
f25d0cf3 6243
c2d936a4
BP
6244 /* OpenFlow 1.3+ instructions. */
6245 static const struct ovsinst_map of13[] = {
6246 { OVSINST_OFPIT11_GOTO_TABLE, 1 },
6247 { OVSINST_OFPIT11_WRITE_METADATA, 2 },
6248 { OVSINST_OFPIT11_WRITE_ACTIONS, 3 },
6249 { OVSINST_OFPIT11_APPLY_ACTIONS, 4 },
6250 { OVSINST_OFPIT11_CLEAR_ACTIONS, 5 },
6251 { OVSINST_OFPIT13_METER, 6 },
6252 { 0, -1 },
6253 };
4cceacb9 6254
c2d936a4
BP
6255 return version < OFP13_VERSION ? of11 : of13;
6256}
f25d0cf3 6257
c2d936a4
BP
6258/* Converts 'ovsinst_bitmap', a bitmap whose bits correspond to OVSINST_*
6259 * values, into a bitmap of instructions suitable for OpenFlow 'version'
6260 * (OFP11_VERSION or later), and returns the result. */
6261ovs_be32
6262ovsinst_bitmap_to_openflow(uint32_t ovsinst_bitmap, enum ofp_version version)
6263{
6264 uint32_t ofpit_bitmap = 0;
6265 const struct ovsinst_map *x;
f25d0cf3 6266
c2d936a4
BP
6267 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6268 if (ovsinst_bitmap & (1u << x->ovsinst)) {
6269 ofpit_bitmap |= 1u << x->ofpit;
6270 }
6271 }
6272 return htonl(ofpit_bitmap);
6273}
f25d0cf3 6274
c2d936a4
BP
6275/* Converts 'ofpit_bitmap', a bitmap of instructions from an OpenFlow message
6276 * with the given 'version' (OFP11_VERSION or later) into a bitmap whose bits
6277 * correspond to OVSINST_* values, and returns the result. */
6278uint32_t
6279ovsinst_bitmap_from_openflow(ovs_be32 ofpit_bitmap, enum ofp_version version)
6280{
6281 uint32_t ovsinst_bitmap = 0;
6282 const struct ovsinst_map *x;
f25d0cf3 6283
c2d936a4
BP
6284 for (x = get_ovsinst_map(version); x->ofpit >= 0; x++) {
6285 if (ofpit_bitmap & htonl(1u << x->ofpit)) {
6286 ovsinst_bitmap |= 1u << x->ovsinst;
6287 }
6288 }
6289 return ovsinst_bitmap;
6290}
f25d0cf3 6291
c2d936a4
BP
6292static inline struct ofp11_instruction *
6293instruction_next(const struct ofp11_instruction *inst)
6294{
6295 return ((struct ofp11_instruction *) (void *)
6296 ((uint8_t *) inst + ntohs(inst->len)));
6297}
f25d0cf3 6298
c2d936a4
BP
6299static inline bool
6300instruction_is_valid(const struct ofp11_instruction *inst,
6301 size_t n_instructions)
6302{
6303 uint16_t len = ntohs(inst->len);
6304 return (!(len % OFP11_INSTRUCTION_ALIGN)
6305 && len >= sizeof *inst
6306 && len / sizeof *inst <= n_instructions);
6307}
f25d0cf3 6308
c2d936a4
BP
6309/* This macro is careful to check for instructions with bad lengths. */
6310#define INSTRUCTION_FOR_EACH(ITER, LEFT, INSTRUCTIONS, N_INSTRUCTIONS) \
6311 for ((ITER) = (INSTRUCTIONS), (LEFT) = (N_INSTRUCTIONS); \
6312 (LEFT) > 0 && instruction_is_valid(ITER, LEFT); \
6313 ((LEFT) -= (ntohs((ITER)->len) \
6314 / sizeof(struct ofp11_instruction)), \
6315 (ITER) = instruction_next(ITER)))
f25d0cf3 6316
c2d936a4
BP
6317static enum ofperr
6318decode_openflow11_instruction(const struct ofp11_instruction *inst,
6319 enum ovs_instruction_type *type)
6320{
6321 uint16_t len = ntohs(inst->len);
b02475c5 6322
c2d936a4
BP
6323 switch (inst->type) {
6324 case CONSTANT_HTONS(OFPIT11_EXPERIMENTER):
6325 return OFPERR_OFPBIC_BAD_EXPERIMENTER;
b02475c5 6326
c2d936a4
BP
6327#define DEFINE_INST(ENUM, STRUCT, EXTENSIBLE, NAME) \
6328 case CONSTANT_HTONS(ENUM): \
6329 if (EXTENSIBLE \
6330 ? len >= sizeof(struct STRUCT) \
6331 : len == sizeof(struct STRUCT)) { \
6332 *type = OVSINST_##ENUM; \
6333 return 0; \
6334 } else { \
6335 return OFPERR_OFPBIC_BAD_LEN; \
6336 }
6337OVS_INSTRUCTIONS
6338#undef DEFINE_INST
29089a54 6339
c2d936a4
BP
6340 default:
6341 return OFPERR_OFPBIC_UNKNOWN_INST;
f25d0cf3
BP
6342 }
6343}
f25d0cf3 6344
c2d936a4
BP
6345static enum ofperr
6346decode_openflow11_instructions(const struct ofp11_instruction insts[],
6347 size_t n_insts,
6348 const struct ofp11_instruction *out[])
f25d0cf3 6349{
c2d936a4
BP
6350 const struct ofp11_instruction *inst;
6351 size_t left;
f25d0cf3 6352
c2d936a4
BP
6353 memset(out, 0, N_OVS_INSTRUCTIONS * sizeof *out);
6354 INSTRUCTION_FOR_EACH (inst, left, insts, n_insts) {
6355 enum ovs_instruction_type type;
6356 enum ofperr error;
f25d0cf3 6357
c2d936a4
BP
6358 error = decode_openflow11_instruction(inst, &type);
6359 if (error) {
6360 return error;
6361 }
f25d0cf3 6362
c2d936a4
BP
6363 if (out[type]) {
6364 return OFPERR_OFPBIC_DUP_INST;
6365 }
6366 out[type] = inst;
6367 }
6368
6369 if (left) {
6370 VLOG_WARN_RL(&rl, "bad instruction format at offset %"PRIuSIZE,
6371 (n_insts - left) * sizeof *inst);
6372 return OFPERR_OFPBIC_BAD_LEN;
6373 }
6374 return 0;
f25d0cf3
BP
6375}
6376
6377static void
c2d936a4
BP
6378get_actions_from_instruction(const struct ofp11_instruction *inst,
6379 const struct ofp_action_header **actions,
6380 size_t *actions_len)
f25d0cf3 6381{
c2d936a4
BP
6382 *actions = ALIGNED_CAST(const struct ofp_action_header *, inst + 1);
6383 *actions_len = ntohs(inst->len) - sizeof *inst;
6384}
f25d0cf3 6385
c2d936a4
BP
6386enum ofperr
6387ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
6388 unsigned int instructions_len,
6389 enum ofp_version version,
6390 struct ofpbuf *ofpacts)
6391{
6392 const struct ofp11_instruction *instructions;
6393 const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
6394 enum ofperr error;
f25d0cf3 6395
9abca1e5 6396 ofpbuf_clear(ofpacts);
c2d936a4
BP
6397 if (version == OFP10_VERSION) {
6398 return ofpacts_pull_openflow_actions__(openflow, instructions_len,
6399 version,
6400 (1u << N_OVS_INSTRUCTIONS) - 1,
d824b5b7 6401 ofpacts, 0);
c2d936a4 6402 }
f25d0cf3 6403
c2d936a4
BP
6404 if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
6405 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
6406 "multiple of %d",
6407 instructions_len, OFP11_INSTRUCTION_ALIGN);
6408 error = OFPERR_OFPBIC_BAD_LEN;
6409 goto exit;
6410 }
f25d0cf3 6411
c2d936a4
BP
6412 instructions = ofpbuf_try_pull(openflow, instructions_len);
6413 if (instructions == NULL) {
6414 VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
6415 "remaining message length (%"PRIu32")",
6fd6ed71 6416 instructions_len, openflow->size);
c2d936a4
BP
6417 error = OFPERR_OFPBIC_BAD_LEN;
6418 goto exit;
6419 }
f25d0cf3 6420
c2d936a4
BP
6421 error = decode_openflow11_instructions(
6422 instructions, instructions_len / OFP11_INSTRUCTION_ALIGN,
6423 insts);
6424 if (error) {
6425 goto exit;
6426 }
f25d0cf3 6427
c2d936a4
BP
6428 if (insts[OVSINST_OFPIT13_METER]) {
6429 const struct ofp13_instruction_meter *oim;
6430 struct ofpact_meter *om;
f25d0cf3 6431
c2d936a4
BP
6432 oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
6433 insts[OVSINST_OFPIT13_METER]);
f25d0cf3 6434
c2d936a4
BP
6435 om = ofpact_put_METER(ofpacts);
6436 om->meter_id = ntohl(oim->meter_id);
6437 }
6438 if (insts[OVSINST_OFPIT11_APPLY_ACTIONS]) {
6439 const struct ofp_action_header *actions;
6440 size_t actions_len;
f25d0cf3 6441
c2d936a4
BP
6442 get_actions_from_instruction(insts[OVSINST_OFPIT11_APPLY_ACTIONS],
6443 &actions, &actions_len);
6444 error = ofpacts_decode(actions, actions_len, version, ofpacts);
6445 if (error) {
6446 goto exit;
6447 }
6448 }
6449 if (insts[OVSINST_OFPIT11_CLEAR_ACTIONS]) {
6450 instruction_get_OFPIT11_CLEAR_ACTIONS(
6451 insts[OVSINST_OFPIT11_CLEAR_ACTIONS]);
6452 ofpact_put_CLEAR_ACTIONS(ofpacts);
6453 }
6454 if (insts[OVSINST_OFPIT11_WRITE_ACTIONS]) {
6455 struct ofpact_nest *on;
6456 const struct ofp_action_header *actions;
6457 size_t actions_len;
2bd318de 6458 size_t start = ofpacts->size;
255a54ea
BP
6459 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
6460 offsetof(struct ofpact_nest, actions));
c2d936a4
BP
6461 get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
6462 &actions, &actions_len);
6463 error = ofpacts_decode_for_action_set(actions, actions_len,
6464 version, ofpacts);
6465 if (error) {
6466 goto exit;
6467 }
6468 on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
6fd6ed71 6469 on->ofpact.len = ofpacts->size - start;
c2d936a4
BP
6470 }
6471 if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
6472 const struct ofp11_instruction_write_metadata *oiwm;
6473 struct ofpact_metadata *om;
ca287d20 6474
c2d936a4
BP
6475 oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
6476 insts[OVSINST_OFPIT11_WRITE_METADATA]);
8dd54666 6477
c2d936a4
BP
6478 om = ofpact_put_WRITE_METADATA(ofpacts);
6479 om->metadata = oiwm->metadata;
6480 om->mask = oiwm->metadata_mask;
6481 }
6482 if (insts[OVSINST_OFPIT11_GOTO_TABLE]) {
6483 const struct ofp11_instruction_goto_table *oigt;
6484 struct ofpact_goto_table *ogt;
7395c052 6485
c2d936a4
BP
6486 oigt = instruction_get_OFPIT11_GOTO_TABLE(
6487 insts[OVSINST_OFPIT11_GOTO_TABLE]);
6488 ogt = ofpact_put_GOTO_TABLE(ofpacts);
6489 ogt->table_id = oigt->table_id;
6490 }
e3f8f887 6491
6fd6ed71 6492 error = ofpacts_verify(ofpacts->data, ofpacts->size,
d824b5b7 6493 (1u << N_OVS_INSTRUCTIONS) - 1, 0);
c2d936a4
BP
6494exit:
6495 if (error) {
6496 ofpbuf_clear(ofpacts);
f25d0cf3 6497 }
c2d936a4 6498 return error;
f25d0cf3 6499}
d01c980f 6500
c2d936a4
BP
6501/* Update the length of the instruction that begins at offset 'ofs' within
6502 * 'openflow' and contains nested actions that extend to the end of 'openflow'.
6503 * If the instruction contains no nested actions, deletes it entirely. */
d01c980f 6504static void
c2d936a4 6505ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
d01c980f 6506{
c2d936a4 6507 struct ofp11_instruction_actions *oia;
d01c980f 6508
c2d936a4 6509 oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
6fd6ed71
PS
6510 if (openflow->size > ofs + sizeof *oia) {
6511 oia->len = htons(openflow->size - ofs);
c2d936a4 6512 } else {
6fd6ed71 6513 openflow->size = ofs;
c2d936a4 6514 }
d01c980f 6515}
c2d936a4
BP
6516\f
6517/* Checks that 'port' is a valid output port for OFPACT_OUTPUT, given that the
6518 * switch will never have more than 'max_ports' ports. Returns 0 if 'port' is
6519 * valid, otherwise an OpenFlow error code. */
6520enum ofperr
6521ofpact_check_output_port(ofp_port_t port, ofp_port_t max_ports)
99086062 6522{
c2d936a4
BP
6523 switch (port) {
6524 case OFPP_IN_PORT:
6525 case OFPP_TABLE:
6526 case OFPP_NORMAL:
6527 case OFPP_FLOOD:
6528 case OFPP_ALL:
6529 case OFPP_CONTROLLER:
c2d936a4
BP
6530 case OFPP_LOCAL:
6531 return 0;
6532
13d2c689
BP
6533 case OFPP_NONE:
6534 return OFPERR_OFPBAC_BAD_OUT_PORT;
6535
c2d936a4
BP
6536 default:
6537 if (ofp_to_u16(port) < ofp_to_u16(max_ports)) {
6538 return 0;
6539 }
6540 return OFPERR_OFPBAC_BAD_OUT_PORT;
99086062
BP
6541 }
6542}
6543
c2d936a4
BP
6544/* Removes the protocols that require consistency between match and actions
6545 * (that's everything but OpenFlow 1.0) from '*usable_protocols'.
6546 *
6547 * (An example of an inconsistency between match and actions is a flow that
6548 * does not match on an MPLS Ethertype but has an action that pops an MPLS
6549 * label.) */
d01c980f 6550static void
c2d936a4
BP
6551inconsistent_match(enum ofputil_protocol *usable_protocols)
6552{
6553 *usable_protocols &= OFPUTIL_P_OF10_ANY;
6554}
6555
6556/* May modify flow->dl_type, flow->nw_proto and flow->vlan_tci,
6557 * caller must restore them.
6558 *
6559 * Modifies some actions, filling in fields that could not be properly set
6560 * without context. */
6561static enum ofperr
6562ofpact_check__(enum ofputil_protocol *usable_protocols, struct ofpact *a,
6563 struct flow *flow, ofp_port_t max_ports,
6564 uint8_t table_id, uint8_t n_tables)
d01c980f 6565{
c2d936a4
BP
6566 const struct ofpact_enqueue *enqueue;
6567 const struct mf_field *mf;
6568
d01c980f
BP
6569 switch (a->type) {
6570 case OFPACT_OUTPUT:
c2d936a4
BP
6571 return ofpact_check_output_port(ofpact_get_OUTPUT(a)->port,
6572 max_ports);
6573
6574 case OFPACT_CONTROLLER:
6575 return 0;
d01c980f
BP
6576
6577 case OFPACT_ENQUEUE:
c2d936a4
BP
6578 enqueue = ofpact_get_ENQUEUE(a);
6579 if (ofp_to_u16(enqueue->port) >= ofp_to_u16(max_ports)
6580 && enqueue->port != OFPP_IN_PORT
6581 && enqueue->port != OFPP_LOCAL) {
6582 return OFPERR_OFPBAC_BAD_OUT_PORT;
6583 }
6584 return 0;
6585
6586 case OFPACT_OUTPUT_REG:
6587 return mf_check_src(&ofpact_get_OUTPUT_REG(a)->src, flow);
6588
6589 case OFPACT_BUNDLE:
6590 return bundle_check(ofpact_get_BUNDLE(a), max_ports, flow);
d01c980f
BP
6591
6592 case OFPACT_SET_VLAN_VID:
c2d936a4
BP
6593 /* Remember if we saw a vlan tag in the flow to aid translating to
6594 * OpenFlow 1.1+ if need be. */
6595 ofpact_get_SET_VLAN_VID(a)->flow_has_vlan =
6596 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6597 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6598 !ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
6599 inconsistent_match(usable_protocols);
6600 }
6601 /* Temporary mark that we have a vlan tag. */
6602 flow->vlan_tci |= htons(VLAN_CFI);
6603 return 0;
d01c980f
BP
6604
6605 case OFPACT_SET_VLAN_PCP:
c2d936a4
BP
6606 /* Remember if we saw a vlan tag in the flow to aid translating to
6607 * OpenFlow 1.1+ if need be. */
6608 ofpact_get_SET_VLAN_PCP(a)->flow_has_vlan =
6609 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6610 if (!(flow->vlan_tci & htons(VLAN_CFI)) &&
6611 !ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
6612 inconsistent_match(usable_protocols);
6613 }
6614 /* Temporary mark that we have a vlan tag. */
6615 flow->vlan_tci |= htons(VLAN_CFI);
6616 return 0;
d01c980f
BP
6617
6618 case OFPACT_STRIP_VLAN:
c2d936a4
BP
6619 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
6620 inconsistent_match(usable_protocols);
6621 }
6622 /* Temporary mark that we have no vlan tag. */
6623 flow->vlan_tci = htons(0);
6624 return 0;
d01c980f 6625
3e34fbdd 6626 case OFPACT_PUSH_VLAN:
c2d936a4
BP
6627 if (flow->vlan_tci & htons(VLAN_CFI)) {
6628 /* Multiple VLAN headers not supported. */
6629 return OFPERR_OFPBAC_BAD_TAG;
6630 }
6631 /* Temporary mark that we have a vlan tag. */
6632 flow->vlan_tci |= htons(VLAN_CFI);
6633 return 0;
276c4e7a 6634
d01c980f 6635 case OFPACT_SET_ETH_SRC:
d01c980f 6636 case OFPACT_SET_ETH_DST:
c2d936a4 6637 return 0;
d01c980f
BP
6638
6639 case OFPACT_SET_IPV4_SRC:
d01c980f 6640 case OFPACT_SET_IPV4_DST:
c2d936a4
BP
6641 if (flow->dl_type != htons(ETH_TYPE_IP)) {
6642 inconsistent_match(usable_protocols);
6643 }
6644 return 0;
d01c980f 6645
04f01c24 6646 case OFPACT_SET_IP_DSCP:
ff14eb7a 6647 case OFPACT_SET_IP_ECN:
0c20dbe4 6648 case OFPACT_SET_IP_TTL:
7bcb1506 6649 case OFPACT_DEC_TTL:
c2d936a4
BP
6650 if (!is_ip_any(flow)) {
6651 inconsistent_match(usable_protocols);
6652 }
6653 return 0;
b02475c5 6654
c2d936a4 6655 case OFPACT_SET_L4_SRC_PORT:
c2d936a4 6656 case OFPACT_SET_L4_DST_PORT:
b8778a0d 6657 if (!is_ip_any(flow) || (flow->nw_frag & FLOW_NW_FRAG_LATER) ||
c2d936a4
BP
6658 (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
6659 && flow->nw_proto != IPPROTO_SCTP)) {
6660 inconsistent_match(usable_protocols);
6661 }
6662 /* Note on which transport protocol the port numbers are set.
6663 * This allows this set action to be converted to an OF1.2 set field
6664 * action. */
b8778a0d
JR
6665 if (a->type == OFPACT_SET_L4_SRC_PORT) {
6666 ofpact_get_SET_L4_SRC_PORT(a)->flow_ip_proto = flow->nw_proto;
6667 } else {
6668 ofpact_get_SET_L4_DST_PORT(a)->flow_ip_proto = flow->nw_proto;
6669 }
c2d936a4 6670 return 0;
b02475c5 6671
c2d936a4
BP
6672 case OFPACT_REG_MOVE:
6673 return nxm_reg_move_check(ofpact_get_REG_MOVE(a), flow);
8dd54666 6674
e3f8f887 6675 case OFPACT_SET_FIELD:
c2d936a4
BP
6676 mf = ofpact_get_SET_FIELD(a)->field;
6677 /* Require OXM_OF_VLAN_VID to have an existing VLAN header. */
6678 if (!mf_are_prereqs_ok(mf, flow) ||
6679 (mf->id == MFF_VLAN_VID && !(flow->vlan_tci & htons(VLAN_CFI)))) {
6680 VLOG_WARN_RL(&rl, "set_field %s lacks correct prerequisities",
6681 mf->name);
6682 return OFPERR_OFPBAC_MATCH_INCONSISTENT;
6683 }
6684 /* Remember if we saw a vlan tag in the flow to aid translating to
6685 * OpenFlow 1.1 if need be. */
6686 ofpact_get_SET_FIELD(a)->flow_has_vlan =
6687 (flow->vlan_tci & htons(VLAN_CFI)) == htons(VLAN_CFI);
6688 if (mf->id == MFF_VLAN_TCI) {
6689 /* The set field may add or remove the vlan tag,
6690 * Mark the status temporarily. */
6691 flow->vlan_tci = ofpact_get_SET_FIELD(a)->value.be16;
6692 }
6693 return 0;
e3f8f887 6694
bd85dac1 6695 case OFPACT_STACK_PUSH:
c2d936a4
BP
6696 return nxm_stack_push_check(ofpact_get_STACK_PUSH(a), flow);
6697
bd85dac1 6698 case OFPACT_STACK_POP:
c2d936a4
BP
6699 return nxm_stack_pop_check(ofpact_get_STACK_POP(a), flow);
6700
6701 case OFPACT_SET_MPLS_LABEL:
6702 case OFPACT_SET_MPLS_TC:
6703 case OFPACT_SET_MPLS_TTL:
6704 case OFPACT_DEC_MPLS_TTL:
6705 if (!eth_type_mpls(flow->dl_type)) {
6706 inconsistent_match(usable_protocols);
6707 }
6708 return 0;
6709
d01c980f 6710 case OFPACT_SET_TUNNEL:
c2d936a4 6711 case OFPACT_SET_QUEUE:
d01c980f 6712 case OFPACT_POP_QUEUE:
d01c980f 6713 case OFPACT_RESUBMIT:
c2d936a4
BP
6714 return 0;
6715
6716 case OFPACT_FIN_TIMEOUT:
6717 if (flow->nw_proto != IPPROTO_TCP) {
6718 inconsistent_match(usable_protocols);
6719 }
6720 return 0;
6721
d01c980f 6722 case OFPACT_LEARN:
c2d936a4
BP
6723 return learn_check(ofpact_get_LEARN(a), flow);
6724
18080541
BP
6725 case OFPACT_CONJUNCTION:
6726 return 0;
6727
d01c980f 6728 case OFPACT_MULTIPATH:
c2d936a4
BP
6729 return multipath_check(ofpact_get_MULTIPATH(a), flow);
6730
d01c980f
BP
6731 case OFPACT_NOTE:
6732 case OFPACT_EXIT:
c2d936a4 6733 return 0;
d01c980f 6734
c2d936a4
BP
6735 case OFPACT_PUSH_MPLS:
6736 flow->dl_type = ofpact_get_PUSH_MPLS(a)->ethertype;
6737 /* The packet is now MPLS and the MPLS payload is opaque.
6738 * Thus nothing can be assumed about the network protocol.
6739 * Temporarily mark that we have no nw_proto. */
6740 flow->nw_proto = 0;
6741 return 0;
1e7db674 6742
c2d936a4
BP
6743 case OFPACT_POP_MPLS:
6744 if (!eth_type_mpls(flow->dl_type)) {
6745 inconsistent_match(usable_protocols);
6746 }
6747 flow->dl_type = ofpact_get_POP_MPLS(a)->ethertype;
6748 return 0;
1e7db674 6749
c2d936a4
BP
6750 case OFPACT_SAMPLE:
6751 return 0;
1e7db674 6752
07659514
JS
6753 case OFPACT_CT: {
6754 struct ofpact_conntrack *oc = ofpact_get_CT(a);
76e3e669 6755 enum ofperr err;
07659514
JS
6756
6757 if (!dl_type_is_ip_any(flow->dl_type)
6758 || (flow->ct_state & CS_INVALID && oc->flags & NX_CT_F_COMMIT)) {
6759 inconsistent_match(usable_protocols);
6760 }
6761
6762 if (oc->zone_src.field) {
6763 return mf_check_src(&oc->zone_src, flow);
6764 }
8e53fe8c 6765
76e3e669
JR
6766 err = ofpacts_check(oc->actions, ofpact_ct_get_action_len(oc),
6767 flow, max_ports, table_id, n_tables,
6768 usable_protocols);
6769 return err;
07659514
JS
6770 }
6771
9ac0aada
JR
6772 case OFPACT_NAT: {
6773 struct ofpact_nat *on = ofpact_get_NAT(a);
6774
6775 if (!dl_type_is_ip_any(flow->dl_type) ||
6776 (on->range_af == AF_INET && flow->dl_type != htons(ETH_TYPE_IP)) ||
6777 (on->range_af == AF_INET6
6778 && flow->dl_type != htons(ETH_TYPE_IPV6))) {
6779 inconsistent_match(usable_protocols);
6780 }
6781 return 0;
6782 }
6783
c2d936a4
BP
6784 case OFPACT_CLEAR_ACTIONS:
6785 return 0;
1e7db674 6786
c2d936a4
BP
6787 case OFPACT_WRITE_ACTIONS: {
6788 /* Use a temporary copy of 'usable_protocols' because we can't check
6789 * consistency of an action set. */
6790 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
6791 enum ofputil_protocol p = *usable_protocols;
6792 return ofpacts_check(on->actions, ofpact_nest_get_action_len(on),
6793 flow, max_ports, table_id, n_tables, &p);
6794 }
1e7db674 6795
c2d936a4
BP
6796 case OFPACT_WRITE_METADATA:
6797 return 0;
1e7db674 6798
c2d936a4
BP
6799 case OFPACT_METER: {
6800 uint32_t mid = ofpact_get_METER(a)->meter_id;
6801 if (mid == 0 || mid > OFPM13_MAX) {
6802 return OFPERR_OFPMMFC_INVALID_METER;
6803 }
6804 return 0;
6805 }
1e7db674 6806
c2d936a4
BP
6807 case OFPACT_GOTO_TABLE: {
6808 uint8_t goto_table = ofpact_get_GOTO_TABLE(a)->table_id;
6809 if ((table_id != 255 && goto_table <= table_id)
6810 || (n_tables != 255 && goto_table >= n_tables)) {
8c87971e 6811 return OFPERR_OFPBIC_BAD_TABLE_ID;
c2d936a4
BP
6812 }
6813 return 0;
6814 }
1e7db674 6815
c2d936a4
BP
6816 case OFPACT_GROUP:
6817 return 0;
1e7db674 6818
e672ff9b
JR
6819 case OFPACT_UNROLL_XLATE:
6820 /* UNROLL is an internal action that should never be seen via
6821 * OpenFlow. */
6822 return OFPERR_OFPBAC_BAD_TYPE;
6823
d4abaff5
BP
6824 case OFPACT_DEBUG_RECIRC:
6825 return 0;
6826
c2d936a4
BP
6827 default:
6828 OVS_NOT_REACHED();
6829 }
6830}
1e7db674 6831
c2d936a4
BP
6832/* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are
6833 * appropriate for a packet with the prerequisites satisfied by 'flow' in a
6834 * switch with no more than 'max_ports' ports.
6835 *
6836 * If 'ofpacts' and 'flow' are inconsistent with one another, un-sets in
6837 * '*usable_protocols' the protocols that forbid the inconsistency. (An
6838 * example of an inconsistency between match and actions is a flow that does
6839 * not match on an MPLS Ethertype but has an action that pops an MPLS label.)
6840 *
6841 * May annotate ofpacts with information gathered from the 'flow'.
6842 *
6843 * May temporarily modify 'flow', but restores the changes before returning. */
6844enum ofperr
6845ofpacts_check(struct ofpact ofpacts[], size_t ofpacts_len,
6846 struct flow *flow, ofp_port_t max_ports,
6847 uint8_t table_id, uint8_t n_tables,
6848 enum ofputil_protocol *usable_protocols)
6849{
6850 struct ofpact *a;
6851 ovs_be16 dl_type = flow->dl_type;
6852 ovs_be16 vlan_tci = flow->vlan_tci;
6853 uint8_t nw_proto = flow->nw_proto;
6854 enum ofperr error = 0;
1e7db674 6855
c2d936a4
BP
6856 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6857 error = ofpact_check__(usable_protocols, a, flow,
6858 max_ports, table_id, n_tables);
6859 if (error) {
1e7db674 6860 break;
c2d936a4
BP
6861 }
6862 }
6863 /* Restore fields that may have been modified. */
6864 flow->dl_type = dl_type;
6865 flow->vlan_tci = vlan_tci;
6866 flow->nw_proto = nw_proto;
6867 return error;
6868}
1e7db674 6869
c2d936a4
BP
6870/* Like ofpacts_check(), but reports inconsistencies as
6871 * OFPERR_OFPBAC_MATCH_INCONSISTENT rather than clearing bits. */
6872enum ofperr
6873ofpacts_check_consistency(struct ofpact ofpacts[], size_t ofpacts_len,
6874 struct flow *flow, ofp_port_t max_ports,
6875 uint8_t table_id, uint8_t n_tables,
6876 enum ofputil_protocol usable_protocols)
6877{
6878 enum ofputil_protocol p = usable_protocols;
6879 enum ofperr error;
1e7db674 6880
c2d936a4
BP
6881 error = ofpacts_check(ofpacts, ofpacts_len, flow, max_ports,
6882 table_id, n_tables, &p);
6883 return (error ? error
6884 : p != usable_protocols ? OFPERR_OFPBAC_MATCH_INCONSISTENT
6885 : 0);
6886}
097d4939 6887
28f5311f
JS
6888/* Returns the destination field that 'ofpact' would write to, or NULL
6889 * if the action would not write to an mf_field. */
6890const struct mf_field *
6891ofpact_get_mf_dst(const struct ofpact *ofpact)
8e53fe8c 6892{
28f5311f
JS
6893 if (ofpact->type == OFPACT_SET_FIELD) {
6894 const struct ofpact_set_field *orl;
8e53fe8c 6895
28f5311f 6896 orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
8e53fe8c 6897 return orl->field;
28f5311f
JS
6898 } else if (ofpact->type == OFPACT_REG_MOVE) {
6899 const struct ofpact_reg_move *orm;
8e53fe8c 6900
28f5311f 6901 orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
8e53fe8c
JS
6902 return orm->dst.field;
6903 }
6904
6905 return NULL;
6906}
6907
6908static enum ofperr
6909unsupported_nesting(enum ofpact_type action, enum ofpact_type outer_action)
6910{
6911 VLOG_WARN("%s action doesn't support nested action %s",
6912 ofpact_name(outer_action), ofpact_name(action));
6913 return OFPERR_OFPBAC_BAD_ARGUMENT;
6914}
6915
6916static bool
6917field_requires_ct(enum mf_field_id field)
6918{
9daf2348 6919 return field == MFF_CT_MARK || field == MFF_CT_LABEL;
8e53fe8c
JS
6920}
6921
6922/* Apply nesting constraints for actions */
d824b5b7
JS
6923static enum ofperr
6924ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
6925{
28f5311f 6926 const struct mf_field *field = ofpact_get_mf_dst(a);
8e53fe8c
JS
6927
6928 if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
6929 VLOG_WARN("cannot set CT fields outside of ct action");
6930 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6931 }
9ac0aada
JR
6932 if (a->type == OFPACT_NAT) {
6933 if (outer_action != OFPACT_CT) {
6934 VLOG_WARN("Cannot have NAT action outside of \"ct\" action");
6935 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
6936 }
6937 return 0;
6938 }
8e53fe8c
JS
6939
6940 if (outer_action) {
6941 ovs_assert(outer_action == OFPACT_WRITE_ACTIONS
6942 || outer_action == OFPACT_CT);
6943
6944 if (outer_action == OFPACT_CT) {
6945 if (!field) {
6946 return unsupported_nesting(a->type, outer_action);
6947 } else if (!field_requires_ct(field->id)) {
6948 VLOG_WARN("%s action doesn't support nested modification "
6949 "of %s", ofpact_name(outer_action), field->name);
6950 return OFPERR_OFPBAC_BAD_ARGUMENT;
6951 }
6952 }
d824b5b7
JS
6953 }
6954
6955 return 0;
6956}
6957
18080541
BP
6958/* Verifies that the 'ofpacts_len' bytes of actions in 'ofpacts' are in the
6959 * appropriate order as defined by the OpenFlow spec and as required by Open
6960 * vSwitch.
6961 *
6962 * 'allowed_ovsinsts' is a bitmap of OVSINST_* values, in which 1-bits indicate
d824b5b7
JS
6963 * instructions that are allowed within 'ofpacts[]'.
6964 *
6965 * If 'outer_action' is not zero, it specifies that the actions are nested
6966 * within another action of type 'outer_action'. */
c2d936a4
BP
6967static enum ofperr
6968ofpacts_verify(const struct ofpact ofpacts[], size_t ofpacts_len,
d824b5b7 6969 uint32_t allowed_ovsinsts, enum ofpact_type outer_action)
c2d936a4
BP
6970{
6971 const struct ofpact *a;
6972 enum ovs_instruction_type inst;
097d4939 6973
c2d936a4
BP
6974 inst = OVSINST_OFPIT13_METER;
6975 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
6976 enum ovs_instruction_type next;
8e53fe8c 6977 enum ofperr error;
1e7db674 6978
18080541
BP
6979 if (a->type == OFPACT_CONJUNCTION) {
6980 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
f08e39dd
BP
6981 if (a->type != OFPACT_CONJUNCTION && a->type != OFPACT_NOTE) {
6982 VLOG_WARN("\"conjunction\" actions may be used along with "
6983 "\"note\" but not any other kind of action "
6984 "(such as the \"%s\" action used here)",
96775a1c 6985 ofpact_name(a->type));
18080541
BP
6986 return OFPERR_NXBAC_BAD_CONJUNCTION;
6987 }
6988 }
6989 return 0;
6990 }
6991
8e53fe8c
JS
6992 error = ofpacts_verify_nested(a, outer_action);
6993 if (error) {
6994 return error;
d824b5b7
JS
6995 }
6996
c2d936a4
BP
6997 next = ovs_instruction_type_from_ofpact_type(a->type);
6998 if (a > ofpacts
6999 && (inst == OVSINST_OFPIT11_APPLY_ACTIONS
7000 ? next < inst
7001 : next <= inst)) {
7002 const char *name = ovs_instruction_name_from_type(inst);
7003 const char *next_name = ovs_instruction_name_from_type(next);
1e7db674 7004
c2d936a4
BP
7005 if (next == inst) {
7006 VLOG_WARN("duplicate %s instruction not allowed, for OpenFlow "
7007 "1.1+ compatibility", name);
7008 } else {
7009 VLOG_WARN("invalid instruction ordering: %s must appear "
7010 "before %s, for OpenFlow 1.1+ compatibility",
7011 next_name, name);
7012 }
7013 return OFPERR_OFPBAC_UNSUPPORTED_ORDER;
7014 }
7015 if (!((1u << next) & allowed_ovsinsts)) {
7016 const char *name = ovs_instruction_name_from_type(next);
1e7db674 7017
c2d936a4
BP
7018 VLOG_WARN("%s instruction not allowed here", name);
7019 return OFPERR_OFPBIC_UNSUP_INST;
1e7db674 7020 }
c2d936a4
BP
7021
7022 inst = next;
7023 }
7024
7025 return 0;
7026}
7027\f
7028/* Converting ofpacts to OpenFlow. */
7029
7030static void
7031encode_ofpact(const struct ofpact *a, enum ofp_version ofp_version,
7032 struct ofpbuf *out)
7033{
7034 switch (a->type) {
7035#define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7036 case OFPACT_##ENUM: \
355ead69
GS
7037 encode_##ENUM(ofpact_get_##ENUM(a), ofp_version, out); \
7038 return;
c2d936a4
BP
7039 OFPACTS
7040#undef OFPACT
7041 default:
7042 OVS_NOT_REACHED();
1e7db674 7043 }
e3f8f887
JR
7044}
7045
7046/* Converts the 'ofpacts_len' bytes of ofpacts in 'ofpacts' into OpenFlow
7047 * actions in 'openflow', appending the actions to any existing data in
d01c980f 7048 * 'openflow'. */
a07c15bc 7049size_t
e3f8f887
JR
7050ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
7051 struct ofpbuf *openflow,
7052 enum ofp_version ofp_version)
d01c980f
BP
7053{
7054 const struct ofpact *a;
6fd6ed71 7055 size_t start_size = openflow->size;
d01c980f
BP
7056
7057 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
c2d936a4 7058 encode_ofpact(a, ofp_version, openflow);
d01c980f 7059 }
6fd6ed71 7060 return openflow->size - start_size;
d01c980f
BP
7061}
7062
c2d936a4
BP
7063static enum ovs_instruction_type
7064ofpact_is_apply_actions(const struct ofpact *a)
d01c980f 7065{
c2d936a4
BP
7066 return (ovs_instruction_type_from_ofpact_type(a->type)
7067 == OVSINST_OFPIT11_APPLY_ACTIONS);
d01c980f 7068}
8dd54666
IY
7069
7070void
e3f8f887
JR
7071ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
7072 size_t ofpacts_len,
7073 struct ofpbuf *openflow,
7074 enum ofp_version ofp_version)
8dd54666 7075{
c2d936a4 7076 const struct ofpact *end = ofpact_end(ofpacts, ofpacts_len);
8dd54666
IY
7077 const struct ofpact *a;
7078
8f2cded4
BP
7079 if (ofp_version == OFP10_VERSION) {
7080 ofpacts_put_openflow_actions(ofpacts, ofpacts_len, openflow,
7081 ofp_version);
7082 return;
7083 }
e3f8f887 7084
c2d936a4
BP
7085 a = ofpacts;
7086 while (a < end) {
7087 if (ofpact_is_apply_actions(a)) {
6fd6ed71 7088 size_t ofs = openflow->size;
8dd54666
IY
7089
7090 instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
c2d936a4
BP
7091 do {
7092 encode_ofpact(a, ofp_version, openflow);
7093 a = ofpact_next(a);
7094 } while (a < end && ofpact_is_apply_actions(a));
7fdb60a7 7095 ofpacts_update_instruction_actions(openflow, ofs);
c2d936a4
BP
7096 } else {
7097 encode_ofpact(a, ofp_version, openflow);
7098 a = ofpact_next(a);
8dd54666
IY
7099 }
7100 }
7101}
d01c980f 7102\f
08d1e234
BP
7103/* Sets of supported actions. */
7104
7105/* Two-way translation between OVS's internal "OFPACT_*" representation of
7106 * actions and the "OFPAT_*" representation used in some OpenFlow version.
7107 * (OFPAT_* numbering varies from one OpenFlow version to another, so a given
7108 * instance is specific to one OpenFlow version.) */
7109struct ofpact_map {
7110 enum ofpact_type ofpact; /* Internal name for action type. */
7111 int ofpat; /* OFPAT_* number from OpenFlow spec. */
7112};
7113
7114static const struct ofpact_map *
7115get_ofpact_map(enum ofp_version version)
7116{
7117 /* OpenFlow 1.0 actions. */
7118 static const struct ofpact_map of10[] = {
7119 { OFPACT_OUTPUT, 0 },
7120 { OFPACT_SET_VLAN_VID, 1 },
7121 { OFPACT_SET_VLAN_PCP, 2 },
7122 { OFPACT_STRIP_VLAN, 3 },
7123 { OFPACT_SET_ETH_SRC, 4 },
7124 { OFPACT_SET_ETH_DST, 5 },
7125 { OFPACT_SET_IPV4_SRC, 6 },
7126 { OFPACT_SET_IPV4_DST, 7 },
7127 { OFPACT_SET_IP_DSCP, 8 },
7128 { OFPACT_SET_L4_SRC_PORT, 9 },
7129 { OFPACT_SET_L4_DST_PORT, 10 },
7130 { OFPACT_ENQUEUE, 11 },
7131 { 0, -1 },
7132 };
7133
7134 /* OpenFlow 1.1 actions. */
7135 static const struct ofpact_map of11[] = {
7136 { OFPACT_OUTPUT, 0 },
7137 { OFPACT_SET_VLAN_VID, 1 },
7138 { OFPACT_SET_VLAN_PCP, 2 },
7139 { OFPACT_SET_ETH_SRC, 3 },
7140 { OFPACT_SET_ETH_DST, 4 },
7141 { OFPACT_SET_IPV4_SRC, 5 },
7142 { OFPACT_SET_IPV4_DST, 6 },
7143 { OFPACT_SET_IP_DSCP, 7 },
7144 { OFPACT_SET_IP_ECN, 8 },
7145 { OFPACT_SET_L4_SRC_PORT, 9 },
7146 { OFPACT_SET_L4_DST_PORT, 10 },
7147 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7148 /* OFPAT_COPY_TTL_IN (12) not supported. */
7149 { OFPACT_SET_MPLS_LABEL, 13 },
7150 { OFPACT_SET_MPLS_TC, 14 },
7151 { OFPACT_SET_MPLS_TTL, 15 },
7152 { OFPACT_DEC_MPLS_TTL, 16 },
7153 { OFPACT_PUSH_VLAN, 17 },
7154 { OFPACT_STRIP_VLAN, 18 },
7155 { OFPACT_PUSH_MPLS, 19 },
7156 { OFPACT_POP_MPLS, 20 },
7157 { OFPACT_SET_QUEUE, 21 },
7158 { OFPACT_GROUP, 22 },
7159 { OFPACT_SET_IP_TTL, 23 },
7160 { OFPACT_DEC_TTL, 24 },
7161 { 0, -1 },
7162 };
7163
7164 /* OpenFlow 1.2, 1.3, and 1.4 actions. */
7165 static const struct ofpact_map of12[] = {
7166 { OFPACT_OUTPUT, 0 },
7167 /* OFPAT_COPY_TTL_OUT (11) not supported. */
7168 /* OFPAT_COPY_TTL_IN (12) not supported. */
7169 { OFPACT_SET_MPLS_TTL, 15 },
7170 { OFPACT_DEC_MPLS_TTL, 16 },
7171 { OFPACT_PUSH_VLAN, 17 },
7172 { OFPACT_STRIP_VLAN, 18 },
7173 { OFPACT_PUSH_MPLS, 19 },
7174 { OFPACT_POP_MPLS, 20 },
7175 { OFPACT_SET_QUEUE, 21 },
7176 { OFPACT_GROUP, 22 },
7177 { OFPACT_SET_IP_TTL, 23 },
7178 { OFPACT_DEC_TTL, 24 },
7179 { OFPACT_SET_FIELD, 25 },
7180 /* OF1.3+ OFPAT_PUSH_PBB (26) not supported. */
7181 /* OF1.3+ OFPAT_POP_PBB (27) not supported. */
7182 { 0, -1 },
7183 };
7184
7185 switch (version) {
7186 case OFP10_VERSION:
7187 return of10;
7188
7189 case OFP11_VERSION:
7190 return of11;
7191
7192 case OFP12_VERSION:
7193 case OFP13_VERSION:
7194 case OFP14_VERSION:
7195 case OFP15_VERSION:
7196 default:
7197 return of12;
7198 }
7199}
7200
7201/* Converts 'ofpacts_bitmap', a bitmap whose bits correspond to OFPACT_*
7202 * values, into a bitmap of actions suitable for OpenFlow 'version', and
7203 * returns the result. */
7204ovs_be32
7205ofpact_bitmap_to_openflow(uint64_t ofpacts_bitmap, enum ofp_version version)
7206{
7207 uint32_t openflow_bitmap = 0;
7208 const struct ofpact_map *x;
7209
7210 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7211 if (ofpacts_bitmap & (UINT64_C(1) << x->ofpact)) {
7212 openflow_bitmap |= 1u << x->ofpat;
7213 }
7214 }
7215 return htonl(openflow_bitmap);
7216}
7217
7218/* Converts 'ofpat_bitmap', a bitmap of actions from an OpenFlow message with
7219 * the given 'version' into a bitmap whose bits correspond to OFPACT_* values,
7220 * and returns the result. */
7221uint64_t
7222ofpact_bitmap_from_openflow(ovs_be32 ofpat_bitmap, enum ofp_version version)
7223{
7224 uint64_t ofpact_bitmap = 0;
7225 const struct ofpact_map *x;
7226
7227 for (x = get_ofpact_map(version); x->ofpat >= 0; x++) {
7228 if (ofpat_bitmap & htonl(1u << x->ofpat)) {
7229 ofpact_bitmap |= UINT64_C(1) << x->ofpact;
7230 }
7231 }
7232 return ofpact_bitmap;
7233}
7234
7235/* Appends to 's' a string representation of the set of OFPACT_* represented
7236 * by 'ofpacts_bitmap'. */
7237void
7238ofpact_bitmap_format(uint64_t ofpacts_bitmap, struct ds *s)
7239{
7240 if (!ofpacts_bitmap) {
7241 ds_put_cstr(s, "<none>");
7242 } else {
7243 while (ofpacts_bitmap) {
7244 ds_put_format(s, "%s ",
7245 ofpact_name(rightmost_1bit_idx(ofpacts_bitmap)));
7246 ofpacts_bitmap = zero_rightmost_1bit(ofpacts_bitmap);
7247 }
7248 ds_chomp(s, ' ');
7249 }
7250}
7251\f
f25d0cf3
BP
7252/* Returns true if 'action' outputs to 'port', false otherwise. */
7253static bool
4e022ec0 7254ofpact_outputs_to_port(const struct ofpact *ofpact, ofp_port_t port)
f25d0cf3
BP
7255{
7256 switch (ofpact->type) {
7257 case OFPACT_OUTPUT:
7258 return ofpact_get_OUTPUT(ofpact)->port == port;
7259 case OFPACT_ENQUEUE:
7260 return ofpact_get_ENQUEUE(ofpact)->port == port;
7261 case OFPACT_CONTROLLER:
7262 return port == OFPP_CONTROLLER;
7263
7264 case OFPACT_OUTPUT_REG:
7265 case OFPACT_BUNDLE:
7266 case OFPACT_SET_VLAN_VID:
7267 case OFPACT_SET_VLAN_PCP:
7268 case OFPACT_STRIP_VLAN:
3e34fbdd 7269 case OFPACT_PUSH_VLAN:
f25d0cf3
BP
7270 case OFPACT_SET_ETH_SRC:
7271 case OFPACT_SET_ETH_DST:
7272 case OFPACT_SET_IPV4_SRC:
7273 case OFPACT_SET_IPV4_DST:
04f01c24 7274 case OFPACT_SET_IP_DSCP:
ff14eb7a 7275 case OFPACT_SET_IP_ECN:
0c20dbe4 7276 case OFPACT_SET_IP_TTL:
f25d0cf3
BP
7277 case OFPACT_SET_L4_SRC_PORT:
7278 case OFPACT_SET_L4_DST_PORT:
7279 case OFPACT_REG_MOVE:
b2dd70be 7280 case OFPACT_SET_FIELD:
bd85dac1
AZ
7281 case OFPACT_STACK_PUSH:
7282 case OFPACT_STACK_POP:
f25d0cf3 7283 case OFPACT_DEC_TTL:
097d4939
JR
7284 case OFPACT_SET_MPLS_LABEL:
7285 case OFPACT_SET_MPLS_TC:
0f3f3c3d 7286 case OFPACT_SET_MPLS_TTL:
b676167a 7287 case OFPACT_DEC_MPLS_TTL:
f25d0cf3 7288 case OFPACT_SET_TUNNEL:
4cceacb9 7289 case OFPACT_WRITE_METADATA:
f25d0cf3
BP
7290 case OFPACT_SET_QUEUE:
7291 case OFPACT_POP_QUEUE:
7292 case OFPACT_FIN_TIMEOUT:
7293 case OFPACT_RESUBMIT:
7294 case OFPACT_LEARN:
18080541 7295 case OFPACT_CONJUNCTION:
f25d0cf3 7296 case OFPACT_MULTIPATH:
f25d0cf3
BP
7297 case OFPACT_NOTE:
7298 case OFPACT_EXIT:
e672ff9b 7299 case OFPACT_UNROLL_XLATE:
b02475c5
SH
7300 case OFPACT_PUSH_MPLS:
7301 case OFPACT_POP_MPLS:
29089a54 7302 case OFPACT_SAMPLE:
b19e8793 7303 case OFPACT_CLEAR_ACTIONS:
7fdb60a7 7304 case OFPACT_WRITE_ACTIONS:
8dd54666 7305 case OFPACT_GOTO_TABLE:
638a19b0 7306 case OFPACT_METER:
7395c052 7307 case OFPACT_GROUP:
d4abaff5 7308 case OFPACT_DEBUG_RECIRC:
07659514 7309 case OFPACT_CT:
9ac0aada 7310 case OFPACT_NAT:
f25d0cf3
BP
7311 default:
7312 return false;
7313 }
7314}
7315
7316/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7317 * to 'port', false otherwise. */
7318bool
7319ofpacts_output_to_port(const struct ofpact *ofpacts, size_t ofpacts_len,
4e022ec0 7320 ofp_port_t port)
c2d936a4
BP
7321{
7322 const struct ofpact *a;
f25d0cf3 7323
4f20179d 7324 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
c2d936a4
BP
7325 if (ofpact_outputs_to_port(a, port)) {
7326 return true;
7327 }
7328 }
f25d0cf3 7329
c2d936a4
BP
7330 return false;
7331}
f25d0cf3 7332
c2d936a4
BP
7333/* Returns true if any action in the 'ofpacts_len' bytes of 'ofpacts' outputs
7334 * to 'group', false otherwise. */
7335bool
7336ofpacts_output_to_group(const struct ofpact *ofpacts, size_t ofpacts_len,
7337 uint32_t group_id)
7338{
7339 const struct ofpact *a;
b02475c5 7340
4f20179d 7341 OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) {
c2d936a4
BP
7342 if (a->type == OFPACT_GROUP
7343 && ofpact_get_GROUP(a)->group_id == group_id) {
7344 return true;
7345 }
7346 }
b02475c5 7347
c2d936a4
BP
7348 return false;
7349}
8dd54666 7350
c2d936a4
BP
7351bool
7352ofpacts_equal(const struct ofpact *a, size_t a_len,
7353 const struct ofpact *b, size_t b_len)
7354{
7355 return a_len == b_len && !memcmp(a, b, a_len);
7356}
29089a54 7357
c2d936a4
BP
7358/* Finds the OFPACT_METER action, if any, in the 'ofpacts_len' bytes of
7359 * 'ofpacts'. If found, returns its meter ID; if not, returns 0.
7360 *
7361 * This function relies on the order of 'ofpacts' being correct (as checked by
7362 * ofpacts_verify()). */
7363uint32_t
7364ofpacts_get_meter(const struct ofpact ofpacts[], size_t ofpacts_len)
7365{
7366 const struct ofpact *a;
7fdb60a7 7367
c2d936a4
BP
7368 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7369 enum ovs_instruction_type inst;
b19e8793 7370
c2d936a4
BP
7371 inst = ovs_instruction_type_from_ofpact_type(a->type);
7372 if (a->type == OFPACT_METER) {
7373 return ofpact_get_METER(a)->meter_id;
7374 } else if (inst > OVSINST_OFPIT13_METER) {
7375 break;
4cceacb9 7376 }
c2d936a4 7377 }
638a19b0 7378
c2d936a4
BP
7379 return 0;
7380}
7381\f
7382/* Formatting ofpacts. */
7395c052 7383
c2d936a4
BP
7384static void
7385ofpact_format(const struct ofpact *a, struct ds *s)
7386{
7387 switch (a->type) {
7388#define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7389 case OFPACT_##ENUM: \
7390 format_##ENUM(ALIGNED_CAST(const struct STRUCT *, a), s); \
7391 break;
7392 OFPACTS
7393#undef OFPACT
7394 default:
7395 OVS_NOT_REACHED();
f25d0cf3
BP
7396 }
7397}
7398
7399/* Appends a string representing the 'ofpacts_len' bytes of ofpacts in
7400 * 'ofpacts' to 'string'. */
7401void
7402ofpacts_format(const struct ofpact *ofpacts, size_t ofpacts_len,
7403 struct ds *string)
7404{
f25d0cf3 7405 if (!ofpacts_len) {
b1c5bf1f 7406 ds_put_format(string, "%sdrop%s", colors.drop, colors.end);
f25d0cf3
BP
7407 } else {
7408 const struct ofpact *a;
7409
7410 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
7411 if (a != ofpacts) {
b1c5bf1f 7412 ds_put_char(string, ',');
f25d0cf3 7413 }
8dd54666 7414
5dca28b5 7415 /* XXX write-actions */
f25d0cf3
BP
7416 ofpact_format(a, string);
7417 }
7418 }
7419}
7420\f
7421/* Internal use by helpers. */
7422
7423void *
7424ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
7425{
7426 struct ofpact *ofpact;
7427
6fd6ed71
PS
7428 ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
7429 ofpact = ofpacts->header;
f25d0cf3
BP
7430 ofpact_init(ofpact, type, len);
7431 return ofpact;
7432}
7433
7434void
7435ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
7436{
7437 memset(ofpact, 0, len);
7438 ofpact->type = type;
c2d936a4 7439 ofpact->raw = -1;
f25d0cf3
BP
7440 ofpact->len = len;
7441}
7442\f
2bd318de
BP
7443/* Finishes composing a variable-length action (begun using
7444 * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
7445 * bytes and updating its embedded length field. See the large comment near
7446 * the end of ofp-actions.h for more information. */
f25d0cf3 7447void
34abaa3d 7448ofpact_finish(struct ofpbuf *ofpacts, struct ofpact *ofpact)
f25d0cf3 7449{
5308056f
JS
7450 ptrdiff_t len;
7451
6fd6ed71 7452 ovs_assert(ofpact == ofpacts->header);
5308056f 7453 len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
847b5649 7454 ovs_assert(len > 0 && len <= UINT16_MAX);
5308056f 7455 ofpact->len = len;
2bd318de 7456 ofpbuf_padto(ofpacts, OFPACT_ALIGN(ofpacts->size));
f25d0cf3 7457}
c2d936a4 7458\f
cab50449 7459static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
7460ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
7461 enum ofputil_protocol *usable_protocols)
7462{
7463 switch (type) {
7464#define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7465 case OFPACT_##ENUM: \
7466 return parse_##ENUM(value, ofpacts, usable_protocols);
7467 OFPACTS
7468#undef OFPACT
7469 default:
7470 OVS_NOT_REACHED();
7471 }
7472}
7473
7474static bool
7475ofpact_type_from_name(const char *name, enum ofpact_type *type)
7476{
7477#define OFPACT(ENUM, STRUCT, MEMBER, NAME) \
7478 if (!strcasecmp(name, NAME)) { \
7479 *type = OFPACT_##ENUM; \
7480 return true; \
7481 }
7482 OFPACTS
7483#undef OFPACT
7484
7485 return false;
7486}
7487
7488/* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
7489 *
7490 * Returns NULL if successful, otherwise a malloc()'d string describing the
d824b5b7
JS
7491 * error. The caller is responsible for freeing the returned string.
7492 *
7493 * If 'outer_action' is specified, indicates that the actions being parsed
7494 * are nested within another action of the type specified in 'outer_action'. */
cab50449 7495static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
7496ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
7497 enum ofputil_protocol *usable_protocols,
d824b5b7 7498 bool allow_instructions, enum ofpact_type outer_action)
c2d936a4
BP
7499{
7500 int prev_inst = -1;
7501 enum ofperr retval;
7502 char *key, *value;
7503 bool drop = false;
7504 char *pos;
7505
7506 pos = str;
7507 while (ofputil_parse_key_value(&pos, &key, &value)) {
7508 enum ovs_instruction_type inst = OVSINST_OFPIT11_APPLY_ACTIONS;
7509 enum ofpact_type type;
7510 char *error = NULL;
7511 ofp_port_t port;
7512
7513 if (ofpact_type_from_name(key, &type)) {
7514 error = ofpact_parse(type, value, ofpacts, usable_protocols);
7515 inst = ovs_instruction_type_from_ofpact_type(type);
7516 } else if (!strcasecmp(key, "mod_vlan_vid")) {
7517 error = parse_set_vlan_vid(value, ofpacts, true);
7518 } else if (!strcasecmp(key, "mod_vlan_pcp")) {
7519 error = parse_set_vlan_pcp(value, ofpacts, true);
7520 } else if (!strcasecmp(key, "set_nw_ttl")) {
7521 error = parse_SET_IP_TTL(value, ofpacts, usable_protocols);
7522 } else if (!strcasecmp(key, "pop_vlan")) {
7523 error = parse_pop_vlan(ofpacts);
7524 } else if (!strcasecmp(key, "set_tunnel64")) {
7525 error = parse_set_tunnel(value, ofpacts,
7526 NXAST_RAW_SET_TUNNEL64);
7eb4b1f1
BP
7527 } else if (!strcasecmp(key, "load")) {
7528 error = parse_reg_load(value, ofpacts);
c2d936a4
BP
7529 } else if (!strcasecmp(key, "bundle_load")) {
7530 error = parse_bundle_load(value, ofpacts);
7531 } else if (!strcasecmp(key, "drop")) {
7532 drop = true;
7533 } else if (!strcasecmp(key, "apply_actions")) {
7534 return xstrdup("apply_actions is the default instruction");
7535 } else if (ofputil_port_from_string(key, &port)) {
7536 ofpact_put_OUTPUT(ofpacts)->port = port;
7537 } else {
7538 return xasprintf("unknown action %s", key);
7539 }
7540 if (error) {
7541 return error;
7542 }
7543
7544 if (inst != OVSINST_OFPIT11_APPLY_ACTIONS) {
7545 if (!allow_instructions) {
7546 return xasprintf("only actions are allowed here (not "
7547 "instruction %s)",
7548 ovs_instruction_name_from_type(inst));
7549 }
7550 if (inst == prev_inst) {
7551 return xasprintf("instruction %s may be specified only once",
7552 ovs_instruction_name_from_type(inst));
7553 }
7554 }
7555 if (prev_inst != -1 && inst < prev_inst) {
7556 return xasprintf("instruction %s must be specified before %s",
7557 ovs_instruction_name_from_type(inst),
7558 ovs_instruction_name_from_type(prev_inst));
7559 }
7560 prev_inst = inst;
7561 }
c2d936a4 7562
6fd6ed71 7563 if (drop && ofpacts->size) {
c2d936a4
BP
7564 return xstrdup("\"drop\" must not be accompanied by any other action "
7565 "or instruction");
7566 }
7567
6fd6ed71 7568 retval = ofpacts_verify(ofpacts->data, ofpacts->size,
c2d936a4
BP
7569 (allow_instructions
7570 ? (1u << N_OVS_INSTRUCTIONS) - 1
d824b5b7
JS
7571 : 1u << OVSINST_OFPIT11_APPLY_ACTIONS),
7572 outer_action);
c2d936a4
BP
7573 if (retval) {
7574 return xstrdup("Incorrect instruction ordering");
7575 }
7576
7577 return NULL;
7578}
7579
cab50449 7580static char * OVS_WARN_UNUSED_RESULT
c2d936a4 7581ofpacts_parse(char *str, struct ofpbuf *ofpacts,
d824b5b7
JS
7582 enum ofputil_protocol *usable_protocols, bool allow_instructions,
7583 enum ofpact_type outer_action)
c2d936a4 7584{
6fd6ed71 7585 uint32_t orig_size = ofpacts->size;
c2d936a4 7586 char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
d824b5b7 7587 allow_instructions, outer_action);
c2d936a4 7588 if (error) {
6fd6ed71 7589 ofpacts->size = orig_size;
c2d936a4
BP
7590 }
7591 return error;
7592}
7593
cab50449 7594static char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
7595ofpacts_parse_copy(const char *s_, struct ofpbuf *ofpacts,
7596 enum ofputil_protocol *usable_protocols,
d824b5b7 7597 bool allow_instructions, enum ofpact_type outer_action)
c2d936a4
BP
7598{
7599 char *error, *s;
7600
7601 *usable_protocols = OFPUTIL_P_ANY;
7602
7603 s = xstrdup(s_);
d824b5b7
JS
7604 error = ofpacts_parse(s, ofpacts, usable_protocols, allow_instructions,
7605 outer_action);
c2d936a4
BP
7606 free(s);
7607
7608 return error;
7609}
7610
7611/* Parses 's' as a set of OpenFlow actions and appends the actions to
d824b5b7
JS
7612 * 'ofpacts'. 'outer_action', if nonzero, specifies that 's' contains actions
7613 * that are nested within the action of type 'outer_action'.
c2d936a4
BP
7614 *
7615 * Returns NULL if successful, otherwise a malloc()'d string describing the
7616 * error. The caller is responsible for freeing the returned string. */
cab50449 7617char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
7618ofpacts_parse_actions(const char *s, struct ofpbuf *ofpacts,
7619 enum ofputil_protocol *usable_protocols)
7620{
d824b5b7 7621 return ofpacts_parse_copy(s, ofpacts, usable_protocols, false, 0);
c2d936a4
BP
7622}
7623
7624/* Parses 's' as a set of OpenFlow instructions and appends the instructions to
7625 * 'ofpacts'.
7626 *
7627 * Returns NULL if successful, otherwise a malloc()'d string describing the
7628 * error. The caller is responsible for freeing the returned string. */
cab50449 7629char * OVS_WARN_UNUSED_RESULT
c2d936a4
BP
7630ofpacts_parse_instructions(const char *s, struct ofpbuf *ofpacts,
7631 enum ofputil_protocol *usable_protocols)
7632{
d824b5b7 7633 return ofpacts_parse_copy(s, ofpacts, usable_protocols, true, 0);
c2d936a4 7634}
08d1e234
BP
7635
7636const char *
7637ofpact_name(enum ofpact_type type)
7638{
7639 switch (type) {
7640#define OFPACT(ENUM, STRUCT, MEMBER, NAME) case OFPACT_##ENUM: return NAME;
7641 OFPACTS
7642#undef OFPACT
7643 }
7644 return "<unknown>";
7645}
c2d936a4
BP
7646\f
7647/* Low-level action decoding and encoding functions. */
7648
7649/* Everything needed to identify a particular OpenFlow action. */
7650struct ofpact_hdrs {
7651 uint32_t vendor; /* 0 if standard, otherwise a vendor code. */
7652 uint16_t type; /* Type if standard, otherwise subtype. */
7653 uint8_t ofp_version; /* From ofp_header. */
7654};
7655
7656/* Information about a particular OpenFlow action. */
7657struct ofpact_raw_instance {
7658 /* The action's identity. */
7659 struct ofpact_hdrs hdrs;
7660 enum ofp_raw_action_type raw;
7661
7662 /* Looking up the action. */
7663 struct hmap_node decode_node; /* Based on 'hdrs'. */
7664 struct hmap_node encode_node; /* Based on 'raw' + 'hdrs.ofp_version'. */
7665
7666 /* The action's encoded size.
7667 *
7668 * If this action is fixed-length, 'min_length' == 'max_length'.
7669 * If it is variable length, then 'max_length' is ROUND_DOWN(UINT16_MAX,
7670 * OFP_ACTION_ALIGN) == 65528. */
7671 unsigned short int min_length;
7672 unsigned short int max_length;
7673
7674 /* For actions with a simple integer numeric argument, 'arg_ofs' is the
7675 * offset of that argument from the beginning of the action and 'arg_len'
7676 * its length, both in bytes.
7677 *
7678 * For actions that take other forms, these are both zero. */
7679 unsigned short int arg_ofs;
7680 unsigned short int arg_len;
7681
7682 /* The name of the action, e.g. "OFPAT_OUTPUT" or "NXAST_RESUBMIT". */
7683 const char *name;
7684
7685 /* If this action is deprecated, a human-readable string with a brief
7686 * explanation. */
7687 const char *deprecation;
7688};
7689
7690/* Action header. */
7691struct ofp_action_header {
7692 /* The meaning of other values of 'type' generally depends on the OpenFlow
7693 * version (see enum ofp_raw_action_type).
7694 *
7695 * Across all OpenFlow versions, OFPAT_VENDOR indicates that 'vendor'
7696 * designates an OpenFlow vendor ID and that the remainder of the action
7697 * structure has a vendor-defined meaning.
7698 */
7699#define OFPAT_VENDOR 0xffff
7700 ovs_be16 type;
7701
7702 /* Always a multiple of 8. */
7703 ovs_be16 len;
7704
7705 /* For type == OFPAT_VENDOR only, this is a vendor ID, e.g. NX_VENDOR_ID or
7706 * ONF_VENDOR_ID. Other 'type's use this space for some other purpose. */
7707 ovs_be32 vendor;
7708};
7709OFP_ASSERT(sizeof(struct ofp_action_header) == 8);
7710
232c1e12
BP
7711/* Header for Nicira-defined actions and for ONF vendor extensions.
7712 *
7713 * This cannot be used as an entirely generic vendor extension action header,
7714 * because OpenFlow does not specify the location or size of the action
7715 * subtype; it just happens that ONF extensions and Nicira extensions share
7716 * this format. */
7717struct ext_action_header {
c2d936a4
BP
7718 ovs_be16 type; /* OFPAT_VENDOR. */
7719 ovs_be16 len; /* At least 16. */
232c1e12 7720 ovs_be32 vendor; /* NX_VENDOR_ID or ONF_VENDOR_ID. */
c2d936a4
BP
7721 ovs_be16 subtype; /* See enum ofp_raw_action_type. */
7722 uint8_t pad[6];
7723};
232c1e12 7724OFP_ASSERT(sizeof(struct ext_action_header) == 16);
c2d936a4
BP
7725
7726static bool
7727ofpact_hdrs_equal(const struct ofpact_hdrs *a,
7728 const struct ofpact_hdrs *b)
7729{
7730 return (a->vendor == b->vendor
7731 && a->type == b->type
7732 && a->ofp_version == b->ofp_version);
7733}
7734
7735static uint32_t
7736ofpact_hdrs_hash(const struct ofpact_hdrs *hdrs)
7737{
7738 return hash_2words(hdrs->vendor, (hdrs->type << 16) | hdrs->ofp_version);
7739}
7740
7741#include "ofp-actions.inc2"
7742
7743static struct hmap *
7744ofpact_decode_hmap(void)
7745{
7746 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7747 static struct hmap hmap;
7748
7749 if (ovsthread_once_start(&once)) {
7750 struct ofpact_raw_instance *inst;
7751
7752 hmap_init(&hmap);
7753 for (inst = all_raw_instances;
7754 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7755 inst++) {
7756 hmap_insert(&hmap, &inst->decode_node,
7757 ofpact_hdrs_hash(&inst->hdrs));
7758 }
7759 ovsthread_once_done(&once);
7760 }
7761 return &hmap;
7762}
7763
7764static struct hmap *
7765ofpact_encode_hmap(void)
7766{
7767 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
7768 static struct hmap hmap;
7769
7770 if (ovsthread_once_start(&once)) {
7771 struct ofpact_raw_instance *inst;
7772
7773 hmap_init(&hmap);
7774 for (inst = all_raw_instances;
7775 inst < &all_raw_instances[ARRAY_SIZE(all_raw_instances)];
7776 inst++) {
7777 hmap_insert(&hmap, &inst->encode_node,
7778 hash_2words(inst->raw, inst->hdrs.ofp_version));
7779 }
7780 ovsthread_once_done(&once);
7781 }
7782 return &hmap;
7783}
7784
7785static enum ofperr
7786ofpact_decode_raw(enum ofp_version ofp_version,
7787 const struct ofp_action_header *oah, size_t length,
7788 const struct ofpact_raw_instance **instp)
7789{
7790 const struct ofpact_raw_instance *inst;
7791 struct ofpact_hdrs hdrs;
7792
7793 *instp = NULL;
7794 if (length < sizeof *oah) {
7795 return OFPERR_OFPBAC_BAD_LEN;
7796 }
7797
7798 /* Get base action type. */
7799 if (oah->type == htons(OFPAT_VENDOR)) {
7800 /* Get vendor. */
7801 hdrs.vendor = ntohl(oah->vendor);
232c1e12
BP
7802 if (hdrs.vendor == NX_VENDOR_ID || hdrs.vendor == ONF_VENDOR_ID) {
7803 /* Get extension subtype. */
7804 const struct ext_action_header *nah;
c2d936a4 7805
232c1e12 7806 nah = ALIGNED_CAST(const struct ext_action_header *, oah);
c2d936a4
BP
7807 if (length < sizeof *nah) {
7808 return OFPERR_OFPBAC_BAD_LEN;
7809 }
7810 hdrs.type = ntohs(nah->subtype);
7811 } else {
7812 VLOG_WARN_RL(&rl, "OpenFlow action has unknown vendor %#"PRIx32,
7813 hdrs.vendor);
7814 return OFPERR_OFPBAC_BAD_VENDOR;
7815 }
7816 } else {
7817 hdrs.vendor = 0;
7818 hdrs.type = ntohs(oah->type);
7819 }
7820
7821 hdrs.ofp_version = ofp_version;
7822 HMAP_FOR_EACH_WITH_HASH (inst, decode_node, ofpact_hdrs_hash(&hdrs),
7823 ofpact_decode_hmap()) {
7824 if (ofpact_hdrs_equal(&hdrs, &inst->hdrs)) {
7825 *instp = inst;
7826 return 0;
7827 }
7828 }
7829
7830 return (hdrs.vendor
7831 ? OFPERR_OFPBAC_BAD_VENDOR_TYPE
7832 : OFPERR_OFPBAC_BAD_TYPE);
7833}
7834
7835static enum ofperr
7836ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7837 enum ofp_raw_action_type *raw, uint64_t *arg)
7838{
6fd6ed71 7839 const struct ofp_action_header *oah = buf->data;
c2d936a4
BP
7840 const struct ofpact_raw_instance *action;
7841 unsigned int length;
7842 enum ofperr error;
7843
7844 *raw = *arg = 0;
6fd6ed71 7845 error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
c2d936a4
BP
7846 if (error) {
7847 return error;
7848 }
7849
7850 if (action->deprecation) {
7851 VLOG_INFO_RL(&rl, "%s is deprecated in %s (%s)",
7852 action->name, ofputil_version_to_string(ofp_version),
7853 action->deprecation);
7854 }
7855
7856 length = ntohs(oah->len);
6fd6ed71 7857 if (length > buf->size) {
b153b990 7858 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
6fd6ed71 7859 "length %"PRIu32, action->name, length, buf->size);
b153b990
BP
7860 return OFPERR_OFPBAC_BAD_LEN;
7861 }
c2d936a4
BP
7862 if (length < action->min_length || length > action->max_length) {
7863 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u not in valid range "
7864 "[%hu,%hu]", action->name, length,
7865 action->min_length, action->max_length);
7866 return OFPERR_OFPBAC_BAD_LEN;
7867 }
7868 if (length % 8) {
7869 VLOG_WARN_RL(&rl, "OpenFlow action %s length %u is not a multiple "
7870 "of 8", action->name, length);
7871 return OFPERR_OFPBAC_BAD_LEN;
7872 }
7873
7874 *raw = action->raw;
7875 *arg = 0;
7876 if (action->arg_len) {
7877 const uint8_t *p;
7878 int i;
7879
7880 p = ofpbuf_at_assert(buf, action->arg_ofs, action->arg_len);
7881 for (i = 0; i < action->arg_len; i++) {
7882 *arg = (*arg << 8) | p[i];
7883 }
7884 }
7885
7886 ofpbuf_pull(buf, length);
7887
7888 return 0;
7889}
7890
7891static const struct ofpact_raw_instance *
7892ofpact_raw_lookup(enum ofp_version ofp_version, enum ofp_raw_action_type raw)
7893{
7894 const struct ofpact_raw_instance *inst;
7895
7896 HMAP_FOR_EACH_WITH_HASH (inst, encode_node, hash_2words(raw, ofp_version),
7897 ofpact_encode_hmap()) {
7898 if (inst->raw == raw && inst->hdrs.ofp_version == ofp_version) {
7899 return inst;
7900 }
7901 }
7902 OVS_NOT_REACHED();
7903}
7904
7905static void *
7906ofpact_put_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
7907 enum ofp_raw_action_type raw, uint64_t arg)
7908{
7909 const struct ofpact_raw_instance *inst;
7910 struct ofp_action_header *oah;
7911 const struct ofpact_hdrs *hdrs;
7912
7913 inst = ofpact_raw_lookup(ofp_version, raw);
7914 hdrs = &inst->hdrs;
7915
7916 oah = ofpbuf_put_zeros(buf, inst->min_length);
7917 oah->type = htons(hdrs->vendor ? OFPAT_VENDOR : hdrs->type);
7918 oah->len = htons(inst->min_length);
7919 oah->vendor = htonl(hdrs->vendor);
7920
7921 switch (hdrs->vendor) {
7922 case 0:
7923 break;
7924
232c1e12
BP
7925 case NX_VENDOR_ID:
7926 case ONF_VENDOR_ID: {
7927 struct ext_action_header *nah = (struct ext_action_header *) oah;
c2d936a4
BP
7928 nah->subtype = htons(hdrs->type);
7929 break;
7930 }
7931
7932 default:
7933 OVS_NOT_REACHED();
7934 }
7935
7936 if (inst->arg_len) {
7937 uint8_t *p = (uint8_t *) oah + inst->arg_ofs + inst->arg_len;
7938 int i;
7939
7940 for (i = 0; i < inst->arg_len; i++) {
7941 *--p = arg;
7942 arg >>= 8;
7943 }
7944 } else {
7945 ovs_assert(!arg);
7946 }
7947
7948 return oah;
7949}
178742f9
BP
7950
7951static void
7952pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
7953{
7954 struct ofp_action_header *oah;
7955
9ac0aada
JR
7956 ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs,
7957 OFP_ACTION_ALIGN));
178742f9
BP
7958
7959 oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
6fd6ed71 7960 oah->len = htons(openflow->size - start_ofs);
178742f9
BP
7961}
7962