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