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