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