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