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