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