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