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