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