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