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