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