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